使用STM32F103VCT6. 用脉宽法测量外部脉冲频率值,TIM4的配置 CNT计数频率4Mhz,CNT计数0~65535应该65536个数后溢出。
void TIM4_IRQHandler(void)
{
if(TIM_GetITStatus(TIM4, TIM_IT_Update) == SET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
PluseWidthFreqConutOverflowCh0++;
if(PluseWidthFreqConutOverflowCh0 > 20)
{
g_PluseWidthFreqValueCh0 = 0;
}
}
if (TIM_GetITStatus(TIM4, TIM_IT_CC2) == SET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC2);
if(CntModeCh0==PULSE_WIDTH_FREQ)
{
g_PluseWidthFreqValueCh0 = (float)(4000000.0/(TIM_GetCapture2(TIM4)+1 + (PluseWidthFreqConutOverflowCh0 - 1) * 0xffff )); PluseWidthFreqConutOverflowCh0 = 0;
}
}
OSIntExit();
}
下面是TIM4的配置
TIM_TimeBaseStructure.TIM_Period = 0xffff;
TIM_TimeBaseStructure.TIM_Prescaler = 17; //72Mhz/17+1 =4Mhz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
//TIM_PWMIConfig(TIM3, &TIM_ICInitStructure);
TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);
TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable);
问题:
使用信号发生器产生各种频率段的信号,均可以测量到准确的频率(使用低频测量)。但是在61Hz时 测量频率会出现各种异常大值如下:
信号发生器61Hz方波,单片机测量的频率值:
0x00073aa0 -> 473760Hz
0x00072eca -> 470730Hz
0x0007caca -> 510666Hz
0x000861d3 -> 549331Hz
0x0007edc2 -> 519618Hz
0x0008f429 -> 586793Hz
其他频率都正常比如60Hz,单片机测量值为:
0x0000003c 稳定测量为 60Hz。
问题分析:4Mhz的tim计数频率,溢出计数值为65536,4Mhz / 65536 = 61.035Hz,所以当外部输入脉冲频率为61Hz时候,刚好TIM4的捕获中断和溢出中断同时产生,而STM32的TIM4中断只有一个配置优先级,估计在61Hz下 溢出中断频繁丢失,导致测量频率跳大值。。。 |