最近使用OV7670做了一个关于激光自动打靶报靶系统,不过只做了用摄像头对靶心的识别。
取样点过多
stm32单片机源程序如下:
- #include <stm32f10x_lib.h>
- #include "sys.h"
- #include "usart.h"
- #include "delay.h"
- #include "led.h"
- #include "key.h"
- #include "exti.h"
- #include "wdg.h"
- #include "timer.h"
- #include "lcd.h"
- #include "ov7670.h"
- #include "usmart.h"
- u16 i,shua1=0,shua2=0;
- u16 aa=0,bb=0,lie=0,y=0,hang=0,x=0 ,a=0,b=0;
- u16 zhongxin1=0,zhongxin2=0,zhongxin3=0,zhongxin4=0;
- u32 u=0;
- extern u8 ov_sta; //在exit.c里面定义
- extern u8 ov_frame; //在timer.c里面定义
- void swap (u16 *p1,u16 *p2)
- {
- u16 temp;
- temp=*p1;
- *p1=*p2;
- *p2=temp ;
- }
-
- void camera_refresh(void)
- {
- u32 j;
- u16 color;
- if(ov_sta==2)
- {
- LCD_Scan_Dir(U2D_L2R); //从上到下,从左到右
- LCD_SetCursor(0x00,0x0000); //设置光标位置
- LCD_WriteRAM_Prepare(); //开始写入GRAM
- OV7670_CS=0;
- OV7670_RRST=0; //开始复位读指针
- OV7670_RCK=0;
- OV7670_RCK=1;
- OV7670_RCK=0;
- OV7670_RRST=1; //复位读指针结束
- OV7670_RCK=1;
- for(j=0;j<76800;j++)
- {
- GPIOB->CRL=0X88888888;
- OV7670_RCK=0;
- color=OV7670_DATA; //读数据
- OV7670_RCK=1;
- color<<=8;
- OV7670_RCK=0;
- color|=OV7670_DATA; //读数据
- OV7670_RCK=1;
- GPIOB->CRL=0X33333333;
- LCD_WR_DATA(color);
- }
- OV7670_CS=1;
- OV7670_RCK=0;
- OV7670_RCK=1;
- EXTI->PR=1<<15; //清除LINE8上的中断标志位
- ov_sta=0; //开始下一次采集
- ov_frame++;
- LCD_Scan_Dir(DFT_SCAN_DIR); //恢复默认扫描方向
- }
- }
- void fengge (void)
- {
-
- for(bb=2;bb<230;bb++)
-
- {
-
- for(aa=0;aa<239;aa++)
-
- {
-
-
- u= (LCD_ReadPoint(20,20)+LCD_ReadPoint(21,21)+LCD_ReadPoint(22,22)+LCD_ReadPoint(23,23)+LCD_ReadPoint(220,20)+LCD_ReadPoint(221,21)+LCD_ReadPoint(222,22)+LCD_ReadPoint(223,23)+LCD_ReadPoint(20,319)+LCD_ReadPoint(21,318)+LCD_ReadPoint(22,317)+LCD_ReadPoint(23,316)+LCD_ReadPoint(220,319)+LCD_ReadPoint(221,318)+LCD_ReadPoint(222,317)+LCD_ReadPoint(223,317) )/16;
-
-
- if((LCD_ReadPoint(aa-2,bb)<(u-10000)))
- {
- if((LCD_ReadPoint(aa,bb)<(u-10000))) //白天45000
- {
- if((LCD_ReadPoint(aa+1,bb)<(u-10000))&&(LCD_ReadPoint(aa+2,bb)<(u-10000)))
- {
- POINT_COLOR=BLACK;
- LCD_DrawPoint(aa,bb);
- }
- }
- }
- }
- }
- }
-
- void fanfenge(void)
- {
- for(bb=0;bb<320;bb++) //210
- {
- for(aa=0;aa<239;aa++)
- {
- if(LCD_ReadPoint(aa,bb)>1000 )
- {
- POINT_COLOR=WHITE ;
- LCD_DrawPoint(aa,bb);
- }
- }
- }
- if(LCD_ReadPoint(aa-1,bb)<1000 )
- {
- if(LCD_ReadPoint(aa+1,bb)<1000 )
- {
- POINT_COLOR=BLACK;
- LCD_DrawPoint(aa,bb);
- }
- }
-
-
-
- }
-
-
-
-
- void huaxin(void)
- {
-
- for(bb=80;bb<160;bb=bb+15)
-
- {
-
- lie=0;
- for(aa=0;aa<235;aa++)
-
- {
-
- /* POINT_COLOR=RED ;
- LCD_ShowxNum(170,260,aa,8,16,0);//显示2个数字
- LCD_ShowxNum(170,280,bb,8,16,0);//显示2个数字
- LCD_ShowxNum(170,300,LCD_ReadPoint(aa,bb),8,16,0);//显示2个数字
- LCD_ShowxNum(10,300,y,8,16,0);//显示2个数字
- LCD_ShowxNum(10,260,lie,8,16,0);//显示2个数字
- LCD_ShowxNum(90,300,LCD_ReadPoint(20,20),8,16,0);//显示2个数字
- LCD_ShowxNum(90,280,LCD_ReadPoint(77,77),8,16,0);//显示2个数字
- LCD_ShowxNum(90,260,u,8,16,0);//显示2个数字 */
- //LCD_DrawLine(20, 20, 25, 25); //画线
-
- // if(LCD_ReadPoint(aa-3,bb)>20000)
- // {
- // if(LCD_ReadPoint(aa-2,bb)>20000)
- // {
- if((LCD_ReadPoint(aa,bb)>20000)) //白天45000
- {
- if(LCD_ReadPoint(aa+1,bb)<(20000)&&(LCD_ReadPoint(aa+2,bb)<20000))
- { lie++; }
- }
- //}
- // }
-
- // if(lie>9)
- // { y=bb; }
- zhongxin3=lie;
- if(zhongxin3>zhongxin4 )
- {
- swap(&zhongxin3,&zhongxin4);
- y=bb;
-
- }
- }
- }
-
-
-
-
-
- for(a=80;a<200;a=a+20)
- {
- hang=0;
- LED0=0;
- for(b=0;b<230;b++)
- {
- /* POINT_COLOR=RED ;
- LCD_ShowxNum(170,260,a,8,16,0);//显示2个数字
- LCD_ShowxNum(170,280,b,8,16,0);//显示2个数字
- LCD_ShowxNum(170,300,LCD_ReadPoint(a,b),8,16,0);//显示2个数字
- LCD_ShowxNum(10,280,x,8,16,0);//显示2个数字
- LCD_ShowxNum(10,260,hang,8,16,0);//显示2个数字
- LCD_ShowxNum(10,300,y,8,16,0);//显示2个数?
- LCD_ShowxNum(90,260,u,8,16,0);//显示2个数字
- */
- if((LCD_ReadPoint(a,b-3)<(u-3000)))
- {
- if((LCD_ReadPoint(a,b-2)<(u-3000)))
- {
- if((LCD_ReadPoint(a,b)<(u-3000))) //50000
- {
- if((LCD_ReadPoint(a,b+1)>(u-3000))&&(LCD_ReadPoint(a,b+2)>(u-3000)))
- { hang++; }
- }
- }
- }
- // if(hang>9)
- // { x=a; }
-
- zhongxin1=hang;
- if(zhongxin1>zhongxin2 )
- {
- swap(&zhongxin1,&zhongxin2);
- x=a;
-
- }
-
-
- }
-
- }
- }
- int main(void)
- {
-
- Stm32_Clock_Init(9); //系统时钟设置
- uart_init(72,9600); //串口初始化为9600
- delay_init(72); //延时初始化
- OV7670_Init();
- LED_Init(); //初始化与LED连接的硬件接口
- LCD_Init(); //初始化LCD
- if(lcddev.id==0X6804) //强制设置屏幕分辨率为320*240.以支持3.5寸大屏
- {
- lcddev.width=240;
- lcddev.height=320;
- }
- usmart_dev.init(72); //初始化USMART
-
- POINT_COLOR=RED;//设置字体为红色
- LCD_ShowString(60,50,200,200,16,"Mini STM32");
- LCD_ShowString(60,70,200,200,16,"OV7670 TEST");
- LCD_ShowString(60,90,200,200,16,"ATOM@ALIENTEK");
- LCD_ShowString(60,110,200,200,16,"2012/10/31");
- LCD_ShowString(60,130,200,200,16,"Use USMART To Set!");
- LCD_ShowString(60,150,200,200,16,"OV7670 Init...");
- while(OV7670_Init())//初始化OV7670
- {
- LCD_ShowString(60,150,200,200,16,"OV7670 Error!!");
- delay_ms(200);
- LCD_Fill(60,230,239,246,WHITE);
- delay_ms(200);
- }
- LCD_ShowString(60,150,200,200,16,"OV7670 Init OK");
- delay_ms(1500);
- Timerx_Init(10000,7199); //TIM3,10Khz计数频率,1秒钟中断
- EXTI15_Init(); //使能定时器捕获
- OV7670_Window_Set(10,174,240,320); //设置窗口
- OV7670_CS=0;
- while(1)
- {
- camera_refresh(); //更新显示
- if(i!=ov_frame) //DS0闪烁.
- {
- i=ov_frame;
- LED0=!LED0;
- }
- if(KEY0==0)
- {
- for(shua1=0;shua1<30000;shua1++)
- {
- for(shua2=0;shua2<10;shua2++)
- {
- camera_refresh();
- if(i!=ov_frame)
- {i=ov_frame;}
- }
- }
-
- fengge();
- fanfenge();
- aa=0;
- bb=0;
- huaxin();
- LCD_Fill(x,y,x+5,y+5,RED);
- delay_ms(3000);
- // while(1);
- }
- }
-
-
-
- }
复制代码
所有资料51hei提供下载:
智能寻找中心按键触发.zip
(131.96 KB, 下载次数: 17)
|