金牌会员
- 积分
- 1740
- 金钱
- 1740
- 注册时间
- 2017-6-23
- 在线时间
- 170 小时
|
1金钱
HAL库中,HAL_Delay(n)在不同系统时钟下都可以实现n毫秒延时,其底层实现在系统初始化时自动完成,使用官方cubeMX自动生成任意一个工程代码,进入main函数后,首先调用下面两个函数- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
-
- /* Configure the system clock */
- SystemClock_Config();
复制代码 在这两个函数中都有调用以下函数
- __weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
- {
- HAL_StatusTypeDef status = HAL_OK;
- /* Check uwTickFreq for MisraC 2012 (even if uwTickFreq is a enum type that doesn't take the value zero)*/
- if ((uint32_t)uwTickFreq != 0U)
- {
- /*Configure the SysTick to have interrupt in 1ms time basis*/
- if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / (uint32_t)uwTickFreq)) == 0U)
- {
- /* Configure the SysTick IRQ priority */
- if (TickPriority < (1UL << __NVIC_PRIO_BITS))
- {
- HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority, 0U);
- uwTickPrio = TickPriority;
- }
- else
- {
- status = HAL_ERROR;
- }
- }
- else
- {
- status = HAL_ERROR;
- }
- }
- else
- {
- status = HAL_ERROR;
- }
- /* Return function status */
- return status;
- }
复制代码 计算每毫秒时间内的系统时钟计数值,调用HAL_SYSTICK_Config()函数,写入寄存器,实现毫秒延时
- #if defined (__Vendor_SysTickConfig) && (__Vendor_SysTickConfig == 0U)
- /**
- \brief System Tick Configuration
- \details Initializes the System Timer and its interrupt, and starts the System Tick Timer.
- Counter is in free running mode to generate periodic interrupts.
- \param [in] ticks Number of ticks between two interrupts.
- \return 0 Function succeeded.
- \return 1 Function failed.
- \note When the variable <b>__Vendor_SysTickConfig</b> is set to 1, then the
- function <b>SysTick_Config</b> is not included. In this case, the file <b><i>device</i>.h</b>
- must contain a vendor-specific implementation of this function.
- */
- __STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks)
- {
- if ((ticks - 1UL) > SysTick_LOAD_RELOAD_Msk)
- {
- return (1UL); /* Reload value impossible */
- }
- SysTick->LOAD = (uint32_t)(ticks - 1UL); /* set reload register */
- NVIC_SetPriority (SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL); /* set Priority for Systick Interrupt */
- SysTick->VAL = 0UL; /* Load the SysTick Counter Value */
- SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
- SysTick_CTRL_TICKINT_Msk |
- SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */
- return (0UL); /* Function successful */
- }
- #endif
复制代码
问题来了,#if defined (__Vendor_SysTickConfig) && (__Vendor_SysTickConfig == 0U)表达式值为true,为啥显示结果是false的样子?
|
|