- 1 #include "sys.h"
- 2
- 3 //时间结构体
- 4 typedef struct
- 5 {
- 6 vu8 hour;//vu8
- 7 vu8 min;
- 8 vu8 sec;
- 9 vu16 msec;
- 10
- 11 //公历日月年周
- 12 vu16 w_year;
- 13 vu8 w_month;
- 14 vu8 w_date;
- 15 vu8 week;
- 16 }_calendar_obj;
- 17
- 18 extern _calendar_obj calendar; //日历结构体
- 19 extern u8 const mon_table[12]; //月份日期数据表
- 20
- 21 u8 RTC_Init(void); //初始化RTC,返回0,失败;1,成功;
- 22 u8 Is_Leap_Year(u16 year);//平年,闰年判断
- 23
- 24 //u8 RTC_Alarm_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec);
- 25 u8 RTC_Get(void); //更新时间
- 26 u8 RTC_Get_Week(u16 year,u8 month,u8 day);
- 27 u8 RTC_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec);//设置时间
- rtc.h
- 1 #include "sys.h"
- 2 #include "delay.h"
- 3 #include "rtc.h"
- 4
- 5 _calendar_obj calendar;//时钟结构体
- 6
- 7 static void RTC_NVIC_Config(void)
- 8 {
- 9 NVIC_InitTypeDef NVIC_InitStructure;
- 10 NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; //RTC全局中断
- 11 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级1位,从优先级3位
- 12 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //先占优先级0位,从优先级4位
- 13 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能该通道中断
- 14 NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
- 15 }
- 16
- 17 //实时时钟配置
- 18 //初始化RTC时钟,同时检测时钟是否工作正常
- 19 //BKP->DR1用于保存是否第一次配置的设置
- 20 //返回0:正常
- 21 //其他:错误代码
- 22
- 23 u8 RTC_Init(void)
- 24 {
- 25 //检查是不是第一次配置时钟
- 26 u8 temp=0;
- 27 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP外设时钟
- 28 PWR_BackupAccessCmd(ENABLE); //使能后备寄存器访问
- 29 if (BKP_ReadBackupRegister(BKP_DR1) != 0x5051) //从指定的后备寄存器中读出数据:读出了与写入的指定数据不相乎
- 30 {
- 31 BKP_DeInit(); //复位备份区域
- 32 RCC_LSEConfig(RCC_LSE_ON); //设置外部低速晶振(LSE),使用外设低速晶振
- 33 while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET&&temp<250) //检查指定的RCC标志位设置与否,等待低速晶振就绪
- 34 {
- 35 temp++;
- 36 delay_ms(10);
- 37 }
- 38 if(temp>=250)return 1;//初始化时钟失败,晶振有问题
- 39 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //设置RTC时钟(RTCCLK),选择LSE作为RTC时钟
- 40 RCC_RTCCLKCmd(ENABLE); //使能RTC时钟
- 41 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成
- 42 RTC_WaitForSynchro(); //等待RTC寄存器同步
- 43 RTC_ITConfig(RTC_IT_SEC, ENABLE); //使能RTC秒中断
- 44 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成
- 45 RTC_EnterConfigMode();/// 允许配置
- 46 RTC_SetPrescaler(32767); //设置RTC预分频的值
- 47 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成
- 48 RTC_Set(2018,4,2,17,37,00); //设置时间
- 49 RTC_ExitConfigMode(); //退出配置模式
- 50 BKP_WriteBackupRegister(BKP_DR1, 0X5051); //向指定的后备寄存器中写入用户程序数据
- 51 }
- 52 else//系统继续计时
- 53 {
- 54
- 55 RTC_WaitForSynchro(); //等待最近一次对RTC寄存器的写操作完成
- 56 RTC_ITConfig(RTC_IT_SEC, ENABLE); //使能RTC秒中断
- 57 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成
- 58 }
- 59 RTC_NVIC_Config();//RCT中断分组设置
- 60 RTC_Get();//更新时间
- 61 return 0; //ok
- 62
- 63 }
- 64 //RTC时钟中断
- 65 //每秒触发一次
- 66 //extern u16 tcnt;
- 67 void RTC_IRQHandler(void)
- 68 {
- 69 // if (RTC_GetITStatus(RTC_IT_SEC) != RESET)//秒钟中断
- 70 // {
- 71 // RTC_Get();//更新时间
- 72 // }
- 73 // if(RTC_GetITStatus(RTC_IT_ALR)!= RESET)//闹钟中断
- 74 // {
- 75 // RTC_ClearITPendingBit(RTC_IT_ALR); //清闹钟中断
- 76 // RTC_Get(); //更新时间
- 77 // //printf("Alarm Time:%d-%d-%d %d:%d:%d\n",calendar.w_year,calendar.w_month,calendar.w_date,calendar.hour,calendar.min,calendar.sec);//输出闹铃时间
- 78 //
- 79 // }
- 80 RTC_ClearITPendingBit(RTC_IT_SEC|RTC_IT_OW); //清闹钟中断
- 81 RTC_WaitForLastTask();
- 82 }
- 83
- 84
- 85 //判断是否是闰年函数
- 86 //月份 1 2 3 4 5 6 7 8 9 10 11 12
- 87 //闰年 31 29 31 30 31 30 31 31 30 31 30 31
- 88 //非闰年 31 28 31 30 31 30 31 31 30 31 30 31
- 89 //输入:年份
- 90 //输出:该年份是不是闰年.1,是.0,不是
- 91 u8 Is_Leap_Year(u16 year)
- 92 {
- 93 if(year%4==0) //必须能被4整除
- 94 {
- 95 if(year%100==0)
- 96 {
- 97 if(year%400==0)return 1;//如果以00结尾,还要能被400整除
- 98 else return 0;
- 99 }else return 1;
- 100 }else return 0;
- 101 }
- 102
- 103
- 104 //设置时钟
- 105 //把输入的时钟转换为秒钟
- 106 //以1970年1月1日为基准
- 107 //1970~2099年为合法年份
- 108 //返回值:0,成功;其他:错误代码.
- 109 //月份数据表
- 110 const u8 table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //月修正数据表
- 111 const u8 mon_table[12]={31,28,31,30,31,30,31,31,30,31,30,31};//平年的月份日期表
- 112
- 113 u8 RTC_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec)
- 114 {
- 115 u16 t;
- 116 u32 seccount=0;
- 117 if(syear<1970||syear>2099)return 1;
- 118 for(t=1970;t<syear;t++) //把所有年份的秒钟相加
- 119 {
- 120 if(Is_Leap_Year(t))seccount+=31622400;//闰年的秒钟数
- 121 else seccount+=31536000; //平年的秒钟数
- 122 }
- 123 smon-=1;
- 124 for(t=0;t<smon;t++) //把前面月份的秒钟数相加
- 125 {
- 126 seccount+=(u32)mon_table[t]*86400;//月份秒钟数相加
- 127 if(Is_Leap_Year(syear)&&t==1)seccount+=86400;//闰年2月份增加一天的秒钟数
- 128 }
- 129 seccount+=(u32)(sday-1)*86400;//把前面日期的秒钟数相加
- 130 seccount+=(u32)hour*3600;//小时秒钟数
- 131 seccount+=(u32)min*60; //分钟秒钟数
- 132 seccount+=sec;//最后的秒钟加上去
- 133
- 134 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP外设时钟
- 135 PWR_BackupAccessCmd(ENABLE); //使能RTC和后备寄存器访问
- 136 RTC_SetCounter(seccount); //设置RTC计数器的值
- 137
- 138 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成
- 139 return 0;
- 140 }
- 141
- 142 //初始化闹钟
- 143 //以1970年1月1日为基准
- 144 //1970~2099年为合法年份
- 145 //syear,smon,sday,hour,min,sec:闹钟的年月日时分秒
- 146 //返回值:0,成功;其他:错误代码.
- 147 u8 RTC_Alarm_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec)
- 148 {
- 149 u16 t;
- 150 u32 seccount=0;
- 151 if(syear<1970||syear>2099)return 1;
- 152 for(t=1970;t<syear;t++) //把所有年份的秒钟相加
- 153 {
- 154 if(Is_Leap_Year(t))seccount+=31622400;//闰年的秒钟数
- 155 else seccount+=31536000; //平年的秒钟数
- 156 }
- 157 smon-=1;
- 158 for(t=0;t<smon;t++) //把前面月份的秒钟数相加
- 159 {
- 160 seccount+=(u32)mon_table[t]*86400;//月份秒钟数相加
- 161 if(Is_Leap_Year(syear)&&t==1)seccount+=86400;//闰年2月份增加一天的秒钟数
- 162 }
- 163 seccount+=(u32)(sday-1)*86400;//把前面日期的秒钟数相加
- 164 seccount+=(u32)hour*3600;//小时秒钟数
- 165 seccount+=(u32)min*60; //分钟秒钟数
- 166 seccount+=sec;//最后的秒钟加上去
- 167 //设置时钟
- 168 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP外设时钟
- 169 PWR_BackupAccessCmd(ENABLE); //使能后备寄存器访问
- 170 //上面三步是必须的!
- 171
- 172 RTC_SetAlarm(seccount);
- 173
- 174 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成
- 175
- 176 return 0;
- 177 }
- 178
- 179
- 180 //得到当前的时间
- 181 //返回值:0,成功;其他:错误代码.
- 182 u8 RTC_Get(void)
- 183 {
- 184 static u16 daycnt=0;
- 185 u32 timecount=0;
- 186 u32 temp=0;
- 187 u16 temp1=0;
- 188 timecount=RTC_GetCounter();
- 189 temp=timecount/86400; //得到天数(秒钟数对应的)
- 190 if(daycnt!=temp)//超过一天了
- 191 {
- 192 daycnt=temp;
- 193 temp1=1970; //从1970年开始
- 194 while(temp>=365)
- 195 {
- 196 if(Is_Leap_Year(temp1))//是闰年
- 197 {
- 198 if(temp>=366)temp-=366;//闰年的秒钟数
- 199 else {temp1++;break;}
- 200 }
- 201 else temp-=365; //平年
- 202 temp1++;
- 203 }
- 204 calendar.w_year=temp1;//得到年份
- 205 temp1=0;
- 206 while(temp>=28)//超过了一个月
- 207 {
- 208 if(Is_Leap_Year(calendar.w_year)&&temp1==1)//当年是不是闰年/2月份
- 209 {
- 210 if(temp>=29)temp-=29;//闰年的秒钟数
- 211 else break;
- 212 }
- 213 else
- 214 {
- 215 if(temp>=mon_table[temp1])temp-=mon_table[temp1];//平年
- 216 else break;
- 217 }
- 218 temp1++;
- 219 }
- 220 calendar.w_month=temp1+1; //得到月份
- 221 calendar.w_date=temp+1; //得到日期
- 222 }
- 223 temp=timecount%86400; //得到秒钟数
- 224 calendar.hour=temp/3600; //小时
- 225 calendar.min=(temp%3600)/60; //分钟
- 226 calendar.sec=(temp%3600)%60; //秒钟
- 227 calendar.week=RTC_Get_Week(calendar.w_year,calendar.w_month,calendar.w_date);//获取星期
- 228 calendar.msec=(32767-RTC_GetDivider())* 1000/32767;
- 229 return 0;
- 230 }
- 231
- 232
- 233 //获得现在是星期几
- 234 //功能描述:输入公历日期得到星期(只允许1901-2099年)
- 235 //输入参数:公历年月日
- 236 //返回值:星期号
- 237 u8 RTC_Get_Week(u16 year,u8 month,u8 day)
- 238 {
- 239 u16 temp2;
- 240 u8 yearH,yearL;
- 241
- 242 yearH=year/100; yearL=year%100;
- 243 // 如果为21世纪,年份数加100
- 244 if (yearH>19)yearL+=100;
- 245 // 所过闰年数只算1900年之后的
- 246 temp2=yearL+yearL/4;
- 247 temp2=temp2%7;
- 248 temp2=temp2+day+table_week[month-1];
- 249 if (yearL%4==0&&month<3)temp2--;
- 250 return(temp2%7);
- 251 }
- rtc.c