找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32F407摄像头实验代码

[复制链接]
跳转到指定楼层
楼主
ID:234742 发表于 2017-9-21 21:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
下载:
实验33 摄像头实验 最终确定.rar (404.07 KB, 下载次数: 15)

  1. #include "led.h"
  2. #include "delay.h"
  3. #include "key.h"
  4. #include "sys.h"
  5. #include "lcd.h"
  6. #include "usart.h"         
  7. #include "string.h"
  8. #include "ov7670.h"
  9. #include "tpad.h"
  10. #include "timer.h"
  11. #include "exti.h"
  12. #include "usmart.h"
  13. #include "color.h"


  14. /************************************************
  15. ALIENTEK精英STM32开发板实验33
  16. 摄像头OV7670 实验

  17. ************************************************/
  18. void RCC_Configuration(void);
  19. void GPIO_Configuration(void);
  20. void TIM_Configuration(u16 value);
  21. void TIM_Configuration1(u16 value);
  22. //int key_Read(void);
  23. int flag = 0;
  24. #define vole(tem)   (0.5+0.5/45*(tem+45))*1000/*换算公式*/

  25. const u8*LMODE_TBL[5]={"Auto","Sunny","Cloudy","Office","Home"};                                                        //5种光照模式            
  26. const u8*EFFECTS_TBL[7]={"Normal","Negative","B&W","Redish","Greenish","Bluish","Antique"};        //7种特效
  27. extern u8 ov_sta;        //在exit.c里 面定义
  28. extern u8 ov_frame;        //在timer.c里面定义                 
  29. //更新LCD显示
  30. void camera_refresh(void)
  31. {
  32.         u32 j;
  33.          u16 color;
  34.      
  35.         if(ov_sta)//有帧中断更新?
  36.         {
  37.                 LCD_Scan_Dir(U2D_L2R);                //从上到下,从左到右  
  38.                 if(lcddev.id==0X1963)LCD_Set_Window((lcddev.width-240)/2,(lcddev.height-320)/2,240,320);//将显示区域设置到屏幕中央
  39.                 else if(lcddev.id==0X5510||lcddev.id==0X5310)LCD_Set_Window((lcddev.width-320)/2,(lcddev.height-240)/2,320,240);//将显示区域设置到屏幕中央
  40.                 LCD_WriteRAM_Prepare();     //开始写入GRAM        
  41.                 OV7670_RRST=0;                                //开始复位读指针
  42.                 OV7670_RCK_L;
  43.                 OV7670_RCK_H;
  44.                 OV7670_RCK_L;
  45.                 OV7670_RRST=1;                                //复位读指针结束
  46.                 OV7670_RCK_H;
  47.                 for(j=0;j<76800;j++)
  48.                 {
  49.                         OV7670_RCK_L;
  50.                         color=GPIOC->IDR&0XFF;        //读数据
  51.                         OV7670_RCK_H;
  52.                         color<<=8;  
  53.                         OV7670_RCK_L;
  54.                         color|=GPIOC->IDR&0XFF;        //读数据
  55.                         OV7670_RCK_H;  
  56. //            if(color>25500)
  57. //                color=0x0000;
  58. //            else
  59. //                color=0xffff;
  60.                         LCD->LCD_RAM=color;            
  61.                 }                                 
  62.             
  63.                  ov_sta=0;                                        //清零帧中断标记
  64. //                ov_frame++;
  65.                 LCD_Scan_Dir(DFT_SCAN_DIR);        //恢复默认扫描方向
  66.         if(Trace(&condition, &result))
  67.                 {
  68.             LCD_DrawRectangle( result.x-result.w/2, result.y-result.h/2, result.x-result.w/2+result.w,  result.y-result.h/2+result.h);
  69.             LCD_DrawRectangle( result.x-2, result.y-2,result.x+2, result.y+2);
  70.            
  71.                 }
  72.         }
  73. }           


  74. int main(void)
  75. {         
  76.         u8 key;
  77.         u8 lightmode=0,saturation=2,contrast=2;
  78.         u8 effect=0;         
  79.          u8 i=0;            
  80.         u8 msgbuf[15];                                //消息缓存区
  81.         u8 tm=0;
  82.          int volume = vole(0);
  83. //     u16 t=0;
  84. //    u16 a,b,c;
  85.           RCC_Configuration();
  86.     GPIO_Configuration();
  87.     TIM_Configuration(volume);
  88.                 TIM_Configuration1(volume);   
  89.          
  90.         delay_init();                     //延时函数初始化         
  91.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
  92.         uart_init(115200);                 //串口初始化为 115200
  93.          usmart_dev.init(72);                //初始化USMART               
  94.          LED_Init();                                          //初始化与LED连接的硬件接口
  95.         KEY_Init();                                        //初始化按键
  96.         LCD_Init();                                           //初始化LCD  
  97. //        TPAD_Init(6);                                //触摸按键初始化
  98.          POINT_COLOR=RED;                        //设置字体为红色
  99.         
  100.                         TIM_Configuration(1500);
  101.                         delay_ms(20);
  102.                         TIM_Configuration(1500);
  103.                         delay_ms(20);
  104.                         TIM_Configuration(1500);
  105.                         delay_ms(20);
  106.                         
  107.                         TIM_Configuration1(1500);
  108.                         delay_ms(20);
  109.                         TIM_Configuration1(1500);
  110.                         delay_ms(20);
  111.                         TIM_Configuration1(1500);
  112.                         delay_ms(20);
  113.         LCD_ShowString(30,50,200,16,16,"ELITE STM32F103 ^_^");        
  114.         LCD_ShowString(30,70,200,16,16,"OV7670 TEST");        
  115.         LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
  116.         LCD_ShowString(30,110,200,16,16,"2015/1/18");
  117.         LCD_ShowString(30,130,200,16,16,"KEY0:Light Mode");
  118.         LCD_ShowString(30,150,200,16,16,"KEY1:Saturation");
  119.         LCD_ShowString(30,170,200,16,16,"KEY_UP:Contrast");
  120.         LCD_ShowString(30,190,200,16,16,"TPAD:Effects");         
  121.           LCD_ShowString(30,210,200,16,16,"OV7670 Init...");         
  122.         while(OV7670_Init())//初始化OV7670
  123.         {
  124.                 LCD_ShowString(30,210,200,16,16,"OV7670 Error!!");
  125.                 delay_ms(200);
  126.             LCD_Fill(30,210,239,246,WHITE);
  127.                 delay_ms(200);
  128.         }
  129.          LCD_ShowString(30,210,200,16,16,"OV7670 Init OK");
  130.         delay_ms(1500);                    
  131.         OV7670_Light_Mode(lightmode);
  132.         OV7670_Color_Saturation(saturation);
  133.         OV7670_Contrast(contrast);
  134.          OV7670_Special_Effects(effect);         
  135.         
  136.         TIM6_Int_Init(10000,7199);                        //10Khz计数频率,1秒钟中断                                                                          
  137.         EXTI8_Init();                                                //使能定时器捕获
  138.         OV7670_Window_Set(12,176,240,320);        //设置窗口         
  139.           OV7670_CS=0;                        
  140.         LCD_Clear(BLACK);               
  141. //                if(result.x!=0&&result.y!=0)        
  142. //                {
  143.                                 printf("ZB= %d,%d ",result.x,result.y);
  144. //                }
  145.          while(1)
  146.         {        
  147.                 POINT_COLOR=BLUE;        
  148.     key=KEY_Scan(0);//不支持连按
  149.                 camera_refresh();//更新显示
  150.                
  151.                 if(KEY0==1)
  152.                 {
  153.                                 if((result.x<170|result.x>140)&&(result.y<210|result.x>190))
  154.                                 {
  155.                                                         if((160-result.x<200-result.y)||(160+result.x<200+result.y))
  156.                                                                         {
  157.                                                                                         TIM_Configuration(1500);
  158.                                                                                         delay_ms(30);
  159.                                                                                                                                                         
  160.                                                                                         TIM_Configuration(1725);
  161.                                                                                         delay_ms(20);
  162.                                                                                        
  163.                                                                                         TIM_Configuration(1500);
  164.                                                                                         delay_ms(30);
  165.                                                                                        
  166.                                                                                         TIM_Configuration(1550);
  167.                                                                                         delay_ms(20);
  168.                                                                                        
  169.                                                                                         TIM_Configuration(1500);
  170.                                                                                         delay_ms(20);
  171.                                                                                         camera_refresh();
  172.                                                                         }                                                                                
  173.                                                         else
  174.                                                                         {
  175.                                                                                         TIM_Configuration1(1500);
  176.                                                                                         delay_ms(30);
  177.                                                                                 
  178.                                                                                        
  179.                                                                                         TIM_Configuration1(1725);
  180.                                                                                         delay_ms(20);
  181.                                                                                        
  182.                                                                                         TIM_Configuration1(1500);
  183.                                                                                         delay_ms(20);
  184.                                                                                        
  185.                                                                                         TIM_Configuration1(1550);
  186.                                                                                         delay_ms(20);
  187.                                                                                        
  188.                                                                                         TIM_Configuration1(1500);
  189.                                                                                         delay_ms(20);
  190.                                                                                         camera_refresh();
  191.                                                                         }
  192.                                                         if(result.x<160&&result.x!=0)
  193.                                                                         {
  194.                                                                                 
  195.                                                                                 TIM_Configuration(1500);
  196.                                                                                 delay_ms(30);
  197.                                                                         
  198.                                                                                 
  199.                                                                                 TIM_Configuration(1725);
  200.                                                                                 delay_ms(20);
  201.                                                                                 
  202.                                                                                 TIM_Configuration(1800);
  203.                                                                                 delay_ms(30);

  204.                                                                                 TIM_Configuration(1400);
  205.                                                                                 delay_ms(20);
  206.                                                                
  207.                                                                                 TIM_Configuration(1525);
  208.                                                                                 delay_ms(20);
  209.                                                                                 
  210.                                                                                 TIM_Configuration(1500);
  211.                                                                                 delay_ms(20);
  212.                                                                                 camera_refresh();
  213.                                                                         }
  214.                                                                
  215.                                                         else
  216.                                                                         {
  217.                                                                                 TIM_Configuration1(1500);
  218.                                                                                 delay_ms(20);
  219.                                                                                                                                        
  220.                                                                                 TIM_Configuration1(1400);
  221.                                                                                 delay_ms(20);
  222.                                                                                 
  223.                                                                                 TIM_Configuration1(1350);
  224.                                                                                 delay_ms(20);

  225.                                                                                 TIM_Configuration1(1250);
  226.                                                                                 delay_ms(20);
  227.                                                                
  228.                                                                                 TIM_Configuration1(1100);
  229.                                                                                 delay_ms(20);
  230.                                                                                 
  231.                                                                                 TIM_Configuration1(1350);
  232.                                                                                 delay_ms(20);
  233.                                                                                 
  234.                                                                                 TIM_Configuration1(1500);
  235.                                                                                 delay_ms(20);
  236.                                                                                 
  237.                                                                                 TIM_Configuration1(1500);
  238.                                                                                 delay_ms(20);
  239.                                                                                 camera_refresh();
  240.                                                                         }
  241.                                                                


  242.                                                         if(result.y<200  &&  result.y!=0)
  243.                                                                         {
  244.                                                                                 TIM_Configuration1(1500);
  245.                                                                                 delay_ms(20);
  246.                                                                                                                                        
  247.                                                                                 TIM_Configuration1(1725);
  248.                                                                                 delay_ms(20);
  249.                                                                                 
  250.                                                                                 TIM_Configuration1(1800);
  251.                                                                                 delay_ms(20);

  252.                                                                                 TIM_Configuration1(1400);
  253.                                                                                 delay_ms(20);
  254.                                                                
  255.                                                                                 TIM_Configuration1(1500);
  256.                                                                                 delay_ms(20);
  257.                                                                                 
  258.                                                                                 TIM_Configuration1(1500);
  259.                                                                                 delay_ms(20);
  260.                                                                                 camera_refresh();
  261.                                                                         }                                                                                                               
  262.                                                         else
  263.                                                                         {
  264.                                                                                         TIM_Configuration1(1500);
  265.                                                                                         delay_ms(20);
  266.                                                                                                                                                         
  267.                                                                                         TIM_Configuration1(1400);
  268.                                                                                         delay_ms(20);
  269.                                                                                        
  270.                                                                                         TIM_Configuration1(1350);
  271.                                                                                         delay_ms(20);

  272.                                                                                         TIM_Configuration1(1200);
  273.                                                                                         delay_ms(20);
  274.                                                                         
  275.                                                                                         TIM_Configuration1(1100);
  276.                                                                                         delay_ms(20);
  277.                                                                                        
  278.                                                                                         TIM_Configuration1(1350);
  279.                                                                                         delay_ms(20);
  280.                                                                                        
  281.                                                                                         TIM_Configuration1(1500);
  282.                                                                                         delay_ms(20);
  283.                                                                                        
  284.                                                                                         TIM_Configuration1(1500);
  285.                                                                                         delay_ms(20);
  286.                                                                                         camera_refresh();
  287.                                                                         }
  288.                                         }
  289.                                 
  290.                         else
  291.                                 {
  292.                                                         TIM_Configuration1(1500);
  293.                                                                                         delay_ms(20);        
  294.                                                         TIM_Configuration1(1500);
  295.                                                                                         delay_ms(20);
  296.                                 }
  297.                 }
  298.                
  299.                 if(KEY1==1)
  300.                 {
  301.                         TIM_Configuration(1500);
  302.                         delay_ms(20);        
  303.                         TIM_Configuration1(1500);
  304.                         delay_ms(20);                        
  305.                                                 camera_refresh();
  306.                         TIM_Configuration1(1400);
  307.                         delay_ms(20);
  308.                         TIM_Configuration(1400);
  309.                         delay_ms(20);
  310.                                                                                 
  311.                         TIM_Configuration1(1350);
  312.                         delay_ms(20);
  313.                         TIM_Configuration(1350);
  314.                         delay_ms(20);
  315.                                                 camera_refresh();
  316.                         TIM_Configuration1(1250);
  317.                         delay_ms(20);
  318.                         TIM_Configuration(1250);
  319.                         delay_ms(20);
  320.                                                                
  321.                         TIM_Configuration1(1100);
  322.                         delay_ms(20);
  323.                         TIM_Configuration(1100);
  324.                         delay_ms(20);
  325.                                                 camera_refresh();                        
  326.                         TIM_Configuration1(1350);
  327.                         delay_ms(20);
  328.                         TIM_Configuration(1350);
  329.                         delay_ms(20);
  330.                                                                         
  331.                         TIM_Configuration1(1500);
  332.                         delay_ms(20);
  333.                                                                                 
  334.                         TIM_Configuration(1500);
  335.                         delay_ms(20);
  336.                                                                                 camera_refresh();
  337.                 }
  338.                 if(WK_UP==0)
  339.                 {
  340.                         TIM_Configuration(1500);
  341.                         delay_ms(20);        
  342.                         TIM_Configuration1(1500);
  343.                         delay_ms(20);                        
  344.                                                 camera_refresh();
  345.                         
  346.                 }
  347.                
  348.                                 if(tm)
  349.                                 {
  350.                                         LCD_ShowString((lcddev.width-240)/2+30,(lcddev.height-320)/2+60,200,16,16,msgbuf);
  351.                                         tm--;
  352.                                 }
  353.                                 i++;
  354.                         if(i==15)//DS0闪烁.
  355.                         {
  356.                                 i=0;
  357.                                 LED0=!LED0;
  358.                         }
  359.                 }           
  360. }




  361. void GPIO_Configuration(void)
  362. {
  363.     GPIO_InitTypeDef GPIO_InitStruct;
  364.     /*TIM1的通道1*/
  365.     GPIO_InitStruct.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_3;
  366.     GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;
  367.     GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
  368.     GPIO_Init(GPIOA,&GPIO_InitStruct);

  369.     GPIO_InitStruct.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_5;
  370.     GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;
  371.     GPIO_Init(GPIOC,&GPIO_InitStruct);

  372. }
复制代码


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

使用道具 举报

沙发
ID:620272 发表于 2019-11-16 08:40 | 只看该作者
这是用到的哪个摄像头模块啊?ov7725可以用么?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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