stm32配置RTC中断并显示,开启RTC中断时间需要自己修改,调试可以通过Debug 或者串口随机发送一个数据即可;具体操作如下,有错误请联系,谢谢;
//redirect fputc goto DEBUG_USARTx
int fputc(int ch, FILE *f)
{
HAL_UART_Transmit(&huart1, (unsigned char *)&ch, 1, 0xFFFF);
return ch;
}
uint8_t aRxBuffer1;
uint8_t test_value[3];
//set RTC time
void rtc_Initconfig(void)
{
//rtc struct init
RTC_DateTypeDef sDate;
RTC_TimeTypeDef sTime;
RTC_AlarmTypeDef sAlarm;
//set rtc datas Year Month Date WeekDay : 2018 8 8 5
sDate.Year = 0x08;
sDate.Month = RTC_MONTH_AUGUST;
sDate.Date = 0x08;
sDate.WeekDay = RTC_WEEKDAY_FRIDAY;
HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD);
//set rtc time : 06:06:06
sTime.Hours = 0x06;
sTime.Minutes = 0x06;
sTime.Seconds = 0x06;
HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD);
//set rtc AlarmTime hour minu second :06:07:01
sAlarm.AlarmTime.Hours = 0x06;
sAlarm.AlarmTime.Minutes = 0x06;
sAlarm.AlarmTime.Seconds = 0x10;
HAL_RTC_SetAlarm(&hrtc, &sAlarm, RTC_FORMAT_BCD);
//Enable RTC timer IT
HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BCD);
}
//printf show time
void RTC_TimeShow(void)
{
RTC_DateTypeDef getSdate;
RTC_TimeTypeDef getTime;
//感谢博主(hwd1007003419的博客_CSDN博客)指出错误,已修改
//获取日期信息函数应该放在获取时间信息之前,要不然打印出来的时间信息更新不及时。
/* Get the RTC current Time */
HAL_RTC_GetTime(&hrtc, &getTime, RTC_FORMAT_BIN);
/* Get the RTC current Date */
HAL_RTC_GetDate(&hrtc, &getSdate, RTC_FORMAT_BIN);
/* Display time Format : Year:Month:Date:WeekDay */
printf("%02d:%02d:%02d:%02d",getSdate.Year, getSdate.Month, getSdate.Date, getSdate.WeekDay);
printf("\t\t");
// /* Display time Format : hh:mm:ss */
printf("%02d:%02d:%02d",getTime.Hours, getTime.Minutes, getTime.Seconds);
printf("\n");
}
主函数
int main(void)
{
//Enable UART Receive IT
HAL_UART_Receive_IT(&huart1,&aRxBuffer1,1);
while (1)
{
// printf("uart test OK!\n");
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
HAL_Delay(1000);
RTC_TimeShow();
//test modify time
if(test_value[0] != 0)
{
test_value[0] =0;
rtc_Initconfig();
}
}
}
//uart Receive interrupt
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART1)
{
HAL_UART_Receive_IT(&huart1,&aRxBuffer1,1);
test_value[0] = aRxBuffer1; //uart rx set RTC
HAL_UART_Transmit(&huart1, &aRxBuffer1, 1, 0xFFFF);
}
}
//RTC alarm event call back
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
printf("RTC Alarm");
}