金牌会员
 
- 积分
- 1784
- 金钱
- 1784
- 注册时间
- 2014-6-27
- 在线时间
- 965 小时
|
发表于 2021-9-7 09:53:34
|
显示全部楼层
可以换一个思路写这种delay函数,再说说你这种写法,实际用也不方便,独自占用一个定时器了,给你2个函数,定时器中断和延时同时进行,延时时间可大可小,没有限制,当然,系统运行堵塞延时尽量短。
void delay_xx_us(u32 x) //time is (x+18)us,so 1us is 19us
{
u32 new_load,old_load,cmp_load=0;//,tickers;
x*=22; //22M
//x=(x<<4)+(x<<3); //tickers=x*24;
old_load=SysTick->VAL;
while(1)
{
new_load=SysTick->VAL;
if(new_load!=old_load)
{
if(new_load<old_load)
cmp_load+=old_load-new_load;
else
cmp_load+=SysTick->LOAD+old_load-new_load;
old_load=new_load;
if(cmp_load>=x) break;
}
}
}
void delay_xx_ms(u16 x)
{
delay_xx_us(x*1000ul);
} |
|