stm32f4驱动ov2640图像处理
stm32单片机源程序如下:
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "key.h"
- #include "lcd.h"
- #include "ltdc.h"
- #include "sdram.h"
- #include "pcf8574.h"
- #include "timer.h"
- #include "ov5640.h"
- #include "usart2.h"
- #include "dcmi.h"
- #include "usmart.h"
- //分辨率 x,y
- #define X 160
- #define Y 120
- #define buf_size X*Y
- //提取R、G、B的参数
- #define RGB_R 0XF800
- #define RGB_G 0X07E0
- #define RGB_B 0X001F
- //fps、FPS数组存入采集回来的信息
- u32 fps[buf_size];
- u16 FPS[buf_size];
- //变量R,G,B
- u16 R,G,B;
- u16 threshold=32767;//阈值
- /*************
- 函数:灰度化且二值化函数
- 返回值类型:u16
- 返回值:二值化后的值
- 形参:采集的像素点数据
- *************/
- u16 Binary(u16 pixel)
- {
- static u16 Gray;
- /*******提取R,G,B值*******/
- R = (pixel&RGB_R)>>11;
- G = (pixel&RGB_G)>>5;
- B = (pixel&RGB_B);
- /*******灰度值计算*******/
- /*******网络上大部分的公式是针对8位的*******/
- /*******这条公式是针对12位的******/
- Gray = (u16)((R*634+G*613+B*232));
- /*******二值化*******/
- if(Gray<threshold)
- Gray = BLACK;
- else if(Gray>=threshold)
- Gray = WHITE;
- return Gray;
- }
- //处理数据
- //当采集完一帧数据后,调用此函数.开始下一帧采集.
- //该函数会被HAL_DCMI_FrameEventCallback调用
- void jpeg_data_process(void)
- {
- u16 i,j,l;
- l = 0;
- /*******显示二值化后的数据*******/
- for(j=0;j<Y;j++)
- {
- for(i=0;i<X;i++)
- {
- LCD_SetCursor(i,j);
- LCD_WriteRAM_Prepare(); //开始写入GRAM
- FPS[l] = fps[l];
-
- LCD->LCD_RAM=Binary(FPS[l]);
- // LCD->LCD_RAM=FPS[l];
- l++;
- }
- }
- l = 0;
- /*******显示原来的数据*******/
- for(j=0;j<Y;j++)
- {
- for(i=0;i<X;i++)
- {
- LCD_SetCursor(i+X+10,j);
- LCD_WriteRAM_Prepare(); //开始写入GRAM
- FPS[l] = fps[l];
-
- // LCD->LCD_RAM=Binary(FPS[l]);
- LCD->LCD_RAM=FPS[l];
- l++;
- }
- }
- }
- //RGB565测试
- //RGB数据直接显示在LCD上面
- void rgb565_test(void)
- {
- u8 key;
- u8 k;
- u16 outputheight=0;
- LCD_Clear(WHITE);
- POINT_COLOR=RED;
- //自动对焦初始化
- OV5640_RGB565_Mode(); //RGB565模式
- OV5640_Focus_Init();
-
- OV5640_Light_Mode(0); //自动模式
- OV5640_Color_Saturation(3);//色彩饱和度0
- OV5640_Brightness(4); //亮度0
- OV5640_Contrast(3); //对比度0
- OV5640_Sharpness(33); //自动锐度
- //OV5640_Focus_Constant();//启动持续对焦
- /*************这里初始化PCLK、VSYNC、HSYNC和数据口******************/
- DCMI_Init(); //DCMI配置
- DCMI_DMA_Init((u32)fps,0,buf_size,DMA_MDATAALIGN_HALFWORD,DMA_MINC_ENABLE);
- TIM3->CR1&=~(0x01); //关闭定时器3,关闭帧率统计,打开的话,RGB屏,在串口打印的时候,会抖
- outputheight=lcddev.height;
- OV5640_OutSize_Set(0,0,X*2,Y*2); //满屏缩放显示
- LCD_Clear(BLACK);
- delay_ms(100);
- LCD_SetCursor(0,0);
- DCMI_Start(); //启动传输
- while(1)
- {
- key=KEY_Scan(0);
- if(key==KEY0_PRES)
- OV5640_Focus_Single();
- else if(key==KEY1_PRES)
- {
- k++;
- if(k%2==1)
- OV5640_Flash_Ctrl(1);//打开闪光灯
- else
- OV5640_Flash_Ctrl(0);//关闭闪光灯
- }
- }
- }
- int main(void)
- {
- HAL_Init(); //初始化HAL库
- Stm32_Clock_Init(360,25,2,8); //设置时钟,180Mhz
- delay_init(180); //初始化延时函数
- uart_init(115200); //初始化USART
- usart2_init(921600); //初始化USART2
- usmart_dev.init(90); //初始化USMART
- LED_Init(); //初始化LED
- KEY_Init(); //初始化按键
- SDRAM_Init(); //初始化SDRAM
- LCD_Init(); //初始化LCD
- TIM3_Init(10000-1,9000-1); //10Khz计数,1秒钟中断一次
- POINT_COLOR=BLUE;//设置字体为红色
- LCD_ShowString(30,50,200,16,16,"Apollo STM32F4/F7");
- LCD_ShowString(30,70,200,16,16,"OV5640 TEST");
- LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
- LCD_ShowString(30,110,200,16,16,"2016/1/16");
- /*************这里初始化一些重要的引脚,SCL,SDA,RESET******************/
- while(OV5640_Init())//初始化OV5640
- {
- LCD_ShowString(30,130,240,16,16,"OV5640 ERR");
- delay_ms(200);
- LCD_Fill(30,130,239,170,WHITE);
- delay_ms(200);
- LED0=!LED0;
- }
- LCD_ShowString(30,130,200,16,16,"OV5640 OK");
- rgb565_test();
- }
复制代码
下载:
二值化5-成功.rar
(1.03 MB, 下载次数: 171)
|