高级定时器和普通定时器的区别(https://zhuanlan.zhihu.com/p/557896041):
定时器1_定时中断
TIM1是高级定时器,使用的时钟总线是RCC_APB2Periph_TIM1,和普通定时器不一样。
timer.c
#include "timer.h"
#include "led.h"
//初始化定时器为中断触发
void TIM1_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //时钟使能
//定时器TIM1初始化
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断
//中断优先级NVIC设置
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
TIM_Cmd(TIM1, ENABLE); //使能TIM1
}
/* 定时器1中断服务程序 */
void TIM1_UP_IRQHandler(void) /* TIM1中断 */
{
if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET) /* 检查TIM1更新中断发生与否 */
{
TIM_ClearITPendingBit(TIM1, TIM_IT_Update); /* 清除TIM1更新中断标志 */
LED0 = !LED0;
}
}
timer.h
#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"
void TIM1_Init(u16 arr,u16 psc);
#endif
调用
TIM1_Init(500 - 1, 7200 - 1);//50ms
定时器2_定时中断
timer.c
#include "timer.h"
#include "led.h"
//初始化定时器为中断触发
void TIM2_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能
//定时器TIM2初始化
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断
//中断优先级NVIC设置
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
TIM_Cmd(TIM2, ENABLE); //使能TIM2
}
/* 定时器2中断服务程序 */
void TIM2_IRQHandler(void) /* TIM2中断 */
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) /* 检查TIM2更新中断发生与否 */
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update); /* 清除TIM2更新中断标志 */
LED0 = !LED0;
}
}
timer.h
#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"
void TIM2_Init(u16 arr,u16 psc);
#endif
调用
TIM2_Init(500 - 1, 7200 - 1);//50ms
定时器3_定时中断
timer.c
#include "timer.h"
#include "led.h"
//初始化定时器为中断触发
void TIM3_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
//定时器TIM3初始化
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断
//中断优先级NVIC设置
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
TIM_Cmd(TIM3, ENABLE); //使能TIM3
}
/* 定时器3中断服务程序 */
void TIM3_IRQHandler(void) /* TIM3中断 */
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) /* 检查TIM3更新中断发生与否 */
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update); /* 清除TIM3更新中断标志 */
LED0 = !LED0;
}
}
timer.h
#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"
void TI3_Init(u16 arr,u16 psc);
#endif
调用
TIM3_Init(500 - 1, 7200 - 1);//50ms
定时器4_定时中断
timer.c
#include "timer.h"
#include "led.h"
//初始化定时器为中断触发
void TIM4_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //时钟使能
//定时器TIM4初始化
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为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的时间基数单位
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断
//中断优先级NVIC设置
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
TIM_Cmd(TIM4, ENABLE); //使能TIM4
}
/* 定时器4中断服务程序 */
void TIM4_IRQHandler(void) /* TIM4中断 */
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) /* 检查TIM4更新中断发生与否 */
{
TIM_ClearITPendingBit(TIM4, TIM_IT_Update); /* 清除TIM4更新中断标志 */
LED0 = !LED0;
}
}
timer.h
#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"
void TIM4_Init(u16 arr,u16 psc);
#endif
调用
TIM4_Init(500 - 1, 7200 - 1);//50ms
定时器5_定时中断
timer.c
#include "timer.h"
#include "led.h"
//初始化定时器为中断触发
void TIM5_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //时钟使能
//定时器TIM5初始化
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断
//中断优先级NVIC设置
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
TIM_Cmd(TIM5, ENABLE); //使能TIM5
}
/* 定时器5中断服务程序 */
void TIM5_IRQHandler(void) /* TIM5中断 */
{
if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET) /* 检查TIM5更新中断发生与否 */
{
TIM_ClearITPendingBit(TIM5, TIM_IT_Update); /* 清除TIM5更新中断标志 */
LED0 = !LED0;
}
}
timer.h文章来源:https://www.toymoban.com/news/detail-508944.html
#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"
void TIM5_Init(u16 arr,u16 psc);
#endif
调用文章来源地址https://www.toymoban.com/news/detail-508944.html
TIM5_Init(500 - 1, 7200 - 1);//50ms
到了这里,关于【单片机】STM32单片机的各个定时器的定时中断程序,标准库的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!