本帖最后由 qzhhr 于 2018-5-28 06:07 编辑
#include "gd32_rtc.h"
//------------------------------
//RTC
//------------------
u8 iRTCSECFlag;
u32 iRTCTmr;
//--------------------
void rtc_nvic_configuration(void)
{
//SCB_SCR_SLEEPDEEP
// nvic_priority_group_set(NVIC_PRIGROUP_PRE1_SUB3);
exti_flag_clear(EXTI_18);
exti_init(EXTI_18,EXTI_EVENT, EXTI_TRIG_RISING);//EXTI_INTERRUPT
exti_interrupt_enable(EXTI_18);
nvic_irq_enable(RTC_IRQn,0,0);//4
}
//void rtc_prescaler_set(u16 factorA,u16 factorS)//32767);
//{
// /* disable the write protection */
//
// RTC_WPK = RTC_UNLOCK_KEY1;
// RTC_WPK = RTC_UNLOCK_KEY2;
// rtc_init_mode_enter();
//
// RTC_PSC&=0x007f00ff;
// RTC_PSC|=PSC_FACTOR_S(factorS);
// RTC_PSC|=PSC_FACTOR_A(factorA);
//
// rtc_init_mode_exit();
// /* enable the write protection */
// RTC_WPK = RTC_LOCK_KEY;
//
//}
void rtc_alarm_INTEn(void)
{
/* disable the write protection */
RTC_WPK = RTC_UNLOCK_KEY1;
RTC_WPK = RTC_UNLOCK_KEY2;
RTC_CTL |= RTC_CTL_ALRM0IE;
/* enable the write protection */
RTC_WPK = RTC_LOCK_KEY;
}
//------------------------------
void rtc_configuration(void)
{
// /* enable PMU and BKPI clocks */
//rcu_periph_clock_enable(RCU_BKPI);
rcu_periph_clock_enable(RCU_PMU);
// /* allow access to BKP domain */
pmu_backup_write_enable();
// /* reset backup domain */
// bkp_deinit();
/* enable LXTAL */
rcu_osci_on(RCU_IRC40K);
// /* wait till LXTAL is ready */
rcu_osci_stab_wait(RCU_IRC40K);
//
// /* select RCU_LXTAL as RTC clock source */
rcu_rtc_clock_config(RCU_RTC_IRC40K);
// /* enable RTC Clock */
rcu_periph_clock_enable(RCU_RTC);
// /* wait for RTC registers synchronization */
rtc_register_sync_wait();
// /* wait until last write operation on RTC registers has finished */
// rtc_lwoff_wait();
// /* enable the RTC second interrupt*/
rtc_interrupt_enable(RTC_INT_ALARM);
// /* wait until last write operation on RTC registers has finished */
// rtc_lwoff_wait();
// /* set RTC prescaler: set RTC period to 1s */
// rtc_prescaler_set(32767);
// /* wait until last write operation on RTC registers has finished */
// rtc_lwoff_wait();
}
//--------------------------------
void RTC_InitAlarmParam(void)
{
rtc_parameter_struct tRTCparam;
rtc_alarm_struct rtc_alarm_time;
rtc_alarm_disable();
iRTCSECFlag=0;
iRTCTmr=0;
tRTCparam.rtc_am_pm=RTC_AM;
tRTCparam.rtc_display_format=RTC_24HOUR;
tRTCparam.rtc_hour=0;
tRTCparam.rtc_minute=0;
tRTCparam.rtc_second=0;
tRTCparam.rtc_year=0x18;
tRTCparam.rtc_month=RTC_MAY;
tRTCparam.rtc_day_of_week=RTC_MONDAY;
tRTCparam.rtc_date=0x21;
tRTCparam.rtc_factor_asyn=0x63;//0;
tRTCparam.rtc_factor_syn=0x18f;//32767; //1S
rtc_alarm_time.rtc_alarm_day=0x21;
rtc_alarm_time.rtc_alarm_hour=0;
rtc_alarm_time.rtc_alarm_mask=RTC_ALARM_DATE_MASK|RTC_ALARM_HOUR_MASK|RTC_ALARM_MINUTE_MASK; //RTC_ALARM_SECOND_MASK;//
rtc_alarm_time.rtc_alarm_minute=0;
rtc_alarm_time.rtc_alarm_second=20;
rtc_alarm_time.rtc_am_pm=RTC_AM;
rtc_alarm_time.rtc_weekday_or_date=RTC_ALARM_DATE_SELECTED;
rtc_init(&tRTCparam);
rtc_alarm_config(&rtc_alarm_time);//1S中断一次
rtc_flag_clear(RTC_STAT_ALRM0F);
exti_flag_clear(EXTI_18);
rtc_alarm_INTEn();
rtc_alarm_enable();
}
void RTC_Init(void)
{
iRTCSECFlag=0;
iRTCTmr=0;
rtc_alarm_disable();
rtc_nvic_configuration();
rtc_configuration();
// rtc_deinit();
RTC_InitAlarmParam();
}
//-----------------------
u32 RTC_getTmr(void)
{
return iRTCTmr;
}
void RTC_SecTmr(void)
{
iRTCSECFlag=1; //1S中断标记.
iRTCTmr++;
}
//-----------------------------
void RTC_IRQHandler(void)
//void RTCAlarm_IRQHandler(void)
{
//1S中断一次。
if(rtc_flag_get(RTC_STAT_ALRM0F))
{
RTC_SecTmr();
}
//rtc_flag_clear(RTC_STAT_RSYNF);
}
能否请楼主或其它高手帮帮忙,问题在哪?
|