1. 程式人生 > >RTC喚醒喚醒低功耗(standby)模式

RTC喚醒喚醒低功耗(standby)模式

這段時間在公司最一個低功耗的專案,採用的的STM32F103的最低功耗standby模式,進入最低功耗模式後,電流降到了3uA,和晶片手冊上的大致相同。對進入低功耗模式,網上有很多程式,我在這裡把我的貼上上來,僅供參考,io口的具體配置要通過電路原理圖來設定。

void enter_standby_mode(void)
{
//IO口配置
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//_IPD輸入上拉
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//_IPD輸入下拉
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//_IPD輸入上拉
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=(GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//_IPD輸入下拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//配置A口 其中9 10 為串列埠,13 14為TMS TCK,共16口 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//_IPD輸入上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//下拉
 GPIO_Init(GPIOB, &GPIO_InitStructure);//配置B口 共16口
GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//下拉
   GPIO_Init(GPIOC, &GPIO_InitStructure);//埠C的配置,共16個埠
 GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_2);
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;//下拉
   GPIO_Init(GPIOD, &GPIO_InitStructure);//埠D的配置,1個埠
//一共64埠。ABCD配置了49個埠,VBAT VDD VSS等11個電源埠不用配置 49+11=60
 //沒有配置的埠有復位埠NRST、boot0埠、PD0(OSC_IN)、PD1(OSC_OUT)等4個埠 
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);//使能PWR外設時鐘
   PWR_EnterSTANDBYMode();  //進入待命(STANDBY)模式 

在成功設定低功耗模式後,需要週期性的執行程式,然後進入低功耗,間隔意見時間後,繼續執行程式。這時就需要用到RTC時鐘來進行對低功耗模式的喚醒。低功耗的stop模式,需要配置EXTI_line17來設定喚醒鬧鐘,然後才能喚醒stop模式。standby模式不需要設定EXTI_line17,可以直接通過void RTC_IRQHandler(void)中斷函式實現喚醒,在配置好RTC的情況下,只需要在主函式加入下列程式即可。

void huanxing(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
RTC_SetAlarm(RTC_GetCounter()+50);
RTC_WaitForLastTask();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
PWR_WakeUpPinCmd(ENABLE); 
PWR_EnterSTANDBYMode();

}

週期喚醒函式,沒過50S,RTC鬧鐘喚醒一次低功耗,該程式只需要載入主函式的最後即可。相當於執行完主函式的語句後,進入這個函式,函式會記錄當前系統時,並進入低功耗的standby模式,在50後,喚醒低功耗,從新執行函式。如此週期下去。