STM32频率计算——捕获方式

来源:互联网 发布:sai软件中文版 编辑:程序博客网 时间:2024/06/05 21:55

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也不是太久。

0 0
原创粉丝点击