我遇到有关RTC警报配置的问题。首先,我尝试了Cube提供的示例代码,它的工作原理。但在我的代码中,它根本不起作用。我猜测RTC格式不匹配。即便如此,我尝试了BCD和BIN的所有可能性。
阅读时间和日期功能正在运行。我可以在OLED屏幕上显示它们。 但是不能产生报警中断。我做错了什么?
void StartRTC_Routine(void)
{
HAL_PWR_EnableBkUpAccess();
__HAL_RCC_RTC_ENABLE();
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 127;
hrtc.Init.SynchPrediv = 255;
if(HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR0) != RTC_BKP_DATE_TIME_UPDTATED)
{
/*RTC Init*/
MX_RTC_Init();
RTC_Default();
HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,RTC_BKP_DATE_TIME_UPDTATED);
}
else
{
/* Check if the Power On Reset flag is set */
if(__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET)
{
MyFlag = 0xBA;
}
/* Check if the Soft Reset flag is set */
if(__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST) != RESET)
{
MyFlag = 0xFB;
}
}
}
void RTC_TimeShow(uint8_t* showtime)
{
/* Get the RTC current Time */
HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BCD);
/* Display time Format : hh:mm:ss */
sprintf((char*)showtime,"%02X:%02X:%02X",stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
ssd1306_SetCursor(20,20);
ssd1306_WriteString((char*)showtime,Font_11x18,White);
ssd1306_UpdateScreen();
}
/* Initial Values of RTC **/
void RTC_Default(void)
{
sdatestructureget.Date = 0x14;
sdatestructureget.Year = 0x17;
sdatestructureget.WeekDay = 0x05;
sdatestructureget.Month = 0x01;
stimestructureget.Hours = 0x02;
stimestructureget.Minutes = 0x04;
stimestructureget.Seconds = 0x30;
stimestructureget.TimeFormat = RTC_HOURFORMAT_24;
stimestructureget.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
stimestructureget.StoreOperation = RTC_STOREOPERATION_RESET;
HAL_RTC_SetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BCD);
HAL_RTC_SetTime(&hrtc, &stimestructureget, RTC_FORMAT_BCD);
}
void Set_Alarm(void)
{
/*################# Configure the RTC Alarm peripheral #################*/
salarmstructure.Alarm = RTC_ALARM_A;
salarmstructure.AlarmDateWeekDay = RTC_WEEKDAY_MONDAY;
salarmstructure.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY;
salarmstructure.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
salarmstructure.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;
salarmstructure.AlarmTime.TimeFormat = RTC_HOURFORMAT_24;
salarmstructure.AlarmTime.Hours = 0x02;
salarmstructure.AlarmTime.Minutes = 0x34;
salarmstructure.AlarmTime.Seconds = 0x10;
salarmstructure.AlarmTime.SubSeconds = 00;
HAL_RTC_SetAlarm_IT(&hrtc,&salarmstructure,RTC_FORMAT_BCD);
}
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
/* Turn LED2 on: Alarm generation */
HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);
}
答案 0 :(得分:0)
您可以查看的一些事项:
答案 1 :(得分:0)
您需要为RTC启用NVIC配置,才能使用_IT方法(例如HAL_RTC_SetAlarm_IT)。 如果使用的是CubeMx,则可以在“配置”->“ RTC”中轻松完成,然后转到NVIC。