timer.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "timer.h"
  2. volatile uint32_t delay_counter = 0; // 延时计数器
  3. void timer5_init(){
  4. rcu_periph_clock_enable(RCU_TIMER5); // 使能Timer5时钟
  5. timer_parameter_struct timer_init_struct;
  6. timer_deinit(TIMER5); // 复位Timer5配置
  7. timer_struct_para_init(&timer_init_struct);
  8. timer_init_struct.prescaler = 119; // 定时器时钟为120MHz
  9. timer_init_struct.alignedmode = TIMER_COUNTER_EDGE; // 边沿对齐模式
  10. timer_init_struct.counterdirection = TIMER_COUNTER_UP; // 向上计数
  11. timer_init_struct.period = 999; // 计数器周期为1000,即10kHz下1秒钟
  12. timer_init_struct.clockdivision = TIMER_CKDIV_DIV1; // 时钟分频1
  13. timer_init_struct.repetitioncounter = 0; // 重复计数器值
  14. timer_init(TIMER5, &timer_init_struct);
  15. timer_interrupt_enable(TIMER5, TIMER_INT_UP); // 使能Timer5更新中断
  16. nvic_irq_enable(TIMER5_IRQn, 0, 0); // 使能Timer5中断,并设置优先级为0
  17. timer_enable(TIMER5); // 启动Timer5
  18. }
  19. //启动定时器
  20. void timer5_start(void){
  21. timer_enable(TIMER5);
  22. }
  23. //关闭定时器
  24. void timer5_stop(void){
  25. timer_disable(TIMER5);
  26. }
  27. void TIMER5_IRQHandler(void)
  28. {
  29. if (timer_interrupt_flag_get(TIMER5, TIMER_INT_UP) != RESET)
  30. {
  31. timer_interrupt_flag_clear(TIMER5, TIMER_INT_UP); // 清除中断标志
  32. delay_counter++; // 延时计数器加1
  33. }
  34. }