找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 6723|回復: 0
打印 上一主題 下一主題
收起左側

STM32學習-功耗控制PWR Standby模式

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:81272 發(fā)表于 2015-5-27 16:32 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
完整程序源代碼工程文件下載地址: 功耗控制PWR Standby模式.rar (222.82 KB, 下載次數(shù): 26)

  1. /*******************************************************************************

  2. STM32學習日志(16)----功耗控制 PWR Standby 模式

  3. 編譯環(huán)境:        EWARM V5.30
  4. 硬件環(huán)境:        LED-----PB1
  5.                         按鈕----PA1/WKUP
  6. STM32 FW:   V3.0.0
  7. 作者        :        szlihongtao
  8. 時間        :          2017-07-17
  9. 說明        :
  10.                         開機后,LED閃爍5秒,實際測量輸入電流為33mA,然后加入standby模式,
  11.                         實際測量電流為10uA。
  12.                         當 WKUP 引腳施加上升沿脈沖時候,退出standby模式,然后系統(tǒng)復位,LED重新
  13.                         閃爍5秒。。。。。。。。。。。
  14. *******************************************************************************/
  15. /**
  16.   ******************************************************************************
  17.   * @file    Project/Template/main.c
  18.   * @author  MCD Application Team
  19.   * @version V3.0.0
  20.   * @date    04/06/2009
  21.   * @brief   Main program body
  22.   ******************************************************************************
  23.   * @copy
  24.   *
  25.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  26.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  27.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  28.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  29.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  30.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  31.   *
  32.   * <h2><center>© COPYRIGHT 2009 STMicroelectronics</center></h2>
  33.   */

  34. /* Includes ------------------------------------------------------------------*/
  35. #include "stm32f10x.h"
  36. #include "stm32_m.h"

  37. /** @addtogroup Template_Project
  38.   * @{
  39.   */

  40. /* Private typedef -----------------------------------------------------------*/
  41. /* Private define ------------------------------------------------------------*/
  42. /* Private macro -------------------------------------------------------------*/
  43. /* Private variables ---------------------------------------------------------*/
  44. /* Private function prototypes -----------------------------------------------*/
  45. /* Private functions ---------------------------------------------------------*/

  46. /**
  47.   * @brief  Main program.
  48.   * @param  None
  49.   * @retval : None
  50.   */
  51. //******************************************************************************
  52. static void delayms(INT16U cnt)
  53. {
  54.         INT16U i;

  55.         while(cnt--)
  56.                 for (i=0; i<7333; i++);
  57. }
  58. //******************************************************************************
  59. // 時鐘設置初始化
  60. //******************************************************************************
  61. static void RCC_Configuration(void)
  62. {
  63.   ErrorStatus HSEStartUpStatus;
  64. /*
  65. RCC_AdjustHSICalibrationValue 調(diào)整內(nèi)部高速晶振(HSI)校準值
  66. RCC_ITConfig 使能或者失能指定的RCC中斷
  67. RCC_ClearFlag 清除RCC的復位標志位
  68. RCC_GetITStatus 檢查指定的RCC中斷發(fā)生與否
  69. RCC_ClearITPendingBit 清除RCC的中斷待處理位
  70. */
  71.           /* RCC system reset(for debug purpose) */
  72.           // 時鐘系統(tǒng)復位
  73.           RCC_DeInit();

  74.         // 使能外部的8M晶振
  75.         // 設置外部高速晶振(HSE)
  76.           /* Enable HSE */
  77.           RCC_HSEConfig(RCC_HSE_ON);

  78.         // 使能或者失能內(nèi)部高速晶振(HSI)
  79.         RCC_HSICmd(DISABLE);

  80.         // 等待HSE起振
  81.         // 該函數(shù)將等待直到HSE就緒,或者在超時的情況下退出
  82.           /* Wait till HSE is ready */
  83.           HSEStartUpStatus = RCC_WaitForHSEStartUp();

  84.           if(HSEStartUpStatus == SUCCESS)
  85.           {
  86.             /* HCLK = SYSCLK */
  87.                 // 設置AHB時鐘(HCLK)
  88.             RCC_HCLKConfig(RCC_SYSCLK_Div1);        // 72 MHz

  89.             /* PCLK1 = HCLK/2 */
  90.                 // 設置低速AHB時鐘(PCLK1)
  91.             RCC_PCLK1Config(RCC_HCLK_Div2);        // 36 MHz

  92.             /* PCLK2 = HCLK */
  93.                 // 設置高速AHB時鐘(PCLK2)
  94.             RCC_PCLK2Config(RCC_HCLK_Div1);        // 72 MHz

  95.             /* ADCCLK = PCLK2/8 */
  96.                 // 設置ADC時鐘(ADCCLK)
  97.                    RCC_ADCCLKConfig(RCC_PCLK2_Div8);

  98.                 // 設置USB時鐘(USBCLK)
  99.                 // USB時鐘 = PLL時鐘除以1.5
  100.                 RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);

  101.                 // 設置外部低速晶振(LSE)
  102.                 RCC_LSEConfig(RCC_LSE_OFF);

  103.                 // 使能或者失能內(nèi)部低速晶振(LSI)
  104.                 // LSE晶振OFF
  105.                 RCC_LSICmd(DISABLE);

  106.                 // 設置RTC時鐘(RTCCLK)
  107.                 // 選擇HSE時鐘頻率除以128作為RTC時鐘
  108.                 RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);

  109.                 // 使能或者失能RTC時鐘
  110.                 // RTC時鐘的新狀態(tài)
  111.                 RCC_RTCCLKCmd(DISABLE);

  112.             /* Flash 2 wait state */
  113.             FLASH_SetLatency(FLASH_Latency_2);

  114.             /* Enable Prefetch Buffer */
  115.             FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  116.             /* PLLCLK = 8MHz * 9 = 72 MHz */
  117.                 // 設置PLL時鐘源及倍頻系數(shù)
  118.             RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  119.             /* Enable PLL */
  120.                 // 使能或者失能PLL
  121.             RCC_PLLCmd(ENABLE);

  122.             /* Wait till PLL is ready */
  123.                 // 檢查指定的RCC標志位設置與否
  124.             while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  125.             {
  126.             }

  127.             /* Select PLL as system clock source */
  128.                 // 設置系統(tǒng)時鐘(SYSCLK)
  129.             RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  130.             /* Wait till PLL is used as system clock source */
  131.                 // 返回用作系統(tǒng)時鐘的時鐘源
  132.             while(RCC_GetSYSCLKSource() != 0x08)
  133.             {
  134.             }
  135.   }


  136.         // 使能或者失能AHB外設時鐘
  137.         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1
  138.                                                         |RCC_AHBPeriph_DMA2
  139.                                                         |RCC_AHBPeriph_SRAM
  140.                                                         |RCC_AHBPeriph_FLITF
  141.                                                         |RCC_AHBPeriph_CRC
  142.                                                         |RCC_AHBPeriph_FSMC
  143.                                                         |RCC_AHBPeriph_SDIO,DISABLE);
  144.         // 使能或者失能APB1外設時鐘
  145.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL,DISABLE);

  146.         // 強制或者釋放高速APB(APB2)外設復位
  147.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,ENABLE);
  148.         // 退出復位狀態(tài)
  149.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,DISABLE);

  150.         // 強制或者釋放低速APB(APB1)外設復位
  151.         // 注意不能加這句話,否則,STM32 wkup從standby模式喚醒后無法正常運行,不知道是什么原因
  152.         RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,ENABLE);
  153.         // 退出復位狀態(tài)
  154.         RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,DISABLE);

  155.         // 強制或者釋放后備域復位
  156.         RCC_BackupResetCmd(ENABLE);

  157.         // 使能或者失能時鐘安全系統(tǒng)
  158.         RCC_ClockSecuritySystemCmd(DISABLE);
  159. }
  160. //******************************************************************************
  161. // GPIO設置
  162. //******************************************************************************
  163. static void GPIO_Configuration(void)
  164. {
  165.         GPIO_InitTypeDef GPIO_InitStructure;

  166.         // 使能或者失能APB2外設時鐘
  167.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);

  168.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  169.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  170.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  171.           GPIO_Init(GPIOB, &GPIO_InitStructure);
  172. }
  173. //******************************************************************************
  174. // 主程序
  175. //******************************************************************************
  176. int main(void)
  177. {
  178.         INT8U i;

  179.         RCC_Configuration();
  180.           GPIO_Configuration();

  181.         for (i=0;i<120;++i)
  182.         {
  183.                 delayms(50);
  184.                 GPIOB->ODR ^= GPIO_Pin_1;// led1 toogle
  185.         }

  186.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
  187.           PWR_WakeUpPinCmd(ENABLE);        /* Enable WKUP pin */
  188.           PWR_EnterSTANDBYMode();        /* Request to enter STANDBY mode (Wake Up flag is cleared in PWR_EnterSTANDBYMode function) */

  189.         NVIC_SystemReset();                // 系統(tǒng)復位
  190.         for(;;);
  191. }
  192. //******************************************************************************
  193. #ifdef  USE_FULL_ASSERT

  194. /**
  195.   * @brief  Reports the name of the source file and the source line number
  196.   *   where the assert_param error has occurred.
  197.   * @param file: pointer to the source file name
  198.   * @param line: assert_param error line source number
  199.   * @retval : None
  200.   */
  201. void assert_failed(uint8_t* file, uint32_t line)
  202. {
  203.   /* User can add his own implementation to report the file name and line number,
  204.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  205.   /* Infinite loop */
  206.   while (1)
  207.   {
  208.   }
  209. }
  210. #endif

  211. /**
  212.   * @}
  213.   */
  214. //******************************************************************************
  215. /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
  216. //******************************************************************************

復制代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩

相關帖子

回復

使用道具 舉報

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

本版積分規(guī)則

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

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

快速回復 返回頂部 返回列表