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

基于STM32G431的RTC电子时钟

[复制链接]
jinglixixi 发布时间:2020-12-11 11:04
在STM32G431内部配置有RTC计时器,将它与OLED屏配合即可实现一个电子时钟。
; a, ?* ]! \0 u  R* V
实现图1所示效果的主程序为:
  1. int main(void)
    0 Y* Q, F' {& z9 H
  2. {
    ; L1 t* F# r8 T) R" y" e' p; L% z( e
  3. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */! N* |; k/ h$ R. X! {, U* ?8 B) g
  4. HAL_Init();
    6 F8 w9 T& h( o( {
  5. /* Configure the system clock */5 s; ^8 M, z7 e8 ~2 ^& i
  6. SystemClock_Config();+ D9 t3 {! }+ S3 f+ }: N
  7. GPIO_OLED_INIT();
    5 t, ~7 f, N! l7 Q, }; C1 r
  8. OLED_Init();; Q1 p: o0 z# O0 G* ?& D9 b
  9. OLED_Clear();
    2 R' V2 R4 ]9 c
  10. OLED_ShowString(18,0,"STM32G431",16);
    - l2 [4 ?7 }) _" q  J/ j0 q
  11. OLED_ShowString(10,2,"OLED & RTC",16);3 B" q6 }* }) U! ~, f
  12. /* Initialize all configured peripherals */8 O4 I; @2 l' B0 t8 ]. [3 w
  13. MX_RTC_Init();( i# K) A4 Y. j
  14. RTCStatus = 1;1 U) v8 o% e: Z1 }
  15. while (1)
    ' R/ Z# ~; {7 a6 R- \5 b7 ~) i
  16. {
    5 `* n7 q& K; }' o  M, ?
  17. /* Display the updated Time and Date */6 D* r2 ?7 L  Y0 Y1 }
  18. RTC_CalendarShow(aShowTime, aShowDate);
    2 n( k" _( F. p/ e8 a( L# ]
  19. Delay(200);
    6 m- W* R% k4 z6 q) q
  20. }
    * K3 i+ k2 L* B9 a2 M
  21. }
复制代码
1.jpg
                              
图1  RTC电子时钟

0 ^" {1 e$ y+ p3 W
实现RTC显示的函数为:
  1. static void RTC_CalendarShow(uint8_t *showtime, uint8_t *showdate)
    1 M% X. Q9 `$ y* _9 P6 m7 s* [
  2. {; M( {  c* q7 `' R
  3. RTC_DateTypeDef sdatestructureget;
    ' V6 t* N" v  u. C
  4. RTC_TimeTypeDef stimestructureget;+ p4 T% u7 ^% W9 E. _& g
  5. /* Get the RTC current Time */# e* h; l& W/ m; _# b# k
  6. HAL_RTC_GetTime(&hrtc, &stimestructureget, RTC_FORMAT_BIN);  d7 Y- B( _7 |! U3 ]4 L$ q1 y
  7. /* Get the RTC current Date *// R2 S1 e0 Q4 V9 ]3 q! ~3 L
  8. HAL_RTC_GetDate(&hrtc, &sdatestructureget, RTC_FORMAT_BIN);
    6 T5 b6 _7 K; V$ p- |3 Y. I. }
  9. /* Display time Format : hh:mm:ss */
    : R$ o) {6 x8 W' g
  10. sprintf((char *)showtime, "%2d:%2d:%2d", stimestructureget.Hours, stimestructureget.Minutes, stimestructureget.Seconds);
    ; a) F$ ]! K- O8 Q; b& D
  11. OLED_ShowString(26,6,showtime,16);: J( b. x$ p1 @
  12. /* Display date Format : mm-dd-yy *// g: R2 h- ^) i! ~/ C
  13. sprintf((char *)showdate, "%2d-%2d-%2d", 2000 + sdatestructureget.Year,sdatestructureget.Month, sdatestructureget.Date);& D8 @! u4 m- d
  14. OLED_ShowString(10,4,showdate,16);
    ' D1 A8 z6 c) M
  15. }
复制代码

) H- H2 r  W/ p( J
调用的字符串显示函数为:
  1. void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size)
    3 X- C3 d- }' T* z
  2. {  X2 _* b+ @& g1 f
  3.     unsigned char j=0;
    ; T. i3 H, l8 x6 {! o0 _
  4.     while (chr[j]!='\0')3 _7 e* f9 H1 ^/ y) q. z
  5.     {       OLED_ShowChar(x,y,chr[j],Char_Size);0 ~- B& B% A- C
  6.             x+=8;1 ~3 z4 ]0 V' O+ h$ b2 y; G
  7.             if(x>120){x=0;y+=2;}
    ( L( H0 Q4 q/ B$ S. L+ s  Z1 v" q
  8.             j++;
    ) x2 z7 ^; }9 h
  9.     }
    - L2 |' ~8 E; \: e
  10. }
复制代码
1 }) q( J6 V( Y, l4 v5 G* v4 v/ t
由于例程所设置的初始时间是2018年,故需要在函数 MX_RTC_Init()中进行修改,修改后的运行效果如图2所示。
2.jpg
4 w5 t; t* e+ G; k6 v: `  `. {+ r! i; c
图2  校时后的显示效果

. C& y" J, w5 A% \) U
赞 收藏 评论5 发布时间:2020-12-11 11:04

举报

5个回答
网络孤客 回答时间:2020-12-11 12:03:37
看看时间准吗。) M5 w# @: s# I' }1 }
有空我也玩玩。
jinglixixi 回答时间:2020-12-11 12:52:27
ldptest 发表于 2020-12-11 12:03
- J/ L( D) m4 _( }2 V1 R看看时间准吗。
2 i& Z! Q1 l0 y0 i/ s' o有空我也玩玩。

8 l3 Q2 X6 ]5 g+ N/ M计时尚可,长时间的误差可能会明显点儿。
Kevin_G 回答时间:2020-12-13 10:17:35
楼主,上源码
radio2radio 回答时间:2020-12-13 14:38:10
楼主,G431的RTC,有没有自动万年历功能?
( X! H) v0 g9 M8 o; ^就是11月31日会不会自动进入12月1日,还有闰年修正等。9 c, b6 h! U' {+ \5 C6 [
jinglixixi 回答时间:2020-12-13 18:20:38
radio2radio 发表于 2020-12-13 14:38
$ \6 h: `& f  r楼主,G431的RTC,有没有自动万年历功能?; }- g8 Q" k1 v  ^5 T( q) W: @
就是11月31日会不会自动进入12月1日,还有闰年修正等。
8 ]( H3 u* k$ i% ?% ]" h ...

0 s, f1 \  q6 T. W我给你测了下,两者都没有问题。
关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版