STM32双定时器测频率源程序
2020-07-12 来源:51hei
最近在准备八月份的全国大学生电子设计大赛,就做了几道往年的电赛真题,其中有一题是测频率,要求测量频率10Hz—2MHz,误差小于0.1%,当时也是一脸懵,就在论坛找资料,发现论坛中使用的基本都是输入捕获方法测频率,但是这种方法不能测高频,最多测几百千赫兹,达不到题目要求。经过和小伙伴们的一天讨论,最终创造了一种程序,它可以测量1Hz-22MHz,并且误差小于0.1%。下面是验证通过的程序,算是作为一种在论坛潜伏一年多对论坛的回报吧。
单片机源程序如下:
#include 'led.h'
#include 'delay.h'
#include 'key.h'
#include 'sys.h'
#include 'usart.h'
#include 'timer.h'
u32 count_s;
u32 count_ms;
extern u32 count_i;
extern u32 count_over;
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(9600); //串口初始化为9600
TIM3_External_Clock_CountingMode();
TIM4_timer_Init();
while(1)
{
if(count_i>1000)
{
count_ms = TIM3->CNT;
count_s = count_over*65536 + count_ms;
printf('%d rn ',count_s); //打印数据
count_i=0;
count_over=0;
TIM3->CNT=0;
}
}
}
#include 'timer.h'
#include 'led.h'
//通用定时器3中断初始化
//这里时钟选择为APB1的2倍,而APB1为36M
//arr:自动重装值。
//psc:时钟预分频数
//这里使用的是定时器3!
void TIM3_External_Clock_CountingMode(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM4中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM4中断
TIM_ETRClockMode2Config(TIM3, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);//2分频,上升沿有效
TIM_SetCounter(TIM3, 0); // 清零计数器CNT
TIM_Cmd(TIM3,ENABLE);
}
/***********************************************************************************************
************************************************************************************************/
void TIM4_timer_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //时钟使能
TIM_TimeBaseStructure.TIM_Period = 9; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =7199; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM4中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE ); //使能指定的TIM4中断
TIM_SetCounter(TIM4, 0); // 清零计数器CNT
TIM_Cmd(TIM4, ENABLE); //使能TIM4
}
/***********************************************************************************************
************************************************************************************************/
u32 count_i = 0;
void TIM4_IRQHandler(void)
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) //检查TIM4更新中断发生与否
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update); //注意清除标志位
count_i++;
}
}
/***********************************************************************************************
************************************************************************************************/
u32 count_over = 0;
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查TIM4更新中断发生与否
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update); //注意清除标志位
count_over++;
}
}
上一篇:STM32控制步进电机源代码