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

基于STM32G431的RTC电子时钟

[复制链接]
jinglixixi 发布时间:2020-12-11 11:04
STM32G431内部配置有RTC计时器,将它与OLED屏配合即可实现一个电子时钟。
" I0 j) `7 q' y# _
实现图1所示效果的主程序为:
  1. int main(void)' O0 H2 _; v% ?7 l
  2. {1 t, R, o% s2 R- }) ?
  3. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */! F. N& T: C: {. y+ m; C
  4. HAL_Init();
    " U: r& e- e, L% z* m
  5. /* Configure the system clock */
    ; f' p( R/ X  x3 v5 o* g
  6. SystemClock_Config();
    * a8 F0 W' c4 s
  7. GPIO_OLED_INIT();3 h. Z& L& V' Z+ D2 O9 I
  8. OLED_Init();/ ^: \6 n# B" _2 }
  9. OLED_Clear();
    7 v, _9 o1 M( ~6 O3 g: I  e4 Q5 u) C6 L
  10. OLED_ShowString(18,0,"STM32G431",16);
    9 P+ [4 h4 w: H0 S  t! `4 K
  11. OLED_ShowString(10,2,"OLED & RTC",16);
    3 ~' t& ^0 q( U  V) D+ R
  12. /* Initialize all configured peripherals */
    ) J+ D' S/ m: z; v: Z+ o# j
  13. MX_RTC_Init();% c* j7 ^& ?; x: U- L% W
  14. RTCStatus = 1;
    ; u0 k+ c" ?0 Z, }. v
  15. while (1)$ R/ }# I# }, V; I6 l+ N% M
  16. {
    + Q6 J5 b9 d6 A: B/ N" h) F
  17. /* Display the updated Time and Date */5 a  n4 m% R2 H! j% d
  18. RTC_CalendarShow(aShowTime, aShowDate);
    ; Y$ A! q3 `' b# y& u
  19. Delay(200);5 Z( j5 o; w+ V
  20. }0 b9 k# ?; P3 w! T- N
  21. }
复制代码
1.jpg
                              
1  RTC电子时钟
* T; a/ N0 a3 n' Y
实现RTC显示的函数为:
  1. static void RTC_CalendarShow(uint8_t *showtime, uint8_t *showdate)
    . N3 d" J. ~0 Q( K/ W
  2. {
      A8 s, B$ i, e9 w. i9 m: e+ B
  3. RTC_DateTypeDef sdatestructureget;
    6 R4 m, H# u: D, s  ]5 ~
  4. RTC_TimeTypeDef stimestructureget;  A% u, t! s! ~. u8 T) x; u
  5. /* Get the RTC current Time */* A8 d( F: i- [0 Q$ i+ s; ~/ x( Q
  6. HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
    8 n7 O6 G) m  i$ \6 k/ G2 y- K1 ^
  7. /* Get the RTC current Date */
    7 Q; R- ?/ e- \
  8. HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
    ; h0 C" E0 u. c0 y& O( L, e
  9. /* Display time Format : hh:mm:ss */. C- d/ _! L/ j# y2 V1 d7 E
  10. sprintf((char *)showtime, "%2d:%2d:%2d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);8 \2 ]- ^0 o! J4 G, |
  11. OLED_ShowString(26,6,showtime,16);. m9 u+ f* N- A5 V
  12. /* Display date Format : mm-dd-yy */( ?- d3 U+ Z3 @, x8 U% S( `( ^
  13. sprintf((char *)showdate, "%2d-%2d-%2d", 2000 + sdatestructureget.Year,sdatestructureget.Month, sdatestructureget.Date);
    # ]! I+ i8 }  l+ f* V
  14. OLED_ShowString(10,4,showdate,16);
    : T4 r  Y/ D$ P, {
  15. }
复制代码
- P* B2 b  G- |, S- J: \* P( u
调用的字符串显示函数为:
  1. void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size). W( ~% r( Z8 k2 G
  2. {
    + Z+ G1 F) j7 n3 u- z
  3.     unsigned char j=0;
      Q& F* w8 w0 s4 _
  4.     while (chr[j]!='\0'): d$ ], s. M) c- R" i7 H
  5.     {       OLED_ShowChar(x,y,chr[j],Char_Size);
    2 S  Z* u; `6 b/ R, A5 ?
  6.             x+=8;
    ; c2 f6 F* i0 \% r1 P) p
  7.             if(x>120){x=0;y+=2;}3 k7 ?8 L; d  P' g* d
  8.             j++;
    0 I9 F, v/ {8 h7 f
  9.     }$ z, L( r3 o3 F: z  Z7 H/ q9 v: W
  10. }
复制代码
2 ~' v2 ~" I( G
由于例程所设置的初始时间是2018年,故需要在函数 MX_RTC_Init()中进行修改,修改后的运行效果如图2所示。
2.jpg
- D) _9 g5 U  d* M9 m! e
2  校时后的显示效果

2 n% H; z  i' c4 B# d% g% p& B
收藏 评论5 发布时间:2020-12-11 11:04

举报

5个回答
网络孤客 回答时间:2020-12-11 12:03:37
看看时间准吗。/ R1 U# `& {. I. @7 H7 u) o
有空我也玩玩。
jinglixixi 回答时间:2020-12-11 12:52:27
ldptest 发表于 2020-12-11 12:03* ]' A3 [5 Z9 m/ O4 g% G' M' m+ e3 I
看看时间准吗。1 q2 A! x& s9 V/ Y  h
有空我也玩玩。
- s; p/ A7 f( }6 ^8 ~! A2 H
计时尚可,长时间的误差可能会明显点儿。
Kevin_G 回答时间:2020-12-13 10:17:35
楼主,上源码
radio2radio 回答时间:2020-12-13 14:38:10
楼主,G431的RTC,有没有自动万年历功能?
' K# d9 s, i# J3 T* l& s, t就是11月31日会不会自动进入12月1日,还有闰年修正等。& y  _5 F$ S+ J( \' W5 L
jinglixixi 回答时间:2020-12-13 18:20:38
radio2radio 发表于 2020-12-13 14:38# j+ m& ?! }% z8 h1 e, U" |
楼主,G431的RTC,有没有自动万年历功能?
3 k% _0 R, s$ P就是11月31日会不会自动进入12月1日,还有闰年修正等。- `4 p: @; b, i$ }# N) F
...

) z0 _! e8 ~* @我给你测了下,两者都没有问题。

所属标签

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