STM32頻率計算——捕獲方式

STM32採用定時器捕獲的方法測低頻信號很準確,我測高頻100K-120K就誤差太大了,大概200Hz,這兒的誤差是個範圍,不是某個值。有的人說兩個定時器一個定時,一個計數,這樣太浪費資源了吧。我項目要採集兩個地感線圈的頻率,所以用兩個定時器捕獲,這兒只說一個定時器的方法,用的是定時器3通道2,下面是用捕獲的方法計算頻率:

void Time3_Configuration()
{  
	TIM_ICInitTypeDef  TIM_ICInitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	//RCC_ClocksTypeDef freq;
	//RCC_GetClocksFreq(&freq);
	/*TIM3時基*/
	TIM_DeInit(TIM3);
  TIM_TimeBaseStructure.TIM_Period = 0xffff;
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);  // Time base configuration
	/*TIM3輸入捕獲*/
  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_CounterMode_Up;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;   /*輸入預分頻*/
  TIM_ICInitStructure.TIM_ICFilter = 0;
	
  TIM_ICInit(TIM3, &TIM_ICInitStructure);

  TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);

  /* Select the TIM3 Input Trigger: TI2FP2 */
  TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);

  /* Select the slave Mode: Reset Mode */
  TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);

  /* Enable the Master/Slave Mode */
  TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);

  /* TIM enable counter */
  TIM_Cmd(TIM3, ENABLE);

  /* Enable the CC2 Interrupt Request */
  TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);
}
中斷部分計算頻率:
void TIM3_IRQHandler(void)
{ 
  if(TIM_GetITStatus(TIM3, TIM_IT_CC2) == SET) 
  {
    /* Clear TIM5 Capture compare interrupt pending bit */
    TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
    if(capture_number2 == 0)
    {
      /* Get the Input Capture value */
      ic3_readvalue3 = TIM_GetCapture2(TIM3);
      capture_number2 = 1;
    }
    else if(capture_number2 == 1)
    {
      /* Get the Input Capture value */
      ic3_readvalue4 = TIM_GetCapture2(TIM3); 
      
      /* Capture computation */
      if (ic3_readvalue4 > ic3_readvalue3)
      {
        CAPTURE2 = ic3_readvalue4 ; 
      }
      
      /* Frequency computation */ 
      Frequency2 = (u32)72000000 / CAPTURE2;
      capture_number2 = 0;
    }
		else
		{
			Frequency2=0;
		}
  }
}

還有一種方法就是用外部中斷計算頻率,不過我沒用過。更精確的計算方法我得慢慢研究。接觸STM32也不是太久。

更多技術文章瀏覽請關注:

百家號:
https://author.baidu.com/home?context=%7B%22app_id%22%3A%221646108714303504%22%7D&wfr=bjh

頭條號:
https://www.toutiao.com/c/user/8115738721/#mid=1646025109246987

發佈了39 篇原創文章 · 獲贊 20 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章