自己網上找的代碼,根據要求修改可二值化,識別黑色物體,用紅色框框框起來,串口返回坐標值
單片機源程序如下:
- #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"
- 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;
- RESULT Resured;//判定為的目標條件
- RESULT Resugreen;
- RESULT Resublue;
-
- //更新LCD顯示
- void camera_refresh(void)//讀取FIFO里面的數據
- {
- u32 j;
- u16 color;
- u16 k;
- // TARGET_CONDI Conditionred={160,160,0,0,240,240,30,30,320,240}; // API參數 hsl的閾值,識別時用的
- TARGET_CONDI Conditiongreen={0,240,0,240,0,50,30,30,320,240}; //黑色1 API參數 hls的閾值,識別時用的
-
- if(ov_sta)//有幀中斷更新?
- {
- LCD_Scan_Dir(U2D_L2R); //從上到下,從左到右
- if(lcddev.id==0X1963)LCD_Set_Window((lcddev.width-240)/2,(lcddev.height-320)/2,960,640);//將顯示區域設置到屏幕中央
- else if(lcddev.id==0X5510||lcddev.id==0X5310)LCD_Set_Window(0,0,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;
- k=color>>11;//rrrrrggggggbbbb
- if(k>=0x0008)
- {
- color=0xffff;//全白
- }
- else
- {
- color=0x0000;//全黑
- }
- LCD->LCD_RAM=color;
- }
-
- 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;
- }
- ov_sta=0; //清零幀中斷標記
- ov_frame++;
- LCD_Scan_Dir(DFT_SCAN_DIR); //恢復默認掃描方向
- }
- }
- int main(void)
- {
- u8 key;
- u8 lightmode=0,saturation=2,brightness=2,contrast=2;
- u8 effect=6;
- 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,"WarShip STM32");
- LCD_ShowString(30,70,200,16,16,"OV7670 TEST");
- LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
- LCD_ShowString(30,110,200,16,16,"2015/1/18");
- LCD_ShowString(30,130,200,16,16,"KEY0:Light Mode");
- LCD_ShowString(30,150,200,16,16,"KEY1:Saturation");
- LCD_ShowString(30,170,200,16,16,"KEY2:Brightness");
- LCD_ShowString(30,190,200,16,16,"KEY_UP:Contrast");
- LCD_ShowString(30,210,200,16,16,"TPAD:Effects");
- LCD_ShowString(30,230,200,16,16,"OV7670 Init...");
- while(OV7670_Init())//初始化OV7670
- {
- LCD_ShowString(30,230,200,16,16,"OV7670 Error!!");
- delay_ms(200);
- LCD_Fill(30,230,239,246,WHITE);
- delay_ms(200);
- }
- LCD_ShowString(30,230,200,16,16,"OV7670 Init OK");
- delay_ms(1500);
- OV7670_Light_Mode(lightmode);
- OV7670_Color_Saturation(saturation);
- OV7670_Brightness(brightness);
- OV7670_Contrast(contrast);
- OV7670_Special_Effects(effect); //是特效
- TIM6_Int_Init(10000,7199); //10Khz計數頻率,1秒鐘中斷
- EXTI8_Init(); //使能定時苃 n韃痘?
- // OV7670_Window_Set(12,176,240,320); //設置窗口
- OV7670_config_window(12,176,240,320);
- OV7670_CS=0;
- LCD_Clear(BLACK);
- while(1)
- {
- key=KEY_Scan(0);//不支持連按
- if(key)
- {
- tm=20;
- switch(key)
- {
- case KEY0_PRES: //燈光模式Light Mode
- lightmode++;
- if(lightmode>4)lightmode=0;
- OV7670_Light_Mode(lightmode);
- sprintf((char*)msgbuf,"%s",LMODE_TBL[lightmode]);
- break;
- case KEY1_PRES: //飽和度Saturation
- saturation++;
- if(saturation>4)saturation=0;
- OV7670_Color_Saturation(saturation);
- sprintf((char*)msgbuf,"Saturation:%d",(signed char)saturation-2);
- break;
- case KEY2_PRES: //亮度Brightness
- brightness++;
- if(brightness>4)brightness=0;
- OV7670_Brightness(brightness);
- sprintf((char*)msgbuf,"Brightness:%d",(signed char)brightness-2);
- break;
- case WKUP_PRES: //對比度Contrast
- contrast++;
- if(contrast>4)contrast=0;
- OV7670_Contrast(contrast);
- sprintf((char*)msgbuf,"Contrast:%d",(signed char)contrast-2);
- break;
- }
- }
- if(TPAD_Scan(0))//檢測到觸摸按鍵
- {
- effect++;
- if(effect>6)effect=0;
- OV7670_Special_Effects(effect);//設置特效
- sprintf((char*)msgbuf,"%s",EFFECTS_TBL[effect]);
- tm=20;
- }
- 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;
- printf("x坐標=%d\r\n",g0);
- printf("y坐標=%d\r\n",g1);
- }
- }
- }
復制代碼
所有資料51hei提供下載:
實驗35 攝像頭實驗.rar
(394.47 KB, 下載次數: 177)
2018-7-19 00:06 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|