STM32实现万年历代码
时间: 2023-10-26 22:20:00 浏览: 185
万年历程序_stm32万年历_
5星 · 资源好评率100%
以下是基于STM32的万年历代码,实现了年、月、日、星期的显示和计算:
```c
#include "stm32f10x.h"
#include "stdio.h"
void Delay(__IO uint32_t nCount) {
while(nCount--) {
}
}
void USART1_Init(void) {
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
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_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void USART1_Send(char *pucBuffer) {
while (*pucBuffer) {
USART_SendData(USART1, (uint16_t) *pucBuffer++);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
void RTC_Configuration(void) {
ErrorStatus HSEStartUpStatus;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
BKP_DeInit();
if (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET) {
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if (HSEStartUpStatus == SUCCESS) {
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while (RCC_GetSYSCLKSource() != 0x08);
}
}
RCC_LSEConfig(RCC_LSE_ON);
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
RTC_WaitForLastTask();
RTC_SetPrescaler(32767);
RTC_WaitForLastTask();
}
uint8_t RTC_Init(void) {
RTC_Configuration();
RTC_WaitForLastTask();
RTC_EnterConfigMode();
RTC_WaitForLastTask();
RTC_SetCounter(0);
RTC_WaitForLastTask();
RTC_SetAlarm(86400);
RTC_WaitForLastTask();
RTC_ExitConfigMode();
RTC_WaitForLastTask();
return 0;
}
uint8_t RTC_Check(void) {
if (RTC_GetFlagStatus(RTC_FLAG_SEC) == SET) {
RTC_ClearFlag(RTC_FLAG_SEC);
return 1;
}
return 0;
}
uint32_t RTC_GetCounter(void) {
uint32_t counter = 0;
counter = RTC_GetCounter();
return counter;
}
uint8_t RTC_GetHour(void) {
uint32_t counter = 0;
uint8_t hour = 0;
counter = RTC_GetCounter();
hour = (counter / 3600) % 24;
return hour;
}
uint8_t RTC_GetMinute(void) {
uint32_t counter = 0;
uint8_t minute = 0;
counter = RTC_GetCounter();
minute = (counter / 60) % 60;
return minute;
}
uint8_t RTC_GetSecond(void) {
uint32_t counter = 0;
uint8_t second = 0;
counter = RTC_GetCounter();
second = counter % 60;
return second;
}
uint8_t RTC_GetYear(void) {
uint32_t counter = 0;
uint16_t year = 0;
counter = RTC_GetCounter();
year = 1970 + counter / 31536000;
return year;
}
uint8_t RTC_GetMonth(void) {
uint32_t counter = 0;
uint8_t month = 0;
counter = RTC_GetCounter();
month = ((counter % 31536000) / 2592000) + 1;
return month;
}
uint8_t RTC_GetDay(void) {
uint32_t counter = 0;
uint8_t day = 0;
counter = RTC_GetCounter();
day = ((counter % 31536000) % 2592000) / 86400 + 1;
return day;
}
uint8_t RTC_GetWeekday(void) {
uint32_t counter = 0;
uint8_t weekday = 0;
counter = RTC_GetCounter();
weekday = ((counter / 86400) + 4) % 7;
return weekday;
}
int main(void) {
char Buffer[80];
uint8_t year, month, day, weekday, hour, minute, second;
USART1_Init();
RTC_Init();
while (1) {
if (RTC_Check()) {
year = RTC_GetYear();
month = RTC_GetMonth();
day = RTC_GetDay();
weekday = RTC_GetWeekday();
hour = RTC_GetHour();
minute = RTC_GetMinute();
second = RTC_GetSecond();
sprintf(Buffer, "Date: %d/%d/%d, Weekday: %d, Time: %d:%d:%d\r\n", year, month, day, weekday, hour, minute, second);
USART1_Send(Buffer);
}
Delay(0xFFFFF);
}
}
```
该代码使用STM32的RTC模块实现了时间的计算,并将年、月、日、星期和时间通过USART串口通信输出。其中,RTC_Configuration()函数用于配置STM32的时钟源和RTC模块,RTC_Init()函数用于初始化RTC模块,RTC_Check()函数用于检测RTC计数器是否发生溢出,RTC_GetCounter()函数用于获取RTC计数器的值,RTC_GetHour()、RTC_GetMinute()和RTC_GetSecond()函数用于分别获取当前的小时、分钟和秒数,RTC_GetYear()、RTC_GetMonth()和RTC_GetDay()函数用于分别获取当前的年、月和日,RTC_GetWeekday()函数用于获取当前的星期。
阅读全文