完整程序源代碼工程文件下載地址:
功耗控制sleep模式.rar
(122.79 KB, 下載次數: 120)
2015-5-27 17:20 上傳
點擊文件名下載附件
- /*******************************************************************************
- STM32學習日志(15)---- 功耗控制 sleep 模式
- 編譯環境: EWARM V5.30
- STM32 FW: V3.0.0
- 作者 : szlihongtao
- 時間 : 2016-07-17
- 說明 : 全速運行,實測功耗為31mA,sleep,實測功耗為10mA,即降低了21mA的功耗
- *******************************************************************************/
- #include "stm32f10x.h"
- #include "stm32_m.h"
- //******************************************************************************
- INT8U f_tb;
- //******************************************************************************
- static void delayms(INT16U cnt)
- {
- INT16U i;
- while(cnt--)
- for (i=0; i<7333; i++);
- }
- //******************************************************************************
- // 時鐘設置初始化
- //******************************************************************************
- static void RCC_Configuration(void)
- {
- ErrorStatus HSEStartUpStatus;
- /*
- RCC_AdjustHSICalibrationValue 調整內部高速晶振(HSI)校準值
- RCC_ITConfig 使能或者失能指定的RCC中斷
- RCC_ClearFlag 清除RCC的復位標志位
- RCC_GetITStatus 檢查指定的RCC中斷發生與否
- RCC_ClearITPendingBit 清除RCC的中斷待處理位
- */
- /* RCC system reset(for debug purpose) */
- // 時鐘系統復位
- RCC_DeInit();
- // 使能外部的8M晶振
- // 設置外部高速晶振(HSE)
- /* Enable HSE */
- RCC_HSEConfig(RCC_HSE_ON);
- // 使能或者失能內部高速晶振(HSI)
- RCC_HSICmd(DISABLE);
- // 等待HSE起振
- // 該函數將等待直到HSE就緒,或者在超時的情況下退出
- /* Wait till HSE is ready */
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)
- {
- /* HCLK = SYSCLK */
- // 設置AHB時鐘(HCLK)
- RCC_HCLKConfig(RCC_SYSCLK_Div1); // 72 MHz
- /* PCLK1 = HCLK/2 */
- // 設置低速AHB時鐘(PCLK1)
- RCC_PCLK1Config(RCC_HCLK_Div2); // 36 MHz
- /* PCLK2 = HCLK */
- // 設置高速AHB時鐘(PCLK2)
- RCC_PCLK2Config(RCC_HCLK_Div1); // 72 MHz
- /* ADCCLK = PCLK2/8 */
- // 設置ADC時鐘(ADCCLK)
- RCC_ADCCLKConfig(RCC_PCLK2_Div8);
- // 設置USB時鐘(USBCLK)
- // USB時鐘 = PLL時鐘除以1.5
- RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
- // 設置外部低速晶振(LSE)
- RCC_LSEConfig(RCC_LSE_OFF);
- // 使能或者失能內部低速晶振(LSI)
- // LSE晶振OFF
- RCC_LSICmd(DISABLE);
- // 設置RTC時鐘(RTCCLK)
- // 選擇HSE時鐘頻率除以128作為RTC時鐘
- RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);
- // 使能或者失能RTC時鐘
- // RTC時鐘的新狀態
- RCC_RTCCLKCmd(DISABLE);
- /* Flash 2 wait state */
- FLASH_SetLatency(FLASH_Latency_2);
- /* Enable Prefetch Buffer */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- /* PLLCLK = 8MHz * 9 = 72 MHz */
- // 設置PLL時鐘源及倍頻系數
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- /* Enable PLL */
- // 使能或者失能PLL
- RCC_PLLCmd(ENABLE);
- /* Wait till PLL is ready */
- // 檢查指定的RCC標志位設置與否
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
- {
- }
- /* Select PLL as system clock source */
- // 設置系統時鐘(SYSCLK)
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- /* Wait till PLL is used as system clock source */
- // 返回用作系統時鐘的時鐘源
- while(RCC_GetSYSCLKSource() != 0x08)
- {
- }
- }
- // 使能或者失能AHB外設時鐘
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1
- |RCC_AHBPeriph_DMA2
- |RCC_AHBPeriph_SRAM
- |RCC_AHBPeriph_FLITF
- |RCC_AHBPeriph_CRC
- |RCC_AHBPeriph_FSMC
- |RCC_AHBPeriph_SDIO,DISABLE);
- // 使能或者失能APB1外設時鐘
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL,DISABLE);
- // 強制或者釋放高速APB(APB2)外設復位
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,ENABLE);
- // 退出復位狀態
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,DISABLE);
- // 強制或者釋放低速APB(APB1)外設復位
- RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,ENABLE);
- // 退出復位狀態
- RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,DISABLE);
- // 強制或者釋放后備域復位
- RCC_BackupResetCmd(ENABLE);
- // 使能或者失能時鐘安全系統
- RCC_ClockSecuritySystemCmd(DISABLE);
- }
- //******************************************************************************
- // SysTick設置初始化
- //******************************************************************************
- static void SysTick_Config1(void)
- {
- #define SystemFreq 72000000.0 // 單位為Hz
- #define TB_SysTick 50000.0 // 單位為uS
- INT32U ticks;
- ticks=(INT32U)((TB_SysTick/1000000.0)*SystemFreq);
- SysTick_Config(ticks);
- }//******************************************************************************
- // GPIO設置
- //******************************************************************************
- static void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- // 使能或者失能APB2外設時鐘
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- //******************************************************************************
- static void myPWR_EnterSleepMode(void)
- {
- /*
- 通過執行WFI或WFE指令進入睡眠狀態。根據Cortex.-M3系統控制寄存器中的SLEEPONEXIT
- 位的值,有兩種選項可用于選擇睡眠模式進入機制:
- ● SLEEP-NOW:如果SLEEPONEXIT位被清除,當WRI或WFE被執行時,微控制器立即進
- 入睡眠模式。
- ● SLEEP-ON-EXIT:如果SLEEPONEXIT位被置位,系統從最低優先級的中斷處理程序中退
- 出時,微控制器就立即進入睡眠模式。
- */
- NVIC_SystemLPConfig(NVIC_LP_SLEEPDEEP,DISABLE);
- NVIC_SystemLPConfig(NVIC_LP_SLEEPONEXIT,DISABLE);
- __WFI(); /* Request Wait For Interrupt */
- }
- //******************************************************************************
- // 主程序
- //******************************************************************************
- int main(void)
- {
- INT8U i;
- RCC_Configuration();
- GPIO_Configuration();
- SysTick_Config1();
- for (i=0;i<100;++i) // 全速運行,實測功耗為31mA
- {
- GPIOB->ODR ^= GPIO_Pin_1; // led toogle
- delayms(50);
- }
- //------------------------------------------------------------------------------
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
- for (;;)
- {
- myPWR_EnterSleepMode(); // sleep,實測功耗為10mA
- if (f_tb)
- {
- f_tb=0;
- GPIOB->ODR ^= GPIO_Pin_1;// led toogle
- }
- }
- }
- //******************************************************************************
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval : None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /**
- * @}
- */
- //******************************************************************************
- /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
- //******************************************************************************
復制代碼
|