STM32Cube_FW_F1_V1.0.0固件库学习(五) Systick
看了很久,总觉得不太对。。。原来新的库已经给你写好了。。。
直接调用HAL_Delay(time)就好
ST固件库给的配置顺序:
(+) The HAL_SYSTICK_Config()function calls the SysTick_Config() function which is a CMSIS function that: (++) Configures the SysTick Reload register with value passed as function parameter. (++) Configures the SysTick IRQ priority to the lowest value (0x0F). (++) Resets the SysTick Counter register. (++) Configures the SysTick Counter clock source to be Core Clock Source (HCLK). (++) Enables the SysTick Interrupt. (++) Starts the SysTick Counter.
简单来说:
1.配置SYSTICK计数值
2.配置SYSTICK IRQ优先级为最低
3.复位SYSTICK计数器
4.配置SYSTICK计数时钟源
5.使能SYSTICK中断
6.启动SYSTICK计数器
那ST是怎么帮我们完成的呢?
看main函数:
在HAL_Init中,已经通过HAL_InitTick(SYSTICK_IRQ_Priority)完成了我们上面的前两步
HAL_StatusTypeDef HAL_Init(void) { /* Configure Flash prefetch */ #if (PREFETCH_ENABLE != 0) #if defined(STM32F101x6) || defined(STM32F101xB) || defined(STM32F101xE) || defined(STM32F101xG) || \ defined(STM32F102x6) || defined(STM32F102xB) || \ defined(STM32F103x6) || defined(STM32F103xB) || defined(STM32F103xE) || defined(STM32F103xG) || \ defined(STM32F105xC) || defined(STM32F107xC) /* Prefetch buffer is not available on value line devices */ __HAL_FLASH_PREFETCH_BUFFER_ENABLE(); #endif #endif /* PREFETCH_ENABLE */ /* Set Interrupt Group Priority */ HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4); //为后面配置STYTICK IRQ做准备 先配置中断组号 全是主中断 /* Use systick as time base source and configure 1ms tick (default clock after Reset is MSI) */ HAL_InitTick(TICK_INT_PRIORITY); /* Init the low level hardware */ HAL_MspInit(); //用户定义 函数内未添加内容 /* Return function status */ return HAL_OK; }
__weak HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) { /*Configure the SysTick to have interrupt in 1ms time basis*/ HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); //第一步 配置systick最小时钟片段 即SYSTICK的计数周期 1ms = 获取当前的系统时钟/1000 /*Configure the SysTick IRQ priority */ HAL_NVIC_SetPriority(SysTick_IRQn, TickPriority ,0); //第二步 配置SYSTICK优先级 /* Return function status */ return HAL_OK; }
那完成这两步剩下的在哪里,看上面的main函数,就只有在SystemClock_Config()里面了
/** System Clock Configuration */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = 16; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; RCC_OscInitStruct.PLL2.PLL2State = RCC_PLL_NONE; HAL_RCC_OscConfig(&RCC_OscInitStruct); RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK; /*!< SYSCLK to configure */ RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; /*!< HSI selected as system clock */ RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; /*!< SYSCLK not divided */ RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; /*!< SYSCLK not divided */ RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; /*!< SYSCLK not divided */ HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0); }
在HAL_RCC_OscConfig()中,如下图红框处位置,就将系统计数器直接清0,然后在后面启用。开始刚上电的时候,应该是用的默认时钟源,