看了很久,總覺得不太對。。。原來新的庫已經給你寫好了。。。
直接調用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,然后在后面啟用。開始剛上電的時候,應該是用的默認時鍾源,