delay.c 943 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include "delay.h"
  2. static volatile uint32_t usTicks = 0;
  3. vu32 sysTickUptime;
  4. void cycleCounterInit(void)
  5. {
  6. RCC_ClocksTypeDef clocks;
  7. RCC_GetClocksFreq(&clocks);
  8. usTicks = clocks.SYSCLK_Frequency / 1000000;
  9. SysTick_Config(clocks.HCLK_Frequency / 1000); //SysTick开启系统tick定时器并初始化其中断,1m
  10. }
  11. uint32_t Micros(void)//得到总的时间。
  12. {
  13. register uint32_t ms, cycle_cnt, usTicks_t;
  14. do {
  15. ms = sysTickUptime;
  16. cycle_cnt = SysTick->VAL;
  17. }while (ms != sysTickUptime);
  18. usTicks_t = usTicks;
  19. return (ms * 1000) + (usTicks_t * 1000 - cycle_cnt) / usTicks_t;
  20. }
  21. void delay_us(u32 nus)//微秒延时
  22. {
  23. uint32_t t0 = Micros();
  24. while(Micros() - t0 < nus);
  25. }
  26. void delay_ms(u16 nms)//毫秒延时
  27. {
  28. uint32_t t0 = Micros();
  29. while(Micros() - t0 < nms * 1000);
  30. }
  31. uint32_t millis(void)// Return system uptime in milliseconds (rollover in 49 days)
  32. {
  33. return sysTickUptime;
  34. }