目录:
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"
void TIM3_Init(unsigned int arr,unsigned int psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_OCInitTypeDef TIM_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
}
unsigned char flag = 0;
void TIM3_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"
unsigned int led_pwm_ctr_cnt = 0;
unsigned int led_pwm = 0;
/*外设时钟初始化*/
void RCC_PeriphClock_Config(void)
{
/*GPIO口时钟初始化*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
}
int main()
{
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"
void TIM3_Init(unsigned int arr,unsigned int psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_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);
//PWM CH2输出初始化
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);
// //PWM CH1输出初始化
// 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的预装载功能*/
TIM_SetCompare2(TIM3, 500);
//TIM_SetCompare1(TIM3, 500);
}
main.c
#include "stm32f10x.h"
#include "timer_driver.h"
#include "led_driver.h"
#include "systick_driver.h"
#include "key_board.h"
unsigned int led_pwm_ctr_cnt = 0;
unsigned int led_pwm = 500;
/*外设时钟初始化*/
void RCC_PeriphClock_Config(void)
{
/*GPIO口时钟初始化*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
}
int main()
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置中断优先级分组
SysTick_Init(INT_10MS,SysTick_CLKSource_HCLK_Div8); //内核滴答定时器配置,用于led任务轮循
RCC_PeriphClock_Config();
Led_Init(); //led灯初始化
Key_Init();
TIM3_Init(999,71); //定时器3初始化,pwm周期为1KHZ
while(1)
{
if(led_pwm_ctr_cnt >= 1) //500ms轮询一次
{
led_pwm_ctr_cnt = 0;
if(K1 == 0) //k1按键被按下
{
if(led_pwm < 1000)
led_pwm++;
TIM_SetCompare2(TIM3, led_pwm);
TIM_SetCompare1(TIM3, led_pwm);
}
else
if(K2 == 0)
{
if(led_pwm > 0)
led_pwm--;
TIM_SetCompare2(TIM3, led_pwm);
TIM_SetCompare1(TIM3, led_pwm);
}
}
}
}
如下为逻辑分析仪抓取的pwm输出波形:
上一篇:89C51与ad0832 输出正弦波,三角波,矩形波,锯齿波
下一篇:51单片机定时器中断程序
推荐阅读最新更新时间:2024-03-16 16:02