当前位置:   article > 正文

STM32F103系列 TIM1~TIM8库函数初始化代码批量发布干货_stm32,库函数高级定时器8初始化

stm32,库函数高级定时器8初始化

前言:大家学生平时使用STM32F103系列的单片机比较多,我发布了一个串口2和串口3的帖子https://blog.csdn.net/weixin_41094315/article/details/80143240深受大家欢迎。

为了方便大家使用,本帖子贴出来了高级定时器TIM1和TIM8,普通定时器TIM2~TIM5,低级定时器TIM6和TIM7共8个定时器的库函数初始化代码,方便大家使用,还请大家继续支持我。

 

高级定时器

TIM1初始化.h头文件

  1. #ifndef __TIMER1_H
  2. #define __TIMER1_H
  3. #include "stm32f10x.h"
  4. void TIM1_Init(unsigned int arr,unsigned int psc);
  5. #endif

TIM1初始化.c源文件

  1. #include "sys.h"
  2. #include "timer1.h"
  3. //arr:自动重装值
  4. //psc:时钟预分频数
  5. void TIM1_Init(unsigned int arr,unsigned int psc)
  6. {
  7. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  8. NVIC_InitTypeDef NVIC_InitStructure;
  9. RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能定时器1时钟
  10. //初始化TIM1
  11. TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
  12. TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
  13. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
  14. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  15. TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
  16. TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //参数初始化
  17. TIM_ClearFlag(TIM1, TIM_FLAG_Update);//清中断标志位
  18. TIM_ITConfig(TIM1, TIM_IT_Update|TIM_IT_Trigger,ENABLE ); //使能指定的TIM1中断,允许更新中断 允许触发中断源
  19. //中断优先级NVIC设置
  20. NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; //TIM1中断
  21. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
  22. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
  23. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
  24. NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
  25. TIM_Cmd(TIM1, ENABLE); //使能TIM1
  26. }
  27. //定时器1中断服务程序
  28. void TIM1_UP_IRQHandler(void) //TIM8中断
  29. {
  30. if(TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)//检查指定的TIM中断发生与否:TIM 中断源
  31. {
  32. //应用代码编写区
  33. TIM_ClearITPendingBit(TIM1, TIM_IT_Update);//清除TIMx的中断待处理位:TIM 中断源
  34. }
  35. }

 

TIM8初始化.h头文件

  1. #ifndef __TIMER8_H
  2. #define __TIMER8_H
  3. #include "stm32f10x.h"
  4. void TIM8_Init(unsigned int arr,unsigned int psc);
  5. #endif

TIM8初始化.c源文件

  1. #include "sys.h"
  2. #include "timer8.h"
  3. //arr:自动重装值
  4. //psc:时钟预分频数
  5. void TIM8_Init(unsigned int arr,unsigned int psc)
  6. {
  7. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  8. NVIC_InitTypeDef NVIC_InitStructure;
  9. RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); //使能定时器1时钟
  10. //初始化TIM8
  11. TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
  12. TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
  13. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
  14. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  15. TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
  16. TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //参数初始化
  17. TIM_ClearFlag(TIM8, TIM_FLAG_Update);//清中断标志位
  18. TIM_ITConfig(TIM8, TIM_IT_Update|TIM_IT_Trigger,ENABLE ); //使能指定的TIM8中断,允许更新中断 允许触发中断源
  19. //中断优先级NVIC设置
  20. NVIC_InitStructure.NVIC_IRQChannel = TIM8_UP_IRQn; //TIM8中断
  21. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
  22. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
  23. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
  24. NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
  25. TIM_Cmd(TIM8, ENABLE); //使能TIM8
  26. }
  27. //定时器8中断服务程序
  28. void TIM8_UP_IRQHandler(void) //TIM8中断
  29. {
  30. if(TIM_GetITStatus(TIM8, TIM_IT_Update) != RESET)//检查指定的TIM中断发生与否:TIM 中断源
  31. {
  32. //代码应用区
  33. TIM_ClearITPendingBit(TIM8, TIM_IT_Update);//清除TIMx的中断待处理位:TIM 中断源
  34. }
  35. }

 

 

普通定时器

TIM2初始化.h头文件

  1. #ifndef __TIMER2_H
  2. #define __TIMER2_H
  3. #include "stm32f10x.h"
  4. void TIM2_Init(unsigned int arr,unsigned int psc);
  5. #endif

TIM2初始化.c源文件

  1. #include "sys.h"
  2. #include "timer2.h"
  3. //arr:自动重装值
  4. //psc:时钟预分频数
  5. void TIM2_Init(unsigned int arr, unsigned int psc)
  6. {
  7. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  8. NVIC_InitTypeDef NVIC_InitStructure;
  9. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //时钟使能
  10. TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//清除中断标志位
  11. TIM_TimeBaseStructure.TIM_Period = arr; //自动重装载周期值
  12. TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频值
  13. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
  14. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  15. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx
  16. TIM_ITConfig( TIM2,TIM_IT_Update | TIM_IT_Trigger, ENABLE );//使能更新触发TIM中断
  17. //中断分组设置
  18. NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM2中断
  19. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级
  20. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
  21. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
  22. NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
  23. TIM_Cmd(TIM2, ENABLE); //使能TIMx外设
  24. }
  25. //定时器2中断服务程序
  26. void TIM2_IRQHandler(void)
  27. {
  28. //判断TIM2更新中断是否发生
  29. if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
  30. {
  31. //代码应用区
  32. TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
  33. }
  34. }

 

TIM3初始化.h头文件

  1. #ifndef __TIMER3_H
  2. #define __TIMER3_H
  3. #include "stm32f10x.h"
  4. void TIM3_Init(unsigned int arr,unsigned int psc);
  5. #endif

TIM3初始化.c源文件

  1. #include "sys.h"
  2. #include "timer3.h"
  3. //arr:自动重装值
  4. //psc:时钟预分频数
  5. void TIM3_Init(unsigned int arr, unsigned int psc)
  6. {
  7. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  8. NVIC_InitTypeDef NVIC_InitStructure;
  9. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //时钟使能
  10. TIM_TimeBaseStructure.TIM_Period = arr; //自动重装载周期值
  11. TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频值
  12. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
  13. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  14. TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx
  15. TIM_ITConfig( TIM3,TIM_IT_Update | TIM_IT_Trigger, ENABLE );//使能更新触发TIM中断
  16. //中断分组设置
  17. NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
  18. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级
  19. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
  20. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
  21. NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
  22. TIM_Cmd(TIM3, ENABLE); //使能TIMx外设
  23. }
  24. //中断服务程序
  25. void TIM3_IRQHandler(void)
  26. {
  27. if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
  28. {
  29. //代码应用区
  30. TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
  31. }
  32. }

 

TIM4初始化.h头文件

  1. #ifndef __TIMER4_H
  2. #define __TIMER4_H
  3. #include "stm32f10x.h"
  4. void TIM4_Init(unsigned int arr,unsigned int psc);
  5. #endif

TIM4初始化.c源文件

  1. #include "sys.h"
  2. #include "timer4.h"
  3. //arr:自动重装值
  4. //psc:时钟预分频数
  5. void TIM4_Init(unsigned int arr, unsigned int psc)
  6. {
  7. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  8. NVIC_InitTypeDef NVIC_InitStructure;
  9. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //时钟使能
  10. TIM_TimeBaseStructure.TIM_Period = arr; //自动重装载周期值
  11. TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频值
  12. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
  13. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  14. TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx
  15. TIM_ITConfig( TIM4,TIM_IT_Update | TIM_IT_Trigger, ENABLE );//使能更新触发TIM中断
  16. //中断分组设置
  17. NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM4中断
  18. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级
  19. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //从优先级3级
  20. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
  21. NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
  22. TIM_Cmd(TIM4, ENABLE); //使能TIMx外设
  23. }
  24. //定时器4中断服务程序
  25. void TIM4_IRQHandler(void)
  26. {
  27. //判断TIM4更新中断是否发生
  28. if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
  29. {
  30. //代码应用区
  31. TIM_ClearITPendingBit(TIM4, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
  32. }
  33. }

 

TIM5初始化.h头文件

  1. #ifndef __TIMER5_H
  2. #define __TIMER5_H
  3. #include "stm32f10x.h"
  4. void TIM5_Init(unsigned int arr,unsigned int psc);
  5. #endif

TIM5初始化.c源文件

  1. #include "sys.h"
  2. #include "timer5.h"
  3. //arr:自动重装值
  4. //psc:时钟预分频数
  5. void TIM5_Init(unsigned int arr, unsigned int psc)
  6. {
  7. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  8. NVIC_InitTypeDef NVIC_InitStructure;
  9. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //时钟使能
  10. TIM_ClearITPendingBit(TIM5,TIM_IT_Update);//清除中断标志位
  11. TIM_TimeBaseStructure.TIM_Period = arr; //自动重装载周期值
  12. TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频值
  13. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
  14. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  15. TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx
  16. TIM_ITConfig( TIM5,TIM_IT_Update | TIM_IT_Trigger, ENABLE );//使能更新触发TIM中断
  17. //中断分组设置
  18. NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; //TIM5中断
  19. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级
  20. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
  21. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
  22. NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
  23. TIM_Cmd(TIM5, ENABLE); //使能TIMx外设
  24. }
  25. //定时器5中断服务程序
  26. void TIM5_IRQHandler(void)
  27. {
  28. //判断TIM5更新中断是否发生
  29. if(TIM_GetITStatus(TIM5,TIM_IT_Update)!=RESET)
  30. {
  31. //代码应用区
  32. TIM_ClearITPendingBit(TIM5,TIM_IT_Update);
  33. }
  34. }

 

 

低级定时器

TIM6初始化.h头文件

  1. #ifndef __TIMER6_H
  2. #define __TIMER6_H
  3. #include "stm32f10x.h"
  4. void TIM6_Init(unsigned int arr,unsigned int psc);
  5. #endif

TIM6初始化.c源文件

  1. #include "sys.h"
  2. #include "timer6.h"
  3. //arr:自动重装值
  4. //psc:时钟预分频数
  5. void TIM6_Init(unsigned int arr, unsigned int psc)
  6. {
  7. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  8. NVIC_InitTypeDef NVIC_InitStructure;
  9. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); //时钟使能
  10. TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 计数到5000为500ms
  11. TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 10Khz的计数频率
  12. TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
  13. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  14. TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
  15. TIM_ITConfig( TIM6,TIM_IT_Update|TIM_IT_Trigger,ENABLE);//使能定时器6更新触发中断
  16. NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn; //TIM3中断
  17. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
  18. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级3级
  19. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
  20. NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
  21. TIM_Cmd(TIM6, ENABLE); //使能TIMx外设
  22. }
  23. //定时器6中断服务程序
  24. void TIM6_IRQHandler(void)
  25. {
  26. if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
  27. {
  28. TIM_ClearITPendingBit(TIM6, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
  29. }
  30. }

 

TIM7初始化.h头文件

  1. #ifndef __TIMER7_H
  2. #define __TIMER7_H
  3. #include "stm32f10x.h"
  4. void TIM7_Init(unsigned int arr,unsigned int psc);
  5. #endif

TIM7初始化.c源文件

  1. #include "sys.h"
  2. #include "timer7.h"
  3. //arr:自动重装值
  4. //psc:时钟预分频数
  5. void TIM7_Init(unsigned int arr, unsigned int psc)
  6. {
  7. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  8. NVIC_InitTypeDef NVIC_InitStructure;
  9. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE); //时钟使能
  10. //定时器TIM7初始化
  11. TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
  12. TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
  13. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
  14. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
  15. TIM_TimeBaseInit(TIM7, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
  16. TIM_ITConfig(TIM7,TIM_IT_Update,ENABLE ); //使能指定的TIMx中断,允许更新中断
  17. //中断优先级NVIC设置
  18. NVIC_InitStructure.NVIC_IRQChannel = TIM7_IRQn; //TIMx中断
  19. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级
  20. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级3级
  21. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
  22. NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
  23. TIM_Cmd(TIM7, ENABLE); //使能TIMx
  24. }
  25. //定时器7中断服务程序
  26. void TIM7_IRQHandler(void)
  27. {
  28. if (TIM_GetITStatus(TIM7, TIM_IT_Update) != RESET) //检查TIM7更新中断发生与否
  29. {
  30. TIM_ClearITPendingBit(TIM7, TIM_IT_Update ); //清除TIMx更新中断标志
  31. }
  32. }

 

总结:至此STM32FI03系列的8个定时器的初始化程序代码干货已经分享完毕,如果觉得还不错,请给博主点个赞哦!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/654978
推荐阅读
相关标签
  

闽ICP备14008679号