2us延时
void sys_udelay(uint32_t us)
{
volatile int i;
uint32_t cnt;
cnt = SystemCoreClock / 1000000;
cnt = (cnt > 0) ? (cnt) : (1);
cnt = us / 1000 / cnt / 4;
for (i = 0; i < cnt; ++i)
__NOP();
}
us延时
1.滴答定时器 us延时, 1us为3us左右
足够用了
void delay_us(uint32_t nus)
{
int32_t temp;
SysTick->LOAD = 9 * nus;
SysTick->VAL = 0X00;
SysTick->CTRL = 0X01;
do
{
temp = SysTick->CTRL;
}
while((temp&0x01) && (!(temp&(1<<16))));
SysTick->CTRL = 0x00;
SysTick->VAL = 0X00;
}
2.定时器us延时
uint16_t differ = 0xffff-nus-5;
__HAL_TIM_SET_COUNTER(&htim1, differ);
HAL_TIM_Base_Start(&htim1);
while(differ < 0xffff - 6)
{
differ = __HAL_TIM_GET_COUNTER(&htim1);
}
HAL_TIM_Base_Stop(&htim1);
|