CMOS智能投影仪设计和图像采集(硬件、程序源码、论文)

CMOS智能投影仪设计和图像采集(硬件、程序源码、论文):

智能投影仪功能概述:

     将摄像头采集到的白板上的触摸物体的位置信息通过USB线传给开发板,开发板做相应的图像处理,并把处理结果传给投影仪显示到白板上,最终实现触摸白板即操作开发板的效果。

      利用开发板实现完整的图像采集及处理系统,即主系统A。主从摄像头将采集到的数据全部交给主系统来处理计算出白板上触控笔映射到电脑屏幕上的位置信息。硬件上由大面积白板、摄像头、微处理器、FPGA等部分组成,软件上实现几个图像处理算法。

如截图所示:
 
none.gif


CMOS摄像头采集智能投影仪系统设计创新:

利用SRAM实现图像数据的存储与调配,从硬件上提高系统的实时性
几种关键图像算法的实现:
利用汇编语言实现图像接收,提高接收速度;
目标识别与目标定位算法准确有效地判断出了触摸物体的位置信息;
将白板枚举成USB设备,实现了白板的即插即用;
计算摄像头参数与映射系数算法实现了白板系统的准确校准、白板平面与PC机屏幕坐标系的高度契合


毕业设计中含有视频播放地址

程序:
  1. /******************** (C) COPYRIGHT 2009 STMicroelectronics ********************
  2. * File Name          : main.c
  3. * Author             : MCD Application Team
  4. * Version            : V3.1.0RC1
  5. * Date               : 09/28/2009
  6. * Description        : Joystick Mouse demo main file
  7. ********************************************************************************
  8. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  9. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  10. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  11. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  12. * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  13. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  14. *******************************************************************************/
  15.  
  16. /* Includes ------------------------------------------------------------------*/
  17. #include "math.h"
  18. #include "stdio.h"
  19. #include "stm32f10x_conf.h "
  20. #include "stm32f10x.h"
  21. #include "usb_lib.h"
  22. #include "hw_config.h"
  23. #include "usb_pwr.h"
  24. #include "usb_conf.h"
  25. //#include "parameters.h"
  26. #include "main.h"
  27. #include "FlashHandle.h"
  28.  
  29.  
  30. #define SET_L GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)0x00);                          //用于表示单片机处理完可切换存储器至摄像头的信号
  31. #define SET_H GPIO_WriteBit(GPIOB, GPIO_Pin_5, (BitAction)0x01);
  32. #define ObSize 6528        ;
  33.  
  34. void IWDG_Configuration(void);
  35. void Delay(__IO uint32_t nCount);
  36. void RCC_Configuration(void);
  37. void GPIO_Configuration(void);
  38. void USART2_Configuration(void);
  39. void USART1_Configuration(void);
  40. void NVIC_Configuration(void);
  41. void TIMER_Configuration(void) ;
  42. void Init_All_Periph(void);
  43.  
  44. /*private define*/
  45. // uint8_t Receive_Buffer[9];
  46. volatile unsigned int ObjectFlag=0;
  47. char flag_int=0;
  48. char f1=1,f2=1,t1=1,t2=0;
  49. unsigned char d1_sub=255,d2_sub=255,t1_sub=0,t2_sub=0;
  50.  
  51. /*
  52. volatile  unsigned char   t_sub,tt_sub;
  53. volatile  unsigned int d2_main[2];
  54. volatile  int t_main[2],tt_main;
  55. extern volatile  int d_main,d_sub;
  56. extern int screen_x,screen_y;
  57. */
  58. volatile unsigned char  t_sub,tt_sub;
  59. volatile unsigned int d2_main[2];
  60. volatile unsigned char  t_main[2],tt_main;
  61. extern volatile unsigned int d_main,d_sub;
  62. extern volatile int screen_x,screen_y;
  63. volatile int screen_prex=0,screen_prey=0;
  64. int screen_datax[5]={0,0,0,0,0};
  65. int screen_datay[5]={0,0,0,0,0};
  66. unsigned char d_x[7]={0};
  67. unsigned char d_y[7]={0};
  68. int screen_dx[2];
  69. int screen_dy[2];
  70. unsigned char dx;
  71. unsigned char dy;
  72. extern unsigned char object[6528];
  73. //unsigned char back[12][544];
  74. extern unsigned char back[6528];
  75. extern signed   char diff[600];
  76.  
  77. signed char pre_maxim[2];
  78. unsigned int zhenshu;
  79. unsigned int zhenshu1;
  80.  
  81.  
  82. void dc_config(void);
  83. unsigned int get_x(void);
  84.  
  85. void bd_to_screen(void)  ;
  86.  
  87.  
  88. /
  89.    int fputc(int ch, FILE *f)
  90.  
  91.    {
  92.       USART_SendData(USART1, (unsigned char) ch);// USART1 可以换成 USART2 等
  93.  
  94.       while (!(USART1->SR & USART_FLAG_TXE));
  95.  
  96.       return (ch);
  97.  
  98.    }
  99.  
  100. /*******************************************************************************
  101. * Function Name  : read from sram
  102. *******************************************************************************/
  103. /* */
  104. __asm void ReadSram(unsigned char *p)   //软件复位 自己添加
  105. {  
  106.          MOV   R2,R0                                 //将数组的起始地址赋给r2
  107.          MOV   R3,#0x0000
  108.                  MOVW   R4,#0x0c08                         //输入数据
  109.                  MOVT   R4,#0x4001
  110.                  MOVW   R5,#0x100C                         //取数据地址
  111.                  MOVT        R5,#0x4001
  112. loop                 
  113.                  STR    R3,[R5]                            //r3赋值地址给r5
  114.                  LDR    R0,[R4]                   //将GPIOB->IDR的值赋给r0
  115.                  LSR    R0,R0,#8         
  116.                  STRH   R0,[R2,R3]                   //将r0的值赋给定义数组
  117.          ADDS   R3,R3,#0x01                   //SUBS   r3,r3,#0x01 r3++
  118.              STR    R3,[R5]                           //将idx的值赋给GPIOC->ODR(地址)
  119.                  CMP    R3,#0x1980
  120.          BNE    loop                      //跳转到循环开始;否则结束
  121.                  BX     LR                                            //返回主程序
  122. }
  123.  
  124. /*
  125. __asm void ReadSramChar(unsigned char *p,int i)   
  126. {
  127.   
  128.          MOV    R2,R0                                  //将地址赋给r2
  129.          MOV    R3,R1                                 //将取数据的地址赋给r3
  130.                  MOVW   R4,#0x0c08                         //输入数据
  131.                  MOVT   R4,#0x4001
  132.                  MOVW   R5,#0x100C                         //取数据地址
  133.                  MOVT        R5,#0x4001
  134.  
  135.                  STRB   R3,[R5]                                                   //r3赋值地址给r5
  136.                  LDR    R0,[R4]                                                 //将GPIOB->IDR的值赋给r0
  137.                  LSR    R0,R0,#8                                   
  138.                  STRB   R0,[R2]                  //将r0的值赋给定义数组
  139. }
  140.         */
  141.  
  142.  
  143.  
  144. /*
  145. void ReadSram(unsigned char *p)//,(unsigned int)ObSize)
  146. {
  147. u16 Adr;
  148. char i;
  149. u16 j;
  150. GPIO_ResetBits(GPIOA, GPIO_Pin_5);
  151. for(Adr=0x0000;Adr<6528;Adr++)
  152.   //for(Adr=0x0000;;Adr++)
  153.    {
  154.        GPIOC->ODR=Adr;
  155.            for(i=0;i<5;i++)
  156.            j=GPIOB->IDR;
  157.            j=(j&0xff00)>>8;
  158.            *p=(char)j;
  159.            p++;
  160.           // GPIOC->BSRR = 0X00000000;
  161.    }
  162.   GPIO_SetBits(GPIOA, GPIO_Pin_5);
  163. }
  164.    */
  165. void ReadSramChar(unsigned char *p,u16 Adr)
  166. {
  167.   char i;
  168.   unsigned int j=0;
  169.   GPIO_ResetBits(GPIOA, GPIO_Pin_5);
  170.   GPIOC->ODR=Adr;
  171.   for(i=0;i<5;i++)
  172.   j=GPIOB->IDR;
  173.   j=(j&0xff00)>>8;
  174.   *p=(unsigned char)j;
  175. // GPIOC->BSRR = 0X00000000;
  176.   GPIO_SetBits(GPIOA, GPIO_Pin_5);
  177. }       
  178.  
  179.  
  180.  
  181.  
  182. void USART2_IRQHandler(void)
  183. {unsigned char data_f_0;
  184.   if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET) //判断发生接收中断
  185.    {   
  186.         USART_ClearITPendingBit(USART2, USART_IT_RXNE);   //清除中断标志          
  187.         //GPIO_WriteBit(GPIOB,GPIO_Pin_10,(BitAction)0x01);     //开始传输
  188.         data_f_0=USART_ReceiveData(USART2);
  189.                 while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE)== 1);
  190.       if(flag_int==0)
  191.                 {
  192.                         if(f1!=0)                                                        //判断接收数据是否为第一个坐标信息
  193.                         {
  194.                                 d1_sub=data_f_0;                                //将接收到的从坐标低八位存入d1_sub[4]
  195.                                 f1=0;                                                        //将标志接收数据为从坐标低八位的寄存器清零。为0代表已接收。
  196.                                 f2=1;                                                       
  197.                                 t1=1;
  198.                                 t2=1;                                                       
  199.                         }
  200.                         else                                                                //判断接收数据是否为第二个坐标信息
  201.                                 if(f2!=0)                                               
  202.                                 {  
  203.                                         d2_sub=data_f_0;                                //将接收到的从坐标高八位存入d2_sub[4]
  204.                                         f1=0;
  205.                                         f2=0;                                                        //将标志接收数据为从坐标高八位的寄存器清零。为0代表已接收。
  206.                                         t1=1;
  207.                                         t2=1;
  208.                                 }
  209.                         else
  210.                                 if(t1!=0)                                                //判断接收数据是否为时间信息
  211.                                 {
  212.                                         t1_sub=data_f_0;                        //将接收到的从时间存入t1_sub[4]
  213.                                         f1=0;
  214.                                         f2=0;
  215.                                         t1=0;
  216.                                         t2=1;                                                //清零标志接收从时间的寄存器。为0代表已接受从时间。                                                       
  217.                                 }
  218.                         else
  219.                                 if(t2!=0)                                                //判断接收数据是否为时间信息
  220.                                 {
  221.                                         t2_sub=data_f_0;                        //将接收到的从时间存入t1_sub[4]
  222.                                         f1=1;
  223.                                         f2=1;
  224.                                         t1=1;
  225.                                         t2=0;                                                //清零标志接收从时间的寄存器。为0代表已接受从时间。
  226.                 }
  227.             }
  228.    }
  229. /*if(USART_GetITStatus(USART2,USART_IT_TXE)!=RESET)
  230.   {         USART_ClearITPendingBit(USART2, USART_IT_TXE)        ;
  231.     USART_SendData(USART2, 0xDD);
  232.         while(USART_GetFlagStatus(USART2, USART_FLAG_TXE)== 0);
  233.         USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
  234.          
  235.   }   */
  236. }
  237.  
  238. void move(void)
  239. {
  240.         unsigned char i;
  241.         unsigned int j;         
  242.  
  243.         for (i=0;i<12;i++)
  244.                 {for (j=0;j<544;j++)
  245.                         {
  246.                         back[i*544+j] = object[i*544+j];
  247.                 }
  248.                 }
  249.  
  250. }          
  251.  
  252. void judge(void)
  253. {
  254.         unsigned int  p1,p2,m1,m2;
  255.         unsigned char p;
  256.         char flag1=0;
  257.  
  258.                 for(p=0;p<2;p++)
  259.                 {
  260.                         if(t_sub==t_main[p])
  261.                         {                       
  262.                         d_main=d2_main[p];
  263.                         flag1=1;
  264.                         break;
  265.                         }                                                       
  266.                 }
  267.                 if(flag1==1)
  268.                 {
  269.                          if(t_main[1]>t_main[0])
  270.                            {
  271.                                       if(t_sub<t_main[1]&&t_sub>t_main[0])
  272.                                                   {       
  273.                                                     m2=t_main[1];
  274.                                                     m1=t_main[0];
  275.                                                         p2=d2_main[1];
  276.                                                         p1=d2_main[0];
  277.                                                         //d_main=(p2-p1)*(t_sub-m1)/(m2-m1)+p1;
  278.                                                         /**/if(p2>p1)
  279.                                                         d_main=(p2-p1)*(t_sub-m1)/(m2-m1)+p1;
  280.                                                         else
  281.                                                         d_main=p1-(p1-p2)*(t_sub-m1)/(m2-m1);
  282.  
  283.                                                        
  284.                                                
  285.                                                   }
  286.                                  }
  287.                          else if(t_main[1]<t_main[0])
  288.                             {
  289.                                             if(t_sub>t_main[1])
  290.                                                   {       
  291.                                                            m2=t_main[1]+tt_main;
  292.                                                     m1=t_main[0];
  293.                                                         p2=d2_main[1];
  294.                                                         p1=d2_main[0];
  295.                                                         //d_main=(p2-p1)*(t_sub-m1)/(m2-m1)+p1;
  296.                                                         /**/if(p2>p1)
  297.                                                         d_main=(p2-p1)*(t_sub-m1)/(m2-m1)+p1;
  298.                                                         else
  299.                                                         d_main=p1-(p1-p2)*(t_sub-m1)/(m2-m1);
  300.                                                        
  301.                                                }
  302.                                                 else if(t_sub<t_main[1])
  303.                                                           {                                                                                                                 
  304.                                                             m2=t_main[1];
  305.                                                             m1=tt_main-t_main[0];
  306.                                                                 p2=d2_main[1];
  307.                                                                 p1=d2_main[0];
  308.                                                                 //d_main=(p2-p1)*(t_sub+m1)/(m2+m1)+p1;
  309.                                                                 /**/if(p2>p1)
  310.                                                             d_main=(p2-p1)*(t_sub+m1)/(m2+m1)+p1;
  311.                                                             else
  312.                                                             d_main=p1-(p1-p2)*(t_sub+m1)/(m2+m1);
  313.                                                        
  314.                                                          }
  315.                                  }                                  
  316.                       flag1=0;
  317.                  }
  318. }
  319.  
  320.  
  321. int main(void)
  322. {
  323.  
  324. //        unsigned int Time_Adr;//u=0,       
  325.     unsigned char *p1,*p2,*p3;
  326.         unsigned char k;
  327. //        unsigned char m;  
  328.         char flag;
  329.         unsigned int j;
  330.  
  331. //        u32 TimCounter=0;
  332. //        int u;
  333.  
  334.         Init_All_Periph();                                //系统初始化
  335.   ReadFromFlash();                                   //从flash读取参数
  336.  
  337.         for(k=0;k<30;k++)
  338.         for(j=0;j<65535;j++);          
  339.         dc_config();                                //配置摄像头
  340.         for(k=0;k<30;k++)
  341.         for(j=0;j<65535;j++);       
  342.                                                        
  343.         for (k=110;k>0;k--);
  344.         SET_L;                                  //为conv切换进行初始化  SET=0
  345.         SET_H;                                           //SET=1
  346.         for (k=60;k>0;k--);
  347.         for(j=0;j<65535;j++);
  348.         SET_L;                                          //SET=0
  349.         k=0;
  350.  
  351.         zhenshu=0;
  352.         zhenshu1=0;
  353.     pre_maxim[0]=0;
  354.         pre_maxim[1]=0;
  355.  
  356.     GPIO_SetBits(GPIOA, GPIO_Pin_4);
  357.         GPIO_SetBits(GPIOA, GPIO_Pin_5);
  358.                
  359.         while((GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0))==1);                 //等待,直到conv变为0
  360.                 //TIM2->CNT=0;
  361.         flag=1;                  
  362.   while (1)
  363.   {
  364.         //if(k<20)
  365.                         {
  366.                          
  367.                 if((GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0))!=RESET)//查询conv是否为1,SRAM是否与单片机相连
  368.                         {
  369.                        
  370.                                  /* TIM2->CNT=0;          //打开计时器,清零开始计时
  371.                                  TimCounter= TIM2->CNT ;
  372.                                 USART_SendData(USART1, TimCounter);
  373.                                         while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)== 0);
  374.                                 USART_SendData(USART1, TimCounter>>8);
  375.                                         while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)== 0);*/          
  376.  
  377.                             
  378.  
  379.                             GPIO_ResetBits(GPIOA, GPIO_Pin_5);
  380.                                 ReadSram(object);
  381.                                 GPIO_SetBits(GPIOA, GPIO_Pin_5);  // SET OE;
  382.  
  383.                                 if(flag==1)
  384.                                 {       
  385.                                         move();                  //将第一幅图像存到背景图像存储区内
  386.                                         flag=0;
  387.                                 }                                
  388.                                 else                               
  389.                            {
  390.                           
  391.                                 /等待将中断接收d1_sub,d2_sub,t1_sub,t2_sub导出,避免使用途中更新/                               
  392.                             while(t2!=0);                       
  393.                                                                                         
  394.                                                 flag_int=1;                          
  395.                                             p1 = (unsigned char*)&d_sub ;
  396.                                                         *p1= d1_sub;
  397.                                                         *(p1+1)= d2_sub;
  398.                                                        
  399.                                                         p2 = (unsigned char*)&t_sub ;
  400.                                                         *p2 = t1_sub;
  401.                                                         p3 = (unsigned char*)&tt_sub ;
  402.                                                         *p3 =t2_sub;       
  403.                                                                                                                                        
  404.                                                 flag_int=0;          
  405.                                 
  406.                                        
  407.                                         d2_main[0]=d2_main[1];                                                 
  408.                                                                                
  409.                                         d_main=get_x();                                         
  410.                                                                                 
  411.                                         d2_main[1]=d_main;
  412.                                   ///保存新参数到flash
  413.                                   if(WriteParaToFlash==2)               
  414.                                      {       
  415.                                           RCC_Disable();       
  416.                                       WriteToFlash();
  417.                                           WriteParaToFlash=0;
  418.                                           RCC_Enable();
  419.                                          }
  420.                                        
  421.  
  422.                 /*                USART_SendData(USART1, d_main);
  423.                         while(USART_GetFlagStatus(USART1, USART_FLAG_TC)== 0); //等待,直到发送标志被清除,发送完成
  424.                         USART_SendData(USART1, d_main>>8);
  425.                         while(USART_GetFlagStatus(USART1, USART_FLAG_TC)== 0);
  426.                         USART_SendData(USART1, 1);
  427.                         while(USART_GetFlagStatus(USART1, USART_FLAG_TC)== 0); //等待,直到发送标志标志被清除,发送完成
  428.                         USART_SendData(USART1, screen_y>>8);
  429.                         while(USART_GetFlagStatus(USART1, USART_FLAG_TC)== 0);          */
  430.               
  431.                                
  432.                                         //windows xp///               
  433.                                    if(Sys_Mode==0)
  434.                                              {
  435.                                                
  436.                                                         if((d_main!=0x00)&&(d_sub!=0x00))                //判断是否有目标物体
  437.                                                            {
  438.                                                                     ObjectFlag=0;         //为0代表有目标
  439.                                                                   
  440.                                                                         bd_to_screen();
  441.                                                                         screen_prex=screen_x;
  442.                                                                         screen_prey=screen_y;
  443.  
  444.        
  445.                                                                    ///SendFlag=1为校准模式
  446.                                                                         if(SendFlag==1)
  447.                                                                                 HID_SendPix(d_main,d_sub) ;  
  448.                                                                   
  449.                                                                     else           //正常工作
  450.                                                                         {
  451.                                                                           if(RightFlag==1)                   //如果发送了一次菜单,接着发送空的左键,然后才发送点下的左键
  452.                                                                              {
  453.                                                                                           Joystick_Send_0(screen_x,screen_y);
  454.                                                                                          RightFlag=0;
  455.                                                                                  }
  456.                                                                           else          
  457.                                                                                      Joystick_Send(screen_x,screen_y);
  458.                                                                         }       
  459.                                                   
  460.                                                                 }
  461.                                                   else
  462.                                                                  {  
  463.                                                                  
  464.                                                                   ObjectFlag++;  
  465.                                                                            RightFlag=0;        //目标拿开后右键标志清零
  466.                                                                           MenuFlag =0;
  467.                                                        
  468.                                                                  if((ObjectFlag<3)&&(SendFlag!=1))
  469.                                                                                     Joystick_Send_0(screen_prex,screen_prey);  //无目标,只发送一次空左键
  470.                                                                          else if(ObjectFlag>3000)                                                        ///发送一次空左键后ObjectFlag一直置为5,直到再次有目标后清0
  471.                                                                                      ObjectFlag=5;                         
  472.  
  473.  
  474.                                                                                      
  475.                                                                                     
  476.                                                                                  for(j=0;j<5;j++)
  477.                                                                                 {
  478.                                                                                         screen_datax[j]=0;
  479.                                                                                         screen_datay[j]=0;
  480.                                                                                
  481.                                                                                 }
  482.                                                                  }
  483.                                                  }         //case0?           
  484.                          else
  485.                                              {
  486.                                                         if((d_main!=0x00)&&(d_sub!=0x00))                //判断是否有目标物体
  487.                                                            {
  488.                                                                     ObjectFlag=0;         //为0代表有目标
  489.                                                                   
  490.                                                                         bd_to_screen();
  491.                                                                         screen_prex=screen_x;
  492.                                                                         screen_prey=screen_y;
  493.                        
  494.                                                                    ///SendFlag=1为校准模式
  495.                                                                         if(SendFlag==1)
  496.                                                                                 HID_SendPix(d_main,d_sub) ;  
  497.                                                                   
  498.                                                                     else           //正常工作
  499.                                                                         {
  500.                                                                          SingleTouch(screen_x,screen_y);
  501.                                                                         // HID_SendPix(d_main,d_sub) ;
  502.                                                                         }       
  503.                                                        
  504.                        
  505.                                                                 }
  506.                                                  else
  507.                                                                  {  
  508.                                                                           ObjectFlag++;  
  509.                                                                            RightFlag=0;        //目标拿开后右键标志清零
  510.                                                                           MenuFlag =0;
  511.                                                                
  512.                                                                     if((ObjectFlag<3)&&(SendFlag!=1))                                                                 ///发送一次空左键后ObjectFlag一直置为5,直到再次有目标后清0
  513.                                                                             SingleTouch_0(screen_prex,screen_prey);  //无目标,只发送一次空左键
  514.                                                                    else if(ObjectFlag>3000)                                                        ///发送一次空左键后ObjectFlag一直置为5,直到再次有目标后清0
  515.                                                                              ObjectFlag=5;               
  516.                                                                                     
  517.                                                                                  for(j=0;j<5;j++)
  518.                                                                                 {
  519.                                                                                         screen_datax[j]=0;
  520.                                                                                         screen_datay[j]=0;
  521.                                                                                
  522.                                                                                 }
  523.                                                                  }
  524.  
  525.                                                  }       
  526.                                                
  527.                                                            
  528.                                                                                                        
  529.                                   }          
  530.  
  531.                                     IWDG_ReloadCounter();
  532.                                                        
  533.                                         SET_H;                //计算完成后发送短复位脉冲
  534.                                        
  535.                                         while((GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0))==SET);
  536.                                                        
  537.                                         SET_L;       
  538.                                        
  539.                                                         
  540.                          }
  541.                 }
  542.        
  543.         }               
  544. }
  545.  
  546. /*******************************************************************************
  547. * Function Name  : RCC_Configuration
  548. * Description    : Configures the different system clocks.
  549. * Input          : None
  550. * Output         : None
  551. * Return         : None
  552. *******************************************************************************/
  553. void RCC_Configuration(void)
  554. {
  555.   SystemInit();
  556.    
  557.   RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
  558.   
  559.   /* Enable the USB clock */
  560.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
  561.  
  562. /* Enable USART2, GPIOA, GPIOx and AFIO clocks */
  563.   RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA |
  564.                          RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
  565.  
  566.   /* Enable USART2 clock */
  567.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  568.   /* Enable USART1 clock */
  569.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
  570.  
  571.   /* Enable TIM2 clock*/
  572. //  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  573. }
  574.  
  575. /*******************************************************************************
  576. * Function Name  : GPIO_Configuration
  577. * Description    : Configures the different GPIO ports.
  578. *******************************************************************************/
  579. void GPIO_Configuration(void)
  580. {
  581.   GPIO_InitTypeDef GPIO_InitStructure;
  582.  
  583. /*-----------------------CONV--------------------------------------------*/          
  584.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  585.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  586.   //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  587.           GPIO_Init(GPIOB, &GPIO_InitStructure);
  588.         /*------------------------SET--------------------------------------------*/          
  589.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  590.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  591.   //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  592.           GPIO_Init(GPIOB, &GPIO_InitStructure);
  593.  
  594.         /*---------------------SCCB_STO_D----------------------------------------*/          
  595.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  596.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  597.   //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  598.           GPIO_Init(GPIOB, &GPIO_InitStructure);
  599.         /*---------------------SCCB_STO_C-----------------------------------------*/          
  600.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  601.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  602.   //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  603.           GPIO_Init(GPIOB, &GPIO_InitStructure);
  604.         /* Configure USART2 Tx (PA.02) as alternate function push-pull */
  605.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  606.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  607.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  608.           GPIO_Init(GPIOA, &GPIO_InitStructure);
  609.    
  610.           /* Configure USART2 Rx (PA.03) as input floating */
  611.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  612.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  613.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  614.           GPIO_Init(GPIOA, &GPIO_InitStructure);
  615.         /* Configure USART1 Tx (PA.9) as alternate function push-pull */
  616.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  617.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  618.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  619.           GPIO_Init(GPIOA, &GPIO_InitStructure);
  620.    
  621.           /* Configure USART1 Rx (PA.10) as input floating */
  622.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  623.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  624.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  625.           GPIO_Init(GPIOA, &GPIO_InitStructure);
  626.  
  627. /**/                //Configure USB D- (PA.11) as alternate function push-pull
  628.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  629.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  630.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  631.           GPIO_Init(GPIOA, &GPIO_InitStructure);
  632.    
  633.                   // Configure USB D+ (PA.12) as function push-pull
  634.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  635.           GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_AF_PP;
  636.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  637.           GPIO_Init(GPIOA, &GPIO_InitStructure);
  638.         /*GPIOC Configuration: THIS GPIOC IS USED AS THE ADDRESS */
  639.           GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0 |GPIO_Pin_1|GPIO_Pin_2 |GPIO_Pin_3 |
  640.                                                           GPIO_Pin_4 |GPIO_Pin_5|GPIO_Pin_6 |GPIO_Pin_7 |
  641.                                                           GPIO_Pin_8 |GPIO_Pin_9|GPIO_Pin_10 |GPIO_Pin_11 |
  642.                                                             GPIO_Pin_12 |GPIO_Pin_13|GPIO_Pin_14 ;
  643.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  644.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  645.   
  646.           GPIO_Init(GPIOC, &GPIO_InitStructure);
  647.  
  648.           //GPIO_InitStructure.GPIO_Pin =    GPIO_Pin_6 |GPIO_Pin_7;
  649.           //GPIO_Init(GPIOB, &GPIO_InitStructure);
  650.   
  651.         /*GPIOB Configuration: GPIOB 8 TO 15 ARE USED AS DATA*/
  652.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 |GPIO_Pin_9|GPIO_Pin_10 |GPIO_Pin_11 |
  653.                                                                   GPIO_Pin_12 |GPIO_Pin_13 |GPIO_Pin_14 |GPIO_Pin_15 ;
  654.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//???
  655.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  656.  
  657.           GPIO_Init(GPIOB, &GPIO_InitStructure);
  658.  
  659.         /*GPIOA Configuration: THIS GPIOC IS USED AS THE control signal*/
  660.  
  661.           GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4 |GPIO_Pin_5;
  662.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//GPIO_Mode_AF_PP;
  663.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  664.  
  665.           GPIO_Init(GPIOA, &GPIO_InitStructure);           //oe,we;
  666.  
  667.  
  668. }
  669.  
  670. /*******************************************************************************
  671. * Function Name  : USART2_Configuration
  672. * Description    : Configures the USART2 controller.
  673. *******************************************************************************/
  674. void USART2_Configuration(void)
  675. {
  676.         USART_InitTypeDef USART_InitStructure;
  677.         USART_InitStructure.USART_BaudRate            = 115200  ;
  678.         USART_InitStructure.USART_WordLength          = USART_WordLength_8b;
  679.         USART_InitStructure.USART_StopBits            = USART_StopBits_1;
  680.         USART_InitStructure.USART_Parity              = USART_Parity_No ;
  681.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  682.         USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;
  683.         USART_Init(USART2, &USART_InitStructure);
  684.         USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  685.        
  686.         USART_Cmd(USART2, ENABLE);   
  687.          
  688. }
  689. /*******************************************************************************
  690. * Function Name  : USART1_Configuration
  691. * Description    : Configures the USART1 controller.
  692. *******************************************************************************/
  693. void USART1_Configuration(void)
  694. {
  695.         USART_InitTypeDef USART_InitStructure;
  696.         USART_InitStructure.USART_BaudRate            = 115200  ;
  697.         USART_InitStructure.USART_WordLength          = USART_WordLength_8b;
  698.         USART_InitStructure.USART_StopBits            = USART_StopBits_1;
  699.         USART_InitStructure.USART_Parity              = USART_Parity_No ;
  700.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  701.         USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;
  702.         USART_Init(USART1, &USART_InitStructure);
  703.        
  704.        
  705.         USART_Cmd(USART1, ENABLE);   
  706.          
  707. }
  708. /*******************************************************************************
  709. * Function Name  : NVIC_Configuration
  710. * Description    : Configures the NVTC controller.
  711. *******************************************************************************/
  712. void NVIC_Configuration(void)
  713. {
  714. NVIC_InitTypeDef NVIC_InitStructure;
  715.  
  716.   /* Configure the NVIC Preemption Priority Bits */  
  717.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  718.  
  719.   /* Enable the USARTy Interrupt */
  720.   NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  721.   NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
  722.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  723.   NVIC_Init(&NVIC_InitStructure);
  724.    /* Enable the USB interrupt */
  725.   NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
  726.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  727.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  728.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  729.   NVIC_Init(&NVIC_InitStructure);
  730.   
  731.   /* Enable the USB Wake-up interrupt */
  732.   NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_IRQn;
  733.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  734.   NVIC_Init(&NVIC_InitStructure);   
  735.  
  736.  
  737. }
  738. /******************************************************************************
  739. * Function Name  : TIMER configuration
  740. * Description    : Configures the SysTick controller.
  741. *******************************************************************************/
  742. void TIMER_Configuration(void)
  743.  
  744. {
  745.   TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  746.  
  747.   TIM_TimeBaseStructure.TIM_Period =10000;   //计数值  
  748.   TIM_TimeBaseStructure.TIM_Prescaler =7200-1;    //预分频,此值+1为分频的除数
  749.   TIM_TimeBaseStructure.TIM_ClockDivision =0x0;   //设置时钟分割
  750.   TIM_TimeBaseStructure.TIM_CounterMode =TIM_CounterMode_Up;  //向上计数
  751.   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  752.   TIM_Cmd(TIM2, ENABLE); //打开定时器
  753. }         
  754.  
  755. /*********************看门狗初始化********************************/
  756. void IWDG_Configuration(void)
  757. {
  758. /* 写入0x5555,用于允许狗狗寄存器写入功能 */
  759. IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
  760.  
  761. /* 狗狗时钟分频,40K/256=156HZ(6.4ms)*/
  762. IWDG_SetPrescaler(IWDG_Prescaler_256);
  763.  
  764. /* 喂狗时间 5s/6.4MS=781 .注意不能大于0xfff*/
  765. IWDG_SetReload(300);
  766.  
  767. /* 喂狗*/
  768. IWDG_ReloadCounter();
  769.  
  770. /* 使能狗狗*/
  771. IWDG_Enable();
  772. }  
  773. /*******************************************************************************
  774. * Function Name  : Delay
  775. * Description    : Inserts a delay time.
  776. * Input          : nCount: specifies the delay time length.
  777. * Output         : None
  778. * Return         : None
  779. ******************************************************************************
  780. void Delay(__IO uint32_t nCount)                                                                  
  781. {
  782.   for (; nCount != 0;nCount--);
  783. }                                                                          */                                                                                       
  784. /*******************************************************************************
  785. * Function Name  : ALL_Periph_Configuration
  786. * Description    : initionalize all periph
  787. *******************************************************************************/
  788. void Init_All_Periph(void)
  789. {
  790.         RCC_Configuration();
  791. //        Delay(100);
  792.         GPIO_Configuration();
  793. //        Delay(100);
  794.         USART2_Configuration();
  795. //        Delay(100);
  796.         USART1_Configuration();
  797. //        Delay(100);
  798.         NVIC_Configuration();
  799. //        Delay(100);
  800.         TIMER_Configuration();
  801. //        Delay(100);
  802.         USB_Init();
  803.         IWDG_Configuration();
  804. }
  805.  
  806.  
  807. #ifdef  USE_FULL_ASSERT
  808. /*******************************************************************************
  809. * Function Name  : assert_failed
  810. * Description    : Reports the name of the source file and the source line number
  811. *                  where the assert_param error has occurred.
  812. * Input          : - file: pointer to the source file name
  813. *                  - line: assert_param error line source number
  814. * Output         : None
  815. * Return         : None
  816. *******************************************************************************/
  817. void assert_failed(uint8_t* file, uint32_t line)
  818. {
  819.   /* User can add his own implementation to report the file name and line number,
  820.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  821.  
  822.   /* Infinite loop */
  823.   while (1)
  824.   {}
  825. }
  826. #endif
  827.  
  828. /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
  829.  
复制代码

 

U客论坛下载-软件源代码.rar

 

文件到原文下载,原文出自:https://bbs.usoftchina.com/thread-210798-1-1.html
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值