stm32f103,正點原子的mini板。配合ov7670,能夠進行顏色識別+舵機控制。圈出顏色范圍
代碼下載:
顏色識別+舵機控制.7z
(251.09 KB, 下載次數: 779)
2021-10-25 14:24 上傳
點擊文件名下載附件
主程序:
- #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開發板擴展實驗9
- //攝像頭實驗
- 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參數 hls的閾值,識別時用的
-
- 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);
- }
- 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上的中斷標志位
- ov_sta=0; //開始下一次采集
- ov_frame++;
- LCD_Scan_Dir(DFT_SCAN_DIR); //恢復默認掃描方向
- }
- }
- int main(void)
- {
- u16 led0pwmval;
- static int t=0;
- u8 i;
- delay_init(); //延時函數初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 設置中斷優先級分組2
- uart_init(9600);
- OV7670_Init();
- LED_Init(); //初始化與LED連接的硬件接口
- LCD_Init(); //初始化LCD
- TIM2_PWM_Init(199,7199); //PWM定時器設置
- if(lcddev.id==0X6804||lcddev.id==0X5310||lcddev.id==0X5510||lcddev.id==0X1963) //強制設置屏幕分辨率為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,"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計數頻率,1秒鐘中斷
- EXTI15_Init(); //使能定時器捕獲
- OV7670_Window_Set(10,174,240,320); //設置窗口
- 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;
-
- }
- }
- }
復制代碼
|