摄像头二值化 STM32 ov7670
单片机源程序如下:
- #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"
- //ALIENTEK Mini STM32开发板扩展实验9
- //摄像头实验
- extern u8 ov_sta; //在exit.c里面定义
- extern u8 ov_frame; //在timer.c里面定义
- //更新LCD显示
- void camera_refresh(void)
- {
- u32 j,i=0;
- u16 color;
- u16 x[320],y[240],m,n;
- u16 Xmin,Xmax,Ymin,Ymax;
- 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;
- if(color<25000)
- {
- color=0x0000;
- m=j%320;
- n=j/320;
- if(i==0)
- {
- x=m;
- y=n;
- Xmin=m;
- Xmax=m;
- Ymin=n;
- Ymax=n;
- i++;
- }
- x=m;
- y=n;
- if((x-x[i-1])<50&&(y-y[i-1])<50)
- {
- if((x<315)&&(x>5)&&(y<235)&&(y>5))
- {
- if(x>Xmax)
- Xmax=x;
- if(x
- Xmin=x;
- if(y>Ymax)
- Ymax=y;
- if(y
- Ymin=y;
- i++;
- }
- }
- }
- else
- color=0xFFFF;
- LCD_WR_DATA(color);
- }
- POINT_COLOR=RED;
- LCD_DrawRectangle(Xmin,Ymin,Xmax,Ymax);
- 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)
- {
- u8 i;
- delay_init(); //延时函数初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
- uart_init(9600);
- OV7670_Init();
- LED_Init(); //初始化与LED连接的硬件接口
- LCD_Init(); //初始化LCD
- 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(); //更新显示
- if(i!=ov_frame) //DS0闪烁.
- {
- i=ov_frame;
- LED0=!LED0;
- }
- }
- }
所有资料51hei提供下载:
扩展实验9 摄像头实验.rar
|