基于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)
|