stm32测方波频率

2019-07-15 10:07发布

利用STM32测方波的频率,以下是我编写的下横须,请问大神该程序有什么问题?屏幕上不能显示方波的频率也就是检测不到。
  1. void tiM3_Cap_Init(u32 arr,u16 psc)
  2. {
  3.         GPIO_InitTypeDef GPIO_InitStructure;
  4.         TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  5.         NVIC_InitTypeDef NVIC_InitStructure;
  6.         TIM_ICInitTypeDef  TIM_ICInitStructure;
  7.          
  8.         TIM_DeInit(TIM3);
  9.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);         
  10.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);               
  11.          
  12.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  13.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP ;
  14.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;      
  15. //        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  16. //        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
  17.         GPIO_Init(GPIOB,&GPIO_InitStructure);
  18.          
  19.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  20.         GPIO_Init(GPIOB,&GPIO_InitStructure);
  21.          
  22.          GPIO_PinRemapConfig( GPIO_FullRemap_TIM3,ENABLE );
  23.         //GPIO_PinRemapConfig( GPIO_FullRemap_TIM3,ENABLE );
  24.          
  25.                                        
  26.         TIM_TimeBaseStructure.TIM_Prescaler=psc;  
  27.         TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
  28.         TIM_TimeBaseStructure.TIM_Period=arr;  
  29.         TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
  30.          
  31.         TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
  32.         //???TIM2??????
  33.         TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
  34.                                 TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;        
  35.                                 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  36.                                 TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;      
  37.                                 TIM_ICInitStructure.TIM_ICFilter = 0x00;
  38.                                 TIM_ICInit(TIM3, &TIM_ICInitStructure);
  39.          
  40.         TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;
  41.         TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;      
  42.          
  43.         TIM_ICInit(TIM3, &TIM_ICInitStructure);
  44.                  
  45.         TIM_ITConfig(TIM3,TIM_IT_Update|TIM_IT_CC3|TIM_IT_CC4,ENABLE);        
  46. //        TIM2_CH1_Cap_DMAInit();
  47.         TIM_Cmd(TIM3,ENABLE );      
  48.   
  49.         NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  50.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
  51.         NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;               
  52.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                       
  53.         NVIC_Init(&NVIC_InitStructure);        
  54.          
  55. }


  56. static double  rising;
  57. static u32 rising_last;
  58. static u32 falling;
  59. void TIM3_IRQHandler(void)
  60. {                     
  61.         if(TIM3->SR&TIM_FLAG_CC3)//TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)//??1??????
  62.         {
  63.                 rising=TIM3->CCR3-rising_last;
  64.                 rising_last=TIM3->CCR3;
  65.                 return;
  66.         }
  67.         if(TIM3->SR&TIM_FLAG_CC4)//TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET)
  68.         {
  69.                 falling=TIM3->CCR4-rising_last;
  70.                 return;
  71.         }
  72.         TIM3->SR=0;
  73. }



  74. char vol[6]={"0000Hz"};
  75. int main(void)
  76. {
  77.         //OLED_Init();
  78.         uint32_t f;
  79.         Lcd_Init();
  80.         LCD_LED_SET;
  81.         Lcd_Clear(GRAY0);
  82.         TIM3_Cap_Init(65535,71);
  83.         while(1){
  84.                                 f = (float) (9000000/rising);
  85.                                 vol[0] = (f/1000);//ÕýÏÒ²¨0.885   Ö±Á÷*0.948*
  86.                           //vol[1] = '.';
  87.                                 vol[2] = ((f%1000)/100);
  88.                                 vol[3] = ((f%100)/10);
  89.                                 vol[4] = (f%10);                                
  90.                           Gui_DrawFont_GBK16(10,10,BLUE,GRAY0,"The frequence is");
  91.               Gui_DrawFont_Num32(25,40,BLUE,GRAY0,vol[0]);
  92.                           //Gui_DrawFont_GBK16(50,56,BLUE,GRAY0,"µã");
  93.                                 Gui_DrawFont_Num32(75-16,40,BLUE,GRAY0,vol[2]);
  94.                           Gui_DrawFont_Num32(100-16,40,BLUE,GRAY0,vol[3]);
  95.                           Gui_DrawFont_Num32(125-16,40,BLUE,GRAY0,vol[4]);
  96.                           Gui_DrawFont_GBK16(170-16,55,BLUE,GRAY0,"Hz");        

  97. }
  98. }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。