利用ov7670实现物体的简单识别及定位
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "lcd.h"
- #include "usart.h"
- #include "string.h"
- #include "ov7670.h"
- #include "tpad.h"
- #include "timer.h"
- #include "exti.h"
- #include "usmart.h"
- #include "EasyTracered.h"
-
- /************************************************
- ALIENTEK精英STM32开发板实验33
- 摄像头OV7670 实验
- 作者:正点原子 @ALIENTEK
- ************************************************/
- const u8*LMODE_TBL[5]={"Auto","Sunny","Cloudy","Office","Home"}; //5种光照模式
- const u8*EFFECTS_TBL[7]={"Normal","Negative","B&W","Redish","Greenish","Bluish","Antique"}; //7种特效
- extern u8 ov_sta; //在exit.c里 面定义
- extern u8 ov_frame; //在timer.c里面定义
- u16 r0,r1;
- u16 g0,g1;
- u16 b0,b1;
- //更新LCD显示
- void camera_refresh(void)
- {
-
- RESULT Resured;//判定为的目标条件
- RESULT Resugreen;
- RESULT Resublue;
-
- TARGET_CONDI Conditionred={215,240,20,240,30,160,30,30,320,240}; //红色1 API参数 hsl的阈值,识别时用的
- TARGET_CONDI Conditiongreen={70,110,20,240,50,100,30,30,320,240}; //绿色1 API参数 hls的阈值,识别时用的
- TARGET_CONDI Conditionblue={120,160,20,240,30,90,30,30,150,150}; //蓝色1 API参数 hsl的阈值,识别时用的
- u32 j;
- u16 color;
- if(ov_sta)//有帧中断更新?
- {
- LCD_Scan_Dir(U2D_L2R); //从上到下,从左到右
- if(lcddev.id==0X1963)LCD_Set_Window((lcddev.width-240)/2,(lcddev.height-320)/2,240,320);//将显示区域设置到屏幕中央
- else if(lcddev.id==0X5510||lcddev.id==0X5310)LCD_Set_Window((lcddev.width-320)/2,(lcddev.height-240)/2,320,240);//将显示区域设置到屏幕中央
- LCD_WriteRAM_Prepare(); //开始写入GRAM
- OV7670_RRST=0; //开始复位读指针
- OV7670_RCK_L;
- OV7670_RCK_H;
- OV7670_RCK_L;
- OV7670_RRST=1; //复位读指针结束
- OV7670_RCK_H;
- for(j=0;j<76800;j++)
- {
- OV7670_RCK_L;
- color=GPIOC->IDR&0XFF; //读数据
- OV7670_RCK_H;
- color<<=8;
- OV7670_RCK_L;
- color|=GPIOC->IDR&0XFF; //读数据
- OV7670_RCK_H;
- LCD->LCD_RAM=color;
- }
- if(Trace(&Conditionred,&Resured) ) //API
- {
- LCD_Fillred(Resured.x-Resured.w/2,Resured.y-Resured.h/2,Resured.x+Resured.w/2,Resured.y-Resured.h/2+1,0xf800);//u16 x,u16 y,u16 width,u16 hight,u16 Color
- LCD_Fillred(Resured.x-Resured.w/2,Resured.y-Resured.h/2,Resured.x-Resured.w/2+1,Resured.y+Resured.h/2,0xf800);
- LCD_Fillred(Resured.x-Resured.w/2,Resured.y+Resured.h/2,Resured.x+Resured.w/2,Resured.y+Resured.h/2+1,0xf800);
- LCD_Fillred(Resured.x+Resured.w/2,Resured.y-Resured.h/2,Resured.x+Resured.w/2+1,Resured.y+Resured.h/2,0xf800);
- LCD_Fillred(Resured.x-2,Resured.y-2,Resured.x+2,Resured.y+2,0xf800);
- //LED1=!LED1;
- r0=Resured.x;
- r1=Resured.y;
- }
- if(Trace1(&Conditiongreen,&Resugreen) ) //API
- {
- LCD_Fillgreen(Resugreen.x-Resugreen.w/2,Resugreen.y-Resugreen.h/2,Resugreen.x+Resugreen.w/2,Resugreen.y-Resugreen.h/2+1,0xf800);//u16 x,u16 y,u16 width,u16 hight,u16 Color
- LCD_Fillgreen(Resugreen.x-Resugreen.w/2,Resugreen.y-Resugreen.h/2,Resugreen.x-Resugreen.w/2+1,Resugreen.y+Resugreen.h/2,0xf800);
- LCD_Fillgreen(Resugreen.x-Resugreen.w/2,Resugreen.y+Resugreen.h/2,Resugreen.x+Resugreen.w/2,Resugreen.y+Resugreen.h/2+1,0xf800);
- LCD_Fillgreen(Resugreen.x+Resugreen.w/2,Resugreen.y-Resugreen.h/2,Resugreen.x+Resugreen.w/2+1,Resugreen.y+Resugreen.h/2,0xf800);
- LCD_Fillgreen(Resugreen.x-2,Resugreen.y-2,Resugreen.x+2,Resugreen.y+2,0xf800);
- // LED1=!LED1;
- g0=Resugreen.x;
- g1=Resugreen.y;
- }
- if(Trace2(&Conditionblue,&Resublue) ) //API
- {
- LCD_Fillblue(Resublue.x-Resublue.w/2,Resublue.y-Resublue.h/2,Resublue.x+Resublue.w/2,Resublue.y-Resublue.h/2+1,0xf800);//u16 x,u16 y,u16 width,u16 hight,u16 Color
- LCD_Fillblue(Resublue.x-Resublue.w/2,Resublue.y-Resublue.h/2,Resublue.x-Resublue.w/2+1,Resublue.y+Resublue.h/2,0xf800);
- LCD_Fillblue(Resublue.x-Resublue.w/2,Resublue.y+Resublue.h/2,Resublue.x+Resublue.w/2,Resublue.y+Resublue.h/2+1,0xf800);
- LCD_Fillblue(Resublue.x+Resublue.w/2,Resublue.y-Resublue.h/2,Resublue.x+Resublue.w/2+1,Resublue.y+Resublue.h/2,0xf800);
- LCD_Fillblue(Resublue.x-2,Resublue.y-2,Resublue.x+2,Resublue.y+2,0xf800);
- // LED1=!LED1;
- b0=Resublue.x;
- b1=Resublue.y;
- }
- ov_sta=0; //清零帧中断标记
- ov_frame++;
- LCD_Scan_Dir(DFT_SCAN_DIR); //恢复默认扫描方向
- }
- }
- int main(void)
- {
-
- u8 lightmode=0,saturation=2,contrast=2;
- u8 effect=0;
- u8 i=0;
- u8 msgbuf[15]; //消息缓存区
- u8 tm=0;
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
- uart_init(115200); //串口初始化为 115200
- usmart_dev.init(72); //初始化USMART
- LED_Init(); //初始化与LED连接的硬件接口
- KEY_Init(); //初始化按键
- LCD_Init(); //初始化LCD
- TPAD_Init(6); //触摸按键初始化
- POINT_COLOR=RED; //设置字体为红色
- LCD_ShowString(30,50,200,16,16,"ELITE STM32F103 ^_^");
- LCD_ShowString(30,210,200,16,16,"OV7670 Init...");
- while(OV7670_Init())//初始化OV7670
- {
- LCD_ShowString(30,210,200,16,16,"OV7670 Error!!");
- delay_ms(200);
- LCD_Fill(30,210,239,246,WHITE);
- delay_ms(200);
- }
- LCD_ShowString(30,210,200,16,16,"OV7670 Init OK");
- delay_ms(1500);
- OV7670_Light_Mode(lightmode);
- OV7670_Color_Saturation(saturation);
- OV7670_Contrast(contrast);
- OV7670_Special_Effects(effect);
-
- TIM6_Int_Init(10000,7199); //10Khz计数频率,1秒钟中断
- EXTI8_Init(); //使能定时器捕获
- OV7670_Window_Set(12,176,240,320); //设置窗口
- OV7670_CS=0;
- LCD_Clear(BLACK);
- while(1)
- {
-
- camera_refresh();//更新显示
- if(tm)
- {
- LCD_ShowString((lcddev.width-240)/2+30,(lcddev.height-320)/2+60,200,16,16,msgbuf);
- tm--;
- }
- i++;
- if(i==15)//DS0闪烁.
- {
- i=0;
- LED0=!LED0;
- }
- }
- }
复制代码
所有资料51hei提供下载:
识别红绿蓝并锁定.rar
(392.16 KB, 下载次数: 156)
|