其他分享
首页 > 其他分享> > STM32 LL库延时函数 LL_mDelay解析

STM32 LL库延时函数 LL_mDelay解析

作者:互联网

注意看上面的CTRL寄存器的CLKSOURCE位(时钟源位),它有两个选择,一个是内核时钟源FLCK(72MHz),一个是外部时钟源HCLK,这里应该是

我们知道,我们设定一个计数,那么每次计数器减到0,时间经过了:系统时钟周期 *计数器初值.我们使用72M作为系统时钟,那么每次计数器减1所用的时间是1/72M,计数器的初值如果是72000,那么每次计数器减到0,时间经过(1/72M)*72000= 0.001,(简单理解:用72M的时钟频率,即1s计数72M=72000000次,那1ms计数72000次,所以计数值为72000),

让我们来看看这些寄存器的配置:

系统库core_cm3.h中

STATIC_INLINE void LL_InitTick(uint32_t HCLKFrequency, uint32_t Ticks)

{

/* Configure the SysTick to have interrupt in 1ms time base */

SysTick->LOAD = (uint32_t)((HCLKFrequency / Ticks) - 1UL); /* set reload register */

SysTick->VAL = 0UL; /* Load the SysTick Counter Value */

SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |

SysTick_CTRL_ENABLE_Msk; /* Enable the Systick Timer */

}

再看看

/* SysTick Control / Status Register Definitions */

#define SysTick_CTRL_COUNTFLAG_Pos 16 /*!< SysTick CTRL: COUNTFLAG Position */

#define SysTick_CTRL_COUNTFLAG_Msk (1ul << SysTick_CTRL_COUNTFLAG_Pos) /*!< SysTick CTRL: COUNTFLAG Mask */



#define SysTick_CTRL_CLKSOURCE_Pos 2 /*!< SysTick CTRL: CLKSOURCE Position */

#define SysTick_CTRL_CLKSOURCE_Msk (1ul << SysTick_CTRL_CLKSOURCE_Pos) /*!< SysTick CTRL: CLKSOURCE Mask */



#define SysTick_CTRL_TICKINT_Pos 1 /*!< SysTick CTRL: TICKINT Position */

#define SysTick_CTRL_TICKINT_Msk (1ul << SysTick_CTRL_TICKINT_Pos) /*!< SysTick CTRL: TICKINT Mask */



#define SysTick_CTRL_ENABLE_Pos 0 /*!< SysTick CTRL: ENABLE Position */

#define SysTick_CTRL_ENABLE_Msk (1ul << SysTick_CTRL_ENABLE_Pos) /*!< SysTick CTRL: ENABLE Mask */



/* SysTick Reload Register Definitions */

#define SysTick_LOAD_RELOAD_Pos 0 /*!< SysTick LOAD: RELOAD Position */

#define SysTick_LOAD_RELOAD_Msk (0xFFFFFFul << SysTick_LOAD_RELOAD_Pos) /*!< SysTick LOAD: RELOAD Mask */



/* SysTick Current Register Definitions */

#define SysTick_VAL_CURRENT_Pos 0 /*!< SysTick VAL: CURRENT Position */

#define SysTick_VAL_CURRENT_Msk (0xFFFFFFul << SysTick_VAL_CURRENT_Pos) /*!< SysTick VAL: CURRENT Mask */

#define SysTick_CTRL_CLKSOURCE_Pos 2 /*!< SysTick CTRL: CLKSOURCE Position */

#define SysTick_CTRL_CLKSOURCE_Msk (1ul << SysTick_CTRL_CLKSOURCE_Pos) /*!< SysTick CTRL: CLKSOURCE Mask */

ul是unsigned long 型, 这两行代码的意思是1左移2位,也就是将2这位置1,这里就是选择了内核时钟源FCLK(72MHz),所以我们要把计数初值设置为72000,这样才能

产生1ms的基准时钟哦~ 当然,如果这里将1改为0,那么就要将计数初值设为9000了~ 哟西~~

好吧~ 其它的就慢慢对着瞧吧.. 剩下的就是写例子了~ 下篇吧. 有点累了!

程序执行顺序

1、 LL_Init1msTick(72000000);

2、 LL_InitTick(HCLKFrequency, 1000U);/初始化systick定时器

3、void LL_mDelay(uint32_t Delay)

void LL_Init1msTick(uint32_t HCLKFrequency)

{

/* Use frequency provided in argument */

LL_InitTick(HCLKFrequency, 1000U);

}

初始化systick定时器跳转;

STATIC_INLINE void LL_InitTick(uint32_t HCLKFrequency, uint32_t Ticks)

{

/* Configure the SysTick to have interrupt in 1ms time base */

SysTick->LOAD = (uint32_t)((HCLKFrequency / Ticks) - 1UL); /* set reload register */

SysTick->VAL = 0UL; /* Load the SysTick Counter Value */

SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |

SysTick_CTRL_ENABLE_Msk; /* Enable the Systick Timer */

}

初始化systick定时器,并重装载为1MS(72Mhz对应1MS为72M/1000)的时间;



void LL_mDelay(uint32_t Delay)

{

__IO uint32_t tmp = SysTick->CTRL; /* Clear the COUNTFLAG first */

/* Add this code to indicate that local variable is not used */

((void)tmp);



/* Add a period to guaranty minimum wait */

if (Delay < LL_MAX_DELAY)

{

Delay++;

}

while (Delay)

{

if ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0U)

{

Delay--;

}

}

}

延时函数,判断SysTick寄存器是否数到0若数到0(SysTick_CTRL_COUNTFLAG_Msk数到0为1读取后自动置0)则Delay减1

标签:CTRL,LL,STM32,mDelay,SysTick,Msk,HCLKFrequency,uint32
来源: https://blog.csdn.net/qq_37040580/article/details/121247636