中级会员
- 积分
- 210
- 威望
- 182
- 贡献
- 22
- 兑换币
- 0
- 注册时间
- 2009-1-11
- 在线时间
- 3 小时
|
弄错了,呵呵
#include <hidef.h> /* common defines and macros */
#include <mc9s12dg128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
unsigned char imagin[40][70];
unsigned char guiji[40];
unsigned int sd_z[4]={450,500,550,600},z_sd,w_sd;
unsigned int sd_w[4]={280,300,310,320};
unsigned char jsd_z[4]={30,20,15,5},z_jsd,w_jsd;
unsigned char jsd_w[2]={30,20};
char i=0,j=0,k=0,complete=2;
unsigned char go,speed,cnt,flag_j,cnt2=210,cnt3=0;
int pingjun;
unsigned char *p;
unsigned int *addr;
//*******************图像分析函数****************************
void fenxi(void) {
unsigned char ii,jj,kk,buf2=0,buf3=3,sum=0;
char buf;
int sum1=0,sum2=0;
//cnt2=0;
/* for(ii=10;ii<60;ii++) {
sum1=0;
kk=0;
for(jj=20;jj<40;jj++) {
if((imagin[jj][ii]<55)&&(imagin[jj][ii-1]<55)){
kk++;
sum1+=jj;
}
}
if(kk)
hx[ii]=(char)(sum1/kk);
else hx[ii]=0;
}
buf2=0;
kk=0;
for(ii=11;ii<60;ii++) {
if(hx[ii]){
//if(buf3>10)
//buf3=0;
buf=hx[ii]-hx[ii-1];
if((buf<2)&&(buf>(-2))&&(buf3>2))
kk++;
else{
if(kk>4) {
buf3=0;
kk=0;
if(kk<20)
buf2++;
}
}
//else if(kk>8)
}
else {
if(kk>4) {
buf3=0;
buf2++;
}
buf3++;
kk=0;
}
}
if(kk>4)
buf2++;
if(buf2==2){
PORTK|=0x04;
cnt3++;
if(cnt3>0){
cnt3=0;
if((cnt2>200)&&go){
cnt2=0;
flag_q++;
}
if(flag_q>1){
PWMDTY0=0;
speed=0;
go=0;
PORTK&=0xfe;
flag_q=0;
}
}
}
else {
PORTK&=0xfb;
cnt3=0;
}*/
jj=39;
kk=0;
there:p=&imagin[jj][0];
for(ii=0;ii<70;ii++){
if(*(p++)<50) {
kk++;
}
else {
if(kk>13) {
pingjun=0;
return;
}
else if(kk>3)break;
else
kk=0;
}
}
if(kk<2){
if(jj>35)jj--;
else return;
goto there;
}
guiji[jj]=ii-1;
buf=ii-7;
if(buf<0)buf=0;
else if(buf>56)buf=56;
kk=0;
buf3=jj-2;
jj--;
for(;jj>18;jj--){
sum=0;
p=&imagin[jj][buf];
kk=0;
buf2=0;
for(ii=0;ii<14;ii++){
if((*(p++)<60)&&(*p<60)) {
kk++;
sum+=ii;
}
}
if(kk>0) {
guiji[jj]=sum/kk+buf;
}
else {
if(guiji[jj+1]>65){
pingjun=140;
flag_j=1;
goto here2;
}
else if(guiji[jj+1]<10){
pingjun=-140;
flag_j=1;
goto here2;
}
if(jj<38)
guiji[jj]=2*guiji[jj+1]-guiji[jj+2];
else guiji[jj]=guiji[jj+1];
}
//if(guiji[jj]>(guiji[jj+1]+1))
buf=guiji[jj]-7; //
//else if((guiji[jj]+1)<guiji[jj+1])
// buf=guiji[jj]-13;
// else buf=guiji[jj]-7;
if(buf<0)buf=0;
else if(buf>56)buf=56;
}
if(buf>50)buf=50;
for(jj=18;jj>0;jj--){
buf2=0;
p=&imagin[jj][buf];
kk=0;
for(ii=0;ii<20;ii++){
if(*p<65) {
kk++;
}
else {
if(kk>0)break;
else{
buf2=ii;
kk=0;
}
}
p++;
}
if(kk<1) {
guiji[jj]=guiji[jj+1];
continue;
}
else{
buf2+=buf;
buf+=ii;
if(buf<guiji[jj+1])
guiji[jj]=buf2;
else
guiji[jj]=buf;
}
buf=guiji[jj]-10;
if(buf<0)buf=0;
else if(buf>50)buf=50;
}
//求平均偏移量//
flag_j=0;
for(jj=3;jj<7;jj++){
if((guiji[jj]>60)||(guiji[jj]<10))
flag_j=1;
}
for(jj=8;jj<12;jj++){
sum1+=guiji[jj];
if((guiji[jj]>60)||(guiji[jj]<10))
flag_j=1;
}
sum1/=4;
for(jj=27;jj<30;jj++){
sum2+=guiji[jj];
}
sum2/=3;
pingjun=5*(sum1-35)-2*(sum1-sum2);
//pingjun=(sum2-35)*4;
if(pingjun>140)pingjun=140;
if(pingjun<(-140))pingjun=-140;
here2:
if(flag_j)PORTK|=0x08;
else PORTK&=0xf7;
}
//*****eeprom初始化*************************
void init_eeprom(void) {
ECLKDIV=0x15;
ECNFG=0;
while(ECLKDIV_EDIVLD==0);
}
//*****写eeprom*************************
char write_eeprom(unsigned int dat) {
while(ESTAT_CBEIF==0);
*addr=dat;
ECMD=0x60;
ESTAT=0x80;
while(ESTAT_CCIF==0);
if(ESTAT_ACCERR)return 0;
return 1;
}
//*****PWM初始化*************************
void init_pwm(void) {
PWME=0;
PWMPRCLK=0x53;
PWMSCLA=25;
PWMSCLB=50;
PWMCLK=0x03;
PWMDTY0=0;
PWMPER0=100;
PWMDTY67=1503;
PWMPER67=20000;
PWMPOL=0x80;
PWMCAE=0x00;
PWMCTL=0x80;
PWME=0x81;
}
//****************************************
void main(void) {
char flag=0;
int buf1;
INITEE = 0x61; /* lock EEPROM block to end at 0x0fff */
CLKSEL &= 0x7F;
REFDV = 1;
SYNR = 3;
while(!(CRGFLG&0x08));
CLKSEL |= 0x80;
init_eeprom();
addr=(unsigned int *)0x6002;
DDRK|=0x2f; //portk的低四位做能为输出
TSCR1=0x80;
TSCR2=0x83;
PACTL=0x40;
TCNT=0x00;
TIOS=0x00;
TCTL4=0x55;
TIE=0x06;
DLYCT=0x00;
INTCR=0xC0; //VSYNC下降沿触发
DDRH&=0xfe;
PPSH=0x01; //HREF上升沿触发
PIEH=0x01;
DDRA=0x00;
SCI0CR2=0x0c;
SCI0BDH=0x00;
SCI0BDL=104; //波特率=19200 208;
DDRM=0x00; //拨码开关设定为输入
PERM=0xff; //上下拉使能
PPSM=0x00; //上拉
init_pwm();
//if(*addr==1){
go=1;
PORTK|=0x01;
//}
buf1=PTM;
z_sd=sd_z[buf1&0x03];
buf1>>=2;
z_jsd=jsd_z[buf1&0x03];
buf1>>=2;
w_sd=sd_w[buf1&0x03];
buf1>>=1;
w_jsd=jsd_w[buf1&0x01];
//PLLCTL=0xf3;
//COPCTL=0x05;
EnableInterrupts;
for(;;) {
//ARMCOP=0X55;
if(complete==1) {
DisableInterrupts;
if(flag==1){
for(i=0;i<40;i++){ //////
while(!(SCI0SR1&0x80));
SCI0DRL=0;
for(j=0;j<70;j++) {
while(!(SCI0SR1&0x80));
SCI0DRL=imagin[i][j];
}
}
while(!(SCI0SR1&0x80));
SCI0DRL=0;
while(!(SCI0SR1&0x80));
SCI0DRL=0;
}
//ARMCOP=0XAA;
fenxi();
buf1=1503-pingjun;
PWMDTY67=buf1;
EnableInterrupts;
flag=0;
complete=2;
}
if(SCI0SR1&0x20) {
if(SCI0DRL==48) {
flag=1;
complete=2;
}
}
}
}
//*********************************************
#pragma CODE_SEG NON_BANKED
void interrupt VSYNC_ISR(void) //场信号中断
{
DisableInterrupts; //关中断
if(complete==2) {
i=0;
complete=0;
k=0;
p=&imagin[0][0];
}
}
void interrupt HREF_PH0(void) { //行信号中断
unsigned char jj;
DisableInterrupts; //关中断
if(complete==0) {
if(k>6) {
for(jj=0;jj<70;jj++){
k=0;
*(p++)= PORTA;
}
i++;
if(i>39) complete=1;
}
k++;
}
PIFH=0x01;
}
//******************************************************
void interrupt Timer(void) { //时间中断,调速
unsigned int buf;
int such;
DisableInterrupts; //关中断
TCNT=0;
buf=PACN32;
PACN32=0;
if(cnt2<210)cnt2++;
if(cnt)
cnt--;
else {
PORTK^=0x02;
if(go==1){
if(flag_j==0){
PORTK&=0xf7;
such=buf-z_sd;
if(such<0){
such/=z_jsd;
}
else such/=30;
}
else {
PORTK|=0x08;
if(buf>420){
cnt=4;
PORTK|=0x20;
PWMDTY0=65;
goto out;
}
if(buf>400){
cnt=4;
PORTK|=0x20;
PWMDTY0=60;
goto out;
}
if(buf>370){
cnt=3;
PORTK|=0x20;
PWMDTY0=60;
goto out;
}
such=buf-w_sd;
if(such>0)
such/=30;
else{
such/=w_jsd;
}
}
PORTK&=0xdf;
if(speed>such)speed-=such;
else speed=0;
if(speed>95)speed=95;
PWMDTY0=speed;
}
}
out: TFLG2=0x80;
}
//******************************************************
void interrupt IC1_ISR(void) {
DisableInterrupts; //关中断 C
TFLG1=0x02;
PORTK|=0x01;
go=1;
while(write_eeprom(1)==0);
}
void interrupt IC2_ISR(void) {
DisableInterrupts; //关中断 B
TFLG1=0x04;
PORTK&=0xfe;
go=0;
PWMDTY0=0;
speed=0;
while(write_eeprom(0)==0);
}试试这个程序 |
|