打印
[牛人杂谈]

长延时函数的巧妙实现

[复制链接]
86|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
yiyigirl2014|  楼主 | 2024-10-13 21:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
/**
  * [url=home.php?mod=space&uid=247401]@brief[/url]      This function execute long delay function.
  * @param[in]  us  Delay time.
  * [url=home.php?mod=space&uid=266161]@return[/url]     None
  * [url=home.php?mod=space&uid=1543424]@Details[/url]    Use the SysTick to generate the long delay time and the UNIT is in us.
  *             The SysTick clock source is from HCLK, i.e the same as system core clock.
  *             User can use SystemCoreClockUpdate() to calculate CyclesPerUs automatically before using this function.
  */
__STATIC_INLINE void CLK_SysTickLongDelay(uint32_t us)
{
    uint32_t delay;
        
    /* It should <= 335544us for each delay loop */
    delay = 335544UL;

    do
    {
        if(us > delay)
        {
            us -= delay;
        }
        else
        {
            delay = us;
            us = 0UL;
        }        
        
        SysTick->LOAD = delay * CyclesPerUs;
        SysTick->VAL  = (0x0UL);
        SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;

        /* Waiting for down-count to zero */
        while((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0UL);

        /* Disable SysTick counter */
        SysTick->CTRL = 0UL;
   
    }while(us > 0UL);
   
}
通过
do
{
if(us > delay)
{
us -= delay;
}
else
{
delay = us;
us = 0UL;
}


实现了任意数值的延时。当大于最大限制时候,就先执行限制,并扣掉该值,循环,直到小于最大计量。

使用特权

评论回复
沙发
chenqianqian| | 2024-10-14 08:10 | 只看该作者
这种延时方式效率太低了

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

211

主题

3527

帖子

10

粉丝