写一个stm32f103c8t6 用标准库函数实现rtc 实时时钟串口输出的代码
时间: 2023-06-05 19:06:57 浏览: 287
好的,以下是实现rtc实时时钟串口输出的代码:
```c
#include "stm32f10x.h"
#include <stdio.h>
void USART1_Init(void) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStruct);
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Tx;
USART_Init(USART1, &USART_InitStruct);
USART_Cmd(USART1, ENABLE);
}
void RTC_Init(void) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
if ((RCC->BDCR & RCC_BDCR_RTCEN) != RCC_BDCR_RTCEN) {
RCC_LSEConfig(RCC_LSE_ON);
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {
}
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RTC_InitTypeDef RTC_InitStruct;
RTC_StructInit(&RTC_InitStruct);
RTC_InitStruct.RTC_AsynchPrediv = 0x7F;
RTC_InitStruct.RTC_SynchPrediv = 0xFF;
RTC_InitStruct.RTC_HourFormat = RTC_HourFormat_24;
RTC_Init(&RTC_InitStruct);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
}
}
void RTC_SetDateTime(uint8_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second) {
RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;
RTC_TimeStruct.RTC_Hours = hour;
RTC_TimeStruct.RTC_Minutes = minute;
RTC_TimeStruct.RTC_Seconds = second;
RTC_TimeStruct.RTC_H12 = RTC_H12_AM;
RTC_DateStruct.RTC_Year = year;
RTC_DateStruct.RTC_Month = month;
RTC_DateStruct.RTC_Date = day;
RTC_DateStruct.RTC_WeekDay = RTC_Weekday_Monday;
RTC_SetTime(RTC_Format_BIN, &RTC_TimeStruct);
RTC_SetDate(RTC_Format_BIN, &RTC_DateStruct);
}
void USART1_SendChar(char c) {
while ((USART1->SR & USART_SR_TXE) == RESET) {
}
USART_SendData(USART1, c);
}
int USART1_SendString(const char* str, uint32_t timeout) {
int i = 0;
while (*str) {
USART1_SendChar(*str++);
i++;
if (timeout != 0xFFFFFFFF) {
uint32_t t = timeout;
while ((USART1->SR & USART_SR_TXE) == RESET) {
if ((t--) == 0) {
return -1;
}
}
}
}
return i;
}
int main(void) {
USART1_Init();
RTC_Init();
RTC_SetDateTime(21, 6, 30, 12, 0, 0);
char buf[32];
while (1) {
RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;
RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct);
RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d\r\n", RTC_DateStruct.RTC_Year, RTC_DateStruct.RTC_Month, RTC_DateStruct.RTC_Date, RTC_TimeStruct.RTC_Hours, RTC_TimeStruct.RTC_Minutes, RTC_TimeStruct.RTC_Seconds);
USART1_SendString(buf, 1000000);
for (int i = 0; i < 1000000; i++) {
__NOP();
}
}
}
```
阅读全文