基于GD32F450的RTC时钟
在“基于GD32F450开发板的LCD5110屏显示”一帖中为大家介绍了GD32F450开发板配置LCD5110显示屏的用法,在此基础上,充分利用MCU内部的RTC则可实现RTC计时控制等。
要完成RTC时钟,需要用到这样几个函数,即RTC配置函数rtc_pre_config()、RTC初值设置函数rtc_setup()、RTC显示函数rtc_show_time()、RTC限值显示函数rtc_show_alarm()等。
为与LCD5110显示函数相配合,修改后的相关函数如下:
- void rtc_show_time(void)
- {
- uint32_t time_subsecond = 0;
- uint8_t subsecond_ss = 0,subsecond_ts = 0,subsecond_hs = 0;
- rtc_current_time_get(&rtc_initpara);
- /* get the subsecond value of current time, and convert it into fractional format */
- time_subsecond = rtc_subsecond_get();
- LCD_set_XY(32,2);
- LCD_write_char(rtc_initpara.year/16+'0',0);
- LCD_write_char(rtc_initpara.year%16+'0',0);
- LCD_write_char(':',0);
- LCD_write_char(rtc_initpara.month/16+'0',0);
- LCD_write_char(rtc_initpara.month%16+'0',0);
- LCD_write_char(':',0);
- LCD_write_char(rtc_initpara.date/16+'0',0);
- LCD_write_char(rtc_initpara.date%16+'0',0);
- LCD_set_XY(32,4);
- LCD_write_char(rtc_initpara.hour/16+'0',0);
- LCD_write_char(rtc_initpara.hour%16+'0',0);
- LCD_write_char(':',0);
- LCD_write_char(rtc_initpara.minute/16+'0',0);
- LCD_write_char(rtc_initpara.minute%16+'0',0);
- LCD_write_char(':',0);
- LCD_write_char(rtc_initpara.second/16+'0',0);
- LCD_write_char(rtc_initpara.second%16+'0',0);
- }
限定值的显示函数如下:
- void rtc_show_alarm(void)
- {
- rtc_alarm_get(RTC_ALARM0,&rtc_alarm);
- LCD_set_XY(32,5);
- LCD_write_char(rtc_alarm.alarm_hour/16+'0',0);
- LCD_write_char(rtc_alarm.alarm_hour%16+'0',0);//(subsecond_hs%10+'0',0);
- LCD_write_char(':',0);
- LCD_write_char(rtc_alarm.alarm_minute/16+'0',0);//(subsecond_ts/10+'0',0);
- LCD_write_char(rtc_alarm.alarm_minute%16+'2',0);
- LCD_write_char(':',0);
- LCD_write_char(rtc_alarm.alarm_second/16+'0',0);//(subsecond_ss/10+'0',0);
- LCD_write_char(rtc_alarm.alarm_second%16+'0',0);
- }
要得到图示的计时效果,则主函数的代码如下:
为了便于时间限值的设置,可利用板上的3个用户键来实现数值的设置,其中以一个键来调整数值,以一个键来移动数值的位置,以一个键来确认设置。此外,为了实现计时控制,可分配一个GPIO引脚与光隔继电器相连接,进而实现电器的控制。
相应的中断处理函数如下:
- void RTC_Alarm_IRQHandler(void)
- {
- if(RESET != rtc_flag_get(RTC_STAT_ALRM0F))
- {
- rtc_flag_clear(RTC_STAT_ALRM0F);
- exti_flag_clear(EXTI_17);
- GPIO_BOP(GPIOD) = GPIO_PIN_7; //改用其他引脚与继电器配合来控制相应得电器
- }
- }
只要前面带有前缀【我是电子发烧友】就可以参加活动了,所以技术贴可以发到相对应的技术板块,我在后台都是可以看到的。
感谢,知道了!
主函数那部分,格式出现问题,重编辑后也没解决,只好将其附在此处了!
int main(void)
{
uint8_t i=0;
// u16 color=0;
systick_config();
SPI_CONFIG();
LCD_init();
LCD_write_english_string(0,0,"GD32 Colibri-",0);
LCD_write_english_string(0,1," - F450VE",0);
LCD_write_english_string(0,3,"ARM Cortex-M4",0);
LCD_write_english_string(0,5," 2017.4.20",0);
delay_1ms(1000);
LCD_clear();
LCD_write_english_string(0,0," RTC Clock",0);
LCD_write_english_string(0,2,"DATE: ",0);
LCD_write_english_string(0,4,"TIME:",0);
//LCD_set_XY(36,0);
//LCD_write_char(Set_V[0]/10+'0',0);
led_init();
/* enable PMU clock */
rcu_periph_clock_enable(RCU_PMU);
/* enable the access of the RTC registers */
pmu_backup_write_enable();
rtc_pre_config();
if (BKP_VALUE != RTC_BKP0){
rtc_setup();
}else{
if (RESET != rcu_flag_get(RCU_FLAG_PORRST))
{
// Gui_DrawFont_GBK16(30, 40, RED, GRAY0,"power on");
}else if (RESET != rcu_flag_get(RCU_FLAG_EPRST)){
// printf("external reset occurred....\n\r");
}
// printf("no need to configure RTC....\n\r");
rtc_flag_clear(RTC_STAT_ALRM0F);
exti_flag_clear(EXTI_17);
rtc_show_time();
rtc_show_alarm();
}
rcu_all_reset_flag_clear();
/* RTC alarm interrupt configuration */
exti_init(EXTI_17,EXTI_INTERRUPT,EXTI_TRIG_RISING);
nvic_irq_enable(RTC_Alarm_IRQn,0,0);
while(1)
{
GPIO_TG(GPIOB) = GPIO_PIN_3;
delay_1ms(500);
i++;
if(i==10) i=0;
rtc_show_time();
}
}
进来看看国产的了,
用习惯也是不错的,当然也会遇见些BUG,发展中也是难免的。
肯定的,还是要支持国产的
只有大家都支持了,才能发展的
不然,不是被米帝奴役,就是倭寇奴役啊
arm被软银收购,arm芯片做的好的全是米帝及其帮凶