STM32L151系列低功耗模式:Stop模式、Stop+RTC模式、Sleep模式
STM32L系列有五種低功耗模式,分別是standby,stop,sleep,low power sleep,low power run.
其中standby模式下功耗最低,實測電流為1uA,此種模式下數據不保存。
stop模式實測功耗為1.3uA,此種模式下數據保存。
sleep模式下功耗為1mA左右,主要受頻率,溫度等影響,功耗發生變化。
此代碼使用RTC喚醒,使用其他方式喚醒方式相對簡單,可以參考st官方例程。
單片機源程序如下:
- /*
- *********************************************************
- ** Filename: stop_mode.c
- ** Abstract: 使用STM32L151C8T6MCU,使用RTC喚醒STOP和STANDBY模式下的低功耗,低功耗時長可以人為的進行設置
- ** 設置低功耗時長時請參考頭文件中關于時長的宏定義
- ** 使用注意事項:使用CubeMX生成函數時,在main()函數后會自動生成SystemClock_Config()函數,此程序中調用了該函數。
- ** 如果該函數在其他文件中,請將該.h文件加入,以免發生錯誤;
- ** Date : 2018-01-04
- ** Author:王翔武
- *********************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "pwr_mode_rtc.h"
- #include "main.h"
- RTC_HandleTypeDef RTCHandle; //RTC結構體變量
- //進入STOP模式低功耗,使用RTC功能喚醒,其中stoptime單位為S,如設置1,低功耗1秒后喚醒
- void enter_stop_rtc(float stoptime)
- {
- uint32_t i; //局部變量,用于計算低功耗時長
- system_power_config();
-
- /* Disable Wakeup Counter */
- HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
-
- /*To configure the wake up timer to 4s the WakeUpCounter is set to 0x242B:
- RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16
- Wakeup Time Base = 16 /(~37KHz) = ~0,432 ms
- Wakeup Time = ~5s = 0,432ms * WakeUpCounter
- ==> WakeUpCounter = ~5s/0,432ms = 11562 */
- i = stoptime*2396;
- HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, i, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
-
- /* Enter Stop Mode */
- HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
- SystemClock_Config();
- }
- //進入STANDBY模式低功耗,使用RTC功能喚醒,其中standbytime單位為S,如設置1,低功耗1秒后喚醒
- void enter_standby_rtc(float standbytime)
- {
- uint32_t i; //局部變量,用于計算低功耗時長
- system_power_config();
-
- if(__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET)
- {
- /* Clear Standby flag */
- __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
- }
-
- /* Disable Wakeup Counter */
- HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
-
- /*To configure the wake up timer to 4s the WakeUpCounter is set to 0x242B:
- RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16
- Wakeup Time Base = 16 /(~37KHz) = ~0,432 ms
- Wakeup Time = ~5s = 0,432ms * WakeUpCounter
- ==> WakeUpCounter = ~5s/0,432ms = 11562 */
- i = standbytime*2396;
- HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, i, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
-
- /* Clear all related wakeup flags */
- __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
- /* Enter the Standby mode */
- HAL_PWR_EnterSTANDBYMode();
- }
- //進入SLEEP模式低功耗,使用RTC功能喚醒,其中sleeptime單位為S,如設置1,低功耗1秒后喚醒
- void enter_sleep_rtc(float sleeptime)
- {
- uint32_t i; //局部變量,用于計算低功耗時長
-
- system_power_config();
-
- /* Disable Wakeup Counter */
- HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
-
- /*To configure the wake up timer to 4s the WakeUpCounter is set to 0x242B:
- RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16
- Wakeup Time Base = 16 /(~37KHz) = ~0,432 ms
- Wakeup Time = ~5s = 0,432ms * WakeUpCounter
- ==> WakeUpCounter = ~5s/0,432ms = 11562 */
- i = sleeptime*2396;
- HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, i, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
-
- /*Suspend Tick increment to prevent wakeup by Systick interrupt.
- Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base)*/
- HAL_SuspendTick();
- /* Enter Sleep Mode , wake up is done once Key push button is pressed */
- HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
- /* Resume Tick interrupt if disabled prior to sleep mode entry*/
- HAL_ResumeTick();
- }
- //低功耗關閉項
- void system_power_config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure = {0};
- /* Enable Power Control clock */
- __HAL_RCC_PWR_CLK_ENABLE();
- /* Enable Ultra low power mode */
- HAL_PWREx_EnableUltraLowPower();
- /* Enable the fast wake up from Ultra low power mode */
- HAL_PWREx_EnableFastWakeUp();
- /* Enable GPIOs clock */
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOD_CLK_ENABLE();
- __HAL_RCC_GPIOH_CLK_ENABLE();
- __HAL_RCC_GPIOE_CLK_ENABLE();
- /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */
- /* Note: Debug using ST-Link is not possible during the execution of this */
- /* example because communication between ST-link and the device */
- /* under test is done through UART. All GPIO pins are disabled (set */
- /* to analog input mode) including UART I/O pins. */
- GPIO_InitStructure.Pin = GPIO_PIN_All;
- GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
- GPIO_InitStructure.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
- HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
- HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
- HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
- HAL_GPIO_Init(GPIOH, &GPIO_InitStructure);
- HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
- /* Disable GPIOs clock */
- __HAL_RCC_GPIOA_CLK_DISABLE();
- __HAL_RCC_GPIOB_CLK_DISABLE();
- __HAL_RCC_GPIOC_CLK_DISABLE();
- __HAL_RCC_GPIOD_CLK_DISABLE();
- __HAL_RCC_GPIOH_CLK_DISABLE();
- __HAL_RCC_GPIOE_CLK_DISABLE();
- /* Configure RTC */
- RTCHandle.Instance = RTC;
- /* Configure RTC prescaler and RTC data registers as follow:
- - Hour Format = Format 24
- - Asynch Prediv = Value according to source clock
- - Synch Prediv = Value according to source clock
- - OutPut = Output Disable
- - OutPutPolarity = High Polarity
- - OutPutType = Open Drain */
- RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
- RTCHandle.Init.AsynchPrediv = 0x7C;
- RTCHandle.Init.SynchPrediv = 0x0127;
- RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
- RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
- RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
- if(HAL_RTC_Init(&RTCHandle) != HAL_OK)
- {
- /* Initialization Error */
- Error_Handler();
- }
- }
- /************************ Johnking *****END OF FILE****/
復制代碼
0.png (5.69 KB, 下載次數: 74)
下載附件
2019-9-23 15:42 上傳
所有資料51hei提供下載:
STML151系列低功耗模式代碼.rar
(2.94 KB, 下載次數: 228)
2019-9-23 09:04 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|