上图才有真相,电路虽然丑点但是可以用的。
花了几天时间用stm32和ov7670搞了个图像采集,这比我预想的要难很多。ov7670一百多个寄存器而且ov公司的datasheet简陋得常常让我想说一句:我去年买了个表。后来还是借鉴了网络上的一些寄存器配置,在加上苦读ov推出的资料终于还是搞出了个简陋的摸样出来。 话不多说了,给大家分享一下心得体会,以便帮助后面学习的朋友少走些弯路。
一:摄像头不比其他的传感器,有一定的难度,而且资料太过简陋,后面学习的朋友尽量买些成熟产品做开发,比如说淘宝买的开发套件。我就吃了这亏,stm32和摄像头完全从零做起,啥都没有,连上位机都准备自己写,这没必要时间成本太高了。 二:不要指望自己一个一个寄存器去配置,本来资料就简陋,有很多寄存器你看了资料未必能明白啥个意思,在网上找个配置方案(也可以借用我的配置方案)修改一些关键地方就行了。 三:注意焦距的调节,如果焦距调节不正确,可能画面模糊一片,慢慢调节一下焦距图像画面应该就出来了(前提是你已经采集到了图像) 四:注意SCCB(也就是I2C)的时序,可以借鉴我的初始化,但配置完成后用串口读取一些寄存器的值打印出来,看是否配置成功,如果这个都没有配置成功,你先别浪费时间找图像了。 五:网上能下载到的上位机,一般都会显示255的灰度图像,而ov7670是不能够输出灰度图像的,所以只能将输出格式配置成为YUV,然后人为去掉UV,将Y传给上位机显示就是灰度图像了。(注意网上下载的上位机软件可能会有一些协议,最好先弄清楚,要不然你显示肯定是显示不出来的) 六:分辨率配置 (具体配置参考代码)主要有一下几个寄存器 0x 17 HSTART 0x 18 HSTOP 0x 19 VSTRT 0x 1A VSTOP 0x 03 VREF HREF 的高电平宽度等于我们所要显示的一行 HREF = WEIGHT*2; 乘以二是因为每个像素点是有两个 HSTOP=START + HREF; 184 + 320*2 = 824; 然后在用824对784求余就是HSTOP的值(其原因,请仔细度ov7670的时序,如果懒得看,直接用就行) 七:配置为YUV格式关键寄存器配置:(引用于网络论坛,感谢他的分享) {0x12, 0x10},//QVGA YUV
{0x3a, 0x14},//使用固定UV输出
{0x3d, 0x80},//使用固定UV输出
{0x67, 0x11},//固定U值,0x11,方便测试
{0x68, 0xFF},//固定V值,0xFF,方便测试
{0x40, 0xC0},//初始配置,YUV模式,这个寄存器必须设置,否则不好使
为方便调试,都是用固定的U值和V值,
向67中写入11,向68中写入FF,出来的是XX11XXFF,说明配置成功。
要注意的就是{0x40, 0xC0},这个必须设置成初始化值,默认配置便是YUV,不能再用RGB565的配置。顺便说一句,YUV模式为YUV 4:2:2,不是说8位里面4位Y,2位U,2位V的意思, Y/U/V都是8位数据,只是U和V是隔着Y轮流输出的,高字节的是Y,低字节为U或V,做飞思卡尔智能车一般用的OV7620就是这样,一般只用Y的数据,用来判断黑白。 八:我的程序说明,各引脚(懒得打字,请看图片吧),串口用的是STM32的UART3,还有需要注意的是我是用一个外部按键作为一帧图片的发送,每按一次发送帧的图片信号,用的是PA3。 有不完善之处欢迎指正,请到原文出处下载工程和交流: http://user.qzone.qq.com/605678913/blog/1370697152#!app=2&via=QZ.HashRefresh&pos=1370697152
- void set_Cmos7670reg(void)
- {
- WrCmos7670(0x67,0xaa);
- WrCmos7670(0x68,0x55);
-
- WrCmos7670(0x3a,0x14);
- WrCmos7670(0x40,0xC0);
- WrCmos7670(0x12,0x10);
-
- WrCmos7670(0x32,0x80);
- WrCmos7670(0x17,0x16);
- WrCmos7670(0x18,0x04);
- WrCmos7670(0x19,0x02);
- WrCmos7670(0x1a,0x7b);
- WrCmos7670(0x03,0x06);
- WrCmos7670(0x0c,0x00);
- WrCmos7670(0x3e,0x00);
- WrCmos7670(0x70,0x3a);
- WrCmos7670(0x71,0x35);
- WrCmos7670(0x72,0x11);
- WrCmos7670(0x73,0x00);
- WrCmos7670(0xa2,0x02);
- WrCmos7670(0x11,0x81);
-
- WrCmos7670(0x7a,0x20);
- WrCmos7670(0x7b,0x1c);
- WrCmos7670(0x7c,0x28);
- WrCmos7670(0x7d,0x3c);
- WrCmos7670(0x7e,0x55);
- WrCmos7670(0x7f,0x68);
- WrCmos7670(0x80,0x76);
- WrCmos7670(0x81,0x80);
- WrCmos7670(0x82,0x88);
- WrCmos7670(0x83,0x8f);
- WrCmos7670(0x84,0x96);
- WrCmos7670(0x85,0xa3);
- WrCmos7670(0x86,0xaf);
- WrCmos7670(0x87,0xc4);
- WrCmos7670(0x88,0xd7);
- WrCmos7670(0x89,0xe8);
-
- WrCmos7670(0x13,0xe0);
- WrCmos7670(0x00,0x00);
-
- WrCmos7670(0x10,0x00);
- WrCmos7670(0x0d,0x00);
- WrCmos7670(0x14,0x28);
- WrCmos7670(0xa5,0x05);
- WrCmos7670(0xab,0x07);
- WrCmos7670(0x24,0x75);
- WrCmos7670(0x25,0x63);
- WrCmos7670(0x26,0xA5);
- WrCmos7670(0x9f,0x78);
- WrCmos7670(0xa0,0x68);
- WrCmos7670(0xa1,0x03);
- WrCmos7670(0xa6,0xdf);
- WrCmos7670(0xa7,0xdf);
- WrCmos7670(0xa8,0xf0);
- WrCmos7670(0xa9,0x90);
- WrCmos7670(0xaa,0x94);
- WrCmos7670(0x13,0xe5);
- WrCmos7670(0x0e,0x61);
- WrCmos7670(0x0f,0x4b);
- WrCmos7670(0x16,0x02);
- WrCmos7670(0x1e,0x37);
- WrCmos7670(0x21,0x02);
- WrCmos7670(0x22,0x91);
- WrCmos7670(0x29,0x07);
- WrCmos7670(0x33,0x0b);
- WrCmos7670(0x35,0x0b);
- WrCmos7670(0x37,0x1d);
- WrCmos7670(0x38,0x71);
- WrCmos7670(0x39,0x2a);
- WrCmos7670(0x3c,0x78);
- WrCmos7670(0x4d,0x40);
- WrCmos7670(0x4e,0x20);
- WrCmos7670(0x69,0x00);
- WrCmos7670(0x6b,0x60);
- WrCmos7670(0x74,0x19);
- WrCmos7670(0x8d,0x4f);
- WrCmos7670(0x8e,0x00);
- WrCmos7670(0x8f,0x00);
- WrCmos7670(0x90,0x00);
- WrCmos7670(0x91,0x00);
- WrCmos7670(0x92,0x00);
- WrCmos7670(0x96,0x00);
- WrCmos7670(0x9a,0x80);
- WrCmos7670(0xb0,0x84);
- WrCmos7670(0xb1,0x0c);
- WrCmos7670(0xb2,0x0e);
- WrCmos7670(0xb3,0x82);
- WrCmos7670(0xb8,0x0a);
- WrCmos7670(0x43,0x14);
- WrCmos7670(0x44,0xf0);
- WrCmos7670(0x45,0x34);
- WrCmos7670(0x46,0x58);
- WrCmos7670(0x47,0x28);
- WrCmos7670(0x48,0x3a);
- WrCmos7670(0x59,0x88);
- WrCmos7670(0x5a,0x88);
- WrCmos7670(0x5b,0x44);
- WrCmos7670(0x5c,0x67);
- WrCmos7670(0x5d,0x49);
- WrCmos7670(0x5e,0x0e);
- WrCmos7670(0x64,0x04);
- WrCmos7670(0x65,0x20);
- WrCmos7670(0x66, 0x05);
- WrCmos7670(0x94,0x04);
- WrCmos7670(0x95,0x08);
- WrCmos7670(0x6c,0x0a);
- WrCmos7670(0x6d,0x55);
- WrCmos7670(0x6e,0x11);
- WrCmos7670(0x6f,0x9f);
- WrCmos7670(0x6a,0x40);
- WrCmos7670(0x01,0x40);
- WrCmos7670(0x02,0x40);
- WrCmos7670(0x13,0xe7);
- WrCmos7670(0x15,0x00);
-
-
- WrCmos7670(0x4f,0x80);
- WrCmos7670(0x50,0x80);
- WrCmos7670(0x51,0x00);
- WrCmos7670(0x52,0x22);
- WrCmos7670(0x53,0x5e);
- WrCmos7670(0x54,0x80);
- WrCmos7670(0x58,0x9e);
-
- WrCmos7670(0x41,0x08);
- WrCmos7670(0x3f, 0x00);
- WrCmos7670(0x75,0x05);
- WrCmos7670(0x76,0xe1);
- WrCmos7670(0x4c,0x00);
- WrCmos7670(0x77,0x01);
- WrCmos7670(0x3d,0x80);
- WrCmos7670(0x4b,0x09);
- WrCmos7670(0xc9,0x60);
- WrCmos7670(0x41,0x38);
- WrCmos7670(0x56,0x40);
-
- WrCmos7670(0x34, 0x11);
- WrCmos7670(0x3b,0x02);
-
- WrCmos7670(0xa4,0x89);
- WrCmos7670(0x96,0x00);
- WrCmos7670(0x97,0x30);
- WrCmos7670(0x98,0x20);
- WrCmos7670(0x99,0x30);
- WrCmos7670(0x9a,0x84);
- WrCmos7670(0x9b,0x29);
- WrCmos7670(0x9c,0x03);
- WrCmos7670(0x9d, 0x4c);
- WrCmos7670(0x9e,0x3f);
- WrCmos7670(0x78,0x04);
-
- WrCmos7670(0x79,0x01);
- WrCmos7670(0xc8,0xf0);
- WrCmos7670(0x79,0x0f);
- WrCmos7670(0xc8,0x00);
- WrCmos7670(0x79,0x10);
- WrCmos7670(0xc8,0x7e);
- WrCmos7670(0x79,0x0a);
- WrCmos7670(0xc8, 0x80);
- WrCmos7670(0x79,0x0b);
- WrCmos7670(0xc8,0x01);
- WrCmos7670(0x79,0x0c);
- WrCmos7670(0xc8,0x0f);
- WrCmos7670(0x79,0x0d);
- WrCmos7670(0xc8,0x20);
- WrCmos7670(0x79,0x09);
- WrCmos7670(0xc8,0x80);
- WrCmos7670(0x79,0x02);
- WrCmos7670(0xc8,0xc0);
- WrCmos7670(0x79,0x03);
- WrCmos7670(0xc8,0x40);
- WrCmos7670(0x79,0x05);
- WrCmos7670(0xc8,0x30);
- WrCmos7670(0x79,0x26);
- WrCmos7670(0x09,0x00);
- }
- 图片采集发送
- void EXTI1_IRQHandler(void)
- {
- u16 i,j,k=0;
- u8 t1,t2;
-
- EXTI_InitTypeDef EXTI_InitStructure;
- NVIC_InitTypeDefNVIC_InitStructure;
-
- EXTI_ClearITPendingBit(EXTI_Line1);
-
- Vsync_Flag++;
-
- if(Vsync_Flag==1) //FIFOD
- {
- GPIO_SetBits(GPIOE,GPIO_Pin_5); //FIFO_WRST=1;
- __asm{nop;}
- GPIO_ResetBits(GPIOE,GPIO_Pin_5); //FIFO_WRST=0;
-
- delay_us(2);
- GPIO_SetBits(GPIOE,GPIO_Pin_5); //FIFO_WRST=1;
- GPIO_SetBits(GPIOE,GPIO_Pin_3);
- }
-
- if(Vsync_Flag==2)
- {
- GPIO_ResetBits(GPIOE,GPIO_Pin_3); //FIFO_WR=0; //½ûÖ¹Êý¾ÝдÈëFIFO
-
- EXTI->IMR&=~(1<<4); //¹Ø±ÕÍⲿÖжÏ
- EXTI->EMR&=~(1<<4);
-
- EXTI_InitStructure.EXTI_Line= EXTI_Line1;
- EXTI_InitStructure.EXTI_Mode= EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger= EXTI_Trigger_Falling;
- EXTI_InitStructure.EXTI_LineCmd= DISABLE;
- EXTI_Init(&EXTI_InitStructure); //
-
- NVIC_InitStructure.NVIC_IRQChannel= TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority= 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd= DISABLE;
- NVIC_Init(&NVIC_InitStructure); //
- GPIO_SetBits(GPIOE,GPIO_Pin_4); //FIFO_RCK=1;
-
- GPIO_ResetBits(GPIOE,GPIO_Pin_6); //FIFO_RRST=0; //¶ÁÖ¸Õ븴λ
- GPIO_ResetBits(GPIOE,GPIO_Pin_4); //FIFO_RCK=0;
- __asm{nop;}__asm{nop;}
- GPIO_SetBits(GPIOE,GPIO_Pin_4); //FIFO_RCK=1;
- __asm{nop;}__asm{nop;}
-
- GPIO_ResetBits(GPIOE,GPIO_Pin_4); //FIFO_RCK=0;
-
- GPIO_SetBits(GPIOE, GPIO_Pin_6); //FIFO_RRST=1;
- __asm{nop;}
- delay_us(2);
- GPIO_ResetBits(GPIOE,GPIO_Pin_2); //FIFO_OE=0;//
- __asm{nop;}
-
- USART3_Putc(0xff); //请注意:这里是发送协议信号,不是图像信号
-
- for(i = 0; i< 9600; i++)
- {
- for(j = 0; j < 8; j ++)
- {
- GPIO_ResetBits(GPIOE,GPIO_Pin_4);//FIFO_RCK=0;
- __asm{nop;}
-
- GPIO_SetBits(GPIOE,GPIO_Pin_4); //FIFO_RCK=1;
- __asm{nop;}
-
- t1=(0x00ff&GPIOC->IDR);
-
- GPIO_ResetBits(GPIOE,GPIO_Pin_4);//FIFO_RCK=0;
- __asm{nop;}
-
- GPIO_SetBits(GPIOE,GPIO_Pin_4); //FIFO_RCK=1;
- __asm{nop;}
-
- t2=(0x00ff&GPIOC->IDR);
- USART3_Putc(t1);
- }
- k=0; //
- }
-
- GPIO_SetBits(GPIOE,GPIO_Pin_2); //FIFO_OE=1;
-
- Vsync_Flag=0;
- NVIC_InitStructure.NVIC_IRQChannel= TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority= 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd= ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- EXTI->IMR|=(1<<4);
- EXTI->EMR|=(1<<4);
- }
- }
复制代码
完整文档下载:
stm32和ov7670图像采集串口上位机显示.docx
(993.85 KB, 下载次数: 269)
|