依稀记得stm32f10x系列的rtc需要自己把秒转换成年、月、日、时、分、秒,今天我们来试试es32f369的rtc,用起来很简单,配置好参数后,直接输出时间和日期,还可以根据自己的喜好设置12小时制还是24小时制。
在上节【东软载波ES32F369x+驱动0.96OLED】的基础上进行扩展,实时显示RTC。
核心代码
#include "periph_rtc.h"
#include "md_bkpc.h"
#include "md_rtc.h"
md_rtc_time_t time;
md_rtc_date_t date;
void RTC_Init()
{
md_rtc_init_t init;
/* Set LOSM as RTC clock */
// md_bkpc_rtc_clock_config(MD_BKPC_PREH_CLK_LOSM);
/* Initialize RTC */
md_rtc_struct_init(&init);
md_rtc_init(&init);
/* Initialize time/date */
md_rtc_time_struct_init(&time);
md_rtc_date_struct_init(&date);
date.year=21;
date.month=1;
date.day=23;
date.week=6;
time.hour=19;
time.minute=11;
time.second=30;
time.sub_sec=10;
md_rtc_set_time(&time, MD_RTC_FORMAT_DEC);
md_rtc_set_date(&date, MD_RTC_FORMAT_DEC);
}
/*
uint8_t hour;
uint8_t minute;
uint8_t second;
uint16_t sub_sec;
uint8_t week;
uint8_t day;
uint8_t month;
uint8_t year;
*/
void Get_Rtc(uint8_t *buff)
{
md_rtc_get_time(&time, MD_RTC_FORMAT_DEC);
md_rtc_get_date(&date, MD_RTC_FORMAT_DEC);
buff[0]=date.year;
buff[1]=date.month;
buff[2]=date.day;
buff[3]=date.week;
buff[4]=time.hour;
buff[5]=time.minute;
buff[6]=time.second;
}
循环获取时间和日期:
while (1)
{
md_delay_1ms(400);
md_gpio_set_pin_low(GPIOF, MD_GPIO_PIN_0);
md_gpio_set_pin_low(GPIOF, MD_GPIO_PIN_1);
md_delay_1ms(400);
md_gpio_set_pin_high(GPIOF, MD_GPIO_PIN_0);
md_gpio_set_pin_high(GPIOF, MD_GPIO_PIN_1);
Get_Rtc(buff);
OLED_ShowCN(40,1,4);
OLED_ShowCN(56,1,5);
OLED_ShowCN(72,1,buff[3]+5);
memset(str,' ',sizeof(str));
sprintf(str,"20%02d-%02d-%02d",buff[0],buff[1],buff[2]);
OLED_ShowString(24,3,str,16);
memset(str,' ',sizeof(str));
sprintf(str,"%02d:%02d:%02d",buff[4],buff[5],buff[6]);
OLED_ShowString(32,5,str,16);
}
发现论坛传不了视频。。。
|