先上代码:
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_RTC_Init();
MX_USART1_UART_Init();
/* Initialize interrupts */
MX_NVIC_Init();
gUSER_SendSec_Index = 0x00;
/* USER CODE BEGIN 2 */
__HAL_UART_CLEAR_IDLEFLAG(&huart1);
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
if(gUSER_SendSec_Index == 0x01)
{
USER_BackInfo();
gUSER_SendSec_Index = 0x00;
}
USER_Uart_DmaRecv();
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
//RTC配置
static void MX_RTC_Init(void)
{
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
/**Initialize RTC Only
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 0;
hrtc.Init.SynchPrediv = 31999;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Initialize RTC and set the Time and Date
*/
if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2){
sTime.Hours = 0x0;
sTime.Minutes = 0x0;
sTime.Seconds = 0x0;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = RTC_MONTH_JANUARY;
sDate.Date = 0x1;
sDate.Year = 0x0;
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);
}
/**Enable the WakeUp
*/
if (HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 0, RTC_WAKEUPCLOCK_RTCCLK_DIV16) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc1)
{
gUSER_SendSec_Index = 0x01;
HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 0, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
}
//通过串口发送至PC机显示时间
void USER_BackInfo(void)
{
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
gUARTSendBuff[0] = '$';
gUARTSendBuff[1] = 'I';
gUARTSendBuff[2] = 'N';
gUARTSendBuff[3] = 'F';
gUARTSendBuff[4] = 'O';
gUARTSendBuff[5] = 0x00;
gUARTSendBuff[6] = 0x0E;
gUARTSendBuff[7] = ID;
gUARTTx_Len = 14;
HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
gUARTSendBuff[8] = sDate.Year;
gUARTSendBuff[9] = sDate.Month;
gUARTSendBuff[10] = sDate.Date;
gUARTSendBuff[11] = sTime.Hours;
gUARTSendBuff[12] = sTime.Minutes;
gUARTSendBuff[13] = sTime.Seconds;
USER_Uart_DataSend();
}
在PC机上同时显示PC机时间和通过串口收到的时间,才过了3个小时就差了30秒,这个精度很低呀,是我哪里没弄对么? |