找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

帖子
查看: 9446|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

STM32L151系列低功耗模式編程

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:432182 發(fā)表于 2019-9-23 09:10 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
STM32L151系列低功耗模式:Stop模式、Stop+RTC模式、Sleep模式
STM32L系列有五種低功耗模式,分別是standby,stop,sleep,low power sleep,low power run.
其中standby模式下功耗最低,實測電流為1uA,此種模式下數(shù)據(jù)不保存。
stop模式實測功耗為1.3uA,此種模式下數(shù)據(jù)保存。
sleep模式下功耗為1mA左右,主要受頻率,溫度等影響,功耗發(fā)生變化。


此代碼使用RTC喚醒,使用其他方式喚醒方式相對簡單,可以參考st官方例程。

單片機源程序如下:
  1. /*
  2. *********************************************************
  3. ** Filename: stop_mode.c
  4. ** Abstract: 使用STM32L151C8T6MCU,使用RTC喚醒STOP和STANDBY模式下的低功耗,低功耗時長可以人為的進行設(shè)置
  5. **                                設(shè)置低功耗時長時請參考頭文件中關(guān)于時長的宏定義
  6. ** 使用注意事項:使用CubeMX生成函數(shù)時,在main()函數(shù)后會自動生成SystemClock_Config()函數(shù),此程序中調(diào)用了該函數(shù)。
  7. **                                如果該函數(shù)在其他文件中,請將該.h文件加入,以免發(fā)生錯誤;
  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. //進入STOP模式低功耗,使用RTC功能喚醒,其中stoptime單位為S,如設(shè)置1,低功耗1秒后喚醒
  17. void enter_stop_rtc(float stoptime)
  18. {
  19.         uint32_t i;                //局部變量,用于計算低功耗時長
  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. //進入STANDBY模式低功耗,使用RTC功能喚醒,其中standbytime單位為S,如設(shè)置1,低功耗1秒后喚醒
  38. void enter_standby_rtc(float standbytime)
  39. {
  40.         uint32_t i;                //局部變量,用于計算低功耗時長
  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. //進入SLEEP模式低功耗,使用RTC功能喚醒,其中sleeptime單位為S,如設(shè)置1,低功耗1秒后喚醒
  66. void enter_sleep_rtc(float sleeptime)
  67. {
  68.         uint32_t i;                //局部變量,用于計算低功耗時長
  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)閉項
  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)

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:655724 發(fā)表于 2019-12-4 10:06 | 只看該作者
說的很詳細,感謝。先拿來試試!
回復(fù)

使用道具 舉報

板凳
ID:777315 發(fā)表于 2020-6-12 14:57 | 只看該作者
正在用這個芯片,學(xué)習(xí)下,感謝
回復(fù)

使用道具 舉報

地板
ID:293301 發(fā)表于 2023-2-19 12:42 | 只看該作者
下載不了了,不知道什么原因
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表