//进入Stop模式
void Sys_Enter_Stop(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
PWR_WakeUpPinCmd(ENABLE);
PWR_EnterSTOPMode(PWR_Regulator_LowPower,PWR_STOPEntry_WFE);
}
//RTC闹钟中断,需要清除中断标志位
void RTCAlarm_IRQHandler(void)
{
EXTI_ClearITPendingBit(EXTI_Line17);
RTC_ClearITPendingBit(RTC_IT_ALR);
}
//RTC初始化,与之前相同
void RTC_Init(void)
{
NVIC_InitTypeDef NVIC_InitSrtucture;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitSrtucture.NVIC_IRQChannel=RTCAlarm_IRQn;
NVIC_InitSrtucture.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitSrtucture.NVIC_IRQChannelSubPriority=1;
NVIC_InitSrtucture.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitSrtucture);
EXTI_ClearITPendingBit(EXTI_Line17);
EXTI_InitStructure.EXTI_Line = EXTI_Line17;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
RCC_APB1PeriphClockCmd (RCC_APB1Periph_PWR | RCC_APB1Periph_BKP,ENABLE );
PWR_BackupAccessCmd(ENABLE);
BKP_DeInit ();
RCC_LSEConfig (RCC_LSE_ON);
while(!RCC_GetFlagStatus (RCC_FLAG_HSERDY)) {}
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
RTC_WaitForLastTask();
RTC_SetPrescaler(32767);
RTC_WaitForLastTask();
RTC_SetCounter(0);
RTC_WaitForLastTask();
RTC_SetAlarm(3);
RTC_WaitForLastTask();
RTC_ITConfig(RTC_IT_ALR,ENABLE);
RTC_WaitForLastTask();
}
//主程序
int main(void)
{
short temperature;
u8 t0,t1,t2;
u8 t4;
//初始化与之前相同
uart_init(115200);
delay_init();
LED_Init();
while(DS18B20_Init()) {}
while(1)
{
SystemInit();
temperature=DS18B20_Get_Temp();
if(temperature<0)
{
temperature=-temperature;
t4=49;
}
else
{
t4=48;
}
t0=temperature%10+48;
temperature=temperature/10;
t1=temperature%10+48;
temperature=temperature/10;
t2=temperature%10+48;
delay_ms(1);
USART_SendData(USART1,t4);
delay_ms(1);
USART_SendData(USART1,t2);
delay_ms(1);
USART_SendData(USART1,t1);
delay_ms(1);
USART_SendData(USART1,t0);
delay_ms(1);
USART_SendData(USART1,0x0d);
delay_ms(1);
USART_SendData(USART1,0x0a);
delay_ms(1);
//判断发送成功
LED1=0;
delay_ms(100);
LED1=1;
//RTC闹钟计时开始
RTC_Init();
Sys_Enter_Stop();
delay_ms(50);
}
}
STOP模式与STANDBY模式的区别在于STOP模式唤醒后进入中断服务程序,再继续进行之Sys_Enter_Stop()之和的程序操作;STANDBY模式唤醒后从main()程序开始进行,可以理解为开机,所有的初始化均无效。如果极致追求低功耗,可以选择STANDBY模式,如果需要保存相关寄存器的值和初始化信息,应当选择STOP模式