timer.c 548 B

123456789101112131415161718192021222324252627
  1. #include "timer.h"
  2. TIM_HandleTypeDef htim7;
  3. void TIMER_config()
  4. {
  5. __HAL_RCC_TIM7_CLK_ENABLE();
  6. htim7.Instance = TIM7;
  7. htim7.Init.Prescaler = 59999;
  8. htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
  9. htim7.Init.Period = 5999;
  10. HAL_TIM_Base_Init(&htim7);
  11. __HAL_TIM_ENABLE(&htim7);
  12. }
  13. void delay_ms(uint32_t ms)
  14. {
  15. uint32_t start_time = __HAL_TIM_GET_COUNTER(&htim7); // 获取当前定时器计数值
  16. while ((TIM7->CNT - start_time) < ms); // 等待经过指定的延时时间
  17. }
  18. void TIM7_IRQHandler(void)
  19. {
  20. HAL_TIM_IRQHandler(&htim7);
  21. }