timer.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "timer.h"
  2. void timer_nvic_config(void)
  3. {
  4. NVIC_InitTypeDef NVIC_InitStructure;
  5. // Usart1 NVIC 配置
  6. NVIC_InitStructure.NVIC_IRQChannel = TIM2_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(TIM2, &TIM_TimeBaseInitStructure); //初始化TIM2
  22. TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //允许定时器2更新中断
  23. TIM_Cmd(TIM2, ENABLE); //使能定时器3
  24. }
  25. void user_timer_init(void)
  26. {
  27. timer_nvic_config();
  28. timer_config();
  29. }
  30. void TIM2_IRQHandler(void)
  31. {
  32. static INT16U cnt = 0;
  33. if (cnt >= 100)
  34. {
  35. IWDG_ReloadCounter();
  36. cnt = 0;
  37. }
  38. else
  39. {
  40. cnt++;
  41. }
  42. }