前言
之前用HMI USART串口屏做了一些东西需要用到闹钟,现在总结一下支持RTC和不支持RTC的屏幕设计时钟吧ovo。
一、RTC是什么?
从串口屏的指令集里我们可以看到,串口屏存在rtc0-rtc67个时钟变量,分别代表年,月,日,时,分,秒,星期,其中rtc6(星期)为只读不可赋值,根据当前的年月日自动计算生成,如果你买的串口屏带RTC的硬件那么设计闹钟就非常简单,在某宝买的时候记得留意一下是否支持RTC,当然如果不带RTC硬件那么可以依靠其他带RTC的外设(如STM32,时钟产生模块)也可以实现。
二、设计步骤
1.支持RTC
这里以X570_011为例:
首先设计好主界面:
?
然后设计时钟设置界面:
?
?最后就是代码设计:
页面前初始化程序:
//页面前初始化程序
//年
covx rtc0,vah.txt,0,0
covx rtc1,vam.txt,0,0
covx rtc2,vas.txt,0,0
t2.txt=vah.txt+" / "+vam.txt+" / "+vas.txt
//星期
if(rtc6==0)
{
t1.txt="星期日"
}else if(rtc6==1)
{
t1.txt="星期一"
}else if(rtc6==2)
{
t1.txt="星期二"
}else if(rtc6==3)
{
t1.txt="星期三"
}else if(rtc6==4)
{
t1.txt="星期四"
}else if(rtc6==5)
{
t1.txt="星期五"
}else if(rtc6==6)
{
t1.txt="星期六"
}
//时间
covx rtc3,vah.txt,2,0
covx rtc4,vam.txt,2,0
covx rtc5,vas.txt,2,0
t0.txt=vah.txt+":"+vam.txt+":"+vas.txt
//填充表盘
sys0=rtc5*360/60
if(sys0<270)
{
zs.val=sys0+90
}else
{
zs.val=sys0-270
}
sys0=rtc4*360/60
if(sys0<270)
{
zm.val=sys0+90
}else
{
zm.val=sys0-270
}
sys0=rtc3*60+rtc4*720/1440
if(sys0>360)
{
sys0-=360
}
if(sys0<270)
{
zh.val=sys0+90
}else
{
zh.val=sys0-270
}
bt1.val=main.bt1.val
?触碰日期位置弹起事件:
year.x=285
month.x=433
day.x=582
noon.x=1285
hour.x=1433
minute.x=1582
b0.x=284
b2.x=508
t5.x=233
t4.x=217
va0.val=1
tm0.en=0
tm1.en=0
?触碰时间位置弹起事件:
noon.x=285
hour.x=433
minute.x=582
year.x=1285
month.x=1433
day.x=1582
b0.x=284
b2.x=508
t6.x=233
t4.x=217
va0.val=0
tm0.en=0
tm1.en=0
确定设置弹起事件:
if(va0.val==1)
{
rtc0=year.val+2000
rtc1=month.val
rtc2=day.val
}else
{
if(noon.val==0)
{
rtc3=hour.val
rtc4=minute.val
}else
{
rtc3=hour.val+12
rtc4=minute.val
}
}
year.x=1285
month.x=1433
day.x=1582
noon.x=1285
hour.x=1433
minute.x=1582
b0.x=1284
b2.x=1508
t5.x=1233
t6.x=1233
t4.x=1217
tm0.en=1
tm1.en=1
2.不支持RTC
这里以X343_011为例:
首先设计好主界面:
然后设计时钟设置界面:
最后是STM32F4程序代码编写了:
//rtc.c
#include "rtc.h"
#include "key.h"
#include "usart.h"
#include "ds18b20.h"
#define FEBRUARY 2
#define STARTOFTIME 1970
#define SECDAY 86400L /* 一天有多少s */
#define SECYR (SECDAY * 365)
#define leapyear(year) ((year) % 4 == 0)
#define days_in_year(a) (leapyear(a) ? 366 : 365)
#define days_in_month(a) (month_days[(a) - 1])
//static int month_days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//
//本程序只供学习使用,未经作者许可,不得用于其它任何用途
//ALIENTEK STM32F407开发板
//RTC 驱动代码
//正点原子@ALIENTEK
//技术论坛:www.openedv.com
//创建日期:2014/5/5
//版本:V1.1
//版权所有,盗版必究。
//Copyright(C) 广州市星翼电子科技有限公司 2014-2024
//All rights reserved
//********************************************************************************
//修改说明
//V1.1 20140726
//新增:RTC_Get_Week函数,用于根据年月日信息,得到星期信息.
//
NVIC_InitTypeDef NVIC_InitStructure;
//RTC时间设置
//hour,min,sec:小时,分钟,秒钟
//ampm:@RTC_AM_PM_Definitions :RTC_H12_AM/RTC_H12_PM
//返回值:SUCEE(1),成功
// ERROR(0),进入初始化模式失败
ErrorStatus RTC_Set_Time(u8 hour,u8 min,u8 sec,u8 ampm)
{
RTC_TimeTypeDef RTC_TimeTypeInitStructure;
RTC_TimeTypeInitStructure.RTC_Hours=hour;
RTC_TimeTypeInitStructure.RTC_Minutes=min;
RTC_TimeTypeInitStructure.RTC_Seconds=sec;
RTC_TimeTypeInitStructure.RTC_H12=ampm;
return RTC_SetTime(RTC_Format_BIN,&RTC_TimeTypeInitStructure);
}
//RTC日期设置
//year,month,date:年(0~99),月(1~12),日(0~31)
//week:星期(1~7,0,非法!)
//返回值:SUCEE(1),成功
// ERROR(0),进入初始化模式失败
ErrorStatus RTC_Set_Date(u8 year,u8 month,u8 date,u8 week)
{
RTC_DateTypeDef RTC_DateTypeInitStructure;
RTC_DateTypeInitStructure.RTC_Date=date;
RTC_DateTypeInitStructure.RTC_Month=month;
RTC_DateTypeInitStructure.RTC_WeekDay=week;
RTC_DateTypeInitStructure.RTC_Year=year;
return RTC_SetDate(RTC_Format_BIN,&RTC_DateTypeInitStructure);
}
//RTC初始化
//返回值:0,初始化成功;
// 1,LSE开启失败;
// 2,进入初始化模式失败;
u8 My_RTC_Init(void)
{
RTC_InitTypeDef RTC_InitStructure;
u16 retry=0X1FFF;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);//使能PWR时钟
PWR_BackupAccessCmd(ENABLE); //使能后备寄存器访问
if(RTC_ReadBackupRegister(RTC_BKP_DR0)!=0x5050) //是否第一次配置?
{
RCC_LSEConfig(RCC_LSE_ON);//LSE 开启
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) //检查指定的RCC标志位设置与否,等待低速晶振就绪
{
retry++;
delay_ms(10);
}
if(retry==0)return 1; //LSE 开启失败.
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //设置RTC时钟(RTCCLK),选择LSE作为RTC时钟
RCC_RTCCLKCmd(ENABLE); //使能RTC时钟
RTC_InitStructure.RTC_AsynchPrediv = 0x7F;//RTC异步分频系数(1~0X7F)
RTC_InitStructure.RTC_SynchPrediv = 0xFF;//RTC同步分频系数(0~7FFF)
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;//RTC设置为,24小时格式
RTC_Init(&RTC_InitStructure);
RTC_Set_Time(20,42,40,RTC_H12_AM); //设置时间
RTC_Set_Date(20,8,13,4); //设置日期
RTC_WriteBackupRegister(RTC_BKP_DR0,0x5050); //标记已经初始化过了
}
return 0;
}
//设置闹钟时间(按星期闹铃,24小时制)
//week:星期几(1~7) @ref RTC_Alarm_Definitions
//hour,min,sec:小时,分钟,秒钟
void RTC_Set_AlarmA(u8 week,u8 hour,u8 min,u8 sec)
{
EXTI_InitTypeDef EXTI_InitStructure;
RTC_AlarmTypeDef RTC_AlarmTypeInitStructure;
RTC_TimeTypeDef RTC_TimeTypeInitStructure;
RTC_AlarmCmd(RTC_Alarm_A,DISABLE);//关闭闹钟A
RTC_TimeTypeInitStructure.RTC_Hours=hour;//小时
RTC_TimeTypeInitStructure.RTC_Minutes=min;//分钟
RTC_TimeTypeInitStructure.RTC_Seconds=sec;//秒
RTC_TimeTypeInitStructure.RTC_H12=RTC_H12_AM;
RTC_AlarmTypeInitStructure.RTC_AlarmDateWeekDay=week;//星期
RTC_AlarmTypeInitStructure.RTC_AlarmDateWeekDaySel=RTC_AlarmDateWeekDaySel_WeekDay;//按星期闹
RTC_AlarmTypeInitStructure.RTC_AlarmMask=RTC_AlarmMask_None;//精确匹配星期,时分秒
RTC_AlarmTypeInitStructure.RTC_AlarmTime=RTC_TimeTypeInitStructure;
RTC_SetAlarm(RTC_Format_BIN,RTC_Alarm_A,&RTC_AlarmTypeInitStructure);
RTC_ClearITPendingBit(RTC_IT_ALRA);//清除RTC闹钟A的标志
EXTI_ClearITPendingBit(EXTI_Line17);//清除LINE17上的中断标志位
RTC_ITConfig(RTC_IT_ALRA,ENABLE);//开启闹钟A中断
RTC_AlarmCmd(RTC_Alarm_A,ENABLE);//开启闹钟A
EXTI_InitStructure.EXTI_Line = EXTI_Line17;//LINE17
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中断事件
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //上升沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//使能LINE17
EXTI_Init(&EXTI_InitStructure);//配置
NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;//抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
NVIC_Init(&NVIC_InitStructure);//配置
}
//周期性唤醒定时器设置
/*wksel: @ref RTC_Wakeup_Timer_Definitions
#define RTC_WakeUpClock_RTCCLK_Div16 ((uint32_t)0x00000000)
#define RTC_WakeUpClock_RTCCLK_Div8 ((uint32_t)0x00000001)
#define RTC_WakeUpClock_RTCCLK_Div4 ((uint32_t)0x00000002)
#define RTC_WakeUpClock_RTCCLK_Div2 ((uint32_t)0x00000003)
#define RTC_WakeUpClock_CK_SPRE_16bits ((uint32_t)0x00000004)
#define RTC_WakeUpClock_CK_SPRE_17bits ((uint32_t)0x00000006)
*/
//cnt:自动重装载值.减到0,产生中断.
void RTC_Set_WakeUp(u32 wksel,u16 cnt)
{
EXTI_InitTypeDef EXTI_InitStructure;
RTC_WakeUpCmd(DISABLE);//关闭WAKE UP
RTC_WakeUpClockConfig(wksel);//唤醒时钟选择
RTC_SetWakeUpCounter(cnt);//设置WAKE UP自动重装载寄存器
RTC_ClearITPendingBit(RTC_IT_WUT); //清除RTC WAKE UP的标志
EXTI_ClearITPendingBit(EXTI_Line22);//清除LINE22上的中断标志位
RTC_ITConfig(RTC_IT_WUT,ENABLE);//开启WAKE UP 定时器中断
RTC_WakeUpCmd( ENABLE);//开启WAKE UP 定时器
EXTI_InitStructure.EXTI_Line = EXTI_Line22;//LINE22
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//中断事件
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //上升沿触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE;//使能LINE22
EXTI_Init(&EXTI_InitStructure);//配置
NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;//抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;//子优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能外部中断通道
NVIC_Init(&NVIC_InitStructure);//配置
}
//RTC闹钟中断服务函数
void RTC_Alarm_IRQHandler(void)
{
if(RTC_GetFlagStatus(RTC_FLAG_ALRAF)==SET)//ALARM A中断
{
RTC_ClearFlag(RTC_FLAG_ALRAF);//清除中断标志
printf("ALARM A!\r\n");
}
EXTI_ClearITPendingBit(EXTI_Line17); //清除中断线17的中断标志
}
//RTC WAKE UP中断服务函数
void RTC_WKUP_IRQHandler(void)
{
if(RTC_GetFlagStatus(RTC_FLAG_WUTF)==SET)//WK_UP中断
{
RTC_ClearFlag(RTC_FLAG_WUTF); //清除中断标志
}
EXTI_ClearITPendingBit(EXTI_Line22);//清除中断线22的中断标志
}
/*计算公历*/
int GregorianDay(struct rtc_time tm)
{
int leapsToDate;
int lastYear;
int day;
int MonthOffset[] = { 0,31,59,90,120,151,181,212,243,273,304,334 };
lastYear=tm.tm_year-1;
/*计算从公元元年到计数的前一年之中一共经历了多少个闰年*/
leapsToDate = lastYear/4 - lastYear/100 + lastYear/400;
/*如若计数的这一年为闰年,且计数的月份在2月之后,则日数加1,否则不加1*/
if((tm.tm_year%4==0) &&
((tm.tm_year%100!=0) || (tm.tm_year%400==0)) &&
(tm.tm_mon>2)) {
/*
* We are past Feb. 29 in a leap year
*/
day=1;
} else {
day=0;
}
day += lastYear*365 + leapsToDate + MonthOffset[tm.tm_mon-1] + tm.tm_mday; /*计算从公元元年元旦到计数日期一共有多少天*/
tm.tm_wday=day%7;
return tm.tm_wday;
}
void week_show(void)
{
RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;
u8 tbuf[40];
u8 zero=0;
RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct);
RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
if(RTC_TimeStruct.RTC_Hours<5&&RTC_TimeStruct.RTC_Hours>=zero)
{
switch(RTC_DateStruct.RTC_WeekDay)
{
case 1:sprintf((char*)tbuf,"周一凌晨"); break;
case 2:sprintf((char*)tbuf,"周二凌晨"); break;
case 3:sprintf((char*)tbuf,"周三凌晨"); break;
case 4:sprintf((char*)tbuf,"周四凌晨"); break;
case 5:sprintf((char*)tbuf,"周五凌晨"); break;
case 6:sprintf((char*)tbuf,"周六凌晨"); break;
case 7:sprintf((char*)tbuf,"周日凌晨"); break;
}
}
else if(RTC_TimeStruct.RTC_Hours<8&&RTC_TimeStruct.RTC_Hours>=5)
{
switch(RTC_DateStruct.RTC_WeekDay)
{
case 1:sprintf((char*)tbuf,"周一早上"); break;
case 2:sprintf((char*)tbuf,"周二早上"); break;
case 3:sprintf((char*)tbuf,"周三早上"); break;
case 4:sprintf((char*)tbuf,"周四早上"); break;
case 5:sprintf((char*)tbuf,"周五早上"); break;
case 6:sprintf((char*)tbuf,"周六早上"); break;
case 7:sprintf((char*)tbuf,"周日早上"); break;
}
}
else if(RTC_TimeStruct.RTC_Hours>=8&&RTC_TimeStruct.RTC_Hours<11)
{
switch(RTC_DateStruct.RTC_WeekDay)
{
case 1:sprintf((char*)tbuf,"周一上午"); break;
case 2:sprintf((char*)tbuf,"周二上午"); break;
case 3:sprintf((char*)tbuf,"周三上午"); break;
case 4:sprintf((char*)tbuf,"周四上午"); break;
case 5:sprintf((char*)tbuf,"周五上午"); break;
case 6:sprintf((char*)tbuf,"周六上午"); break;
case 7:sprintf((char*)tbuf,"周日上午"); break;
}
}
else if(RTC_TimeStruct.RTC_Hours>=11&&RTC_TimeStruct.RTC_Hours<13)
{
switch(RTC_DateStruct.RTC_WeekDay)
{
case 1:sprintf((char*)tbuf,"周一中午"); break;
case 2:sprintf((char*)tbuf,"周二中午"); break;
case 3:sprintf((char*)tbuf,"周三中午"); break;
case 4:sprintf((char*)tbuf,"周四中午"); break;
case 5:sprintf((char*)tbuf,"周五中午"); break;
case 6:sprintf((char*)tbuf,"周六中午"); break;
case 7:sprintf((char*)tbuf,"周日中午"); break;
}
}
else if(RTC_TimeStruct.RTC_Hours>=13&&RTC_TimeStruct.RTC_Hours<17)
{
switch(RTC_DateStruct.RTC_WeekDay)
{
case 1:sprintf((char*)tbuf,"周一下午"); break;
case 2:sprintf((char*)tbuf,"周二下午"); break;
case 3:sprintf((char*)tbuf,"周三下午"); break;
case 4:sprintf((char*)tbuf,"周四下午"); break;
case 5:sprintf((char*)tbuf,"周五下午"); break;
case 6:sprintf((char*)tbuf,"周六下午"); break;
case 7:sprintf((char*)tbuf,"周日下午"); break;
}
}
else if(RTC_TimeStruct.RTC_Hours>=17&&RTC_TimeStruct.RTC_Hours<19)
{
switch(RTC_DateStruct.RTC_WeekDay)
{
case 1:sprintf((char*)tbuf,"周一晚上"); break;
case 2:sprintf((char*)tbuf,"周二晚上"); break;
case 3:sprintf((char*)tbuf,"周三晚上"); break;
case 4:sprintf((char*)tbuf,"周四晚上"); break;
case 5:sprintf((char*)tbuf,"周五晚上"); break;
case 6:sprintf((char*)tbuf,"周六晚上"); break;
case 7:sprintf((char*)tbuf,"周日晚上"); break;
}
}
else if(RTC_TimeStruct.RTC_Hours>=19&&RTC_TimeStruct.RTC_Hours<20)
{
switch(RTC_DateStruct.RTC_WeekDay)
{
case 1:sprintf((char*)tbuf,"周一半夜"); break;
case 2:sprintf((char*)tbuf,"周二半夜"); break;
case 3:sprintf((char*)tbuf,"周三半夜"); break;
case 4:sprintf((char*)tbuf,"周四半夜"); break;
case 5:sprintf((char*)tbuf,"周五半夜"); break;
case 6:sprintf((char*)tbuf,"周六半夜"); break;
case 7:sprintf((char*)tbuf,"周日半夜"); break;
}
}
else if(RTC_TimeStruct.RTC_Hours>=20&&RTC_TimeStruct.RTC_Hours<24)
{
switch(RTC_DateStruct.RTC_WeekDay)
{
case 1:sprintf((char*)tbuf,"周一深夜"); break;
case 2:sprintf((char*)tbuf,"周二深夜"); break;
case 3:sprintf((char*)tbuf,"周三深夜"); break;
case 4:sprintf((char*)tbuf,"周四深夜"); break;
case 5:sprintf((char*)tbuf,"周五深夜"); break;
case 6:sprintf((char*)tbuf,"周六深夜"); break;
case 7:sprintf((char*)tbuf,"周日深夜"); break;
}
}
printf("week.txt=\x22%s\x22\xff\xff\xff",tbuf);
}
void rtc_show_set(void)
{
RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;
u8 tbuf[40];
u8 t=0,seconds=0,hour=0,day=0;
struct rtc_time tm;
while(page==2)
{
t++;
if((t%10)==0) //每100ms更新一次显示数据
{
RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct);
RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
if(seconds!=RTC_TimeStruct.RTC_Seconds)
{
if(seconds!=RTC_TimeStruct.RTC_Seconds)
{
sprintf((char*)tbuf,"%02d:%02d:%02d",RTC_TimeStruct.RTC_Hours,RTC_TimeStruct.RTC_Minutes,RTC_TimeStruct.RTC_Seconds);
printf("time.txt=\x22%s\x22\xff\xff\xff",tbuf);
printf("hour.txt=\x22%02d\x22\xff\xff\xff",RTC_TimeStruct.RTC_Hours);
printf("minute.txt=\x22%02d\x22\xff\xff\xff",RTC_TimeStruct.RTC_Minutes);
printf("second.txt=\x22%02d\x22\xff\xff\xff",RTC_TimeStruct.RTC_Seconds);
seconds = RTC_TimeStruct.RTC_Seconds;
}
}
if(RTC_TimeStruct.RTC_Hours-hour>=1)
{
week_show();
hour=RTC_TimeStruct.RTC_Hours;
}
if(RTC_DateStruct.RTC_Date-day>=1)
{
sprintf((char*)tbuf,"20%02d.%02d.%02d",RTC_DateStruct.RTC_Year,RTC_DateStruct.RTC_Month,RTC_DateStruct.RTC_Date);
printf("data.txt=\x22%s\x22\xff\xff\xff",tbuf);
printf("year.txt=\x22 20%02d\x22\xff\xff\xff",RTC_DateStruct.RTC_Year);
printf("month.txt=\x22%02d\x22\xff\xff\xff",RTC_DateStruct.RTC_Month);
printf("day.txt=\x22%02d\x22\xff\xff\xff",RTC_DateStruct.RTC_Date);
day=RTC_DateStruct.RTC_Date;
}
}
}
if(page==3)
{
while(page==3);
}
if(page==4)
{
while(page==4);
}
if(page==5)
{
while(page==5)
{
if(time_flag==3)
{
tm.tm_year=timebuf[0];
tm.tm_mon=timebuf[1];
tm.tm_mday=timebuf[2];
tm.tm_wday=GregorianDay(tm);
RTC_Set_Date(tm.tm_year,tm.tm_mon,tm.tm_mday,tm.tm_wday); //设置日期
time_flag=0;
page=2;
break;
}
}
}
if(page==6)
{
while(page==6)
{
if(time_flag==3)
{
tm.tm_min=timebuf[1];
tm.tm_sec=timebuf[2];
tm.tm_wday=GregorianDay(tm);
if(tm.tm_sec==0)
{
tm.tm_hour=timebuf[0];
RTC_Set_Time(tm.tm_hour,tm.tm_min,tm.tm_sec,RTC_H12_AM); //设置时间
}
else
{
tm.tm_hour=timebuf[0]+12;
RTC_Set_Time(tm.tm_hour,tm.tm_min,tm.tm_sec,RTC_H12_PM); //设置时间
}
time_flag=0;
page=2;
break;
}
}
}
}
void main_interface_show(void)
{
RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;
u8 tbuf[40];
u8 t=0,seconds=0,hour=0,day=0;
short temperature;
while(page==1)
{
t++;
if((t%10)==0) //每100ms更新一次显示数据
{
RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct);
RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
sprintf((char*)tbuf,"%02d:%02d:%02d",RTC_TimeStruct.RTC_Hours,RTC_TimeStruct.RTC_Minutes,RTC_TimeStruct.RTC_Seconds);
if(temperature!=DS18B20_Get_Temp())
{
temperature=DS18B20_Get_Temp();
printf("t11.txt=\x22%d℃\x22\xff\xff\xff",temperature/10);
}
if(seconds!=RTC_TimeStruct.RTC_Seconds)
{
if(seconds!=RTC_TimeStruct.RTC_Seconds)
printf("time.txt=\x22%s\x22\xff\xff\xff",tbuf);
seconds = RTC_TimeStruct.RTC_Seconds;
}
if(RTC_TimeStruct.RTC_Hours-hour>=1)
{
week_show();
hour=RTC_TimeStruct.RTC_Hours;
}
if(RTC_DateStruct.RTC_Date-day>=1)
{
sprintf((char*)tbuf,"%02d月%02d日",RTC_DateStruct.RTC_Month,RTC_DateStruct.RTC_Date);
printf("data.txt=\x22%s\x22\xff\xff\xff",tbuf);
day=RTC_DateStruct.RTC_Date;
}
}
}
}
//主函数
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(9600); //串口初始化为9600
delay_init(168);
KEY_Init();
My_RTC_Init(); //初始化RTC
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
RTC_Set_WakeUp(RTC_WakeUpClock_CK_SPRE_16bits,0); //配置WAKE UP中断,1秒钟中断一次
while(1)
{
switch(page)
{
case 0:
break;
case 1:
main_interface_show();break;//显示屏显示时间
case 2:rtc_show_set();break;//显示屏时间设置
}
}
}
总结
两种时钟设置方法就介绍到这,需要工程的同学可以参考我设计的两个例程,链接我等我资源审核通过后会放在下面。
不支持RTC代码工程:基于STM32F407的HMI USART串口屏智能药箱设计
支持RTC代码工程:HMI USART串口屏步进电机控制.zip
|