高级会员
- 积分
- 954
- 威望
- 279
- 贡献
- 145
- 兑换币
- 161
- 注册时间
- 2013-3-19
- 在线时间
- 265 小时
- 毕业学校
- 中国计量学院
|
只要把DDRB改DDRA吗。。为什么采集不到图像。。。#include <hidef.h>#include <mc9s12xs128.h>
#include "derivative.h"
#define COLUMN 65
#define ROW 32
#define BLACK 0
#define WHITE 2
#define THRESHOLD 110
#define STEER_DTY_CENTER 1875 //1875
#define VIDEO_CENTER 32
unsigned char Buffer[ROW][COLUMN];
unsigned char after[ROW][COLUMN];
unsigned char row=0,column=0,row_counter,n,zuo,you;
unsigned char Interval,flag ;
unsigned char SampleFlag;
byte flag1=0,start=1;
byte crossflag=0;
int motor=250,moto=250;
unsigned int duoji= 1875;//1875;
unsigned int Line_Center[26];
unsigned int black_zuo[26],black_you[26];
unsigned int flage_good=0;
unsigned int ui_Direction,ui_Speed,last_Direction;
unsigned char lineable=0;
unsigned char left_flag=0;
unsigned char right_flag=0;
unsigned char image_lost , image_lost_str,img_lost_cnt;
unsigned char black_lost=0;
unsigned char black_lost_str=0;
unsigned char zuo, you;
//计算
int Imagesum;
int Imageave;
int chaave ,chasum ;
int err0=0,err1=0;
void PLL_Init(void)
{
CLKSEL=0X00;
PLLCTL_PLLON=1;
SYNR =0xc0 | 0x09;
REFDV=0x80 | 0x01;
POSTDIV=0x00;
_asm(nop);
_asm(nop);
while(!(CRGFLG_LOCK==1)); //when pll is steady ,then use it;
CLKSEL_PLLSEL =1; //engage PLL to system;
}
void init_pwm(void)
{
PWMCTL_CON23= 1;
PWMCTL_CON01= 1;
PWMCTL_CON45= 1;
PWMPOL_PPOL1= 1;
PWMPOL_PPOL3= 0;
PWMPOL_PPOL5= 1;
PWMPRCLK = 0x44;
PWMSCLA = 2; //SA时钟频率2MHz
PWMSCLB = 1; //SB时钟频率为5MHz
PWMCLK = 0xa0; //45用SA时钟作为时钟源,01和23用A,B时钟
PWMCAE = 0x00;
PWMPER01 = 500; //01和23用A,B时钟 5MHZ 通道01的周期为10KHz
PWMPER23 = 500; //通道23的周期为10KHz
PWMPER45 = 25000; //周期20ms 50HZ
PWMDTY23 = 300; //通道23的占空比设置
PWMDTY01 = 300; //通道01的占空比设置
PWMDTY45 = 1875;
PWME_PWME3=1;
PWME_PWME1=1;
PWME_PWME5=1;
}
void TIM_Init(void)
{
TIOS=0x00;
TCTL4=0x09;
TSCR1=0x80; //使能
TIE=0x03;
TFLG1=0xFF;
}
void IO_Init(void)
{
// DDRA=0X00;
//PPSH = 0xff;
//PIFH = 0x0f;
//PIEH = 0x0f;
DDRA=0X00;
DDRB=0x00;
PTM=0X00;
DDRM=0X0f;
}
void SCI_Init()
{
SCI0BD=86;
SCI0CR1=0;
SCI0CR2=0X2C; //发送允许
}
void SCI_Write(unsigned char SendChar)
{
while (!(SCI0SR1&0x80)); //等待上一次数据发送完成
SCI0DRH=0;
SCI0DRL=SendChar;
}
void PIT_Init(void)
{
INT_CFADDR = 0x70;
INT_CFDATA5 = 0x07; //设置为最高级
PITCFLMT_PITE = 0; //PIT is disabled.
PITCE_PCE0 = 1; //0 Chanel is enabled.
PITMUX_PMUX0 = 0;
PITMTLD0 = 64;
PITLD0 = 500;
PITCFLMT_PFLMT = 1;
PITFLT_PFLT0 = 1;
PITINTE_PINTE0 = 1;
PITTF_PTF0 = 1;
PITCFLMT_PITE = 1;
}
void Process()
{
unsigned char j,i;
DisableInterrupts;
flag = 0;
SCI_Write(0x00);
SCI_Write(0xff);
SCI_Write(0x01);
SCI_Write(0x00);
for(i=0;i<ROW;i++)
{
for(j=0;j<COLUMN;j++)
{
SCI_Write(after[i][j]); // SCI1_Write(Buffer[i][j]); SCI1_Write(after[i][j])
}
}
EnableInterrupts;
n++;
}
void Image_binaryzation()
{
int i,j;
for(i=0;i<ROW;i++)
{
for(j=0;j<COLUMN;j++)
{
if(Buffer[i][j]<THRESHOLD)
after[i][j]=BLACK;
else
after[i][j]=WHITE;
}
}
}
void STEERControl()
{
if(ui_Direction<1170)ui_Direction=1170;
if(ui_Direction>2150)ui_Direction=2150;
PWMDTY45=ui_Direction;
}
void MOTOControl(int moto)
{
if(moto<80)moto=80;
if(moto>450)moto=450;
PWMDTY01=moto;
PWMDTY23=moto;
}
void turntrace(void) //左打1250 1750 2150右打
{
unsigned char kd=3,diff,i;
unsigned int sum=0;
Imagesum=0; Imageave=0;
diff=8; //diff=abs(moto-250)/10;
for(i=25-diff;i>=20-diff;i--)
sum+=Line_Center[i];
sum=sum/6;
err0=VIDEO_CENTER-sum;
if(abs(err0)<10)
ui_Direction=STEER_DTY_CENTER+35*err0;
else
ui_Direction=STEER_DTY_CENTER+60*err0;
STEERControl();
if(abs(err0)<10)
moto=310;
else
moto=300;
MOTOControl(moto);
last_Direction=ui_Direction;
err1=err0;
}
void bianjie(void)
{
char i,j,flag_zuo=0,flag_you=0,temp=0,count=0;
int leftfirst=0,rightfirst=64,lastmid=0,pre_left_point=3,pre_right_point=60;
zuo=0; you=0 ;
image_lost=0;
image_lost_str=0;
black_lost=0;
black_lost_str=0;
lineable=0;
i=31;
for(j=33;j>=1;j--)
{
if(after[i][j]==0&&after[i][j-1]==0&&after[i][j+1]==2&&after[i][j+2]==2)
{
flag_zuo=1;
leftfirst=j;
pre_left_point=j;
after[i][leftfirst]=0;
black_zuo[zuo]=leftfirst;
zuo++;
break;
}
}
for(j=35;j<=63;j++)
{
if(after[i][j]==0&&after[i][j+1]==0&&after[i][j-1]==2&&after[i][j-2]==2)
{
flag_you=1;
rightfirst=j;
pre_right_point=j;
after[i][rightfirst]=0;
black_you[you]=rightfirst;
you++;
break;
}
}
if( pre_right_point==0)
pre_right_point=61;
lastmid=leftfirst+rightfirst;
lastmid=lastmid/2;
for(i=30; i>=5; i--)
{
for(j=pre_left_point-3;j<=pre_left_point+15;j++)
{
if(after[i][j]==0&&after[i][j-1]==0&&after[i][j+1]==2&&after[i][j+2]==2)
{
flag_zuo=1;
leftfirst=j;
pre_left_point=j;
after[i][leftfirst]=0;
black_zuo[zuo]=leftfirst;
zuo++;
break;
}
}
for(j=pre_right_point+3;j>=pre_right_point-15;j--)
{
if(after[i][j]==0&&after[i][j+1]==0&&after[i][j-1]==2&&after[i][j-2]==2)
{
flag_you=1;
rightfirst=j;
after[i][rightfirst]=0;
black_you[you]=rightfirst;
you++;
break;
}
}
if(rightfirst==0)
rightfirst=64;
if(abs(rightfirst-pre_right_point)>10)
rightfirst=pre_right_point;
else
if(flag_you)
pre_right_point=rightfirst;
temp=leftfirst+rightfirst;
temp=temp/2;
if(abs(temp-lastmid)>5)
temp=lastmid;
lastmid=temp;
after[i][temp]=0;
Line_Center[25-count]=temp;
count++;
}
}
void arrinit(void)
{
char i,j;
for(i=0;i<ROW;i++)
{
for(j=0;j<COLUMN;j++)
{
after[i][j]=2;
}
}
}
void main(void)
{
DDRA=0XFF;
DDRB=0xFF;
DDRK=0xFF;
PORTK=0x00;
PLL_Init();
TIM_Init();
IO_Init();
SCI_Init();
init_pwm();
EnableInterrupts;
arrinit();
for(;;)
{
if(flag)
{
flag=0;
Image_binaryzation();
bianjie();
turntrace();
Process();
}
}
}
OLED_Init();
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 8 PT0_Interrupt()
{
static unsigned int m;
TFLG1_C0F=1;
row_counter++;
if(row_counter>=239) TIE= 0x02;
if ( SampleFlag==0||row_counter<20||row_counter>162)
{
return;
}
if(row_counter>=20&&row_counter<128) Interval=4;
if(row_counter>=128&&row_counter<162) Interval=6;
if(row_counter%Interval==0)
{
if(row>=32)
{
SampleFlag = 0;
flag = 1;
row_counter=0;
return;
}
for( m = 0; m < COLUMN; m++ )
{
_asm(nop);_asm(nop);_asm(nop);
_asm(nop);_asm(nop);_asm(nop);
_asm(nop);_asm(nop);_asm(nop);
_asm(nop);_asm(nop);_asm(nop);
_asm(nop);_asm(nop);_asm(nop);
_asm(nop);_asm(nop);_asm(nop);
_asm(nop);_asm(nop);_asm(nop);
_asm(nop);_asm(nop);_asm(nop);
_asm(nop);_asm(nop);_asm(nop);
Buffer[row][m]= PORTB;
}
row++;
}
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 9 PT1_Interrupt()
{
TFLG1_C1F=1;
TFLG1_C0F=1;
TIE_C0I = 1;
row_counter=0;
row=0;
SampleFlag=1;
}
|
|