rtc.c
cpp
#include "rtc.h"
#define BKP_VALUE 0x32F0
rtc_parameter_struct rtc_initpara;
__IO uint32_t prescaler_a = 0, prescaler_s = 0;
uint32_t RTCSRC_FLAG = 0;
ErrStatus RtcInit(void)
{
rcu_periph_clock_enable(RCU_PMU); /* enable PMU clock */
pmu_backup_write_enable();/* enable the access of the RTC registers */
rtc_pre_config();//配置外部低速时钟32.768KHz
RTCSRC_FLAG = GET_BITS(RCU_BDCTL, 8, 9);/* get RTC clock entry selection */
if ((BKP_VALUE != RTC_BKP0) || (0x00 == RTCSRC_FLAG))/* check if RTC has aready been configured */
{
return rtc_setup();
}
return SUCCESS;
}
void rtc_pre_config(void)
{
rcu_osci_on(RCU_LXTAL);
rcu_osci_stab_wait(RCU_LXTAL);
rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);
prescaler_s = 0xFF;
prescaler_a = 0x7F;
rcu_periph_clock_enable(RCU_RTC);
rtc_register_sync_wait();
}
ErrStatus rtc_setup(void)
{
rtc_initpara.factor_asyn = prescaler_a;
rtc_initpara.factor_syn = prescaler_s;
rtc_initpara.year = 0x24;//2024年
rtc_initpara.day_of_week = RTC_MONDAY;//星期一
rtc_initpara.month = RTC_FEB;//二月
rtc_initpara.date = 0x05;//五日
rtc_initpara.display_format = RTC_24HOUR;
rtc_initpara.am_pm = RTC_AM;
rtc_initpara.hour = 0x09;//9点
rtc_initpara.minute = 0x52;//52分
rtc_initpara.second = 0x00;//0秒
if(ERROR == rtc_init(&rtc_initpara))
{
return ERROR;
}
else
{
RTC_BKP0 = BKP_VALUE;
}
return SUCCESS;
}
void rtc_show_time(void)
{
rtc_current_time_get(&rtc_initpara);
printf("Current date: %0.2x/%0.2x/%0.2x \r\n", \
rtc_initpara.year, rtc_initpara.month, rtc_initpara.date);
printf("Current time: %0.2x:%0.2x:%0.2x \r\n", \
rtc_initpara.hour, rtc_initpara.minute, rtc_initpara.second);
}
rtc.h
cpp
#ifndef __RTC_H
#define __RTC_H
#include "head.h"
ErrStatus RtcInit(void);
void rtc_show_time(void);
void rtc_pre_config(void);
ErrStatus rtc_setup(void);
#endif
main.c
cpp
#include "head.h"
int main(void)
{
systick_config();//时钟配置
gd_485_com_init(PCS_COM,9600);
gd_485_com_en(1);//发送使能
RtcInit();
while(1)
{
rtc_show_time();
delay_1ms(1000);
}
}