timer.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "timer.h"
  2. void timer_nvic_config(void)
  3. {
  4. NVIC_InitTypeDef NVIC_InitStructure;
  5. // Usart1 NVIC 配置
  6. NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  7. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  8. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  9. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  10. NVIC_Init(&NVIC_InitStructure);
  11. }
  12. void timer_config(void)
  13. {
  14. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
  15. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); ///使能TIM2时钟
  16. /* TIMER1 configuration */
  17. TIM_TimeBaseInitStructure.TIM_Period = 16800; // 239;
  18. TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
  19. TIM_TimeBaseInitStructure.TIM_Prescaler = 10000; // 7999;
  20. TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  21. TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure); //初始化TIM2
  22. TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //允许定时器2更新中断
  23. TIM_Cmd(TIM3, ENABLE); //使能定时器3
  24. }
  25. void user_timer_init(void)
  26. {
  27. timer_nvic_config();
  28. timer_config();
  29. }
  30. void TIM3_IRQHandler(void)
  31. {
  32. // static uint16_t cnt = 0;
  33. // if (cnt >= 100)
  34. // {
  35. // IWDG_ReloadCounter();
  36. // cnt = 0;
  37. // }
  38. // else
  39. // {
  40. // cnt++;
  41. // }
  42. if (TIM_GetITStatus(TIM3, TIM_IT_Update))
  43. {
  44. IWDG_ReloadCounter();
  45. }
  46. TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  47. }