#include <stdio.h>#include "stm32f10x.h"
// UART配置void uart_init() { RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);}
// 发送AT指令并等待响应int send_at_command(char* command, char* response, uint32_t timeout) { // 发送命令 USART_SendData(USART1, (uint8_t*)command, strlen(command)); // 等待响应 uint32_t start_time = HAL_GetTick(); while ((HAL_GetTick() - start_time) < timeout) { if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET) { char c = USART_ReceiveData(USART1); // 检查是否收到了预期的响应 if (strstr(response, c) != NULL) { return 0; // 成功 } } } return -1; // 超时或没有收到预期的响应}
// 连接ESP8266到Wi-Fivoid connect_to_wifi() { char command[50]; char response[100]; // 设置Wi-Fi SSID和密码 sprintf(command, "AT+CWJAP=\"%s\",\"%s\"\r\n", "YourSSID", "YourPassword"); send_at_command(command, "OK", 5000);}
// 连接到NTP服务器并获取时间int get_ntp_time(uint32_t* time) { char response[100]; // 配置SNTP客户端 send_at_command("AT+CIPSNTPCFG=0,1,\"pool.ntp.org\"\r\n", "OK", 5000); // 获取时间 send_at_command("AT+CIPSNTPTIME?\r\n", response, 5000); // 解析响应并提取时间戳 char* token = strtok(response, ","); uint32_t timestamp = atoi(token); *time = timestamp - 2208988800UL; // 转换为Unix时间戳 return 0;}
// 将时间设置到RTCvoid set_rtc_time(uint32_t time) { // 启用PWR和BKP外设时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); // 解锁备份寄存器区域 PWR_BackupAccessCmd(ENABLE); // 配置RTC RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128); // RTC时钟源为HSE/128 RCC_RTCCLKCmd(ENABLE); // 启用RTC时钟 RTC_InitTypeDef RTC_InitStructure; // 配置RTC时钟 RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_InitStructure.RTC_AsynchPrediv = 127; RTC_InitStructure.RTC_SynchPrediv = 255; RTC_Init(&RTC_InitStructure);
// 设置RTC时间 RTC_TimeTypeDef RTC_TimeStruct; RTC_DateTypeDef RTC_DateStruct;
// 将Unix时间戳转换为RTC时间和日期 uint32_t days = time / 86400; uint32_t seconds = time % 86400; uint32_t hours = seconds / 3600; uint32_t minutes = (seconds % 3600) / 60; uint32_t secs = (seconds % 3600) % 60; uint32_t year = 1970; uint32_t month = 1; while (days > 365) { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { days -= 366; } else { days -= 365; } year++; } while (days > 0) { if (month == 2) { if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { if (days > 29) { days -= 29; } else { break; } } else { if (days > 28) { days -= 28; } else { break; } } } else if (month == 4 || month == 6 || month == 9 || month == 11) { if (days > 30) { days -= 30; } else { break; } } else { if (days > 31) { days -= 31; } else { break; } } month++; if (month > 12) { month = 1; year++; } }
RTC_TimeStruct.RTC_Hours = hours; RTC_TimeStruct.RTC_Minutes = minutes; RTC_TimeStruct.RTC_Seconds = secs; RTC_DateStruct.RTC_Date = days; RTC_DateStruct.RTC_Month = month; RTC_DateStruct.RTC_Year = year - 2000;
// 设置RTC时间和日期 RTC_SetTime(RTC_Format_BIN, &RTC_TimeStruct); RTC_SetDate(RTC_Format_BIN, &RTC_DateStruct); }
int main() { // 初始化UART串口 uart_init();
// 连接ESP8266到Wi-Fi connect_to_wifi();
// 获取NTP时间 uint32_t ntp_time; get_ntp_time(&ntp_time);
// 将时间设置到 RTC set_rtc_time(ntp_time);
while (1) { // 做其他的事情... } }
评论