1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #include "delay.h"
- static volatile uint32_t usTicks = 0;
- vu32 sysTickUptime;
- void cycleCounterInit(void)
- {
- RCC_ClocksTypeDef clocks;
- RCC_GetClocksFreq(&clocks);
- usTicks = clocks.SYSCLK_Frequency / 1000000;
- SysTick_Config(clocks.HCLK_Frequency / 1000); //SysTick开启系统tick定时器并初始化其中断,1m
- }
- uint32_t Micros(void)//得到总的时间。
- {
- register uint32_t ms, cycle_cnt, usTicks_t;
-
- do {
- ms = sysTickUptime;
- cycle_cnt = SysTick->VAL;
- }while (ms != sysTickUptime);
- usTicks_t = usTicks;
-
- return (ms * 1000) + (usTicks_t * 1000 - cycle_cnt) / usTicks_t;
- }
- void delay_us(u32 nus)//微秒延时
- {
- uint32_t t0 = Micros();
- while(Micros() - t0 < nus);
- }
- void delay_ms(u16 nms)//毫秒延时
- {
- uint32_t t0 = Micros();
- while(Micros() - t0 < nms * 1000);
- }
- uint32_t millis(void)// Return system uptime in milliseconds (rollover in 49 days)
- {
- return sysTickUptime;
- }
|