【新提醒】基于HC32F460开发板的RTC电子时钟

在HC32F460内部配有RTC功能,这里我们为它配上OLED屏显示,这样它就构成一个RTC电子时钟。
所用的OLED屏就是随板子自带的,该OLED屏的原理图见图1所示。

图1  OLED屏原理图
本想以I2C通讯来驱动OLED屏,但将其移到RTC的程序中,比较难协调,最终还是以模拟I2C的方式来轻松地将两者结合在一起。
由于SDA和SCL 仍占用原位置,故定义其输出高低电平的语句如下:
#define OLED_SCLK_Set()   PORT_SetBits(PortD, Pin00)
#define OLED_SCLK_Clr()   PORT_ResetBits(PortD, Pin00)
#define OLED_SDIN_Set()   PORT_SetBits(PortD, Pin01)
#define OLED_SDIN_Clr()   PORT_ResetBits(PortD, Pin01)
所用的字符显示函数为:
  1. void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size)
  2. {
  3. unsigned char j=0;
  4. while (chr[j]!='\0')
  5. {
  6. OLED_ShowChar(x,y,chr[j],Char_Size);
  7. x+=8;
  8. if(x>120){x=0;y+=2;}
  9. j++;
  10. }
  11. }

复制代码

所用的数值显示函数为:

  1. void OLED_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t size2)
  2. {
  3. uint8_t t,temp;
  4. uint8_t enshow=0;
  5. for(t=0;t<len;t++)
  6. {
  7. temp=(num/oled_pow(10,len-t-1))%10;
  8. if(enshow==0&&t<(len-1))
  9. {
  10. if(temp==0)
  11. {
  12. OLED_ShowChar(x+(size2/2)*t,y,' ',size2);
  13. continue;
  14. }else enshow=1;
  15. }
  16. OLED_ShowChar(x+(size2/2)*t,y,temp+'0',size2);
  17. }
  18. }

复制代码

进行RTC时间设置的函数为:

  1. static void Rtc_CalendarConfig(void)
  2. {
  3. stc_rtc_date_time_t stcRtcDateTimeCfg;
  4. MEM_ZERO_STRUCT(stcRtcDateTimeCfg);
  5. stcRtcDateTimeCfg.u8Year = 20u;
  6. stcRtcDateTimeCfg.u8Month = 9;
  7. stcRtcDateTimeCfg.u8Day = 27u;
  8. stcRtcDateTimeCfg.u8Weekday = RtcWeekdaySunday;
  9. stcRtcDateTimeCfg.u8Hour = 12u;
  10. stcRtcDateTimeCfg.u8Minute = 23u;
  11. stcRtcDateTimeCfg.u8Second = 01u;
  12. if (RTC_SetDateTime(RtcDataFormatDec, &stcRtcDateTimeCfg, Enable, Enable) != Ok)
  13. {
  14. printf("write calendar failed!\r\n");
  15. }
  16. }

复制代码

实现图1和图2显示效果的主程序如下:

  1. int32_t main(void)
  2. {
  3. stc_port_init_t stcPortInit;
  4. stc_rtc_date_time_t stcCurrDateTime;
  5. MEM_ZERO_STRUCT(stcPortInit);
  6. MEM_ZERO_STRUCT(stcCurrDateTime);
  7. LED0_OFF();
  8. stcPortInit.enPinMode = Pin_Mode_Out;
  9. PORT_Init(LED0_PORT, LED0_PIN, &stcPortInit);
  10. stcPortInit.enPinMode = Pin_Mode_Out;
  11. PORT_Init(PortD, Pin00, &stcPortInit);
  12. PORT_Init(PortD, Pin01, &stcPortInit);
  13. Xtal32_ClockConfig();
  14. Ddl_UartInit();
  15. Rtc_Config();
  16. OLED_Init();
  17. OLED_Clear();
  18. OLED_ShowString(0,0,"HC32F460 TEST",16);
  19. OLED_ShowString(0,2,"OLED & RTC",16);
  20. Ddl_Delay1ms(1000u);
  21. OLED_Clear();
  22. OLED_ShowString(0,0,"20  -  -",16);
  23. OLED_ShowString(0,2,"    :  :",16);
  24. while (1)
  25. {
  26. if (1u == u8SecIntFlag)
  27. {
  28. u8SecIntFlag = 0u;
  29. LED0_TOGGLE();
  30. if (RTC_GetDateTime(RtcDataFormatDec, &stcCurrDateTime) = Ok)
  31. {
  32. OLED_ShowNum(16,0,stcCurrDateTime.u8Year,2,16);
  33. OLED_ShowNum(40,0,stcCurrDateTime.u8Month,2,16);
  34. OLED_ShowNum(64,0,stcCurrDateTime.u8Day,2,16);
  35. OLED_ShowNum(16,2,stcCurrDateTime.u8Hour,2,16);
  36. OLED_ShowNum(40,2,stcCurrDateTime.u8Minute,2,16);
  37. OLED_ShowNum(64,2,stcCurrDateTime.u8Second,2,16);
  38. }
  39. }
  40. }
  41. }

复制代码

图2  初始界面

3.jpg (30.75 KB, 下载次数: 2)

下载附件 保存到相册

2020-9-29 11:31 上传

图3  RTC运行界面
(0)

相关推荐