完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
为了方便大家使用,本帖子贴出来了高级定时器TIM1和TIM8,普通定时器TIM2~TIM5,低级定时器TIM6和TIM7共8个定时器的库函数初始化代码,方便大家使用,还请大家继续支持我。 高级定时器 TIM1初始化.h头文件 #ifndef __TIMER1_H #define __TIMER1_H #include "stm32f10x.h" void TIM1_Init(unsigned int arr,unsigned int psc); #endif TIM1初始化.c源文件 #include "sys.h" #include "timer1.h" //arr:自动重装值 //psc:时钟预分频数 void TIM1_Init(unsigned int arr,unsigned int psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能定时器1时钟 //初始化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); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //参数初始化 TIM_ClearFlag(TIM1, TIM_FLAG_Update);//清中断标志位 TIM_ITConfig(TIM1, TIM_IT_Update|TIM_IT_Trigger,ENABLE ); //使能指定的TIM1中断,允许更新中断 允许触发中断源 //中断优先级NVIC设置 NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; //TIM1中断 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) //TIM8中断 { if(TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)//检查指定的TIM中断发生与否:TIM 中断源 { //应用代码编写区 TIM_ClearITPendingBit(TIM1, TIM_IT_Update);//清除TIMx的中断待处理位:TIM 中断源 } } TIM8初始化.h头文件 #ifndef __TIMER8_H #define __TIMER8_H #include "stm32f10x.h" void TIM8_Init(unsigned int arr,unsigned int psc); #endif TIM8初始化.c源文件 #include "sys.h" #include "timer8.h" //arr:自动重装值 //psc:时钟预分频数 void TIM8_Init(unsigned int arr,unsigned int psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); //使能定时器1时钟 //初始化TIM8 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(TIM8, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //参数初始化 TIM_ClearFlag(TIM8, TIM_FLAG_Update);//清中断标志位 TIM_ITConfig(TIM8, TIM_IT_Update|TIM_IT_Trigger,ENABLE ); //使能指定的TIM8中断,允许更新中断 允许触发中断源 //中断优先级NVIC设置 NVIC_InitStructure.NVIC_IRQChannel = TIM8_UP_IRQn; //TIM8中断 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(TIM8, ENABLE); //使能TIM8 } //定时器8中断服务程序 void TIM8_UP_IRQHandler(void) //TIM8中断 { if(TIM_GetITStatus(TIM8, TIM_IT_Update) != RESET)//检查指定的TIM中断发生与否:TIM 中断源 { //代码应用区 TIM_ClearITPendingBit(TIM8, TIM_IT_Update);//清除TIMx的中断待处理位:TIM 中断源 } } 普通定时器 TIM2初始化.h头文件 #ifndef __TIMER2_H #define __TIMER2_H #include "stm32f10x.h" void TIM2_Init(unsigned int arr,unsigned int psc); #endif TIM2初始化.c源文件 #include "sys.h" #include "timer2.h" //arr:自动重装值 //psc:时钟预分频数 void TIM2_Init(unsigned int arr, unsigned int psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能 TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//清除中断标志位 TIM_TimeBaseStructure.TIM_Period = arr; //自动重装载周期值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频值 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx TIM_ITConfig( TIM2,TIM_IT_Update | TIM_IT_Trigger, ENABLE );//使能更新触发TIM中断 //中断分组设置 NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM2中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 TIM_Cmd(TIM2, ENABLE); //使能TIMx外设 } //定时器2中断服务程序 void TIM2_IRQHandler(void) { //判断TIM2更新中断是否发生 if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET) { //代码应用区 TIM_ClearITPendingBit(TIM2,TIM_IT_Update); } } TIM3初始化.h头文件 #ifndef __TIMER3_H #define __TIMER3_H #include "stm32f10x.h" void TIM3_Init(unsigned int arr,unsigned int psc); #endif TIM3初始化.c源文件 #include "sys.h" #include "timer3.h" //arr:自动重装值 //psc:时钟预分频数 void TIM3_Init(unsigned int arr, unsigned int psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能 TIM_TimeBaseStructure.TIM_Period = arr; //自动重装载周期值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频值 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx TIM_ITConfig( TIM3,TIM_IT_Update | TIM_IT_Trigger, ENABLE );//使能更新触发TIM中断 //中断分组设置 NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 TIM_Cmd(TIM3, ENABLE); //使能TIMx外设 } //中断服务程序 void TIM3_IRQHandler(void) { if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 { //代码应用区 TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源 } } TIM4初始化.h头文件 #ifndef __TIMER4_H #define __TIMER4_H #include "stm32f10x.h" void TIM4_Init(unsigned int arr,unsigned int psc); #endif TIM4初始化.c源文件 #include "sys.h" #include "timer4.h" //arr:自动重装值 //psc:时钟预分频数 void TIM4_Init(unsigned int arr, unsigned int psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //时钟使能 TIM_TimeBaseStructure.TIM_Period = arr; //自动重装载周期值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频值 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx TIM_ITConfig( TIM4,TIM_IT_Update | TIM_IT_Trigger, ENABLE );//使能更新触发TIM中断 //中断分组设置 NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM4中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //从优先级3级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 TIM_Cmd(TIM4, ENABLE); //使能TIMx外设 } //定时器4中断服务程序 void TIM4_IRQHandler(void) { //判断TIM4更新中断是否发生 if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 { //代码应用区 TIM_ClearITPendingBit(TIM4, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源 } } TIM5初始化.h头文件 #ifndef __TIMER5_H #define __TIMER5_H #include "stm32f10x.h" void TIM5_Init(unsigned int arr,unsigned int psc); #endif TIM5初始化.c源文件 #include "sys.h" #include "timer5.h" //arr:自动重装值 //psc:时钟预分频数 void TIM5_Init(unsigned int arr, unsigned int psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //时钟使能 TIM_ClearITPendingBit(TIM5,TIM_IT_Update);//清除中断标志位 TIM_TimeBaseStructure.TIM_Period = arr; //自动重装载周期值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频值 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx TIM_ITConfig( TIM5,TIM_IT_Update | TIM_IT_Trigger, ENABLE );//使能更新触发TIM中断 //中断分组设置 NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; //TIM5中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 TIM_Cmd(TIM5, ENABLE); //使能TIMx外设 } //定时器5中断服务程序 void TIM5_IRQHandler(void) { //判断TIM5更新中断是否发生 if(TIM_GetITStatus(TIM5,TIM_IT_Update)!=RESET) { //代码应用区 TIM_ClearITPendingBit(TIM5,TIM_IT_Update); } } 低级定时器 TIM6初始化.h头文件 #ifndef __TIMER6_H #define __TIMER6_H #include "stm32f10x.h" void TIM6_Init(unsigned int arr,unsigned int psc); #endif TIM6初始化.c源文件 #include "sys.h" #include "timer6.h" //arr:自动重装值 //psc:时钟预分频数 void TIM6_Init(unsigned int arr, unsigned int psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); //时钟使能 TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 计数到5000为500ms TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 10Khz的计数频率 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 TIM_ITConfig( TIM6,TIM_IT_Update|TIM_IT_Trigger,ENABLE);//使能定时器6更新触发中断 NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn; //TIM3中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级3级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 TIM_Cmd(TIM6, ENABLE); //使能TIMx外设 } //定时器6中断服务程序 void TIM6_IRQHandler(void) { if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 { TIM_ClearITPendingBit(TIM6, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源 } } TIM7初始化.h头文件 #ifndef __TIMER7_H #define __TIMER7_H #include "stm32f10x.h" void TIM7_Init(unsigned int arr,unsigned int psc); #endif TIM7初始化.c源文件 #include "sys.h" #include "timer7.h" //arr:自动重装值 //psc:时钟预分频数 void TIM7_Init(unsigned int arr, unsigned int psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE); //时钟使能 //定时器TIM7初始化 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(TIM7, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位 TIM_ITConfig(TIM7,TIM_IT_Update,ENABLE ); //使能指定的TIMx中断,允许更新中断 //中断优先级NVIC设置 NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn; //TIMx中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级3级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器 TIM_Cmd(TIM7, ENABLE); //使能TIMx } //定时器7中断服务程序 void TIM7_IRQHandler(void) { if (TIM_GetITStatus(TIM7, TIM_IT_Update) != RESET) //检查TIM7更新中断发生与否 { TIM_ClearITPendingBit(TIM7, TIM_IT_Update ); //清除TIMx更新中断标志 } } 总结:至此STM32FI03系列的8个定时器的初始化程序代码干货已经分享完毕,如果觉得还不错,请给博主点个赞哦! |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1649 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1566 浏览 1 评论
994 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
694 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1613 浏览 2 评论
1872浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
659浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
528浏览 3评论
544浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
517浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-28 07:39 , Processed in 0.603920 second(s), Total 46, Slave 40 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号