我用外部计数STM32 ETR计算频率,测量100K频率,用定时器2定时1S,定时器三PD2(TIM3_ETR)计数,计算公式f=n/t;测量出来的结果是9999 HZ,整整少了一位数字,这是什么原因呢,大家分析一下,谢谢,附我的程序
void Time3_Configuration()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); // Time base configuration
TIM_ETRClockMode2Config(TIM3, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
//TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_SetCounter(TIM3, 0);
TIM_Cmd(TIM3, ENABLE);
}
/*1S定时*/
void Tim2_Config(void)
{
TIM_TimeBaseInitTypeDef TIM2_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_DeInit(TIM2);
TIM2_TimeBaseStructure.TIM_Period =999;
TIM2_TimeBaseStructure.TIM_Prescaler = (7200-1);
TIM2_TimeBaseStructure.TIM_ClockDivision = 0x0;//ÉèÖÃʱÖÓϵÊý ²»·ÖƵ
TIM2_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//ÏòÉϼÆÊýģʽ
TIM_TimeBaseInit(TIM2, &TIM2_TimeBaseStructure); // Time base configuration
TIM_ClearFlag(TIM2,TIM_FLAG_Update);//Çå³þ¸üбê־λ
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE );
TIM_Cmd(TIM2, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
最后的计算部分:
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
CAPTURE1=(u32)TIM_GetCounter(TIM3);
}
sum+=CAPTURE1;
count++;
if(count==4)
{
Frequency1=sum/4;//求频率公式F=n/t;这里求了4次的计数平均值,结果是1S的频率
sum=0;
count=0;
}
TIM_SetCounter(TIM3,0);
}