初中生
最后登录1970-1-1
在线时间 小时
注册时间2015-7-14
|
本帖最后由 STC8050 于 2016-4-19 00:06 编辑
最近在做32的简易频率计,网上搜到的大多是用捕获来求频率,都说频率精度很高。。可我试了感觉也不怎么高,频率一高误差高得吓人。。(应该是太菜,不懂算法。)
然后就尝试用两个定时器做。一个外部模式2(TIM2),一个来数数(TIM3)。低频的是直接测两个周期时间得频率,频率高点的1s中断得TIM2的计数值求频率。。
可是在计算频率方法转换时(由高频率计算方式到低频率计算方式)程序会跑飞。。其余效果还是可以的。。有哪位大神可以帮我看看!!
代码:
中断:
void TIM3_IRQHandler(void)
{
if ( TIM_GetITStatus(TIM3 , TIM_IT_Update) != RESET )
{
IWDG_ReloadCounter();
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
if(MODE_T == modeSlect)
{
over_load +=1;
}
else
{
//获取1s内脉冲计数个数
nCount = TIM_GetCounter(TIM2);
TIM_SetCounter(TIM2,0);
TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
// TIM_Cmd(TIM2, DISABLE);
Freq = 65535 * Index_1 + nCount;
Index_1 = 0;
CollectFlag = 1;
}
}
}
void TIM2_IRQHandler(void)
{
if ( TIM_GetITStatus(TIM2 , TIM_IT_Update) != RESET )
{
IWDG_ReloadCounter();
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
if(MODE_T == modeSlect)
{
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM3,ENABLE);
if(Index == 1)
{
tim_1 = over_load *65535 + TIM_GetCounter(TIM3);
}
if(Index == 2)
{
tim_2 = over_load *65535 + TIM_GetCounter(TIM3);
TIM_Cmd(TIM3,DISABLE);
TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE);
TIM_SetCounter(TIM3,0);
Index = 0;
over_load = 0;
Freq = (2 * 72000000.0/(tim_2-tim_1));
CollectFlag = 1;
// TIM_Cmd(TIM2,DISABLE);
// TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);
}
Index ++;
}
if(MODE_D == modeSlect)
{
Index_1 ++;
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //中断配置
TIM_Cmd(TIM3, ENABLE);
}
}
}
模式选择:
void SlectMode(double freq)
{
if(Freq >= 9999.0)
{
modeSlect = 1;
}
else modeSlect = 0;
IWDG_ReloadCounter();
if(modeSlect == 0)
{
//测周模式
TIM_Cmd(TIM3,DISABLE);
TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE);
TIM2->ARR = 1;
TIM2->SC = 0;
TIM3->ARR = 0xffff-1;
TIM3->SC = 0;
TIM_Cmd(TIM3,DISABLE);
TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE);
TIM2->CNT = 0;
TIM3->CNT = 0;
}
else if(modeSlect == 1)
{
//测频模式
TIM2->ARR = 0xffff-1;
TIM2->SC = 0;
TIM3->ARR = 2000-1;
TIM3->SC = 36000-1;
TIM2->CNT = 0;
}
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
IWDG_ReloadCounter();
}
|
-
-
1.04Hz
-
500.83kHz
-
500.85kHz
|