標(biāo)題: STM32L151系列低功耗模式編程 [打印本頁(yè)]

作者: 蝸牛漫步    時(shí)間: 2019-9-23 09:10
標(biāo)題: STM32L151系列低功耗模式編程
STM32L151系列低功耗模式:Stop模式、Stop+RTC模式、Sleep模式
STM32L系列有五種低功耗模式,分別是standby,stop,sleep,low power sleep,low power run.
其中standby模式下功耗最低,實(shí)測(cè)電流為1uA,此種模式下數(shù)據(jù)不保存。
stop模式實(shí)測(cè)功耗為1.3uA,此種模式下數(shù)據(jù)保存。
sleep模式下功耗為1mA左右,主要受頻率,溫度等影響,功耗發(fā)生變化。


此代碼使用RTC喚醒,使用其他方式喚醒方式相對(duì)簡(jiǎn)單,可以參考st官方例程。

單片機(jī)源程序如下:
  1. /*
  2. *********************************************************
  3. ** Filename: stop_mode.c
  4. ** Abstract: 使用STM32L151C8T6MCU,使用RTC喚醒STOP和STANDBY模式下的低功耗,低功耗時(shí)長(zhǎng)可以人為的進(jìn)行設(shè)置
  5. **                                設(shè)置低功耗時(shí)長(zhǎng)時(shí)請(qǐng)參考頭文件中關(guān)于時(shí)長(zhǎng)的宏定義
  6. ** 使用注意事項(xiàng):使用CubeMX生成函數(shù)時(shí),在main()函數(shù)后會(huì)自動(dòng)生成SystemClock_Config()函數(shù),此程序中調(diào)用了該函數(shù)。
  7. **                                如果該函數(shù)在其他文件中,請(qǐng)將該.h文件加入,以免發(fā)生錯(cuò)誤;
  8. ** Date : 2018-01-04
  9. ** Author:王翔武
  10. *********************************************************
  11. */

  12. /* Includes ------------------------------------------------------------------*/
  13. #include "pwr_mode_rtc.h"
  14. #include "main.h"

  15. RTC_HandleTypeDef RTCHandle;        //RTC結(jié)構(gòu)體變量       

  16. //進(jìn)入STOP模式低功耗,使用RTC功能喚醒,其中stoptime單位為S,如設(shè)置1,低功耗1秒后喚醒
  17. void enter_stop_rtc(float stoptime)
  18. {
  19.         uint32_t i;                //局部變量,用于計(jì)算低功耗時(shí)長(zhǎng)
  20.         system_power_config();
  21.        
  22.     /* Disable Wakeup Counter */
  23.     HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
  24.        
  25.         /*To configure the wake up timer to 4s the WakeUpCounter is set to 0x242B:
  26.         RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16
  27.         Wakeup Time Base = 16 /(~37KHz) = ~0,432 ms
  28.         Wakeup Time = ~5s = 0,432ms  * WakeUpCounter
  29.         ==> WakeUpCounter = ~5s/0,432ms = 11562 */
  30.         i = stoptime*2396;
  31.     HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, i, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
  32.    
  33.     /* Enter Stop Mode */
  34.     HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
  35.         SystemClock_Config();
  36. }

  37. //進(jìn)入STANDBY模式低功耗,使用RTC功能喚醒,其中standbytime單位為S,如設(shè)置1,低功耗1秒后喚醒
  38. void enter_standby_rtc(float standbytime)
  39. {
  40.         uint32_t i;                //局部變量,用于計(jì)算低功耗時(shí)長(zhǎng)
  41.         system_power_config();
  42.        
  43.         if(__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET)
  44.         {
  45.                 /* Clear Standby flag */
  46.                 __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
  47.         }
  48.   
  49.     /* Disable Wakeup Counter */
  50.     HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
  51.        
  52.         /*To configure the wake up timer to 4s the WakeUpCounter is set to 0x242B:
  53.         RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16
  54.         Wakeup Time Base = 16 /(~37KHz) = ~0,432 ms
  55.         Wakeup Time = ~5s = 0,432ms  * WakeUpCounter
  56.         ==> WakeUpCounter = ~5s/0,432ms = 11562 */
  57.         i = standbytime*2396;
  58.     HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, i, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
  59.    
  60.         /* Clear all related wakeup flags */
  61.         __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);

  62.          /* Enter the Standby mode */
  63.         HAL_PWR_EnterSTANDBYMode();
  64. }

  65. //進(jìn)入SLEEP模式低功耗,使用RTC功能喚醒,其中sleeptime單位為S,如設(shè)置1,低功耗1秒后喚醒
  66. void enter_sleep_rtc(float sleeptime)
  67. {
  68.         uint32_t i;                //局部變量,用于計(jì)算低功耗時(shí)長(zhǎng)
  69.        

  70.         system_power_config();
  71.        
  72.     /* Disable Wakeup Counter */
  73.     HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
  74.        
  75.         /*To configure the wake up timer to 4s the WakeUpCounter is set to 0x242B:
  76.         RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16
  77.         Wakeup Time Base = 16 /(~37KHz) = ~0,432 ms
  78.         Wakeup Time = ~5s = 0,432ms  * WakeUpCounter
  79.         ==> WakeUpCounter = ~5s/0,432ms = 11562 */
  80.         i = sleeptime*2396;
  81.     HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, i, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
  82.    
  83.     /*Suspend Tick increment to prevent wakeup by Systick interrupt.
  84.     Otherwise the Systick interrupt will wake up the device within 1ms (HAL time base)*/
  85.     HAL_SuspendTick();

  86.     /* Enter Sleep Mode , wake up is done once Key push button is pressed */
  87.     HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);

  88.     /* Resume Tick interrupt if disabled prior to sleep mode entry*/
  89.     HAL_ResumeTick();
  90. }

  91. //低功耗關(guān)閉項(xiàng)
  92. void system_power_config(void)
  93. {
  94.         GPIO_InitTypeDef GPIO_InitStructure = {0};

  95.         /* Enable Power Control clock */
  96.         __HAL_RCC_PWR_CLK_ENABLE();

  97.         /* Enable Ultra low power mode */
  98.         HAL_PWREx_EnableUltraLowPower();

  99.         /* Enable the fast wake up from Ultra low power mode */
  100.         HAL_PWREx_EnableFastWakeUp();

  101.         /* Enable GPIOs clock */
  102.         __HAL_RCC_GPIOA_CLK_ENABLE();
  103.         __HAL_RCC_GPIOB_CLK_ENABLE();
  104.         __HAL_RCC_GPIOC_CLK_ENABLE();
  105.         __HAL_RCC_GPIOD_CLK_ENABLE();
  106.         __HAL_RCC_GPIOH_CLK_ENABLE();
  107.         __HAL_RCC_GPIOE_CLK_ENABLE();

  108.         /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */
  109.         /* Note: Debug using ST-Link is not possible during the execution of this   */
  110.         /*       example because communication between ST-link and the device       */
  111.         /*       under test is done through UART. All GPIO pins are disabled (set   */
  112.         /*       to analog input mode) including  UART I/O pins.           */
  113.         GPIO_InitStructure.Pin = GPIO_PIN_All;
  114.         GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
  115.         GPIO_InitStructure.Pull = GPIO_NOPULL;

  116.         HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
  117.         HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
  118.         HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
  119.         HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
  120.         HAL_GPIO_Init(GPIOH, &GPIO_InitStructure);
  121.         HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);

  122.         /* Disable GPIOs clock */
  123.         __HAL_RCC_GPIOA_CLK_DISABLE();
  124.         __HAL_RCC_GPIOB_CLK_DISABLE();
  125.         __HAL_RCC_GPIOC_CLK_DISABLE();
  126.         __HAL_RCC_GPIOD_CLK_DISABLE();
  127.         __HAL_RCC_GPIOH_CLK_DISABLE();
  128.         __HAL_RCC_GPIOE_CLK_DISABLE();

  129.         /* Configure RTC */
  130.         RTCHandle.Instance = RTC;
  131.         /* Configure RTC prescaler and RTC data registers as follow:
  132.         - Hour Format = Format 24
  133.         - Asynch Prediv = Value according to source clock
  134.         - Synch Prediv = Value according to source clock
  135.         - OutPut = Output Disable
  136.         - OutPutPolarity = High Polarity
  137.         - OutPutType = Open Drain */
  138.         RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
  139.         RTCHandle.Init.AsynchPrediv = 0x7C;
  140.         RTCHandle.Init.SynchPrediv = 0x0127;
  141.         RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
  142.         RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  143.         RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  144.         if(HAL_RTC_Init(&RTCHandle) != HAL_OK)
  145.         {
  146.                 /* Initialization Error */
  147.                 Error_Handler();
  148.         }
  149. }


  150. /************************ Johnking *****END OF FILE****/
復(fù)制代碼

所有資料51hei提供下載:
STML151系列低功耗模式代碼.rar (2.94 KB, 下載次數(shù): 228)


作者: 一個(gè)菜鳥(niǎo)渣渣    時(shí)間: 2019-12-4 10:06
說(shuō)的很詳細(xì),感謝。先拿來(lái)試試!
作者: xmkg    時(shí)間: 2020-6-12 14:57
正在用這個(gè)芯片,學(xué)習(xí)下,感謝
作者: chenhejia    時(shí)間: 2023-2-19 12:42
下載不了了,不知道什么原因




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1