首先申明,小白一枚,一直是在网上找自己需要的代码,某些能找到,某些是怎么也没找到,有问题,大神也不屑(可能是觉得档次太低),苦了我们这些还在入门的同学,或者回复就是我顶,帮顶,实际帮助是没得。言归正传,我把我昨天测量频率代码贴出来(借鉴别人的),误差很小。1.信号源
有信号发生器的跳过本段,没有的就用MCU本身做发生器,用定时器1做的10KHZ,方波。
在TIMER.C中初始化函数:
void TIM1_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //使能GPIO外设时钟使能
//设置该引脚为复用输出功能,输出TIM1 CH2的PWM脉冲波形
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 80K
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 不分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_CtrlPWMOutputs(TIM1,ENABLE); //MOE 主输出使能
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //CH1预装载使能
TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIMx在ARR上的预装载寄存器
TIM_Cmd(TIM1, ENABLE); //使能TIM1
}
在主函数初始化PWM1 TIM1_PWM_Init(7199,0); //不分频。PWM频率=72000/(7199+1)=10Khz
在主函数中设置占空比TIM_SetCompare1(TIM1,40);//
上面就把信号发生器弄好了。
2.计数器
当时参考别人的用定时器的输入捕获功能,发现在低频率的时候,误差还可以接受,当频率达到K级别的时候,发现无法玩了,误差大了,放弃,也有可能是我不会玩的原因。最后选择是方案是,单位时间里计数,就得到频率。用定时器2做250ms的定时,定时器4用于脉冲计数TIM4_CH1。
定时器2初始化
void Tim2_Timer(void)
{
TIM_TimeBaseInitTypeDef TIM2_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_DeInit(TIM2);
TIM2_TimeBaseStructure.TIM_Period =2499;//250MS
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);
}
定时器2的中端服务函数
u32 CAPTURE1,sum,count, Frequency1=0;
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
CAPTURE1=(u32)TIM_GetCounter(TIM4);
}
sum+=CAPTURE1;
count++;
if(count==4)//4次为1S,即可得到频率
{
Frequency1=sum/4/0.25;
sum=0;
count=0;
}
TIM_SetCounter(TIM4,0);
}
定时器4初始化
void Time4_Configuration()
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度
GPIO_Init(GPIOB, &GPIO_InitStructure);
//TIM_DeInit(TIM4);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler =0 ;
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit( TIM4, &TIM_TimeBaseStructure); // Time base configuration
TIM_TIxExternalClockConfig(TIM4,TIM_TS_TI1FP1,TIM_ICPolarity_Rising,0);
TIM_SetCounter(TIM4, 0); // 清零计数器CNT
TIM_Cmd(TIM4,ENABLE);
}
主函数中调用Time4_Configuration();
Tim2_Timer();
最后在主函数中用串口将测量到的结果打印出来
printf("Frequency1 is %d HZ.\r\n",Frequency1); |