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

基于STM32G431的RTC电子时钟

[复制链接]
jinglixixi 发布时间:2020-12-11 11:04
STM32G431内部配置有RTC计时器,将它与OLED屏配合即可实现一个电子时钟。
, z$ w% r& j8 }
实现图1所示效果的主程序为:
  1. int main(void)
    - k! }" B: w9 H% ~
  2. {
    + _7 L+ o: q) A* ], f
  3. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    7 u) g" \+ E; ?
  4. HAL_Init();$ h; m; D" ^# j% U+ k* [7 b
  5. /* Configure the system clock */
    * \; u* _3 H  }2 C5 I
  6. SystemClock_Config();
    % C8 T5 I$ k* f3 P/ O; K
  7. GPIO_OLED_INIT();
    0 y, r* n. n" u3 I# r
  8. OLED_Init();
    $ \/ w) W2 n1 I# O
  9. OLED_Clear();
    . G, _0 Q- P0 F% r" @; _
  10. OLED_ShowString(18,0,"STM32G431",16);
    $ i+ ]3 d$ s& r+ U: g
  11. OLED_ShowString(10,2,"OLED & RTC",16);  Y: \$ f! z; h1 A2 n' i
  12. /* Initialize all configured peripherals */5 R! T' j2 i5 b, H* X
  13. MX_RTC_Init();
    # f/ V5 l& S; [6 v8 x  T9 R; D1 W
  14. RTCStatus = 1;% |$ L! |4 o  Z9 N! r3 l
  15. while (1)
    ' y3 N# N& M8 L9 e% l
  16. {8 }; d& h: ], M2 |5 p; T
  17. /* Display the updated Time and Date */) }3 V( s4 @+ X- Q8 v
  18. RTC_CalendarShow(aShowTime, aShowDate);" x( B1 J! x( Z$ O9 ?# j
  19. Delay(200);0 X) s9 n/ C  Q! p# o) T2 D2 t
  20. }3 O4 q2 w6 Z1 b" {& Q# N1 y
  21. }
复制代码
1.jpg
                              
1  RTC电子时钟
: A- r/ m' m, p
实现RTC显示的函数为:
  1. static void RTC_CalendarShow(uint8_t *showtime, uint8_t *showdate)
      A5 t- x, D$ T
  2. {% Y2 y  Y- [; Q2 L
  3. RTC_DateTypeDef sdatestructureget;; W- [5 a6 |+ z# }2 W. W0 j+ T
  4. RTC_TimeTypeDef stimestructureget;
    / _/ ^* \- O$ V  P
  5. /* Get the RTC current Time */# s- K8 \4 f. O6 H" S0 t- q1 P
  6. HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);
    ; S  Z+ w( D8 @! C& ]& U
  7. /* Get the RTC current Date */
    + ~+ t- j; N( j  H& P3 Y, h9 ^8 |
  8. HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
    8 d0 t8 E0 V* d4 j1 c4 P
  9. /* Display time Format : hh:mm:ss */8 q) H2 t* n7 e+ V
  10. sprintf((char *)showtime, "%2d:%2d:%2d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);+ a1 u' Z' y! d& a2 @
  11. OLED_ShowString(26,6,showtime,16);4 \, w! m  D$ g6 a& e9 p, y
  12. /* Display date Format : mm-dd-yy */
    " ]: q& T& N& O6 a; G; M4 Z: c
  13. sprintf((char *)showdate, "%2d-%2d-%2d", 2000 + sdatestructureget.Year,sdatestructureget.Month, sdatestructureget.Date);% t( z. _$ |9 ]6 T* M: {: L$ O3 O  x- D
  14. OLED_ShowString(10,4,showdate,16);
    7 f. E# ?3 Y" R
  15. }
复制代码
/ S% G' `  ~' W% b- I5 u
调用的字符串显示函数为:
  1. void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size)
    0 u- g4 V* u, F2 I
  2. {
    6 y8 b/ U: i0 d5 F2 ]* ^1 C
  3.     unsigned char j=0;6 z! }* m. G- o  B7 F9 k
  4.     while (chr[j]!='\0')4 p, E3 ^; ^8 Z) e, H, y4 O
  5.     {       OLED_ShowChar(x,y,chr[j],Char_Size);
    & t& ]5 l9 }4 U5 ^8 A* n' V: H
  6.             x+=8;
    5 \8 H! f6 R  E( n8 z, k
  7.             if(x>120){x=0;y+=2;}- u9 l) S& d0 y) a" t
  8.             j++;
    0 a) k$ ?$ h0 U, p/ |, g) r
  9.     }& b# z: ?! u4 c* f6 q
  10. }
复制代码

+ h1 A8 F" c1 _3 U$ j7 r
由于例程所设置的初始时间是2018年,故需要在函数 MX_RTC_Init()中进行修改,修改后的运行效果如图2所示。
2.jpg
" K* |, K5 s: Q! z! V2 ?1 `
2  校时后的显示效果
: a. z/ \, A$ A+ m2 l
收藏 评论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:033 h' N3 \# N1 ^. ~+ Q, E
看看时间准吗。; Q8 I3 q; W" f1 N) t! u& c3 s
有空我也玩玩。
# E4 t7 V/ M; s! D3 P
计时尚可,长时间的误差可能会明显点儿。
Kevin_G 回答时间:2020-12-13 10:17:35
楼主,上源码
radio2radio 回答时间:2020-12-13 14:38:10
楼主,G431的RTC,有没有自动万年历功能?* x6 b& w/ I8 Q) k! s) G5 p
就是11月31日会不会自动进入12月1日,还有闰年修正等。
& T6 b$ X5 c! s7 b7 U1 o
jinglixixi 回答时间:2020-12-13 18:20:38
radio2radio 发表于 2020-12-13 14:38' O' z( x# P" J; j; W' ?
楼主,G431的RTC,有没有自动万年历功能?$ P5 a% _! N( @* ~+ C
就是11月31日会不会自动进入12月1日,还有闰年修正等。
, e% p) y4 V+ F" U( K& d4 P3 Y ...
4 |0 i) Z' r: f( `% @
我给你测了下,两者都没有问题。

所属标签

相似分享

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新和工艺
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版