使用STM32的RTC功能,制作一份私人专属的日历,就SO简单了。不废话,上效果图:
鄙人通过串口输出时间到超级终端的(SecureCRT 5.5这货最好),如果配上TFT就制作一个简易的电子日历了。这个里面主要涉及RTC的初始化,时间数据的初始化输入,串口输出,还有就是公历时间和农历时间的转换处理。通过串口初始化RTC数据,RTC通过串口把时间显示出来,上代码:
工程结构图:
1、main.c如下:
#include"stm32f10x.h"
#include"beep.h"
#include"led.h"
#include"usart1.h"
#include"rtc.h"
int main(void)
{
USART_Config();
Beep_Init();
Beep_State(1,BeepOn);
Led_Init();
Led_Spark(LedAll,1,LedOn);
RTC_NVIC_Config(); //RTC嵌套中断向量初始化
RTC_Display();
}
2、beep led usart这些在博客其他文章里面出现过,这里就不提也罢。
3、RTC.c这是本文的重点了。
C文件如下:
#include"stm32f10x.h"
#include"rtc.h"
#include"usart1.h"
#include"date.h"
#include"calendar.h"
#include
u8 SecondFlag=0;
struct rtc_time systmtime;
u8 const *WEEK_STR[] = {"日", "一", "二", "三", "四", "五", "六"}; //这是一个指针数组的赋值 不是指向一个数组的指针 而是一个数组的每一个元素都是一个指针
//==============================================================================================1-RTC初始化部分
void RTC_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0000); //起始地址位于FLASH
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //优先级分组方式2
NVIC_InitStructure.NVIC_IRQChannel =RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0;
NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
static void RTC_Config(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR|RCC_APB1Periph_BKP,ENABLE);
PWR_BackupAccessCmd(ENABLE);
BKP_DeInit();
RCC_LSEConfig(RCC_LSE_ON);
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY)==RESET); //等待LSE准备就绪
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForSynchro();
RTC_ITConfig(RTC_IT_SEC,ENABLE);
RTC_WaitForLastTask();
RTC_SetPrescaler(32767);
RTC_WaitForLastTask();
}
//==============================================================================================2--设置时间部分
static u8 RTC_USART_Scanf(uint32_t value)
{
uint32_t index = 0;
uint32_t tmp[2] = {0, 0};
while (index < 2)
{
while (USART_GetFlagStatus(USART, USART_FLAG_RXNE) == RESET)
{}
tmp[index++] = (USART_ReceiveData(USART));
if ((tmp[index - 1] < 0x30) || (tmp[index - 1] > 0x39))
{
if((index == 2) && (tmp[index - 1] == )) //第一个输入的是数字,第二个是按的回车键的情况
{
tmp[1] = tmp[0];
tmp[0] = 0x30;
}
else
{
printf("Please enter valid number between 0 and 9 -->: ");
index--;
}
}
else
{
printf("%c", tmp[index - 1]); //输入一个显示一个数字
}
}
index = (tmp[1] - 0x30) + ((tmp[0] - 0x30) * 10);
if (index > value)
{
printf("Please enter valid number between 0 and %d -->: ", value);
return 0xFF;
}
return index;
}
static void RTC_Time_Setting(struct rtc_time *tm)
{
uint32_t DataIn = 0xFF;
printf("==============请设置时间==================");
printf(" 请输入年份(Please Set Years): 20");
while (DataIn == 0xFF)
{
DataIn = RTC_USART_Scanf(99);
}
printf(" 年份被设置为: 20%0.2d", DataIn);
tm->tm_year = DataIn+2000;
DataIn = 0xFF;
printf(" 请输入月份(Please Set Months): ");
while (DataIn == 0xFF)
{
DataIn = RTC_USART_Scanf(12);
}
printf(" 月份被设置为: %d", DataIn);
tm->tm_mon= DataIn;
DataIn = 0xFF;
printf(" 请输入日期(Please Set Dates): ");
while (DataIn == 0xFF)
{
DataIn = RTC_USART_Scanf(31);
}
printf(" 日期被设置为: %d", DataIn);
tm->tm_mday= DataIn;
DataIn = 0xFF;
printf(" 请输入时钟(Please Set Hours): ");
while (DataIn == 0xFF)
{
DataIn = RTC_USART_Scanf(23);
}
printf(" 时钟被设置为: %d", DataIn);
tm->tm_hour= DataIn;
DataIn = 0xFF;
printf(" 请输入分钟(Please Set Minutes): ");
while (DataIn == 0xFF)
{
DataIn = RTC_USART_Scanf(59);
}
printf(" 分钟被设置为: %d", DataIn);
tm->tm_min= DataIn;
DataIn = 0xFF;
printf(" 请输入秒钟(Please Set Seconds): ");
while (DataIn == 0xFF)
{
DataIn = RTC_USART_Scanf(59);
}
printf(" 秒钟被设置为: %d", DataIn);
tm->tm_sec= DataIn;
}