|
stm32f103,正點(diǎn)原子的mini板。配合ov7670,能夠進(jìn)行顏色識(shí)別+舵機(jī)控制。圈出顏色范圍
代碼下載:
顏色識(shí)別+舵機(jī)控制.7z
(251.09 KB, 下載次數(shù): 779)
2021-10-25 14:24 上傳
點(diǎn)擊文件名下載附件
主程序:
- #include "sys.h"
- #include "usart.h"
- #include "delay.h"
- #include "led.h"
- #include "usmart.h"
- #include "lcd.h"
- #include "ov7670.h"
- #include "exti.h"
- #include "timer.h"
- #include "EasyTrace.h"
- #include "PWM.h"
- //ALIENTEK Mini STM32開(kāi)發(fā)板擴(kuò)展實(shí)驗(yàn)9
- //攝像頭實(shí)驗(yàn)
- extern u8 ov_sta; //在exit.c里面定義
- extern u8 ov_frame; //在timer.c里面定義
- u16 r,y;
- //更新LCD顯示
- void camera_refresh(void)
- {
- RESULT Resu;
- TARGET_CONDI Condition={215,240,20,240,30,160,30,30,320,240}; //API參數(shù) hls的閾值,識(shí)別時(shí)用的
-
- u32 j;
- u16 color;
- if(ov_sta==2)
- {
- LCD_Scan_Dir(U2D_L2R); //從上到下,從左到右
- LCD_SetCursor(0x00,0x0000); //設(shè)置光標(biāo)位置
- LCD_WriteRAM_Prepare(); //開(kāi)始寫入GRAM
- OV7670_CS=0;
- OV7670_RRST=0; //開(kāi)始復(fù)位讀指針
- OV7670_RCK=0;
- OV7670_RCK=1;
- OV7670_RCK=0;
- OV7670_RRST=1; //復(fù)位讀指針結(jié)束
- OV7670_RCK=1;
- for(j=0;j<76800;j++)
- {
- GPIOB->CRL=0X88888888;
- OV7670_RCK=0;
- color=OV7670_DATA; //讀數(shù)據(jù)
- OV7670_RCK=1;
- color<<=8;
- OV7670_RCK=0;
- color|=OV7670_DATA; //讀數(shù)據(jù)
- OV7670_RCK=1;
- GPIOB->CRL=0X33333333;
- //
- //加算法,例灰階轉(zhuǎn)換,二值化之類的
- //
- LCD_WR_DATA(color);
- }
- if(Trace(&Condition,&Resu) ) //API
- {
-
-
- LCD_Fill(Resu.x-Resu.w/2,Resu.y-Resu.h/2,Resu.x+Resu.w/2,Resu.y-Resu.h/2+1,0xf800);//u16 x,u16 y,u16 width,u16 hight,u16 Color
- LCD_Fill(Resu.x-Resu.w/2,Resu.y-Resu.h/2,Resu.x-Resu.w/2+1,Resu.y+Resu.h/2,0xf800);
- LCD_Fill(Resu.x-Resu.w/2,Resu.y+Resu.h/2,Resu.x+Resu.w/2,Resu.y+Resu.h/2+1,0xf800);
- LCD_Fill(Resu.x+Resu.w/2,Resu.y-Resu.h/2,Resu.x+Resu.w/2+1,Resu.y+Resu.h/2,0xf800);
- LCD_Fill(Resu.x-2,Resu.y-2,Resu.x+2,Resu.y+2,0xf800);
-
- LED1=!LED1;
- r=Resu.x;
- y=Resu.y;
- }
-
-
-
-
- OV7670_CS=1;
- OV7670_RCK=0;
- OV7670_RCK=1;
- EXTI->PR=1<<15; //清除LINE8上的中斷標(biāo)志位
- ov_sta=0; //開(kāi)始下一次采集
- ov_frame++;
- LCD_Scan_Dir(DFT_SCAN_DIR); //恢復(fù)默認(rèn)掃描方向
- }
- }
- int main(void)
- {
- u16 led0pwmval;
- static int t=0;
- u8 i;
- delay_init(); //延時(shí)函數(shù)初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 設(shè)置中斷優(yōu)先級(jí)分組2
- uart_init(9600);
- OV7670_Init();
- LED_Init(); //初始化與LED連接的硬件接口
- LCD_Init(); //初始化LCD
- TIM2_PWM_Init(199,7199); //PWM定時(shí)器設(shè)置
- if(lcddev.id==0X6804||lcddev.id==0X5310||lcddev.id==0X5510||lcddev.id==0X1963) //強(qiáng)制設(shè)置屏幕分辨率為320*240.以支持3.5寸大屏
- {
- lcddev.width=240;
- lcddev.height=320;
- }
- usmart_dev.init(72); //初始化USMART
-
- POINT_COLOR=RED;//設(shè)置字體為紅色
- 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,"2014/3/27");
- 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,150,239,166,WHITE);
- delay_ms(200);
- }
- LCD_ShowString(60,150,200,200,16,"OV7670 Init OK");
- delay_ms(1500);
- TIM3_Int_Init(10000,7199); //TIM3,10Khz計(jì)數(shù)頻率,1秒鐘中斷
- EXTI15_Init(); //使能定時(shí)器捕獲
- OV7670_Window_Set(10,174,240,320); //設(shè)置窗口
- OV7670_CS=0;
-
-
- while(1)
- {
- camera_refresh(); //更新顯示
-
- printf("Resu:%d-%d\r\n",r,y);
- if(y>120)
- t++;
- if(t==0)
- led0pwmval=188;
- else if(t==1)
- led0pwmval=190;
- else if(t==2)
- led0pwmval=194;
- else
- led0pwmval=194;
-
-
- printf("Result:%d\r\n",t);
- delay_ms(10);
- delay_ms(10);
-
- TIM_SetCompare3(TIM2,led0pwmval);
- printf("Re:%d\r\n",led0pwmval);
-
- if(i!=ov_frame) //DS0閃爍.
- {
- i=ov_frame;
- LED0=!LED0;
-
- }
- }
- }
復(fù)制代碼
|
評(píng)分
-
查看全部評(píng)分
|