本帖最后由 jinglixixi 于 2017-5-2 09:12 编辑
为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);
- }
复制代码
要得到图示的计时效果,主函数的代码如下: - int main(void)
- {
- uint8_t i=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);
- 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
- {
- 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();
- }
- }
复制代码为了便于时间限值的设置,可利用板上的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; // 改用其它引脚配继电器来控制相关电器
- }
- }
复制代码 |