找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2535|回复: 1
收起左侧

ov7670测试程序,用于stm32图像识别,未完善

[复制链接]
ID:212467 发表于 2018-3-19 10:16 | 显示全部楼层 |阅读模式
ov7670测试程序,用于图像识别,未完善

单片机源程序如下:
  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 "EasyTracer.h"

  14. #define min3v(v1, v2, v3)   ((v1)>(v2)? ((v2)>(v3)?(v3):(v2)):((v1)>(v3)?(v3):(v1)))
  15. #define max3v(v1, v2, v3)   ((v1)<(v2)? ((v2)<(v3)?(v3):(v2)):((v1)<(v3)?(v3):(v1)))

  16. /************************************************
  17. ************************************************/


  18. const u8*LMODE_TBL[5]={"Auto","Sunny","Cloudy","Office","Home"};                                                        //5种光照模式            
  19. const u8*EFFECTS_TBL[7]={"Normal","Negative","B&W","Redish","Greenish","Bluish","Antique"};        //7种特效
  20. extern u8 ov_sta;        //在exit.c里 面定义
  21. extern u8 ov_frame;        //在timer.c里面定义        

  22. u16 data1[500];
  23. //更新LCD显示
  24. void camera_refresh(void)
  25. { u8 r,g,b,maxVal,minVal;
  26.         u32 j;
  27.          u16 color;         
  28.         if(ov_sta)//有帧中断更新?
  29.         {
  30.                 LCD_Scan_Dir(U2D_L2R);                //从上到下,从左到右  
  31.                 if(lcddev.id==0X1963)LCD_Set_Window((lcddev.width-240)/2,(lcddev.height-320)/2,240,320);//将显示区域设置到屏幕中央
  32.                 else if(lcddev.id==0X5510||lcddev.id==0X5310)LCD_Set_Window((lcddev.width-320)/2,(lcddev.height-240)/2,320,240);//将显示区域设置到屏幕中央
  33.                 LCD_WriteRAM_Prepare();     //开始写入GRAM        
  34.                 OV7670_RRST=0;                                //开始复位读指针
  35.                 OV7670_RCK_L;
  36.                 OV7670_RCK_H;
  37.                 OV7670_RCK_L;
  38.                 OV7670_RRST=1;                                //复位读指针结束
  39.                 OV7670_RCK_H;
  40.                 for(j=0;j<76800;j++)
  41.                 {
  42.                         OV7670_RCK_L;
  43.                         color=GPIOC->IDR&0XFF;        //读数据
  44.                         OV7670_RCK_H;
  45.                         color<<=8;  
  46.                         OV7670_RCK_L;
  47.                         color|=GPIOC->IDR&0XFF;        //读数据
  48.                         OV7670_RCK_H;
  49.                         r   =         (unsigned char)((color&0xf800)>>8);
  50.             g =         (unsigned char)((color&0x07e0)>>3);
  51.             b  =     (unsigned char)((color&0x001f)<<3);
  52.                         maxVal = max3v(r, g, b);
  53.             minVal = min3v(r, g, b);
  54.                         color = (maxVal+minVal)*240/255/2;
  55.                         if(j<400)data1[j]=color;
  56.                         if(color>55)color=0xffff;
  57.                         else color=0;
  58.                         
  59.                         LCD->LCD_RAM=color;   
  60.                 }                                                            
  61.                  ov_sta=0;                                        //清零帧中断标记
  62.                 ov_frame++;
  63.                 LCD_Scan_Dir(DFT_SCAN_DIR);        //恢复默认扫描方向
  64.         }
  65. }           


  66. int main(void)
  67. {
  68.   RESULT Resu;
  69.         TARGET_CONDI Condition={180,230,20,250,20,200,32,32,320,240};
  70.         int  ch;
  71.         int  cl=0;
  72.         int  cr=0;
  73.         int  cf=0;
  74.         int  cb=0;
  75.   /*四轴行动参数*/
  76.         int zuoyou=0;
  77.   int qianhou=0;
  78.          
  79. //        u8 key;
  80.         u8 lightmode=0,saturation=0,brightness=0,contrast=0;
  81.         u8 effect=0;
  82.         
  83. //         u8 i=0;            
  84. //        u8 msgbuf[15];                                //消息缓存区
  85. //        u8 tm=0;

  86.         delay_init();                     //延时函数初始化         
  87.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
  88.         uart_init(115200);                 //串口初始化为 115200
  89.          usmart_dev.init(72);                //初始化USMART               
  90.          LED_Init();                                          //初始化与LED连接的硬件接口
  91. //        KEY_Init();                                        //初始化按键
  92.         LCD_Init();                                           //初始化LCD  
  93. //        TPAD_Init(6);                                //触摸按键初始化
  94.          POINT_COLOR=RED;                        //设置字体为红色
  95.         LCD_ShowString(30,50,200,16,16,"WarShip STM32");        
  96.         LCD_ShowString(30,70,200,16,16,"OV7670 TEST");        
  97.         LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
  98.         LCD_ShowString(30,110,200,16,16,"2015/1/18");
  99.         LCD_ShowString(30,130,200,16,16,"KEY0:Light Mode");
  100.         LCD_ShowString(30,150,200,16,16,"KEY1:Saturation");
  101.         LCD_ShowString(30,170,200,16,16,"KEY2:Brightness");
  102.         LCD_ShowString(30,190,200,16,16,"KEY_UP:Contrast");
  103.         LCD_ShowString(30,210,200,16,16,"TPAD:Effects");         
  104.           LCD_ShowString(30,230,200,16,16,"OV7670 Init...");         
  105.         while(OV7670_Init())//初始化OV7670
  106.         {
  107.                 LCD_ShowString(30,230,200,16,16,"OV7670 Error!!");
  108.                 delay_ms(200);
  109.             LCD_Fill(30,230,239,246,WHITE);
  110.                 delay_ms(200);
  111.         }
  112.          LCD_ShowString(30,230,200,16,16,"OV7670 Init OK");
  113.         delay_ms(1500);                    
  114.         OV7670_Light_Mode(lightmode);
  115.         OV7670_Color_Saturation(saturation);
  116.         OV7670_Brightness(brightness);
  117.         OV7670_Contrast(contrast);
  118.          OV7670_Special_Effects(effect);         
  119.         TIM6_Int_Init(10000,7199);                        //10Khz计数频率,1秒钟中断                                                                          
  120.         EXTI8_Init();                                                //使能定时器捕获
  121.         OV7670_Window_Set(12,176,240,320);        //设置窗口         
  122.           OV7670_CS=0;                                       
  123.         LCD_Clear(BLACK);
  124.          while(1)
  125.         {        
  126. //                key=KEY_Scan(0);//不支持连按
  127. //                if(key)
  128. //                {
  129. //                        tm=20;
  130. //                        switch(key)
  131. //                        {                                    
  132. //                                case KEY0_PRES:        //灯光模式Light Mode
  133. //                                        lightmode++;
  134. //                                        if(lightmode>4)lightmode=0;
  135. //                                        OV7670_Light_Mode(lightmode);
  136. //                                        sprintf((char*)msgbuf,"%s",LMODE_TBL[lightmode]);
  137. //                                        break;
  138. //                                case KEY1_PRES:        //饱和度Saturation
  139. //                                        saturation++;
  140. //                                        if(saturation>4)saturation=0;
  141. //                                        OV7670_Color_Saturation(saturation);
  142. //                                        sprintf((char*)msgbuf,"Saturation:%d",(signed char)saturation-2);
  143. //                                        break;
  144. //                                case KEY2_PRES:        //亮度Brightness                                 
  145. //                                        brightness++;
  146. //                                        if(brightness>4)brightness=0;
  147. //                                        OV7670_Brightness(brightness);
  148. //                                        sprintf((char*)msgbuf,"Brightness:%d",(signed char)brightness-2);
  149. //                                        break;
  150. //                                case WKUP_PRES:        //对比度Contrast                           
  151. //                                        contrast++;
  152. //                                        if(contrast>4)contrast=0;
  153. //                                        OV7670_Contrast(contrast);
  154. //                                        sprintf((char*)msgbuf,"Contrast:%d",(signed char)contrast-2);
  155. //                                        break;
  156. //                        }
  157. //                }         
  158. //                if(TPAD_Scan(0))//检测到触摸按键
  159. //                {
  160. //                        effect++;
  161. //                        if(effect>6)effect=0;
  162. //                        OV7670_Special_Effects(effect);//设置特效
  163. //                         sprintf((char*)msgbuf,"%s",EFFECTS_TBL[effect]);
  164. //                        tm=20;
  165. //                }
  166.     delay_ms(1);         
  167.                 camera_refresh();//更新显示
  168.         //        LCD_Fill(100,100,200, 200,RED);
  169.         
  170.                 if(Trace(&Condition,&Resu))
  171.                         {
  172.         //                        LCD_Fill(Resu.x-Resu.w/2,Resu.y-Resu.h/2,Resu.x+Resu.w/2, Resu.y-Resu.h/2+20,RED);
  173.                                 LCD_Draw_Circle(Resu.x,Resu.y,20);
  174.                                 LED0=0;
  175.                                 /*四轴行动*/
  176.         
  177.         zuoyou=Resu.x-120;
  178.                                 qianhou=Resu.y-160;
  179.                                 
  180.                                 if(abs(zuoyou)>3)
  181.                                 {
  182.                                         if(zuoyou<0)
  183.                                         {left=0;right=1;}
  184.                                         else{left=1;right=0;}
  185.                                 }
  186.                                 else  {left=0;right=0;}
  187.                                 if(abs(qianhou)>3)
  188.                                  {
  189.                                          if(qianhou<0)
  190.                                          {forward=0;backward=1;}
  191.                                          else{forward=1;backward=0;}                        
  192.                                  }
  193.                                 else   {forward=0;backward=0;}
  194.                                 
  195.                                 LCD_Draw_Circle(120,160,ch*5);
  196.                                 LCD_Draw_Circle(80,160,cl*10);
  197.                                 LCD_Draw_Circle(160,160,cr*10);
  198.                                 LCD_Draw_Circle(120,120,cf*10);
  199.                                 LCD_Draw_Circle(120,200,cb*10);
  200.                           
  201. //                                GUI_Fill(Resu.x-Resu.w/2,Resu.y-Resu.h/2,Resu.w,1,0xf800);
  202. //                                GUI_Fill(Resu.x-Resu.w/2,Resu.y-Resu.h/2,1,Resu.h,0xf800);
  203. //                                GUI_Fill(Resu.x-Resu.w/2,Resu.y+Resu.h/2,Resu.w,1,0xf800);
  204. //                                GUI_Fill(Resu.x+Resu.w/2,Resu.y-Resu.h/2,1,Resu.h,0xf800);
  205. //                                GUI_Fill(Resu.x-2,Resu.y-2,4,4,0xf800);
  206.                         }
  207.                         else
  208.                         {LCD_ShowString(30,240,200,16,16,"Target is not found");
  209.                         Resu->x=120,Resu->y,=160;
  210.                   LED0=1;}
  211. //                 if(tm)
  212. //                {
  213. //                        LCD_ShowString((lcddev.width-240)/2+30,(lcddev.height-320)/2+60,200,16,16,msgbuf);
  214. //                        tm--;
  215. //                }
  216. //                i++;
  217. //                if(i==15)//DS0闪烁.
  218. //                {
  219. //                        i=0;
  220. //                        LED0=!LED0;
  221. //                 }
  222.         }           
  223. }
复制代码

所有资料51hei提供下载:
ov7670测试程序.rar (395.23 KB, 下载次数: 25)
回复

使用道具 举报

ID:585414 发表于 2019-7-26 15:56 | 显示全部楼层
楼主,请教一下219—223里的 tm 是做什么用的呢?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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