我用外部计数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); } |
对,考虑到16位定时器65536会溢出,我定时250MS,现在测试100K-130K频率误差在8HZ-12HZ,不过每次测量频率变化,测量出来的数值有些不稳定 有些波动4HZ,怎么解决这个问题呢
刚才是0.1S,现在是定时1S,
TIM2_TimeBaseStructure.TIM_Period =9999;
TIM2_TimeBaseStructure.TIM_Prescaler = (7200-1);
测量100K怎么是34456 HZ.大家分析一下
你说的测M个周期所用的时间.应该怎么做,我现在就是外部计数F=N/T,这么算的
那你认为是好多
目的是什么,TIM3只是计数
示波器测试TIM3的实际间隔。
考虑到65536+34456 月月等于 100000 排查下是否存在溢出
你回啦,我还在等他继续出这个错。
这...
抖动无法避免,因为微调频率一定能到达产生质变(+1)的临界点.
另外可以式式测M个周期所用的时间.