基于OV7670和stm32的顏色識別算法
單片機源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "lcd.h"
- #include "usart.h"
- #include "string.h"
- #include "ov7670.h"
- #include "timer.h"
- #include "exti.h"
- #include "Trace.h"
- #include "can.h"
- u8 Way_Angle=1; //獲取角度的算法,1:四元數 2:卡爾曼 3:互補濾波
- u8 Flag_Qian,Flag_Hou,Flag_Left,Flag_Right,Flag_Direction=0; //藍牙遙控相關的變量
- u8 Flag_Stop=1,Flag_Show=0; //停止標志位和 顯示標志位 默認停止 顯示打開
- int Encoder_A,Encoder_B,Encoder_C; //編碼器的脈沖計數
- int Encoder_A_EXTI;
- int Motor_A,Motor_B,Motor_C; //電機PWM變量
- int Target_A,Target_B,Target_C; //電機目標速度
- int Temperature; //顯示溫度
- int Voltage; //電池電壓采樣相關的變量
- float Angle_Balance,Gyro_Balance,Gyro_Turn; //平衡傾角 平衡陀螺儀 轉向陀螺儀
- float Show_Data_Mb; //全局顯示變量,用于顯示需要查看的數據
- u32 Distance; //超聲波測距
- u8 delay_50,delay_flag; //默認情況下,不開啟避障功能,長按用戶按鍵2s以上可以進入避障模式
- int Velocity=30;
- u8 Usart3_Receive;
- u8 rxbuf[8],Rxbuf[16];
- u8 txbuf[8];
- int AZ,GZ;
- float Pitch,Roll,Yaw;
- u32 count,usart_count;
- int temp,temp2,temp3,count_temp,flag;
- u8 ON_txbuf[8]={10,12,15,19,24,30,37,1} ;
- u8 Velocity_txbuf[8]={11,13,16,20,25,31,38,50} ;
- void car(u16 command);
- void carturn(u16 commandturn,u16 turnangle);
- char* bian(int a);
- u8 flag2=1;
- 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 r,y;
- u16 i;
- u16 j;
- unsigned short C16;
- //更新LCD顯示
- void camera_refresh(void)
- {
- RESULT Resu;
- TARGET_CONDI Condition={0,240,0,240,200,210,30,30,320,240}; //API參數 hls的閾值,識別時用的
- u16 color;
- if(ov_sta==2)
- {
- LCD_Scan_Dir(U2D_L2R); //從上到下,從左到右
- LCD_SetCursor(0x00,0x0000); //設置光標位置
- LCD_WriteRAM_Prepare(); //開始寫入GRAM
- OV7670_RRST=0; //開始復位讀指針
- OV7670_RCK=0;
- OV7670_RCK=1;
- OV7670_RCK=0;
- OV7670_RRST=1; //復位讀指針結束
- OV7670_RCK=1;
- for(i=0;i<240;i++)
- {
- for(j=0;j<320;j++)
- {
- OV7670_RCK=0;
- color=GPIOC->IDR&0XFF; //讀數據
- OV7670_RCK=1;
- color<<=8;
- OV7670_RCK=0;
- color|=GPIOC->IDR&0XFF; //讀數據
- OV7670_RCK=1;
- LCD->LCD_RAM=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;
-
- LCD_ShowString(60,130,200,16,16,bian(r));
-
- if(r>130) //偏左//右轉
- {
- txbuf[0]=10;
- txbuf[1]=12;
- txbuf[2]=15;
- txbuf[3]=19;
- txbuf[4]=24;
- txbuf[5]=30;
- txbuf[6]=37;
- txbuf[7]=1; //00000111
-
- CAN1_SEND(0X121,txbuf);//CAN發送
- carturn(2,3);
- //flag2=1;
- //car(1);
- }
- if(r<110)//偏右//左轉
- {
- txbuf[0]=10;
- txbuf[1]=12;
- txbuf[2]=15;
- txbuf[3]=19;
- txbuf[4]=24;
- txbuf[5]=30;
- txbuf[6]=37;
- txbuf[7]=1; //00000111
-
- CAN1_SEND(0X121,txbuf);//CAN發送
- carturn(1,3);
- //flag2=2;
- //car(1);
-
- }
- /*else if((r<=130)&&(r>=110))//前
- {
- car(1);
- }
- else
- LCD_ShowString(60,130,200,16,16,"Error");*/
- }
- /*else
- {
-
- carturn(1,3);
- }*/
-
-
-
- EXTI_ClearITPendingBit(EXTI_Line8); //清除LINE8上的中斷標志位
- ov_sta=0; //開始下一次采集
- ov_frame++;
- LCD_Scan_Dir(DFT_SCAN_DIR); //恢復默認掃描方向
- }
- }
- int main(void)
- {
- u8 key;
- u8 lightmode=2,saturation=2,brightness=2,contrast=2;
- u8 effect=0;
- u8 i=0;
- u8 msgbuf[15];//消息緩存區
- u8 tm=0;
- delay_init(); //延時函數初始化
- NVIC_Configuration(); //設置NVIC中斷分組2:2位搶占優先級,2位響應優先級
- uart_init(9600); //串口初始化為9600
- LED_Init(); //LED端口初始化
- LCD_Init();
- if(lcddev.id==0X6804||lcddev.id==0X5310) //強制設置屏幕分辨率為320*240.以支持3.5寸大屏
- {
- lcddev.width=240;
- lcddev.height=320;
- }
-
- POINT_COLOR=RED;//設置字體為紅色
- LCD_ShowString(60,130,200,16,16,"S1:Light Mode");
- LCD_ShowString(60,150,200,16,16,"KS2:Saturation");
- LCD_ShowString(60,170,200,16,16,"S3:Brightness");
- LCD_ShowString(60,190,200,16,16,"S4:Contrast");
- LCD_ShowString(60,210,200,16,16,"TPAD(SD2):Effects");
- LCD_ShowString(60,230,200,16,16,"OV7670 Init...");
- while(OV7670_Init())//初始化OV7670
- {
- LCD_ShowString(60,230,200,16,16,"OV7670 Error!!");
- delay_ms(200);
- LCD_Fill(60,230,239,246,WHITE);
- delay_ms(200);
- }
- LCD_ShowString(60,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(); //使能定時器捕獲
- OV7670_Window_Set(10,174,240,320); //設置窗口
- OV7670_CS=0;
- CAN1_Mode_Init(1,2,3,6,0); //CAN初始化
-
- while(1)
- {
-
-
- /*txbuf[0]=10;
- txbuf[1]=12;
- txbuf[2]=15;
- txbuf[3]=19;
- txbuf[4]=24;
- txbuf[5]=30;
- txbuf[6]=37;
- txbuf[7]=1; //00000111
- CAN1_SEND(0X121,txbuf);//CAN發送*/
-
- camera_refresh();//更新顯示
-
-
- }
- }
- void car(u16 command)
- {
- switch(command)
- {
- case 1: //前進,速度為25
- txbuf[0]=1;
- txbuf[1]=0;
- txbuf[2]=0;
- txbuf[3]=0;
- txbuf[4]=3;
- txbuf[5]=0;
- txbuf[6]=0;
- txbuf[7]=0x00; //00000111
- CAN1_SEND(0X121,txbuf);
- break;
- case 2: //后退,速度為25
- txbuf[0]=1;
- txbuf[1]=0;
- txbuf[2]=0;
- txbuf[3]=0;
- txbuf[4]=25;
- txbuf[5]=0;
- txbuf[6]=0;
- txbuf[7]=0x02; //00000111
- CAN1_SEND(0X121,txbuf);
- break;
- case 3: //向右,速度為25
- txbuf[0]=1;
- txbuf[1]=0;
- txbuf[2]=25;
- txbuf[3]=0;
- txbuf[4]=0;
- txbuf[5]=0;
- txbuf[6]=0;
- txbuf[7]=0x00; //00000111
- CAN1_SEND(0X121,txbuf);
- break;
- case 4: //向左,速度為25
- txbuf[0]=1;
- txbuf[1]=0;
- txbuf[2]=25;
- txbuf[3]=0;
- txbuf[4]=0;
- txbuf[5]=0;
- txbuf[6]=0;
- txbuf[7]=0x04; //00000111
- CAN1_SEND(0X121,txbuf);
- break;
- default:printf("error");
-
- }
- }
- void carturn(u16 commandturn,u16 turnangle)
- {
- if(commandturn==1)//右轉turnangle 角度
- {
- txbuf[0]=1;
- txbuf[1]=0;
- txbuf[2]=0;
- txbuf[3]=0;
- txbuf[4]=0;
- txbuf[5]=0;
- txbuf[6]=turnangle;
- txbuf[7]=0x00; //00000111
- CAN1_SEND(0X121,txbuf);
- }
- else if(commandturn==2)//左轉turnangle角度
- {
- txbuf[0]=1;
- txbuf[1]=0;
- txbuf[2]=0;
- txbuf[3]=0;
- txbuf[4]=0;
- txbuf[5]=0;
- txbuf[6]=turnangle;
- txbuf[7]=0x03; //00000111
- CAN1_SEND(0X121,txbuf);
- }
- else
- printf("error");
- }
- char* bian(int a){
- int i=0,l;
- char*s;
- char n[10];
- while(a>0){n[i++]=a%10;a/=10;}
- l=--i;
- for(;i>=0;i--)s[l-i]=n[i]+'0';
- s[l+1]=0;
- return s;
- }
復制代碼 所有資料51hei提供下載:
Track2.rar
(439.27 KB, 下載次數: 31)
2018-5-16 00:31 上傳
點擊文件名下載附件
識別顏色
|