你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

基于STM32G431的RTC电子时钟

[复制链接]
jinglixixi 发布时间:2020-12-11 11:04
STM32G431内部配置有RTC计时器,将它与OLED屏配合即可实现一个电子时钟。

实现图1所示效果的主程序为:
  1. int main(void)
  2. {
  3. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  4. HAL_Init();
  5. /* Configure the system clock */
  6. SystemClock_Config();
  7. GPIO_OLED_INIT();
  8. OLED_Init();
  9. OLED_Clear();
  10. OLED_ShowString(18,0,"STM32G431",16);
  11. OLED_ShowString(10,2,"OLED & RTC",16);
  12. /* Initialize all configured peripherals */
  13. MX_RTC_Init();
  14. RTCStatus = 1;
  15. while (1)
  16. {
  17. /* Display the updated Time and Date */
  18. RTC_CalendarShow(aShowTime, aShowDate);
  19. Delay(200);
  20. }
  21. }
复制代码
1.jpg
                              
1  RTC电子时钟

实现RTC显示的函数为:
  1. static void RTC_CalendarShow(uint8_t *showtime, uint8_t *showdate)
  2. {
  3. RTC_DateTypeDef sdatestructureget;
  4. RTC_TimeTypeDef stimestructureget;
  5. /* Get the RTC current Time */
  6. HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
  7. /* Get the RTC current Date */
  8. HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
  9. /* Display time Format : hh:mm:ss */
  10. sprintf((char *)showtime, "%2d:%2d:%2d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
  11. OLED_ShowString(26,6,showtime,16);
  12. /* Display date Format : mm-dd-yy */
  13. sprintf((char *)showdate, "%2d-%2d-%2d", 2000 + sdatestructureget.Year,sdatestructureget.Month, sdatestructureget.Date);
  14. OLED_ShowString(10,4,showdate,16);
  15. }
复制代码

调用的字符串显示函数为:
  1. void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size)
  2. {
  3.     unsigned char j=0;
  4.     while (chr[j]!='\0')
  5.     {       OLED_ShowChar(x,y,chr[j],Char_Size);
  6.             x+=8;
  7.             if(x>120){x=0;y+=2;}
  8.             j++;
  9.     }
  10. }
复制代码

由于例程所设置的初始时间是2018年,故需要在函数 MX_RTC_Init()中进行修改,修改后的运行效果如图2所示。
2.jpg

2  校时后的显示效果

收藏 评论5 发布时间:2020-12-11 11:04

举报

5个回答
网络孤客 回答时间:2020-12-11 12:03:37
看看时间准吗。
有空我也玩玩。
jinglixixi 回答时间:2020-12-11 12:52:27
ldptest 发表于 2020-12-11 12:03
看看时间准吗。
有空我也玩玩。

计时尚可,长时间的误差可能会明显点儿。
Kevin_G 回答时间:2020-12-13 10:17:35
楼主,上源码
radio2radio 回答时间:2020-12-13 14:38:10
楼主,G431的RTC,有没有自动万年历功能?
就是11月31日会不会自动进入12月1日,还有闰年修正等。
jinglixixi 回答时间:2020-12-13 18:20:38
radio2radio 发表于 2020-12-13 14:38
楼主,G431的RTC,有没有自动万年历功能?
就是11月31日会不会自动进入12月1日,还有闰年修正等。
...

我给你测了下,两者都没有问题。

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版