功能实现:
- 修改RTC的时间(BCD码)
- 、、、
- 、、、
uint8_t uart5_rxBuffer[32];//使用全局变量
uint8_t readyDataBuffer[32];
/* 声明要使用的函数 */
void MenuTerminal(void);
int TimeModification(void);
uint8_t strToBcdConverted(char c1_index,char c2_index);
int main()
{
MX_UART5_Init();
HAL_UARTEx_ReceiveToIdle_IT(&huart5, uart5_rxBuffer, 32);//开启串口空闲中断
while()
{
MenuTerminal();
}
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
if (huart->Instance == UART5)
{
if (huart->RxEventType == HAL_UART_RXEVENT_IDLE)
{
HAL_UARTEx_ReceiveToIdle_IT(&huart5, uart5_rxBuffer, 32);
}
}
}
void MenuTerminal(void)
{
int time_value;
if(NULL != (strstr((char *)uart5_rxBuffer,"\r\n")))
{
memset(readyDataBuffer,0,sizeof(readyDataBuffer));
memcpy(readyDataBuffer,uart5_rxBuffer,sizeof(uart5_rxBuffer));
memset(uart5_rxBuffer,0,sizeof(uart5_rxBuffer));
switch (readyDataBuffer[0]) {
case 'H':
HAL_UART_Transmit_DMA(&huart5, (uint8_t *) "Reference format for the input below:\r\n"
"T 2024 12 16 08 30 10 01\r\n",
strlen("Reference format for the input below:\r\n"
"T 2024 12 16 08 30 10 01\r\n"));
case 'T':
//修改时间
time_value=TimeModification();
if(!(time_value))
{
HAL_UART_Transmit_DMA(&huart5, (uint8_t *) "Successful modification time.\r\n",
strlen("Successful modification time.\r\n"));
}
else if((-1)==time_value)
{
HAL_UART_Transmit_DMA(&huart5, (uint8_t *) "The data format you entered about 'T' is incorrect. The correct reference format is: T 2024 05 16 08 30 10 07\r\n",
strlen("The data format you entered about 'T' is incorrect. The correct reference format is: T 2024 05 16 08 30 10 07\r\n"));
}
else if((-2)==time_value)
{
HAL_UART_Transmit_DMA(&huart5, (uint8_t *) "The month you entered is wrong, please re-enter!\r\n",
strlen("The month you entered is wrong, please re-enter!\r\n"));
}
else if((-3)==time_value)
{
HAL_UART_Transmit_DMA(&huart5, (uint8_t *) "The date you entered is wrong, please re-enter!\r\n",
strlen("The date you entered is wrong, please re-enter!\r\n"));
}
else if((-4)==time_value)
{
HAL_UART_Transmit_DMA(&huart5, (uint8_t *) "The hours you entered is wrong, please re-enter!\r\n",
strlen("The hours you entered is wrong, please re-enter!\r\n"));
}
else if((-5)==time_value)
{
HAL_UART_Transmit_DMA(&huart5, (uint8_t *) "The minutes you entered is wrong, please re-enter!\r\n",
strlen("The minutes you entered is wrong, please re-enter!\r\n"));
}
else if((-6)==time_value)
{
HAL_UART_Transmit_DMA(&huart5, (uint8_t *) "The seconds you entered is wrong, please re-enter!\r\n",
strlen("The seconds you entered is wrong, please re-enter!\r\n"));
}
else
{
HAL_UART_Transmit_DMA(&huart5, (uint8_t *) "The weekday you entered is wrong, please re-enter!\r\n",
strlen("The weekday you entered is wrong, please re-enter!\r\n"));
}
break;
default:
HAL_UART_Transmit_DMA(&huart5, (uint8_t *) "Your input is incorrect, please re-enter! You can type in 'H' to get some hints.\r\n",
strlen("Your input is incorrect, please re-enter! You can type in 'H' to get some hints.\r\n"));
break;
}
}
}
int TimeModification(void)
{
// Uncomment this section to set Date and time
RTC_DateTypeDef rtcDate;
RTC_TimeTypeDef rtcTime;
uint8_t year;
uint8_t month;
uint8_t date;
uint8_t hours;
uint8_t minutes;
uint8_t seconds;
uint8_t weekday;
uint16_t year_judge;
char resultStr[5]= {0};
year=strToBcdConverted(4,5);
month=strToBcdConverted(7,8);
date=strToBcdConverted(10,11);
hours=strToBcdConverted(13,14);
minutes=strToBcdConverted(16,17);
seconds=strToBcdConverted(19,20);
weekday=strToBcdConverted(22,23);
// printf("year=%x,month=%x,date=%x,hours=%x,minutes=%x,seconds=%x,weekday=%x\r\n",year,month,date,hours,minutes,seconds,weekday);
if(strlen((char *)readyDataBuffer) != 26)
{
// printf("%d\r\n",strlen((char *)time_rxBuffer));
return -1;
}
if(month>0x12 || month<0)
{
return -2;
}
if(date<0)
{
return -3;
}
if((1==month || 3==month || 5==month || 7==month || 8==month || 10==month || 12==month) && (date>0x31))
{
return -3;
}
if((4==month || 6==month || 9==month || 11==month) && (date>0x30))
{
return -3;
}
if(2==month)
{
memcpy(resultStr,readyDataBuffer+2,4);
resultStr[4]='\0';
year_judge=strtol(resultStr,0,10);
// printf("%s\r\n",resultStr);
// printf("%d\r\n",year_judge);
if( (year_judge%4==0) && ((year_judge%100)!=0) || year_judge %400 == 0)
{//闰年
if(date>0x29)
{
printf("[%d] is a leap year, and February has only 29 days.\r\n",year_judge);
return -3;
}
}
else
{//平年
if(date>0x28)
{
printf("[%d] is a normal year, with only 28 days in February.\r\n",year_judge);
return -3;
}
}
}
if(hours>0x24 || hours<0)
{
return -4;
}
if(minutes>0x60 || minutes<0)
{
return -5;
}
if(seconds>0x60 || seconds<0)
{
return -6;
}
if(weekday>0x07 || weekday<0x01)
{
return -7;
}
/* The RTC real-time clock uses the BCD code format */
rtcDate.Year = year;
rtcDate.Month = month;
rtcDate.Date = date;
rtcDate.WeekDay = weekday;
if(HAL_RTC_SetDate(&hrtc,&rtcDate,RTC_FORMAT_BCD) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
rtcTime.Hours = hours;
rtcTime.Minutes = minutes;
rtcTime.Seconds = seconds;
rtcTime.TimeFormat = RTC_HOURFORMAT_24;
rtcTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
rtcTime.StoreOperation = RTC_STOREOPERATION_RESET;
if(HAL_RTC_SetTime(&hrtc,&rtcTime,RTC_FORMAT_BCD) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}
return 0;
}
/* Convert to BCD code format */
uint8_t strToBcdConverted(char c1_index,char c2_index)
{
uint8_t data;
data=(readyDataBuffer[c1_index]-'0')<<4 | (readyDataBuffer[c2_index]-'0');
return data;
}
通过串口助手发送命令来修改时间的结果: