【上海航芯 ACM32F070开发板+触控功能评估板测评】+RTC电子时钟
[复制链接]
本帖最后由 jinglixixi 于 2022-10-19 09:55 编辑
在ACM32F070内部配有RTC计时器,如使用段码LCD屏来显示会有时间内容显示不完整的问题。为此,可使用前面介绍的OLED屏来完成显示工作,其显示效果如图1和图2所示。
图1 显示效果
图2 显示效果
显示RTC时间设置与显示的函数为:
void RTC_Test(void)
{
uint32_t fu32_Seconds;
RTC_ConfigTypeDef RTC_Handle;
RTC_TimeTypeDef gstr_Time_Set;
RTC_TimeTypeDef gstr_Time_Get;
RTC_TimeTypeDef gstr_Time_Get_Again;
RTC_DateTypeDef gstr_Date_Set;
RTC_DateTypeDef gstr_Date_Get;
RTC_Handle.u32_ClockSource = RTC_CLOCK_XTL;
RTC_Handle.u32_Compensation = COMPENSATION_INCREASE;
RTC_Handle.u32_CompensationValue = 0x05;
HAL_RTC_Config(&RTC_Handle);
gstr_Time_Set.u8_Hours = 0x12;
gstr_Time_Set.u8_Minutes = 0x30;
gstr_Time_Set.u8_Seconds = 0x00;
HAL_RTC_SetTime(&gstr_Time_Set);
gstr_Date_Set.u8_Year = 0x22;
gstr_Date_Set.u8_Month = RTC_MONTH_ OCTOBER;
gstr_Date_Set.u8_Date = 0x10;
gstr_Date_Set.u8_WeekDay = RTC_WEEKDAY_ MONDAY;
HAL_RTC_SetDate(&gstr_Date_Set);
while (1)
{
HAL_RTC_GetTime(&gstr_Time_Get);
HAL_RTC_GetDate(&gstr_Date_Get);
HAL_RTC_GetTime(&gstr_Time_Get_Again);
if (gstr_Time_Get.u8_Seconds != gstr_Time_Get_Again.u8_Seconds)
{
continue;
}
if (fu32_Seconds != gstr_Time_Get.u8_Seconds)
{
fu32_Seconds = gstr_Time_Get.u8_Seconds;
OLED_ShowNum(0,5,gstr_Time_Get.u8_Hours,2,16);
OLED_ShowNum(24,5,gstr_Time_Get.u8_Minutes,2,16);
OLED_ShowNum(48,5,gstr_Time_Get.u8_Seconds,2,16);
}
}
}
由于读取的RTC时间值是以十六进制数来输出的,因此需配置一个 相对应的数值实现函数,其内容如下:
void OLED_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t size2)
{
uint8_t t,temp;
uint8_t enshow=0;
for(t=0;t<len;t++)
{
temp=(num/oled_pow(16,len-t-1))%16;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
OLED_ShowChar(x+(size2/2)*t,y,' ',size2);
continue;
}
else enshow=1;
}
OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2);
}
}
在此基础上,实现图示效果的主程序为:
int main(void)
{
System_Init();
GPIO_Test();
app_OLED_Init();
OLED_Init();
OLED_Clear();
OLED_ShowString(0,0,"ACM32F070",16);
OLED_ShowString(0,2,"OLED & RTC",16);
OLED_ShowString(0,5," : :",16);
RTC_Test();
while (1);
}
|