摄像头 程序.docx
《摄像头 程序.docx》由会员分享,可在线阅读,更多相关《摄像头 程序.docx(11页珍藏版)》请在冰豆网上搜索。
摄像头程序
最终还是没能赶上校内赛,虽然是很伤心,在这里只想现在的,以后的孩子不要再话那么多的时间在图像采集方面,可以放多点时间在数据处理方面,以免重蹈我们的悲剧,现在上传上位机程序,如果你喜欢,可以买个20块的上位机(淘宝有),这样就可以看到图像。
(PS:
绝对没有帮他们卖广告的嫌疑,只是想同学们可以早点搞好图像。
)
//T0接受行中断,上升沿触发,T1接受场中断,下降沿触发
//数据口为PA0-PA7
//波特率为115200
//串口分别接单片机S0S1
//图像大小选择60*30
//设置帧头帧尾为0x01
#include/*commondefinesandmacros*/
#include"derivative.h"/*derivative-specificdefinitions*/
#defineuintunsignedint
#defineucharunsignedchar
ucharhang_count;
#definehangshu30
#definelieshu100
#defineMid1365
#defineTHRESHOLD0x70
unsignedcharDianji_data;
unsignedintDuoji_data;
unsignedintzuo_danxian=1,you_danxian=1;
uinthang,lie,center[hangshu],left[hangshu],right[hangshu],image_center[];
ucharrow,line1,line2;
uintsteer;
ucharshuzu[hangshu][lieshu];
uinthang_table[]={65,69,74,79,83,86,93,100,
107,114,121,128,135,142,145,149,152,156,160,163,166,
169,173,176,180,183,187,190,193,197,};
bytecount,second;
voidDelay(intDe_a)
{
intDe_b;
for(De_b=0;De_b{
_asm(nop);
//_asm(nop);
}
}
/***********************************************************************/
voidcrg_init(void)
{
CLKSEL=0x00;//脱离锁相环
PLLCTL_PLLON=1;//打开锁相环
SYNR=0XC0|0X09;//SYNR=3,REFDV=1
REFDV=0X80|0X01;//pllclock=2*16*(1+SYNR)/(1+REFDV)=96MHz;
POSTDIV=0X00;//锁相环时钟为96MHz
_asm(nop);
_asm(nop);
while(0==CRGFLG_LOCK);//锁相环锁定
CLKSEL_PLLSEL=1;
}
voidPWM_Init(void)
{
PWME=0X00;//禁止通道
PWMPRCLK=0X33;//A=B=80/8=10MHZ
PWMSCLA=5;//SA=A/2/5=1MHZ
PWMSCLB=5;//SB=B/2/5=1MHZ
/*PWM0,1级联初始化*/
PWMCTL_CON01=1;//0,1级联,寄存器为通道1的
PWMCLK_PCLK1=1;//选时钟SB
PWMPOL_PPOL1=1;//初始电平为高
PWMCAE_CAE1=0;//左对齐方式
PWMCNT01=0;//计数器清零
PWMPER01=20000;//输出周期=SB/10000=100HZ
PWMDTY01=1580;//舵机静止占空比为7.5%=1500/20000
PWME_PWME1=1;//使能
PWMCTL_CON23=1;
PWMCLK_PCLK3=0;
PWMPOL_PPOL3=1;
PWMCAE_CAE3=0;
PWMCNT23=0;
PWMPER23=600;
PWMDTY23=0;
PWME_PWME3=1;
PWMCTL_CON45=1;
PWMCLK_PCLK5=0;
PWMPOL_PPOL5=1;
PWMCAE_CAE5=0;
PWMCNT45=0;
PWMPER45=0;
PWMDTY45=500;
PWME_PWME5=1;
}
/*舵机控制*/
voidPWMDuo_Dutycycle(inta)
{
PWMDTY01=a;
}
voidPWMDian_Dutycycle(intb)
{
PWMDTY45=b;
}
/******************************************************************
-功能描述:
T0接受行中断,上升沿触发,T1接受场中断,下降沿触发,T7计数
-参数说明:
-返回说明:
-修改记录:
-注意事项:
总线时钟为40M时,该程序适用。
******************************************************************/
voidect_init()
{
TIOS=0x00;//T0、T1设置为输入捕捉,摄像头行场中断---关输出比较通道7
TCTL4=0x09;//通道0为上升沿捕捉,1为下降沿捕捉
TIE=0x03;//开场中断,行中断
TSCR1=0x80;//定时器正常工作
TSCR2=0x00;//最后三位为预分频因子选择位
TFLG1=0xFF;//清除该位
TFLG2=0x80;//清除TOF
}
/********************************************************************************************/
//IO初始化子函数
/********************************************************************************************/
voidIO_init(void)
{//IO初始化函数
DDRA=0X00;
}
voidphoto()
{
unsignedchari,j;
for(i=0;i<=29;i++)
for(j=0;j<=65;j++)
{
if(shuzu[j]<=60)
shuzu[j]=1;
else
shuzu[j]=0;
}
}
/******************************************************************
-功能描述:
8位数据位,无奇偶校验,115200波特率,发送器和接收器都使能
-参数说明:
-返回说明:
-修改记录:
-注意事项:
48M总线时钟下适用
******************************************************************/
voidInit_SCI(void)
{
SCI0CR1=0x00;//8位数据位,无奇偶校验
SCI0CR2=0x2c;//允许接收和发送数据,允许接收中断功能
SCI0BD=0x2B;//115200波特率
}
/******************************************************************
-功能描述:
SCI发送一个字节数据
-参数说明:
-返回说明:
-修改记录:
-注意事项:
******************************************************************/
voidSCI_Send_Byte(unsignedchardata)
{
while(!
SCI0SR1_TDRE);//等待发送数据寄存器中的值->发送移位寄存器中
while(!
SCI0SR1_TC);//等待发送移位寄存器中的数值发送完成
SCI0DRL=data;
}
/******************************************************************
-功能描述:
SCI发送一个字符串
-参数说明:
-返回说明:
-修改记录:
-注意事项:
******************************************************************/
voidSCI_Send_Uchar_String(unsignedchar*putchar,uintnum)
{
uintcounter=0;
while((counter++){
SCI_Send_Byte(*(putchar++));
}
}
voidSCI_Send_Image_String(unsignedchar*putchar,uintnum)
{
uintcounter=0;
for(counter=0;counter{
if(putchar[counter]==0x01)putchar[counter]=0x02;
SCI_Send_Byte(putchar[counter]);
}
}
voidmain(void){
/*putyourowncodehere*/
/*ucharx,data;*/
crg_init();
ect_init();
IO_init();
Init_SCI();
PWM_Init();
EnableInterrupts;
for(;;){
_FEED_COP();/*feedsthedog*/
}/*loopforever*/
/*pleasemakesurethatyouneverleavemain*/
}
/*********************************************
功能描述:
行中断服务程序,接PT0
参数说明:
返回说明:
修改记录:
注意事项:
*********************************************/
#pragmaCODE_SEGNON_BANKED
voidinterrupt8Port0_Interrupt()
{
inti=0,j=0;
unsignedchar*p;
TFLG1=0x01;
hang++;//行计数器加1
if((hang<30)||(hang>240))
{
return;//判断是否从新的一场开始
}
if(hang==hang_table[hang_count])//行数组采样变量
{
Delay
(1);
p=&shuzu[hang_count][0];
shuzu[hang_count][0]=PORTA;_asm();shuzu[hang_count][1]=PORTA;_asm();shuzu[hang_count][2]=PORTA;_asm();shuzu[hang_count][3]=PORTA;_asm();shuzu[hang_count][4]=PORTA;_asm();
shuzu[hang_count][5]=PORTA;_asm();shuzu[hang_count][6]=PORTA;_asm();shuzu[hang_count][7]=PORTA;_asm();shuzu[hang_count][8]=PORTA;_asm();shuzu[hang_count][9]=PORTA;_asm();
shuzu[hang_count][10]=PORTA;_asm();shuzu[hang_count][11]=PORTA;_asm();shuzu[hang_count][12]=PORTA;_asm();shuzu[hang_count][13]=PORTA;_asm();shuzu[hang_count][14]=PORTA;_asm();
shuzu[hang_count][15]=PORTA;_asm();shuzu[hang_count][16]=PORTA;_asm();shuzu[hang_count][17]=PORTA;_asm();shuzu[hang_count][18]=PORTA;_asm();shuzu[hang_count][19]=PORTA;_asm();
shuzu[hang_count][20]=PORTA;_asm();shuzu[hang_count][21]=PORTA;_asm();shuzu[hang_count][22]=PORTA;_asm();shuzu[hang_count][23]=PORTA;_asm();shuzu[hang_count][24]=PORTA;_asm();
shuzu[hang_count][25]=PORTA;_asm();shuzu[hang_count][26]=PORTA;_asm();shuzu[hang_count][27]=PORTA;_asm();shuzu[hang_count][28]=PORTA;_asm();shuzu[hang_count][29]=PORTA;_asm();
shuzu[hang_count][30]=PORTA;_asm();shuzu[hang_count][31]=PORTA;_asm();shuzu[hang_count][32]=PORTA;_asm();shuzu[hang_count][33]=PORTA;_asm();shuzu[hang_count][34]=PORTA;_asm();
shuzu[hang_count][35]=PORTA;_asm();shuzu[hang_count][36]=PORTA;_asm();shuzu[hang_count][37]=PORTA;_asm();shuzu[hang_count][38]=PORTA;_asm();shuzu[hang_count][39]=PORTA;_asm();
shuzu[hang_count][40]=PORTA;_asm();shuzu[hang_count][41]=PORTA;_asm();shuzu[hang_count][42]=PORTA;_asm();shuzu[hang_count][43]=PORTA;_asm();shuzu[hang_count][44]=PORTA;_asm();
shuzu[hang_count][45]=PORTA;_asm();shuzu[hang_count][46]=PORTA;_asm();shuzu[hang_count][47]=PORTA;_asm();shuzu[hang_count][48]=PORTA;_asm();shuzu[hang_count][49]=PORTA;_asm();
shuzu[hang_count][50]=PORTA;_asm();shuzu[hang_count][51]=PORTA;_asm();shuzu[hang_count][52]=PORTA;_asm();shuzu[hang_count][53]=PORTA;_asm();shuzu[hang_count][54]=PORTA;_asm();
shuzu[hang_count][55]=PORTA;_asm();shuzu[hang_count][56]=PORTA;_asm();shuzu[hang_count][57]=PORTA;_asm();shuzu[hang_count][58]=PORTA;_asm();shuzu[hang_count][59]=PORTA;_asm();
shuzu[hang_count][60]=PORTA;_asm();shuzu[hang_count][61]=PORTA;_asm();shuzu[hang_count][62]=PORTA;_asm();shuzu[hang_count][63]=PORTA;_asm();shuzu[hang_count][64]=PORTA;_asm();
shuzu[hang_count][65]=PORTA;_asm();shuzu[hang_count][66]=PORTA;_asm();shuzu[hang_count][67]=PORTA;_asm();shuzu[hang_count][68]=PORTA;_asm();shuzu[hang_count][69]=PORTA;_asm();
shuzu[hang_count][70]=PORTA;_asm();shuzu[hang_count][71]=PORTA;_asm();shuzu[hang_count][72]=PORTA;_asm();shuzu[hang_count][73]=PORTA;_asm();shuzu[hang_count][74]=PORTA;_asm();
shuzu[hang_count][75]=PORTA;_asm();shuzu[hang_count][76]=PORTA;_asm();shuzu[hang_count][77]=PORTA;_asm();shuzu[hang_count][78]=PORTA;_asm();shuzu[hang_count][79]=PORTA;_asm();
shuzu[hang_count][80]=PORTA;_asm();shuzu[hang_count][81]=PORTA;_asm();shuzu[hang_count][82]=PORTA;_asm();shuzu[hang_count][83]=PORTA;_asm();shuzu[hang_count][84]=PORTA;_asm();
shuzu[hang_count][85]=PORTA;_asm();shuzu[hang_count][86]=PORTA;_asm();shuzu[hang_count][87]=PORTA;_asm();shuzu[hang_count][88]=PORTA;_asm();shuzu[hang_count][89]=PORTA;_asm();
shuzu[hang_count][90]=PORTA;_asm();shuzu[hang_count][91]=PORTA;_asm();shuzu[hang_count][92]=PORTA;_asm();shuzu[hang_count][93]=PORTA;_asm();shuzu[hang_count][94]=PORTA;_asm();
shuzu[hang_count][95]=PORTA;_asm();shuzu[hang_count][96]=PORTA;_asm();shuzu[hang_count][97]=PORTA;_asm();shuzu[hang_count][98]=PORTA;_asm();shuzu[hang_count][99]=PORTA;_asm();
for(j=0;j<100;j++,p++)
{
*p=shuzu[hang_count][j];
}
hang_count++;
}
if(hang_count==hangshu)
{
TIE=0x00;//关行中断,场中断
SCI_Send_Byte(0x01);
SCI_Send_Image_String(&shuzu[0][0],hangshu*lieshu);
SCI_Send_Byte(0x01);
TIE=0x02;//开场中断
return;
}
}
/*********************************************
功能描述:
场中断服务程序,接PT1
参数说明:
返回说明:
修改记录:
注意事项:
*********************************************/
#pragmaCODE_SEGNON_BANKED
interrupt9voidPT1_Interrupt(void)
{
TFLG1=0x03;//清场中断,行中断
TIE=0x03;//开场中断,关行中断
hang=0;
lie=0;
hang_count=0;
}
WelcomeTo
Download!
!
!
欢迎您的下载,资料仅供参考!