通用定时器(中断功能和PWM输出)
扫描二维码
随时随地手机看文章
目录:
1:概述
2:常用中断功能
3:PWM输出
1:概述
在开发中,定时器的应用很广泛,简单总结为三个方面:
1.1:中断功能的应用,常用的是利用定时器中断,实现定时、记时、延时、超时判断,前面博文中有总结过内核定时器的各种使用的方法;
1.2:比较输出,常用应用就是PWM输出,用脉宽调制来实现led、电机等的控制;
1.3:输入捕获,可以抓取输入的方波信号,统计波形周期和占空比,最常见利用就是门控,将外部模拟量转化为数字量(定时器的计数值);
2:常用中断功能
定时器的初始化详细参考手册,主要的一点是确定定时器的溢出周期,利用公式:Tout = (arr+1)*(psc+1)/Tclk,可计算出定时器的溢出频率;
如下代码实现:在中断函数中实现led翻转,翻转频率1HZ,既led闪烁频率1HZ;
timer_driver.c
-
#include"timer_driver.h"
-
#include"led_driver.h"
-
-
voidTIM3_Init(unsignedintarr,unsignedintpsc)
-
{
-
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
-
NVIC_InitTypeDefNVIC_InitStructure;
-
TIM_OCInitTypeDefTIM_OCInitStruct;
-
-
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);//将TIM3的CH2映射到PB5上
-
-
//定时器TIM3初始化
-
TIM_TimeBaseStructure.TIM_Period=arr;
-
TIM_TimeBaseStructure.TIM_Prescaler=psc;
-
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
-
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
-
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
-
-
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);//使能指定的TIM3中断,允许更新中断
-
-
//中断优先级NVIC设置
-
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;//TIM3中断
-
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//先占优先级0级
-
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;//从优先级3级
-
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//IRQ通道被使能
-
NVIC_Init(&NVIC_InitStructure);//初始化NVIC寄存器
-
-
TIM_Cmd(TIM3,ENABLE);//使能TIMx
-
}
-
-
unsignedcharflag=0;
-
voidTIM3_IRQHandler(void)
-
{
-
flag=!flag;
-
if(flag==0)
-
{
-
LED1_ON;
-
LED2_ON;
-
LED3_ON;
-
}
-
else
-
{
-
LED1_OFF;
-
LED2_OFF;
-
LED3_OFF;
-
}
-
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
-
}
main.c
-
#include"stm32f10x.h"
-
#include"timer_driver.h"
-
#include"led_driver.h"
-
#include"systick_driver.h"
-
#include"key_board.h"
-
-
unsignedintled_pwm_ctr_cnt=0;
-
unsignedintled_pwm=0;
-
-
/*外设时钟初始化*/
-
voidRCC_PeriphClock_Config(void)
-
{
-
/*GPIO口时钟初始化*/
-
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOD,ENABLE);
-
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
-
}
-
-
intmain()
-
{
-
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组
-
SysTick_Init(INT_10MS,SysTick_CLKSource_HCLK_Div8);//内核滴答定时器配置,用于led任务轮循
-
RCC_PeriphClock_Config();
-
Led_Init();//led灯初始化
-
TIM3_Init(9999,7199);//定时器3初始化,溢出频率为1hz
-
-
while(1)
-
{
-
;
-
}
-
}
3:PWM输出
pwm输出需确定两点:PWM输出波形的周期,PWM输出波形的占空比,具体需要设置PWM波形的有效电平和PWM输出模式;
如下代码实现1khz的pwm波形输出,通过安检可以改变pwm波的占空比;
timer_driver.c
-
#include"timer_driver.h"
-
#include"led_driver.h"
-
-
voidTIM3_Init(unsignedintarr,unsignedintpsc)
-
{
-
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
-
TIM_OCInitTypeDefTIM_OCInitStruct;
-
-
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);//将TIM3的CH2映射到PB5上
-
-
//定时器TIM3初始化
-
TIM_TimeBaseStructure.TIM_Period=arr;
-
TIM_TimeBaseStructure.TIM_Prescaler=psc;
-
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
-
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
-
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
-
-
//PWMCH2输出初始化
-
TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM2;/*开发板高电平led灯亮,因为设置向上计数方式,并且设置OC1有效电平为高电平,根据手册,设置为PWM模式2*/
-
TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;/*设置OC2输出使能*/
-
TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_Low;/*设置OC2有效电平为低电平*/
-
TIM_OC2Init(TIM3,&TIM_OCInitStruct);
-
-
////PWMCH1输出初始化
-
//TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM2;/*开发板高电平led灯亮,因为设置向上计数方式,并且设置OC1有效电平为高电平,根据手册,设置为PWM模式2*/
-
//TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;/*设置OC1输出使能*/
-
//TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_Low;/*设置OC1有效电平为低电平*/
-
//TIM_OC1Init(TIM3,&TIM_OCInitStruct);
-
-
TIM_Cmd(TIM3,ENABLE);//使能TIMx
-
-
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);/*开启输出比较2的预装载功能*/
-
//TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);/*开启输出比较2的预装载功能*/
-