找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1948|回复: 0
打印 上一主题 下一主题
收起左侧

OV7670带FIFO的CMOS摄像头STM32程序

[复制链接]
跳转到指定楼层
楼主
ID:569034 发表于 2019-6-21 18:17 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
图像采集程序
  1. #include "stm32f10x.h"                  
  2. #include<stdio.h>
  3. //应用层函数头文件
  4. #include"Application.h"        
  5. /*与板子相关的头文件*/
  6. #include"LED.h"
  7. #include"KEY.h"
  8. #include"EXIT.h"
  9. #include"USART.h"
  10. #include"delay.h"
  11. #include"I2C.h"         
  12. #include"OV7670.h"

  13. uint8_t PIC[R][C];
  14. //当前的状态
  15. //下一步的状态
  16. OV7670_State OV_State=Wait_Vsync1;
  17. u16 TrueLine;
  18. u8 datareadyflag=0;



  19. /*
  20.         摄像头模块初始化

  21. */
  22. void Data_Bus_Config(void)
  23. {                                          
  24.         
  25.         /*通用GPIO结构体声明*/
  26.         GPIO_InitTypeDef GPIO_InitStructure;
  27.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;        
  28.         /*开启时钟*/        
  29.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);               
  30.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);               
  31.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);

  32.         GPIO_InitStructure.GPIO_Pin=D0_Pin;               
  33.         GPIO_Init(D0_Port,&GPIO_InitStructure);
  34.         GPIO_InitStructure.GPIO_Pin=D1_Pin;               
  35.         GPIO_Init(D1_Port,&GPIO_InitStructure);
  36.         GPIO_InitStructure.GPIO_Pin=D2_Pin;               
  37.         GPIO_Init(D2_Port,&GPIO_InitStructure);
  38.         GPIO_InitStructure.GPIO_Pin=D3_Pin;               
  39.         GPIO_Init(D3_Port,&GPIO_InitStructure);
  40.         GPIO_InitStructure.GPIO_Pin=D4_Pin;               
  41.         GPIO_Init(D4_Port,&GPIO_InitStructure);
  42.         GPIO_InitStructure.GPIO_Pin=D5_Pin;               
  43.         GPIO_Init(D5_Port,&GPIO_InitStructure);
  44.         GPIO_InitStructure.GPIO_Pin=D6_Pin;               
  45.         GPIO_Init(D6_Port,&GPIO_InitStructure);
  46.         GPIO_InitStructure.GPIO_Pin=D7_Pin;               
  47.         GPIO_Init(D7_Port,&GPIO_InitStructure);

  48.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  49.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  50.         //PWDN
  51.         GPIO_InitStructure.GPIO_Pin=PWDN_Pin;
  52.         GPIO_Init(PWDN_Port,&GPIO_InitStructure);
  53.         //RESET
  54.         GPIO_InitStructure.GPIO_Pin=RESET_Pin;
  55.         GPIO_Init(RESET_Port,&GPIO_InitStructure);
  56.         //FIFO_RCK
  57.         GPIO_InitStructure.GPIO_Pin=FIFO_RCK_Pin;
  58.         GPIO_Init(FIFO_RCK_Port,&GPIO_InitStructure);
  59.         //FIFO_EO
  60.         GPIO_InitStructure.GPIO_Pin=FIFO_OE_Pin;
  61.         GPIO_Init(FIFO_OE_Port,&GPIO_InitStructure);
  62.         //FIFO_RRST
  63.         GPIO_InitStructure.GPIO_Pin=FIFO_RRST_Pin;
  64.         GPIO_Init(FIFO_RRST_Port,&GPIO_InitStructure);
  65.         //FIFO_WR
  66.         GPIO_InitStructure.GPIO_Pin=FIFO_WR_Pin;
  67.         GPIO_Init(FIFO_WR_Port,&GPIO_InitStructure);
  68.         //FIFO_WRST
  69.         GPIO_InitStructure.GPIO_Pin=FIFO_WRST_Pin;
  70.         GPIO_Init(FIFO_WRST_Port,&GPIO_InitStructure);
  71.         //STORBE
  72.         GPIO_InitStructure.GPIO_Pin=STORBE_Pin;
  73.         GPIO_Init(STORBE_Port,&GPIO_InitStructure);
  74. }
  75. void OV7670_Init(void)
  76. {
  77.         /*I2C控制引脚初始化*/
  78.         I2C_Configuration();
  79.         /*数据总线与控制总线初始化*/
  80.         Data_Bus_Config();
  81.         OV7670_Register_Init();
  82.         /*以上初始化结束之后才可以初始化下面的*/
  83.         EXTI_ClearITPendingBit(EXTI_Line0);
  84.         EXTI_Config(EN);

  85.         
  86. }
  87. void OV7670_Register_Init(void)
  88. {
  89.         PWDN_L;/*电源工作方式*/
  90.         RESET_L;//寄存器复位
  91.         delay_ms(20);
  92.         RESET_H;

  93.         //YUV格式
  94.         I2C_WriteByte(0x12,0x80);
  95.         I2C_WriteByte(0x3a, 0x04);
  96.         I2C_WriteByte(0x40, 0xd0);
  97.         I2C_WriteByte(0x12, 0x14);
  98.         I2C_WriteByte(0x32, 0x80);
  99.         I2C_WriteByte(0x17, 0x16);
  100.         I2C_WriteByte(0x18, 0x04);
  101.         I2C_WriteByte(0x19, 0x02);
  102.         I2C_WriteByte(0x1a, 0x7b);
  103.         I2C_WriteByte(0x03, 0x06);
  104.         I2C_WriteByte(0x0c, 0x00);
  105.         I2C_WriteByte(0x3e, 0x00);
  106.         I2C_WriteByte(0x70, 0x3a);
  107.         I2C_WriteByte(0x71, 0x35);
  108.         I2C_WriteByte(0x72, 0x11);
  109.         I2C_WriteByte(0x73, 0x00);
  110.         I2C_WriteByte(0xa2, 0x02);
  111.         I2C_WriteByte(0x11, 0x81);         
  112.         I2C_WriteByte(0x7a, 0x20);
  113.         I2C_WriteByte(0x7b, 0x1c);
  114.         I2C_WriteByte(0x7c, 0x28);
  115.         I2C_WriteByte(0x7d, 0x3c);
  116.         I2C_WriteByte(0x7e, 0x55);
  117.         I2C_WriteByte(0x7f, 0x68);
  118.         I2C_WriteByte(0x80, 0x76);
  119.         I2C_WriteByte(0x81, 0x80);
  120.         I2C_WriteByte(0x82, 0x88);
  121.         I2C_WriteByte(0x83, 0x8f);
  122.         I2C_WriteByte(0x84, 0x96);
  123.         I2C_WriteByte(0x85, 0xa3);
  124.         I2C_WriteByte(0x86, 0xaf);
  125.         I2C_WriteByte(0x87, 0xc4);
  126.         I2C_WriteByte(0x88, 0xd7);
  127.         I2C_WriteByte(0x89, 0xe8);        
  128.         I2C_WriteByte(0x13, 0xe0);
  129.         I2C_WriteByte(0x00, 0x00);         
  130.         I2C_WriteByte(0x10, 0x00);
  131.         I2C_WriteByte(0x0d, 0x00);
  132.         I2C_WriteByte(0x14, 0x28);
  133.         I2C_WriteByte(0xa5, 0x05);
  134.         I2C_WriteByte(0xab, 0x07);
  135.         I2C_WriteByte(0x24, 0x75);
  136.         I2C_WriteByte(0x25, 0x63);
  137.         I2C_WriteByte(0x26, 0xA5);
  138.         I2C_WriteByte(0x9f, 0x78);
  139.         I2C_WriteByte(0xa0, 0x68);
  140.         I2C_WriteByte(0xa1, 0x03);
  141.         I2C_WriteByte(0xa6, 0xdf);
  142.         I2C_WriteByte(0xa7, 0xdf);
  143.         I2C_WriteByte(0xa8, 0xf0);
  144.         I2C_WriteByte(0xa9, 0x90);
  145.         I2C_WriteByte(0xaa, 0x94);
  146.         I2C_WriteByte(0x13, 0xe5);
  147.         I2C_WriteByte(0x0e, 0x61);
  148.         I2C_WriteByte(0x0f, 0x4b);
  149.         I2C_WriteByte(0x16, 0x02);
  150.         I2C_WriteByte(0x1e, 0x37);
  151.         I2C_WriteByte(0x21, 0x02);
  152.         I2C_WriteByte(0x22, 0x91);
  153.         I2C_WriteByte(0x29, 0x07);
  154.         I2C_WriteByte(0x33, 0x0b);
  155.         I2C_WriteByte(0x35, 0x0b);
  156.         I2C_WriteByte(0x37, 0x1d);
  157.         I2C_WriteByte(0x38, 0x71);
  158.         I2C_WriteByte(0x39, 0x2a);
  159.         I2C_WriteByte(0x3c, 0x78);
  160.         I2C_WriteByte(0x4d, 0x40);
  161.         I2C_WriteByte(0x4e, 0x20);
  162.         I2C_WriteByte(0x69, 0x00);
  163.         I2C_WriteByte(0x6b, 0x60);
  164.         I2C_WriteByte(0x74, 0x19);
  165.         I2C_WriteByte(0x8d, 0x4f);
  166.         I2C_WriteByte(0x8e, 0x00);
  167.         I2C_WriteByte(0x8f, 0x00);
  168.         I2C_WriteByte(0x90, 0x00);
  169.         I2C_WriteByte(0x91, 0x00);
  170.         I2C_WriteByte(0x92, 0x00);
  171.         I2C_WriteByte(0x96, 0x00);
  172.         I2C_WriteByte(0x9a, 0x80);
  173.         I2C_WriteByte(0xb0, 0x84);
  174.         I2C_WriteByte(0xb1, 0x0c);
  175.         I2C_WriteByte(0xb2, 0x0e);
  176.         I2C_WriteByte(0xb3, 0x82);
  177.         I2C_WriteByte(0xb8, 0x0a);
  178.         I2C_WriteByte(0x43, 0x14);
  179.         I2C_WriteByte(0x44, 0xf0);
  180.         I2C_WriteByte(0x45, 0x34);
  181.         I2C_WriteByte(0x46, 0x58);
  182.         I2C_WriteByte(0x47, 0x28);
  183.         I2C_WriteByte(0x48, 0x3a);
  184.         I2C_WriteByte(0x59, 0x88);
  185.         I2C_WriteByte(0x5a, 0x88);
  186.         I2C_WriteByte(0x5b, 0x44);
  187.         I2C_WriteByte(0x5c, 0x67);
  188.         I2C_WriteByte(0x5d, 0x49);
  189.         I2C_WriteByte(0x5e, 0x0e);
  190.         I2C_WriteByte(0x64, 0x04);
  191.         I2C_WriteByte(0x65, 0x20);
  192.         I2C_WriteByte(0x66, 0x05);
  193.         I2C_WriteByte(0x94, 0x04);
  194.         I2C_WriteByte(0x95, 0x08);
  195.         I2C_WriteByte(0x6c, 0x0a);
  196.         I2C_WriteByte(0x6d, 0x55);
  197.         I2C_WriteByte(0x6e, 0x11);
  198.         I2C_WriteByte(0x6f, 0x9f);
  199.         I2C_WriteByte(0x6a, 0x40);
  200.         I2C_WriteByte(0x01, 0x40);
  201.         I2C_WriteByte(0x02, 0x40);
  202.         I2C_WriteByte(0x13, 0xe7);
  203.         I2C_WriteByte(0x15, 0x00);         
  204.         I2C_WriteByte(0x4f, 0x80);
  205.         I2C_WriteByte(0x50, 0x80);
  206.         I2C_WriteByte(0x51, 0x00);
  207.         I2C_WriteByte(0x52, 0x22);
  208.         I2C_WriteByte(0x53, 0x5e);
  209.         I2C_WriteByte(0x54, 0x80);
  210.         I2C_WriteByte(0x58, 0x9e);         
  211.         I2C_WriteByte(0x41, 0x08);
  212.         I2C_WriteByte(0x3f, 0x00);
  213.         I2C_WriteByte(0x75, 0x05);
  214.         I2C_WriteByte(0x76, 0xe1);
  215.         I2C_WriteByte(0x4c, 0x00);
  216.         I2C_WriteByte(0x77, 0x01);
  217.         I2C_WriteByte(0x3d, 0xc2);        
  218.         I2C_WriteByte(0x4b, 0x09);
  219.         I2C_WriteByte(0xc9, 0x60);
  220.         I2C_WriteByte(0x41, 0x38);
  221.         I2C_WriteByte(0x56, 0x40);        
  222.         I2C_WriteByte(0x34, 0x11);
  223.         I2C_WriteByte(0x3b, 0x02);                                                                 
  224.         I2C_WriteByte(0xa4, 0x89);
  225.         I2C_WriteByte(0x96, 0x00);
  226.         I2C_WriteByte(0x97, 0x30);
  227.         I2C_WriteByte(0x98, 0x20);
  228.         I2C_WriteByte(0x99, 0x30);
  229.         I2C_WriteByte(0x9a, 0x84);
  230.         I2C_WriteByte(0x9b, 0x29);
  231.         I2C_WriteByte(0x9c, 0x03);
  232.         I2C_WriteByte(0x9d, 0x4c);
  233.         I2C_WriteByte(0x9e, 0x3f);
  234.         I2C_WriteByte(0x78, 0x04);        
  235.         I2C_WriteByte(0x79, 0x01);
  236.         I2C_WriteByte(0xc8, 0xf0);
  237.         I2C_WriteByte(0x79, 0x0f);
  238.         I2C_WriteByte(0xc8, 0x00);
  239.         I2C_WriteByte(0x79, 0x10);
  240.         I2C_WriteByte(0xc8, 0x7e);
  241.         I2C_WriteByte(0x79, 0x0a);
  242.         I2C_WriteByte(0xc8, 0x80);
  243.         I2C_WriteByte(0x79, 0x0b);
  244.         I2C_WriteByte(0xc8, 0x01);
  245.         I2C_WriteByte(0x79, 0x0c);
  246.         I2C_WriteByte(0xc8, 0x0f);
  247.         I2C_WriteByte(0x79, 0x0d);
  248.         I2C_WriteByte(0xc8, 0x20);
  249.         I2C_WriteByte(0x79, 0x09);
  250.         I2C_WriteByte(0xc8, 0x80);
  251.         I2C_WriteByte(0x79, 0x02);
  252.         I2C_WriteByte(0xc8, 0xc0);
  253.         I2C_WriteByte(0x79, 0x03);
  254.         I2C_WriteByte(0xc8, 0x40);
  255.         I2C_WriteByte(0x79, 0x05);
  256.         I2C_WriteByte(0xc8, 0x30);
  257.         I2C_WriteByte(0x79, 0x26);
  258.         I2C_WriteByte(0x09, 0x00);        



  259.         /*
  260.         

  261. //        {0x3a, 0x04},//RGB
  262.         {0x12, 0x10},//QVGA  YUV
  263.         {0x3a, 0x14},//使用固定UV输出
  264.         {0x3d, 0x80},//使用固定UV输出
  265.                                                         //0         0       :      Y U Y V (3a:14 3d:80)
  266.                                                         //0         1       :      Y V Y U  (3a:14 3d:81)
  267.                                                         //1         0       :      U Y V Y (3a:18 3d:80)
  268.                                                         //1         1       :      V Y U Y (3a:18 3d:81)
  269.         {0x67, 0x11},//固定U值,0x11,方便测试
  270.         {0x68, 0xFF},//固定V值,0xFF,方便测试
  271. //        {0x40, 0x10},//RGB565
  272.         {0x40, 0xC0},//初始配置,YUV模式,这个寄存器必须设置,否则不好使
  273. //        {0x12, 0x14},//QVGA  RGB,


  274.            I2C_WriteByte(0x12, 0x10);
  275.            I2C_WriteByte (0x3a, 0x14);
  276.            I2C_WriteByte (0x3d, 0x80);
  277.            I2C_WriteByte(0x67, 0x11);
  278.            I2C_WriteByte(0x68, 0xFF);
  279.            I2C_WriteByte(0x40, 0xC0);
  280.          

  281.         */
  282.            I2C_WriteByte(0x12, 0x10);
  283.            I2C_WriteByte (0x3a, 0x14);
  284.            I2C_WriteByte (0x3d, 0x80);
  285.            I2C_WriteByte(0x67, 0x11);
  286.            I2C_WriteByte(0x68, 0xFF);
  287.            I2C_WriteByte(0x40, 0xC0);

  288.            LED_1(ON);
  289.            LED_2(ON);
  290. }

  291. void Read_Pic(void)
  292. {
  293.         u16 i,j;
  294.         u16 t1,t2;
  295. //以下代码完成,图像的采集,并显示在tft上                                                                                         
  296.         for(i = 0; i < 240; i ++)         
  297.         {        
  298.                  if(i%6==1)
  299.                 {
  300.                         for(j = 0; j < 320; j ++)
  301.                         {
  302.                                 if (j<160)
  303.                                 {
  304.                                     //获得一个Y分量的过程                                                                                
  305.                                         FIFO_RCK_L;                                       
  306.                                         FIFO_RCK_H;                                                
  307.                                         t1=(0x00ff&GPIOC->IDR);                                         
  308.                                         FIFO_RCK_L;                                                
  309.                                         FIFO_RCK_H;                                                
  310.                                         t2=(0x00ff&GPIOC->IDR);
  311.         
  312.                                         //跳过一个点,隔开取点
  313.                                     FIFO_RCK_L;                                       
  314.                                         FIFO_RCK_H;                                                
  315.                                         t2=(0x00ff&GPIOC->IDR);                                         
  316.                                         FIFO_RCK_L;                                                
  317.                                         FIFO_RCK_H;                                                
  318.                                         t2=(0x00ff&GPIOC->IDR);
  319.                                         PIC[TrueLine][j]=t1;
  320.                                         }
  321.                     }
  322.                   TrueLine++;
  323.                 }
  324.                 else
  325.                 {          //否则跳过这些像素点
  326.                       for(j = 0; j < 320; j ++)
  327.                         {
  328.                                 FIFO_RCK_L;                                       
  329.                                 FIFO_RCK_H;                                                
  330.                                 t2=(0x00ff&GPIOC->IDR);                                         
  331.                                 FIFO_RCK_L;                                                
  332.                                 FIFO_RCK_H;                                                
  333.                                 t2=(0x00ff&GPIOC->IDR);
  334.                         }
  335.                 }
  336.         }
  337.         FIFO_OE_H;                           //禁止FIFO输出
  338. }
  339. void Send_Pic(void)
  340. {
  341.         u8 i;
  342.         u8 j;
  343.         USART_ClearFlag(USART1, USART_FLAG_TC);
  344.         USART_SendData(USART1,0xff);
  345.           while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  346.         for(i=0;i<40;i++)
  347.         {
  348.                 for(j=0;j<160;j++)
  349.                 {
  350.                         USART_ClearFlag(USART1, USART_FLAG_TC);
  351.                           USART_SendData(USART1,PIC[i][j]);
  352.                           while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  353.                 }
  354.         }
  355. }

  356. void I2C_Configuration(void)
  357. {
  358.         GPIO_InitTypeDef I2C;
  359.         /*开漏输出,速度50MHZ*/        
  360.         I2C.GPIO_Speed=GPIO_Speed_50MHz;
  361.         /*必须设置成开漏模式*/
  362.         I2C.GPIO_Mode=GPIO_Mode_Out_OD;
  363.         //I2C SDA
  364.         RCC_APB2PeriphClockCmd(SDA_Clock,ENABLE);
  365.         I2C.GPIO_Pin=SDA_Pin;
  366.         GPIO_Init(SDA_Port,&I2C);
  367.         //I2C SCL
  368.         RCC_APB2PeriphClockCmd(SCL_Clock,ENABLE);
  369.         I2C.GPIO_Pin=SCL_Pin;
  370.         GPIO_Init(SCL_Port,&I2C);
  371.         SCL_H;
  372.         SDA_H;
  373. }
  374. /*
  375. *********************************************************************************************************
  376. *        函数名称:I2C_Start()
  377. *        函数功能:I2C开始序列
  378. *        输入形参:
  379. *        返回值:
  380. *        函数说明:当SCL高电平时,SDA出现一个下跳沿表示I2C总线启动信号
  381. *********************************************************************************************************
  382. */

  383. void I2C_Start(void)
  384. {
  385.            SDA_H;
  386.         delay_us(100);
  387.         SCL_H;//延时5us 等待电平稳定
  388.         delay_us(100);
  389.         SDA_L;
  390.         delay_us(100);
  391.         SCL_L;        
  392.         delay_us(100);
  393.         
  394. }

  395. /*
  396. *********************************************************************************************************
  397. *        函数名称:I2C_Stop(void)
  398. *        函数功能:发送I2C总线停止信号
  399. *        输入形参:无
  400. *        返回值:  无
  401. *        函数说明:当SCL高电平时,SDA出现一个上跳沿表示I2C总线停止信号
  402. *********************************************************************************************************
  403. */

  404. void I2C_Stop(void)
  405. {
  406.         SDA_L;
  407.         delay_us(100);
  408.         SCL_H;
  409.         delay_us(100);
  410.         SDA_H;
  411.         delay_us(100);

  412. }

  413. /*
  414. *********************************************************************************************************
  415. *        函 数 名: i2c_SendByte
  416. *        功能说明: CPU向I2C总线设备发送8bit数据
  417. *        形    参:_ucByte : 等待发送的字节
  418. *        返 回 值: 无
  419. *********************************************************************************************************
  420. */

  421. void I2C_SendByte(uint8_t _ucByte)
  422. {
  423.         uint8_t i;
  424.         SCL_L;//发送之前先拉低
  425.         for (i = 0; i < 8; i++)
  426.         {               
  427.                 if (_ucByte & 0x80)
  428.                 {
  429.                         SDA_H;
  430.                 }
  431.                 else
  432.                 {
  433.                         SDA_L;
  434.                 }
  435.                 _ucByte <<= 1;        /* 左移一个bit */
  436.                 delay_us(100);
  437.                 SCL_H;//高电平采集数据
  438.                 delay_us(100);
  439.                 SCL_L;
  440.                 delay_us(100);
  441.                 if (i == 7)
  442.                 {
  443.                          SDA_H; // 释放总线
  444.                 }        
  445.         }
  446. }

  447. /*
  448. *********************************************************************************************************
  449. *        函 数 名: i2c_ReadByte
  450. *        功能说明: CPU从I2C总线设备读取8bit数据
  451. *        形    参:无
  452. *        返 回 值: 读到的数据
  453. *********************************************************************************************************
  454. */

  455. uint8_t I2C_ReadByte(void)
  456. {
  457.         uint8_t i;
  458.         uint8_t value;
  459.         /* 读到第1个bit为数据的bit7 */
  460.         value = 0;
  461.         SCL_L;

  462.         for (i = 0; i < 8; i++)
  463.         {
  464.                 value <<= 1;
  465.                 SCL_H;
  466.                 delay_us(100);
  467.                 if (I2C_SDA_READ())
  468.                 {
  469.                         value++;
  470.                 }
  471.                 else
  472.                 {
  473.                
  474.                 }
  475.                 SCL_L;
  476.                 delay_us(100);
  477.         }
  478.         return value;
  479. }

  480. /*
  481. *********************************************************************************************************
  482. *        函 数 名: i2c_WaitAck
  483. *        功能说明: CPU产生一个时钟,并读取器件的ACK应答信号
  484. *        形    参:无
  485. *        返 回 值: 返回0表示正确应答,1表示无器件响应
  486. *********************************************************************************************************
  487. */

  488. uint8_t I2C_WaitAck(void)
  489. {
  490.         uint8_t re=0;
  491.         SCL_L;
  492.         SDA_H;        /* CPU释放SDA总线 */        
  493.         SCL_H;        /* CPU驱动SCL = 1, 此时器件会返回ACK应答 */
  494.         delay_us(100);
  495.         if(I2C_SDA_READ())
  496.         {
  497.                  re=1;                 
  498.         }
  499.         else
  500.         {
  501.                 re=0;
  502.         }  
  503.         SCL_L;
  504.         delay_us(100);
  505.         return re;
  506. }

  507. /*
  508. *********************************************************************************************************
  509. *        函 数 名: i2c_Ack
  510. *        功能说明: CPU产生一个ACK信号
  511. *        形    参:无
  512. *        返 回 值: 无
  513. *********************************************************************************************************
  514. */

  515. void I2C_Ack(void)
  516. {
  517.         /* CPU驱动SDA = 0 */
  518.         SCL_L;
  519.         delay_us(10);
  520.         SDA_L;
  521.         delay_us(10);        
  522.         /* CPU产生1个时钟 */
  523.         SCL_H;
  524.         delay_us(10);        
  525.         SCL_L;
  526.         delay_us(10);
  527. }

  528. /*
  529. *********************************************************************************************************
  530. *        函 数 名: i2c_NAck
  531. *        功能说明: CPU产生1个NACK信号
  532. *        形    参:无
  533. *        返 回 值: 无
  534. *********************************************************************************************************
  535. */
  536. void I2C_NAck(void)
  537. {
  538.         /* CPU驱动SDA = 1 */
  539.         SDA_H;
  540.         delay_us(100);        
  541.         /* CPU产生1个时钟 */
  542.         SCL_H;
  543.         delay_us(100);        
  544.         SCL_L;
  545.         delay_us(100);
  546.         SDA_H;
  547.         delay_us(100);

  548. }
  549. /*******************************************************************************
  550. * Function Name  : I2C_WriteByte
  551. * Description    : 写一字节数据
  552. * Input          : - WriteAddress: 待写入地址
  553. *                      - SendByte: 待写入数据
  554. *                  - DeviceAddress: 器件类型
  555. * Output         : None
  556. * Return         : 返回为:=1成功写入,=0失败
  557. * Attention                 : None
  558. *******************************************************************************/
  559. int I2C_WriteByte( uint16_t WriteAddress , uint8_t SendByte )
  560. {
  561.         I2C_Start();
  562.         delay_us(100);
  563.         //发送器件地址
  564.         I2C_SendByte(ADDR_OV7670);
  565.         //如果器件没有应答,那么就发送停止信号
  566.         if(I2C_WaitAck()==1)
  567.         {
  568.                 I2C_Stop();
  569.                 printf("器件没有应答\n"); //调试使用
  570.                 return 0;
  571.         }
  572.         delay_us(5);
  573.     I2C_SendByte((uint8_t)(WriteAddress & 0x00FF));   /* 设置低起始地址 */  
  574.         //如果器件没有应答,那么就发送停止信号
  575.         if(I2C_WaitAck()==1)
  576.         {
  577.                 I2C_Stop();
  578.                 printf("写寄存器地址失败\n"); //调试使用
  579.                 return 0;
  580.         }
  581.         I2C_SendByte(SendByte);   /* 设置低起始地址写寄存器数据 */  
  582.         //如果器件没有应答,那么就发送停止信号
  583.         if(I2C_WaitAck()==1)
  584.         {
  585.                 I2C_Stop();
  586.                 printf("写寄存器内容失败\n"); //调试使用
  587.                 return 0;
  588.         }
  589.         I2C_Stop();
  590.         return 1;

  591. }
  592. //读书菊
  593. uint8_t I2C_ReadOneByte(uint8_t Address)
  594. {
  595.         uint8_t  temp;
  596.         I2C_Start();
  597.         delay_us(100);
  598.         //        写器件地址
  599.         I2C_SendByte(ADDR_OV7670);
  600.         //如果器件没有应答,那么就发送停止信号
  601.         if(I2C_WaitAck()==1)
  602.         {
  603.                 I2C_Stop();
  604.                 printf("器件没有应答\n"); //调试使用
  605.                 return 0;
  606.         }
  607.         delay_us(5);
  608.         I2C_SendByte(Address);
  609.         //如果器件没有应答,那么就发送停止信号
  610.         if(I2C_WaitAck()==1)
  611.         {
  612.                 I2C_Stop();
  613.                 printf("写寄存器地址失败\n"); //调试使用
  614.                 return 0;
  615.         }
  616.         I2C_Stop();
  617.         delay_us(500);
  618.         //开始读
  619.         I2C_Start();
  620.         //发送读命令
  621.         I2C_SendByte(ADDR_OV7670|0x01);
  622.         if(I2C_WaitAck()==1)
  623.         {
  624.                 I2C_Stop();
  625.                 printf("读失败\n"); //调试使用
  626.                 return 0;
  627.         }
  628.         delay_us(100);
  629.         temp=I2C_ReadByte();
  630.            I2C_NAck();
  631.         I2C_Stop();
  632.         return temp;
  633. }

  634. int main(void)
  635. {
  636.         /*硬件初始化*/
  637.         Board_Init();
  638.         /*应用函数*/
  639.         Application();
  640.         /*正常情况下不会执行到这一步*/
  641.         return 0;
  642. }
复制代码

AL422b.pdf

254.28 KB, 下载次数: 4, 下载积分: 黑币 -5

CMOS_FIFO电路.pdf

55.08 KB, 下载次数: 8, 下载积分: 黑币 -5

FIFO尺寸图.pdf

163.14 KB, 下载次数: 6, 下载积分: 黑币 -5

OmniVision_ProductGuide[1].pdf

674.09 KB, 下载次数: 4, 下载积分: 黑币 -5

OV7670_DS_(1_4).pdf

738.26 KB, 下载次数: 4, 下载积分: 黑币 -5

OV7670带FIFO的CMOS摄像头使用说明.pdf

2.75 MB, 下载次数: 8, 下载积分: 黑币 -5

OV7670照相模组硬件应用指南1.01 OVT.pdf

892.67 KB, 下载次数: 9, 下载积分: 黑币 -5

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表