25_R1_chassis/User/bsp/bsp_delay.c
2025-05-25 20:10:14 +08:00

81 lines
1.5 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
HAL_delayÄÑÓã¬Ô¤ÁôÁ˱àдµÄdelay
*/
//111111
#include "bsp_delay.h"
#include "main.h"
static uint8_t fac_us = 0;
static uint32_t fac_ms = 0;
void delay_init(void)
{
fac_us = SystemCoreClock / 1000000;
fac_ms = SystemCoreClock / 1000;
}
void delay_us(uint16_t nus)
{
uint32_t ticks = 0;
uint32_t told = 0;
uint32_t tnow = 0;
uint32_t tcnt = 0;
uint32_t reload = 0;
reload = SysTick->LOAD;
ticks = nus * fac_us;
told = SysTick->VAL;
while (1)
{
tnow = SysTick->VAL;
if (tnow != told)
{
if (tnow < told)
{
tcnt += told - tnow;
}
else
{
tcnt += reload - tnow + told;
}
told = tnow;
if (tcnt >= ticks)
{
break;
}
}
}
}
void delay_ms(uint16_t nms)
{
uint32_t ticks = 0;
uint32_t told = 0;
uint32_t tnow = 0;
uint32_t tcnt = 0;
uint32_t reload = 0;
reload = SysTick->LOAD;
ticks = nms * fac_ms;
told = SysTick->VAL;
while (1)
{
tnow = SysTick->VAL;
if (tnow != told)
{
if (tnow < told)
{
tcnt += told - tnow;
}
else
{
tcnt += reload - tnow + told;
}
told = tnow;
if (tcnt >= ticks)
{
break;
}
}
}
}