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****/
复制代码
所有资料51hei提供下载:
STML151系列低功耗模式代码.rar
(2.94 KB, 下载次数: 228)
|