STM32L053闹钟唤醒:
设置实现原理,当闹钟时间==日历时间时,闹钟中断产生,但是闹钟中断与外部中断线17相连,所以如果不初始化外部中断线17,则无法进入RTC中断服务函数。
目的:实现毫秒级闹钟中断唤醒睡眠模式。
设置日历时间=闹钟时间,计时进入中断之后,再重新设置日历时间=闹钟时间。
注意:中断之中也要清除外部中断标志,否则下次再次休眠电流会增加。
LL库
void MX_RTC_Init(void)
{
LL_RTC_InitTypeDef RTC_InitStruct = {0};
LL_RTC_TimeTypeDef RTC_TimeStruct = {0};
LL_RTC_AlarmTypeDef RTC_AlarmStruct = {0};
LL_EXTI_InitTypeDef EXTI_InitStruct = {0};
LL_RTC_TimeTypeDef RTC_ALMA_InitStruct = {0};
LL_RCC_EnableRTC();
NVIC_SetPriority(RTC_IRQn, 2);
NVIC_EnableIRQ(RTC_IRQn);
/*开启中断线17否则不会被唤醒*/
EXTI_InitStruct.Line_0_31 = LL_EXTI_LINE_17;
EXTI_InitStruct.LineCommand = ENABLE;
EXTI_InitStruct.Mode = LL_EXTI_MODE_IT;
EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_RISING;
LL_EXTI_Init(&EXTI_InitStruct);
RTC_InitStruct.HourFormat=LL_RTC_HOURFORMAT_24HOUR;
RTC_InitStruct.AsynchPrescaler=36;
RTC_InitStruct.SynchPrescaler=10000; //设置10秒唤醒
LL_RTC_Init(RTC, &RTC_InitStruct);
RTC_TimeStruct.TimeFormat=LL_RTC_TIME_FORMAT_AM_OR_24;
RTC_TimeStruct.Hours=0x8;
RTC_TimeStruct.Minutes=0x30;
RTC_TimeStruct.Seconds=0x10;
LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BCD, &RTC_TimeStruct);
RTC_ALMA_InitStruct.TimeFormat=LL_RTC_TIME_FORMAT_AM_OR_24;
RTC_ALMA_InitStruct.Hours=0x8;
RTC_ALMA_InitStruct.Minutes=0x30;
RTC_ALMA_InitStruct.Seconds=0x10;
LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BCD, &RTC_ALMA_InitStruct);
RTC_AlarmStruct.AlarmDateWeekDay=7;
RTC_AlarmStruct.AlarmDateWeekDaySel=LL_RTC_ALMA_DATEWEEKDAYSEL_WEEKDAY;
RTC_AlarmStruct.AlarmMask=LL_RTC_ALMA_MASK_DATEWEEKDAY;
RTC_AlarmStruct.AlarmTime=RTC_ALMA_InitStruct;
LL_RTC_ALMA_Init(RTC, LL_RTC_FORMAT_BCD, &RTC_AlarmStruct);
LL_RTC_DisableWriteProtection(RTC);
LL_RTC_ALMA_SetSubSecond(RTC, 1);
LL_RTC_ALMA_SetMask(RTC, LL_RTC_ALMA_MASK_DATEWEEKDAY);
LL_RTC_ALMA_SetSubSecondMask(RTC, 0xF);
LL_RTC_ALMA_Enable(RTC);
LL_RTC_EnableIT_ALRA(RTC);
LL_RTC_EnableWriteProtection(RTC);
}
终端到来设置时间
void RTC_IRQHandler(void)
{
LL_RTC_TimeTypeDef RTC_RstTimeStruct = {0};
if(LL_RTC_IsActiveFlag_ALRA(RTC))
{
printf("IRQn WKUP Timer Enter...\r\n");
LL_RTC_ClearFlag_ALRA(RTC);
LL_EXTI_ClearFlag_0_31(LL_EXTI_LINE_17);
}
RTC_RstTimeStruct.TimeFormat=LL_RTC_TIME_FORMAT_AM_OR_24;
RTC_RstTimeStruct.Hours=0x8;
RTC_RstTimeStruct.Minutes=0x30;
RTC_RstTimeStruct.Seconds=0x10;
LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BCD, &RTC_RstTimeStruct);
}
HAL库
RTC_HandleTypeDef hrtc;
RTC_TimeTypeDef sTime = {0};
RTC_AlarmTypeDef sAlarm = {0};
/**
* @brief RTC中断初始化,用于睡眠定时唤醒
* @param
* @retval
*/
void rtc_INT_init(uint16_t ms)
{
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24; //LL_RTC_SetHourFormat(RTC_TypeDef *RTCx, uint32_t HourFormat);
hrtc.Init.AsynchPrediv = 36; //LL_RTC_SetAsynchPrescaler(RTC_TypeDef *RTCx, uint32_t AsynchPrescaler);
hrtc.Init.SynchPrediv = ms; //LL_RTC_SetSynchPrescaler(RTC_TypeDef *RTCx, uint32_t SynchPrescaler);
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE; //
hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE; //LL_RTC_DisableOutRemap(RTC_TypeDef *RTCx);
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;//LL_RTC_SetOutputPolarity(RTC_TypeDef *RTCx, uint32_t Polarity);
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN; //LL_RTC_SetAlarmOutputType(RTC_TypeDef *RTCx, uint32_t Output);
HAL_RTC_Init(&hrtc);
/** 初始化RTC并且设置时间
*/
sTime.Hours = 0x08; //LL_RTC_TIME_SetHour(RTC_TypeDef *RTCx, uint32_t Hours)
sTime.Minutes = 0x30; //LL_RTC_TIME_SetMinute(RTC_TypeDef *RTCx, uint32_t Minutes)
sTime.Seconds = 0x10; //LL_RTC_TIME_SetSecond(RTC_TypeDef *RTCx, uint32_t Seconds)
sTime.TimeFormat = RTC_HOURFORMAT12_AM; //LL_RTC_TIME_SetFormat(RTC_TypeDef *RTCx, uint32_t TimeFormat);
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ; //LL_RTC_TIME_DisableDayLightStore(RTC_TypeDef *RTCx)
sTime.StoreOperation = RTC_STOREOPERATION_RESET; //
HAL_RTC_SetTime(&hrtc,&sTime,RTC_FORMAT_BCD);
/** 中短线A
*/
sAlarm.Alarm = RTC_ALARM_A; //LL_RTC_ALMA_Enable(RTC_TypeDef *RTCx)
sAlarm.AlarmDateWeekDay = RTC_WEEKDAY_MONDAY; //LL_RTC_ALMA_SetWeekDay(RTC_TypeDef *RTCx, uint32_t WeekDay)
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY;
sAlarm.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY; //LL_RTC_ALMA_SetMask(RTC_TypeDef *RTCx, uint32_t Mask)
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;//LL_RTC_ALMA_SetSubSecondMask(RTC_TypeDef *RTCx, uint32_t Mask)
sAlarm.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM; //LL_RTC_ALMA_SetTimeFormat(RTC_TypeDef *RTCx, uint32_t TimeFormat);
sAlarm.AlarmTime.Hours = 0x08; //LL_RTC_ALMA_SetHour(RTC_TypeDef *RTCx, uint32_t Hours)
sAlarm.AlarmTime.Minutes = 0x30; //LL_RTC_ALMA_SetMinute(RTC_TypeDef *RTCx, uint32_t Minutes)
sAlarm.AlarmTime.Seconds = 0x10; //LL_RTC_ALMA_SetSecond(RTC_TypeDef *RTCx, uint32_t Seconds)
sAlarm.AlarmTime.SubSeconds = 1; //LL_RTC_ALMA_SetSubSecond(RTC_TypeDef *RTCx, uint32_t Subsecond)
HAL_RTC_SetAlarm_IT(&hrtc,&sAlarm,RTC_FORMAT_BCD);
HAL_NVIC_SetPriority(RTC_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(RTC_IRQn);
__HAL_RCC_RTC_ENABLE(); //LL_RTC_EnableIT_ALRA(RTC_TypeDef *RTCx)
}
void RTC_IRQHandler(void)
{
/* USER CODE BEGIN RTC_IRQn 0 */
/* USER CODE END RTC_IRQn 0 */
HAL_RTC_AlarmIRQHandler(&hrtc);
/* USER CODE BEGIN RTC_IRQn 1 */
/* USER CODE END RTC_IRQn 1 */
}
uint32_t count=0;
/* USER CODE BEGIN 1 */
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
count++;
debug_print(0, "Enter Count%d\r\n",count);
HAL_RTC_SetTime(hrtc, &sTime, RTC_FORMAT_BCD);
}
电流值,12微安,因为我还有其他外围器件耗电