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

基于STM32G431的RTC电子时钟

[复制链接]
jinglixixi 发布时间:2020-12-11 11:04
STM32G431内部配置有RTC计时器,将它与OLED屏配合即可实现一个电子时钟。
" @8 K7 q: i* G  P
实现图1所示效果的主程序为:
  1. int main(void)
    , Z5 G% _) i# L) k3 g" n
  2. {1 B6 g  @$ W3 B
  3. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */- A& Q9 `0 v2 Q; ^
  4. HAL_Init();
    ' @% {+ C( ^/ p, w" E0 x
  5. /* Configure the system clock */+ |; H. t+ [1 k, G/ }
  6. SystemClock_Config();
    " Z' W9 e) c4 t4 y$ f" T  g
  7. GPIO_OLED_INIT();
    . p" h" P# a6 h' Q" X
  8. OLED_Init();! z4 a4 j  N& Y* D+ v+ I$ M  D  {6 o
  9. OLED_Clear();' E2 r' O: F) f) H7 d
  10. OLED_ShowString(18,0,"STM32G431",16);
    5 U" B( H$ S' _* `7 l) [
  11. OLED_ShowString(10,2,"OLED & RTC",16);
    * ]; r: V! Z7 r6 ?, x/ F2 z* C" {+ U
  12. /* Initialize all configured peripherals */
    % g0 F6 D  t, ^( m
  13. MX_RTC_Init();0 b. N. m0 n6 J
  14. RTCStatus = 1;4 i* M; ^4 b4 e$ T2 L5 T6 @# b
  15. while (1)
    . I$ |) Y# {6 L) _
  16. {
    # s# r3 N- D% S" x! q3 o
  17. /* Display the updated Time and Date */
      n! @, L% s2 l8 {- Z- m
  18. RTC_CalendarShow(aShowTime, aShowDate);! q, @, }  f" h# \
  19. Delay(200);# @) U8 x  q+ _- H2 p/ F
  20. }5 n- q" v" G2 }2 Z4 y+ n+ ?* W3 Y
  21. }
复制代码
1.jpg
                              
1  RTC电子时钟

: V0 s. @# _  @- u; X5 B9 H+ O9 Z5 @
实现RTC显示的函数为:
  1. static void RTC_CalendarShow(uint8_t *showtime, uint8_t *showdate)# j- w  n) i2 O+ y
  2. {6 t* T9 I2 r) d" y& l* `; x+ C
  3. RTC_DateTypeDef sdatestructureget;/ B1 L4 r& p( k8 x( E1 s3 Z( y/ u# h
  4. RTC_TimeTypeDef stimestructureget;
    * H/ }3 J4 b8 r" d
  5. /* Get the RTC current Time */! U3 {7 L2 k# P. m/ N9 D6 o
  6. HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);$ U6 _: }. N1 M: l( P! w7 j
  7. /* Get the RTC current Date */
    : ~9 C/ {" b9 ]
  8. HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
    + o2 H1 u; l7 Q4 _
  9. /* Display time Format : hh:mm:ss */2 W9 G+ T! E2 K) c$ [
  10. sprintf((char *)showtime, "%2d:%2d:%2d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
    6 ?( C% R6 ?: J+ Y" I; o6 D2 b
  11. OLED_ShowString(26,6,showtime,16);
    1 Q  `+ U$ ?+ S4 T5 f! A
  12. /* Display date Format : mm-dd-yy */
    - Y4 C  l5 s- q. ~5 p7 }& B# T1 n
  13. sprintf((char *)showdate, "%2d-%2d-%2d", 2000 + sdatestructureget.Year,sdatestructureget.Month, sdatestructureget.Date);7 u( u% W; z, k0 a3 ?
  14. OLED_ShowString(10,4,showdate,16);, y  l* p' L4 f' H9 {; x$ S* I6 ^
  15. }
复制代码

! ]' E+ I$ O$ _; R; G! H
调用的字符串显示函数为:
  1. void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size)
    7 T# c: Q. s) ?1 g! h# o! X
  2. {
    ; \  R0 u9 f7 M* {8 Z
  3.     unsigned char j=0;  P2 f* B6 }$ }* k+ d
  4.     while (chr[j]!='\0')
    4 J" e9 ?5 `9 n- F6 ]5 ]2 o8 F
  5.     {       OLED_ShowChar(x,y,chr[j],Char_Size);
    % p8 s9 e3 C: S! L: C5 p3 I
  6.             x+=8;
    + M, T. B2 }$ Z3 R( j
  7.             if(x>120){x=0;y+=2;}9 O3 @# M# O) M- N
  8.             j++;
    . S% B- j; A) G8 C
  9.     }* s& ]$ I$ b' c! G: t2 U
  10. }
复制代码
, b* t- L$ U" \
由于例程所设置的初始时间是2018年,故需要在函数 MX_RTC_Init()中进行修改,修改后的运行效果如图2所示。
2.jpg
. W" E  I+ [' E7 a
2  校时后的显示效果

. R0 K# |& W3 B3 z
收藏 评论5 发布时间:2020-12-11 11:04

举报

5个回答
网络孤客 回答时间:2020-12-11 12:03:37
看看时间准吗。
  R* S6 f, Y; ]6 m2 R" D" D有空我也玩玩。
jinglixixi 回答时间:2020-12-11 12:52:27
ldptest 发表于 2020-12-11 12:03
! k; ~+ ~6 C) f  K7 ^7 s看看时间准吗。
9 C! n, j( g3 i+ x5 h有空我也玩玩。

1 W  T) {( ]" D/ v计时尚可,长时间的误差可能会明显点儿。
Kevin_G 回答时间:2020-12-13 10:17:35
楼主,上源码
radio2radio 回答时间:2020-12-13 14:38:10
楼主,G431的RTC,有没有自动万年历功能?
) U# \( P' T$ D3 y) d就是11月31日会不会自动进入12月1日,还有闰年修正等。6 C1 T' u8 Q- D( Z6 r& x; [- e+ t
jinglixixi 回答时间:2020-12-13 18:20:38
radio2radio 发表于 2020-12-13 14:38
# ^  A1 [& o& Y" L楼主,G431的RTC,有没有自动万年历功能?: _% |! U: `% ~) I9 C
就是11月31日会不会自动进入12月1日,还有闰年修正等。1 b0 ]. e% k. N0 T8 ?0 z: y& l1 C. T
...
9 P4 D) }; x7 e. a
我给你测了下,两者都没有问题。

所属标签

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