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

基于STM32G431的RTC电子时钟

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

4 O5 ?: N) d* y; T' \; g( H* E
实现图1所示效果的主程序为:
  1. int main(void)
    5 p$ }/ i7 {" o, X
  2. {
    6 {1 S; \+ F7 u) P2 A( F+ ^7 _+ {* M
  3. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */" V5 A  x9 q0 o
  4. HAL_Init();, v" m1 m1 J: T9 @  n/ R
  5. /* Configure the system clock */
    " r+ }+ p4 V* i2 p' ~. J% W
  6. SystemClock_Config();2 ]& v8 I" S4 ?
  7. GPIO_OLED_INIT();( D  x  ]8 r0 M7 A+ l
  8. OLED_Init();
    ) k5 W# Y' _8 K, }, h8 ?$ F( t( D  t
  9. OLED_Clear();  ^* F7 e* P" _; M* _; J
  10. OLED_ShowString(18,0,"STM32G431",16);
    1 ~: J$ c& X9 R& J
  11. OLED_ShowString(10,2,"OLED & RTC",16);2 i: r, f1 B5 A, E6 d9 ]
  12. /* Initialize all configured peripherals */
      ~! m7 c+ L6 H: X* V5 b/ I
  13. MX_RTC_Init();3 U( y) `7 x3 k6 Q* d
  14. RTCStatus = 1;
    + y/ m$ L( ]8 H! p8 Q
  15. while (1)
    ; @- k3 T+ l+ j6 x+ A
  16. {
    4 ?  U5 D6 `+ N0 p9 ^+ q# @1 i4 J
  17. /* Display the updated Time and Date */
    . U8 H% _3 ^6 ^
  18. RTC_CalendarShow(aShowTime, aShowDate);! e* n+ j3 N+ Y& ~+ x
  19. Delay(200);
    & v' V( Q/ p. x% L& Z# X
  20. }
      F$ e9 S' x- {( r4 B
  21. }
复制代码
1.jpg
                              
1  RTC电子时钟
$ O# ?" C$ B! O! n/ X+ E
实现RTC显示的函数为:
  1. static void RTC_CalendarShow(uint8_t *showtime, uint8_t *showdate)
    7 `! R# A9 q7 D7 B. s( |
  2. {# U- a; F2 A: m. t) M
  3. RTC_DateTypeDef sdatestructureget;
    5 m' @5 ~& N9 E6 w/ t
  4. RTC_TimeTypeDef stimestructureget;
    5 z3 T0 {% m  G% @0 Z9 W
  5. /* Get the RTC current Time */: }0 _5 U' p- v! \( ^* g
  6. HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
    : ~; u2 \2 G5 A
  7. /* Get the RTC current Date */1 s8 B* w4 O5 v; f
  8. HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);2 j" q" q, B; o
  9. /* Display time Format : hh:mm:ss */
    - }8 h( L5 M: [4 {; K5 [
  10. sprintf((char *)showtime, "%2d:%2d:%2d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);; s* s1 @3 X" @7 ~8 ^
  11. OLED_ShowString(26,6,showtime,16);
    " v. }  ~/ h# F, V7 ]3 ?
  12. /* Display date Format : mm-dd-yy */
    ; v" |% o, |+ e2 H) f
  13. sprintf((char *)showdate, "%2d-%2d-%2d", 2000 + sdatestructureget.Year,sdatestructureget.Month, sdatestructureget.Date);; w4 s+ R% B9 ?% R# ?! s
  14. OLED_ShowString(10,4,showdate,16);
    $ W0 G0 e5 r- {  |
  15. }
复制代码

( Q( O, Y$ R( Z5 T0 Z
调用的字符串显示函数为:
  1. void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size)
    4 H$ s- d! a9 S5 S0 [
  2. {' w5 ?3 k" Z2 F/ V$ m4 P
  3.     unsigned char j=0;3 d% v0 u; y- r
  4.     while (chr[j]!='\0')' p; P* b! F9 ^% n# _6 C9 b- }
  5.     {       OLED_ShowChar(x,y,chr[j],Char_Size);
    . q& l0 O6 J8 {: [- \
  6.             x+=8;
    7 U+ A$ H& C8 f; y  Q# H. b
  7.             if(x>120){x=0;y+=2;}
    6 u) F/ r5 S% F" N" G+ ]
  8.             j++;5 M; F- `$ o% o" P: p
  9.     }: _! H4 C  v- Q, O! c: q/ q
  10. }
复制代码

$ `& `) w& @6 D$ ~
由于例程所设置的初始时间是2018年,故需要在函数 MX_RTC_Init()中进行修改,修改后的运行效果如图2所示。
2.jpg
& d$ v2 Y  K" g& j- ]
2  校时后的显示效果

3 j; w, A) G5 \3 s
收藏 评论5 发布时间:2020-12-11 11:04

举报

5个回答
网络孤客 回答时间:2020-12-11 12:03:37
看看时间准吗。
) g  }& y1 \0 H* c7 t/ L有空我也玩玩。
jinglixixi 回答时间:2020-12-11 12:52:27
ldptest 发表于 2020-12-11 12:03: Z+ @: k3 M% i# O, f
看看时间准吗。
7 b+ R3 J* B, P$ o' P有空我也玩玩。

- i+ ]4 W$ ]; s( G计时尚可,长时间的误差可能会明显点儿。
Kevin_G 回答时间:2020-12-13 10:17:35
楼主,上源码
radio2radio 回答时间:2020-12-13 14:38:10
楼主,G431的RTC,有没有自动万年历功能?- L* r/ z+ M# t, }
就是11月31日会不会自动进入12月1日,还有闰年修正等。4 ^- N. j8 J6 Q3 |; {+ Z
jinglixixi 回答时间:2020-12-13 18:20:38
radio2radio 发表于 2020-12-13 14:38
7 R! i3 S+ U# T5 y6 z- o& G楼主,G431的RTC,有没有自动万年历功能?
* }% |8 h5 D5 H* }6 e+ I" j& R+ N就是11月31日会不会自动进入12月1日,还有闰年修正等。
9 a; E9 B, _! q# D0 [ ...
# p6 h/ h% s+ j2 P. U: \2 a! m6 q
我给你测了下,两者都没有问题。

所属标签

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