STM32常用延迟函数

#include "delay.h" // Device header

void Delay_us(uint16_t us)
{
    /* 设计定时器重装值 */
    SysTick->LOAD = 72 * us;
    /* 清除当前计数值 */
    SysTick->VAL = 0;
    /*设置内部时钟源(2位->1),不需要中断(1位->0),并启动定时器(0位->1)*/
    SysTick->CTRL = 0x5;
    /*等待计数到0, 如果计数到0则16位会置为1*/
    while (!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG));
    /* 关闭定时器 */
    SysTick->CTRL &= ~SysTick_CTRL_ENABLE; 
}

void Delay_ms(uint16_t ms)
{
    while (ms--)
    {
        Delay_us(1000);
    }
}

void Delay_s(uint16_t s)
{
    while (s--)
    {
        Delay_ms(1000);
    }
}
void Delay_us(uint16_t us)
{
    __HAL_TIM_SET_COUNTER(&htim3, 0);
    HAL_TIM_Base_Start(&htim3);
    while (__HAL_TIM_GET_COUNTER(&htim3) != us)
        ;
    HAL_TIM_Base_Stop(&htim3);
}

添加新评论