stm32f103,正点原子的mini板。配合ov7670,能够进行颜色识别+舵机控制。圈出颜色范围
代码下载:
颜色识别+舵机控制.7z
(251.09 KB, 下载次数: 779)
主程序:
- #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;
-
- }
- }
- }
复制代码
|