配置芯片进入停止模式,RTC唤醒中断唤醒。
成功进入停止模式4S,然后由RTC唤醒,ADC、日历、串口等功能成功启动。
关闭高速时钟; RCC中关闭所有外设时钟; 关闭外设使能;
要使能 RTC 唤醒中断,需按照以下顺序操作: 1. 将 EXTI 线 20 配置为中断模式并将其使能,然后选择上升沿有效。 2. 配置 NVIC 中的 RTC_WKUP IRQ 通道并将其使能。 3. 配置 RTC 以生成 RTC 唤醒定时器事件。
void RTC_configration(void){ NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; RTC_InitTypeDef RTC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /*!< Allow access to RTC */ PWR_RTCAccessCmd(ENABLE); /* Clear WakeUp flag */ PWR_ClearFlag(PWR_FLAG_WU); /*!< Reset RTC Domain */ RCC_RTCResetCmd(ENABLE); RCC_RTCResetCmd(DISABLE); /*!< LSE Enable */ RCC_LSEConfig(RCC_LSE_ON); /*!< Wait till LSE is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {} /*!<RTC Clock Source Selection */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /*!< Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /*!< Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Calendar Configuration这里时假设RTC时钟是32.768,如果不是,需要在system设置中更改 */ RTC_InitStructure.RTC_AsynchPRediv = 0x7F; RTC_InitStructure.RTC_SynchPrediv = 0x120; /* (37KHz / 128) - 1 = 0x120*/ RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); /* Set the date: Wednesday January 17th 2017 */ RTC_DateStructure.RTC_Year =0x11; RTC_DateStructure.RTC_Month = RTC_Month_January; RTC_DateStructure.RTC_Date =0x13; RTC_DateStructure.RTC_WeekDay = RTC_Weekday_Tuesday; RTC_SetDate(RTC_Format_BIN, &RTC_DateStructure);//注意设置格式 /* Set the time to 01h 00mn 00s AM */ RTC_TimeStructure.RTC_H12 = RTC_H12_PM; RTC_TimeStructure.RTC_Hours = 0x0a; RTC_TimeStructure.RTC_Minutes = 0x15; RTC_TimeStructure.RTC_Seconds = 0x03; RTC_SetTime(RTC_Format_BIN, &RTC_TimeStructure); //注意设置格式 /* EXTI configuration *******************************************************/ EXTI_ClearITPendingBit(EXTI_Line20); EXTI_InitStructure.EXTI_Line = EXTI_Line20; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Enable the RTC Wakeup Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* RTC Wakeup Interrupt Generation: Clock Source: RTCDiv_16, Wakeup Time Base: 4s */ RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits); RTC_SetWakeUpCounter(0x0004); /* Enable the Wakeup Interrupt */ RTC_ITConfig(RTC_IT_WUT, ENABLE); }然后在需要进入停止模式的地方运行以下函数
void To_enter_Stop(void){GPIO_InitTypeDef GPIO_InitStructure; //1)关闭所有不用的外设及时钟//2)设置IO /* Enable Wakeup Counter */ RTC_WakeUpCmd(ENABLE); /* Enter Stop Mode */ PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);}所有管脚设置为模拟输入以降低功耗。 如果需要使能低功耗调试DEBUG,响应管脚保持原有设置。
/Enable debug in stop mode/
DBGMCU_Config(DBGMCU_STOP,ENABLE);
RCC_AHBPeriphClockLPModeCmd(RCC_AHBPeriph_GPIOA,ENABLE);
先配置时钟,然后开启GPIO,然后重新初始化相关外设。 注意:重新初始化之前Deinit一下,否则有的会重新配置 不成功。例如GPIO
void To_Exit_Stop(void){ RTC_WakeUpCmd(DISABLE); /* Enable HSI Clock */ RCC_HSICmd(ENABLE); /*!< Wait till HSI is ready */ while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); /* Enable 64-bit access */ Flash_ReadAccess64Cmd(ENABLE); /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(ENABLE); /* Flash 1 wait state */ FLASH_SetLatency(FLASH_Latency_1); RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); while (RCC_GetSYSCLKSource() != 0x04); RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK */ RCC_PCLK1Config(RCC_HCLK_Div1); PWR_VoltageScalingConfig(PWR_VoltageScaling_Range1); /* Wait Until the Voltage Regulator is ready */ while (PWR_GetFlagStatus(PWR_FLAG_VOS) != RESET); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC | RCC_AHBPeriph_GPIOD | RCC_AHBPeriph_GPIOE | RCC_AHBPeriph_GPIOH | RCC_AHBPeriph_GPIOF | RCC_AHBPeriph_GPIOG, ENABLE); GPIO_DeInit(GPIOA); GPIO_DeInit(GPIOB); GPIO_DeInit(GPIOC); GPIO_DeInit(GPIOD); GPIO_DeInit(GPIOE); GPIO_DeInit(GPIOF); GPIO_DeInit(GPIOG); GPIO_DeInit(GPIOH);}新闻热点
疑难解答