1. 使用 CubeMx 创建 RTC 工程1 J1 F9 d0 E* M1 s" j. B$ P2 S* X
打开 CubeMx 软件,选中我们此次使用的单片机型号 STM32G030F6P6 ,点击 StartProject.6 z2 ~: \, ^/ e9 k" q# N; @
1 f& F$ E2 `& Q5 I0 B
先配置一下串口,用来打印相关信息
! a6 P, F/ p L% p2 R C/ |4 m1 |' s* h; c( }1 `2 U) D
3 p4 ?; r3 w: H! k0 F# v
. U6 t* o3 a' u" _再来配置 RTC& B6 ?) V2 O1 J4 J& q4 [$ Q3 Q
6 N. x( O* a/ {5 T
+ W5 f7 u1 h. c6 v3 {3 u# e, ]6 v& @; x* C) {8 b5 e) W( s
/ S5 M2 ^) y4 a. C9 Z; F
+ L# V) `* O1 j; G. }( X配置时钟. U! F1 u; S9 `; O; p# e
& A3 F A) T* ~" C1 W$ ]% j7 l/ d. |6 K- J5 E: f
) ]( }$ {, F% ?8 }2 Ops:本实验使用内部低速时钟测试,未使用外部晶振.
8 J+ ]' N0 Z) i2 O0 ?4 e7 y( }
) e8 n, L" J" ^# q0 [0 S配置工程相关选项4 ~! ~2 K- \- t% F+ t8 J3 g1 q
2 v8 {* `& u M& k
! @5 A6 S1 U. I( m2 _% z) c! G& C$ U" u7 N4 _
配置完成后点击右上角 GENERATE CODE完成工程的创建
& x* s' a0 p! M' d
) b9 l) S( a2 Y" J% K2 D9 a3 n2. 编程
" z1 ]! h; D* F+ l2.1 熟悉一下 Hal 库中 RTC 使用到的相关 API 接口
' f2 \ ^; {& C' b) P$ l X- /*设置系统时间*/, w7 L" [* m: d6 s3 L7 K3 @. M; l
- HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
- B5 F6 z; C9 J8 T; \. `! f. \ - /*读取系统时间*/ X }' G p/ s: E4 l
- HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)! D9 W0 ]/ P* X7 L
- /*设置系统日期*/! K2 h0 a4 q: h
- HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)% T, b# _" c& T4 E. }) X* u& }
- /*读取系统日期*/
R; D6 l" R; V7 F" e - HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
: @; ]" b3 t2 }; H O! O3 H) i. i' C - /*启动报警功能*/. C, ?, U. o" M4 m1 k
- HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)# U' `) M q$ Q3 c
- /*设置报警中断*/
5 k1 u8 W6 k k2 o: S# ` - HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)3 f- n- _2 a% K( e U" ?$ {# O4 X- o
- /*报警时间回调函数*/
( A& L; g8 W$ Q9 n - __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
; O$ V3 B/ R& n# y - /*写入后备储存器*/
/ ?5 J Y* E" b) A+ L. ` - void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)) A0 `1 A- m3 R
- /*读取后备储存器*/% c& @. Z8 [4 W+ x4 v6 Y: R# b
- uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister
复制代码
6 q* L; d& p/ h( J; O% l7 K* N2.2 串口相关的代码
7 [4 [; H! B$ L因打印相关信息需要使用 printf ,需要包含 stdio.h 的头文件,且需要重新设 fputc 的函数+ s+ K. x7 O- W) L
- /* Includes ------------------------------------------------------------------*/
7 D' ^" n" Z, H8 W - #include "main.h"
9 a4 d0 o3 h: m9 f
: }" ~2 r0 J, K0 @6 c- /* Private includes ----------------------------------------------------------*/; _! d/ U% x! M! u# B4 v
- /* USER CODE BEGIN Includes */
) n9 q. C5 s; {. ]9 e - #include "stdio.h", K) A" i( z7 z+ c, b# ?5 \5 R
- /* USER CODE END Includes */
" v( W" g4 e2 p1 | `, R - 0 i2 y, ?, t9 O4 E0 E4 f5 |
- /* Private typedef -----------------------------------------------------------*/0 s5 S! N8 X! s3 k
- /* USER CODE BEGIN PTD */9 f m( E' Z% {& V0 ~# [1 R
- ; [9 X; ]+ x3 h# C+ I# {9 _2 M
- /* USER CODE END PTD */+ Z* Q; L- @/ o
( ^2 T/ j6 }, K- F8 I- t- /* Private define ------------------------------------------------------------*/3 e$ f& P! I1 g& ?0 _
- /* USER CODE BEGIN PD */" J# u) P1 h- X* D( i' L6 N
- /* USER CODE END PD */$ y: ~: K) e0 A6 m8 V8 [8 X
- / t3 s. ~( n# m$ _6 P
- /* Private macro -------------------------------------------------------------*/
* s( O! ?3 o8 U- \& j5 s) W - /* USER CODE BEGIN PM */. i" K" V' k2 i5 G: @9 S, ^/ j, `
- ( @0 J8 P# ?) D
- /* USER CODE END PM */
8 g _- m% S2 h* ?, j - 3 P% a0 h; N: x: ^' z8 w5 d
- /* Private variables ---------------------------------------------------------*/) {2 G( f- {; Q$ [$ ?" g1 T" w
- RTC_HandleTypeDef hrtc;, ^; ^/ A+ X* Q
- 3 Q* q0 v9 m4 W! v& f0 c
- UART_HandleTypeDef huart1;
& c P1 Q i/ q0 R2 [3 E9 Q3 E' [! i
% Q: q% s" z* y6 D+ t# Z: X- {- /* USER CODE BEGIN PV */; l, z1 O/ x( H! |9 c
- RTC_DateTypeDef date; //读取日期
2 F9 h0 u/ @( S4 ]6 G) r - RTC_TimeTypeDef time; //读取时间5 |2 O! X* M+ T) D: k# z8 \6 m8 l3 [
- uint8_t RTCStatus = 0; //指示RTC状态0 C7 Y& P7 F* I9 F; w: _* ~
- 5 k# K. O1 m2 F1 u: N: O
- /* USER CODE END PV */) [6 Q9 P9 e! [& f" V5 Q
8 Y) M/ I& S/ M$ @6 Y6 u+ y- /* Private function prototypes -----------------------------------------------*/) f+ k. _ f e% w1 j; q4 P# D' J3 V
- void SystemClock_Config(void);; O4 r9 @& d- K3 p( l
- static void MX_GPIO_Init(void);
- A# P! x# A# K - static void MX_RTC_Init(void);
; v, v; n7 ~% L! s3 J$ u' h3 N - static void MX_USART1_UART_Init(void);
4 Z( [6 Y& k- \ - /* USER CODE BEGIN PFP *// ^& J" h/ P6 z# s8 ^: s
- // fputc 函数重映射 F' {9 d0 ^! T- J
- #ifdef __GNUC_: ]/ |2 F" p4 @8 g
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)6 ?9 T6 ]1 N0 m/ _6 s+ C5 J
- #else1 H8 Q9 o' c+ w% F2 z* p- @
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)1 S9 t( m: \% i! s _ G% K
- #endif
! S2 |3 `# ?$ D - PUTCHAR_PROTOTYPE
5 I9 m, D& ~# D8 p) o$ O5 Q: \! v - {: n1 r/ ~3 T6 z- e+ S
- HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, 0xFFFF);
0 v, w, Y2 E0 c - ' y; r& S2 {3 n. ^/ M) D# _4 }
- return ch;
( O* h7 E1 R, H1 G0 L - }
2 h6 x/ d+ d G0 Z- x - /* USER CODE END PFP */
复制代码
, n& l! t- e1 m% W, s8 V* l9 z- |# X3 b- y: I
RTC 初始化4 o/ H7 U) X- z4 ?
- static void MX_RTC_Init(void)
7 \! ^; H' t- Z" d9 u7 h- A - {
: w0 E% `5 O2 K, [! J
9 q& L# _- r! M* d9 B- /* USER CODE BEGIN RTC_Init 0 */0 Z, I. f8 y) V$ Z4 R2 [+ L8 t
- 7 ?- B" d* X% ]- s3 {4 c8 `
- /* USER CODE END RTC_Init 0 */
7 w0 M8 N+ t B - 9 D2 r2 V9 a, R! @" c6 x/ ?8 ^
- RTC_TimeTypeDef sTime = {0};
. a) t4 B% m9 ^( ^7 f D7 R - RTC_DateTypeDef sDate = {0};- M! R7 [7 I/ u& Z: b* d; F
- RTC_AlarmTypeDef sAlarm = {0};' d( |* a) h6 ?
- 1 ^' K( N' k* W% p& `% {9 i# g
- /* USER CODE BEGIN RTC_Init 1 */
5 C* Y) J0 J" m/ T
) m# I% \$ n6 Q3 R5 Q% a- /* USER CODE END RTC_Init 1 */
7 S9 g2 f! F2 S& Z2 a; \ - /** Initialize RTC Only; _9 ]( e) N9 N( u
- */
9 J! Z% K2 B0 t: d" J7 V - hrtc.Instance = RTC;
( X- ], S4 E* h" O2 @* f - hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
3 n: q& [% t* h% K - hrtc.Init.AsynchPrediv = 127;( d2 s9 ~( |3 M5 X
- hrtc.Init.SynchPrediv = 255;" {& z6 z5 H+ @4 M, ^; l. ^9 r+ V
- hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;! P# N6 \' l1 \
- hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;; W9 Y' ~5 z$ U: i" ?; P
- hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;1 U4 Q# I6 R! ~% m- i; D5 J
- hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;8 t; z0 P& q6 E% G: t& f
- hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;% b# ~9 x" r2 K
- if (HAL_RTC_Init(&hrtc) != HAL_OK)5 R9 y- W& t6 Y& Y- _
- {6 {8 U% M" {8 t
- Error_Handler();+ E- e( {* H9 @ U- [9 }* v1 }% v
- }
- E& ~4 w( P- H& @ - 4 f7 F5 z3 A! c# r2 L
- /* USER CODE BEGIN Check_RTC_BKUP */9 a% w( p( R2 y" U8 d( Z
- // //重启后判断该寄存器是否有值,判定是不是第丿次初始化,是否要装载初始倿
J+ ^& \+ x6 a, [, O1 [ - // if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) == 0xAA)2 q$ q( n- k- G+ r$ O- u& u4 Y
- // {/ B) }' {' W1 k
- // //已经初始化过了,直接跳出初始化函" C; U; c1 ]2 s2 Q% O1 b Z0 u! y
- // return;
2 L9 w2 s; u6 r+ N9 ^. r" W3 a - // }
9 H- c: a' a2 L% h. x/ s- \ - // //第一次初始化,将任意后备寄存器写任意值,做个标记,标记已经初始化过了,下次系统复位时不用初始匿+ [9 L1 j j. L: A5 A& R
- // HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0xAA);' o2 p R, l' r( ]3 G
- /* USER CODE END Check_RTC_BKUP */- x( @5 B2 Y7 v i; T5 f
7 N/ E [8 h3 O1 y* r3 n4 ?- /** Initialize RTC and set the Time and Date
- z; i5 F9 H" J9 t! x$ M$ a - */
' K/ Q; V- f. U; S8 A - sTime.Hours = 14;5 n& @: n5 M& l$ x3 T; V
- sTime.Minutes = 50;
% q0 R3 a H. c - sTime.Seconds = 0;: B: \% X* K2 o
- sTime.SubSeconds = 0;
9 l1 n! Y1 S: c! @ - sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;) ~0 x: C6 B9 ^; O! m
- sTime.StoreOperation = RTC_STOREOPERATION_RESET;9 A7 R9 r3 j6 b8 k$ K2 N o7 P, r \
- if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)/ x9 q5 J* O- c* i3 j
- {
, T9 R' S i8 u: | h - Error_Handler();6 x" M6 F& t% P
- }
" _! @3 C5 T& v. Q) [ - sDate.WeekDay = RTC_WEEKDAY_THURSDAY;1 D* _. N/ w2 @$ ^% U9 Q2 X
- sDate.Month = RTC_MONTH_JANUARY;
. S0 M( p# E* u% b - sDate.Date = 28;
: [8 ?% @1 M9 X9 Q; } - sDate.Year = 21;
2 j" D: t# j9 Y9 Z" c* A- x0 Q
9 O0 }, q. \* y' O3 y- if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
^3 Q1 ]4 S+ p, |& `- v - {$ i; t* R& B5 @7 a* d+ i
- Error_Handler();
! v- O* c: ~4 M, i, l+ W, u - }) _% _- v+ s/ r% f2 U! f; [% O$ o; c) f
- /** Enable the Alarm A* p; d$ i; ]$ E' t" V( ?
- */- g. o+ H! g& E6 v1 O
- sAlarm.AlarmTime.Hours = 14;
( G3 O0 f9 J2 P - sAlarm.AlarmTime.Minutes = 50;1 b& ?0 ?+ i$ z8 ], t. p! W3 c
- sAlarm.AlarmTime.Seconds = 10; //设置 10s 后产生闹钟中断+ q3 _ h6 B, X' k0 W
- sAlarm.AlarmTime.SubSeconds = 0;) l+ V1 T& R& K1 H' }" x
- sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
7 K- u- g1 ~: S* C' ]% a - sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;# b u7 g8 B" b% J
- sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
# H, u0 M- u- m a - sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;
: T T. h8 l& P2 L3 k7 U - sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;& l W1 v6 V* p; t7 C! [
- sAlarm.AlarmDateWeekDay = 28;7 X9 P" P- l/ E, j+ V/ m' x
- sAlarm.Alarm = RTC_ALARM_A;
# q7 b* l1 u$ V' _/ n% b. J - if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
/ F4 [7 W, a. j1 \! t - {2 A3 e" w0 V" r7 @+ {
- Error_Handler();, m5 v ^; R; Z+ k
- }( U; Q/ P5 n4 n
- /* USER CODE BEGIN RTC_Init 2 */
. X6 }6 K2 k7 ?; d - 4 f7 q' L* B6 p3 r% P
- /* USER CODE END RTC_Init 2 */7 m0 U3 h& ]+ Z$ ]& A) y" L$ c
- - G+ U, B0 k" a
- }
复制代码
" _$ I8 i9 }# _此初始化函数中 设置了当前的时间和日期,并使能了闹钟中断在 10s 后产生. C$ |! {& X1 e: e
4 g( x7 {% N, r" g0 W5 [/ C" E配置 RTC 闹钟中断的回调函数及设置下次产生闹钟中断的时间函数
. j1 v; s i$ c/ E( a. F- /* 设置闹钟下次产生的时间 */
- W6 f! T0 j# Q' T/ ^ - void setalarm(int hours, int minutes, int seconds)
. X8 z) j3 A6 U9 Z9 g8 j, L# l - {
+ y% a7 z% {8 r: V - RTC_AlarmTypeDef sAlarm = {0};, ~0 z& R, c- @. U0 Q; a( v% Z4 d
- RTC_DateTypeDef sdate = {0}; : j" k/ M! w% J5 o& O
- RTC_TimeTypeDef stime = {0};
" P* S; D% u9 Y
. |( y$ q% X2 t4 u7 p7 F- //读取当前时间
* l3 P) F2 P7 m - HAL_RTC_GetTime(&hrtc, &stime, RTC_FORMAT_BIN);
2 J; @, T. j9 X' _; m - HAL_RTC_GetDate(&hrtc, &sdate, RTC_FORMAT_BIN);
9 _" J9 a& p6 s" e* O5 p1 E) B+ o3 j - //获取设置中断时的时间
$ A9 \7 Z9 ?% b5 k8 D' [ - sAlarm.Alarm = RTC_ALARM_A;. |1 s. {6 ^' E: s. V4 u+ ^+ ^- q
- sAlarm.AlarmTime.Hours = hours + stime.Hours;, t! A E: h" @% b4 V4 E$ H* g' y9 T
- sAlarm.AlarmTime.Minutes = minutes + stime.Minutes;3 {: w+ w9 A4 {- R2 h
- sAlarm.AlarmTime.Seconds = seconds + stime.Seconds;
& _# _( D" s5 p; I9 r. X - if(sAlarm.AlarmTime.Seconds >= 60)
5 h& ?& G+ a6 P' f2 p) H+ g4 Y8 w - {/ }; U9 R8 B' b. D
- sAlarm.AlarmTime.Seconds = sAlarm.AlarmTime.Seconds % 60;
% C% F6 V9 A4 @; o - sAlarm.AlarmTime.Minutes += 1;
: H. }* E9 q! B& P - if(sAlarm.AlarmTime.Minutes >= 60)
/ ]9 _7 }9 ]2 S. X$ z4 M% c - {5 [* `% ^ z7 N
- sAlarm.AlarmTime.Minutes = sAlarm.AlarmTime.Minutes % 60;
; Y4 R4 {8 v5 y - sAlarm.AlarmTime.Hours += 1;
6 t/ K4 _7 x6 y9 Y2 @2 t y - if(sAlarm.AlarmTime.Hours >= 24)
9 k; x+ V7 M2 e3 d8 W7 W - {' J5 ~ |' S, y
- sAlarm.AlarmTime.Hours = sAlarm.AlarmTime.Hours % 24;5 t7 `$ n( B6 w/ X' @# c
- sdate.Date += 1;
8 o/ L$ Z& i# v' O - }# `, l/ s% p+ S/ a$ R
- }
0 E% \- S& C3 C3 U0 N- R - }
! X5 H. t Q, d% W: U/ z7 k3 }( m - sAlarm.AlarmTime.SubSeconds = 0;
6 i% V5 j j( I - sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;9 _% e& l6 N& j Y+ Z
- sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
* R* Y' q) T! B/ d9 }5 T - sAlarm.AlarmMask = RTC_ALARMMASK_NONE;8 P R a! \* F3 S J3 o/ R
- sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;6 B6 a; i- i3 d0 L P e1 x
- sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
$ S+ v A5 r& N) Z6 q* [( w - sAlarm.AlarmDateWeekDay = sdate.Date; //跨天数定时,需要注意
4 M' H: d7 ?5 N4 v6 @ - 8 N d6 _' c; u) A, o$ G
2 \9 j5 V& l$ N2 r$ u1 U- if(HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
* a+ k& S# S, U# X - {9 `4 f! S4 J% s x4 n( }# E* w5 ^5 W
- Error_Handler();
9 }( j/ V% ^* R+ x" j) Q" @8 H - }
1 i0 W8 z p8 S* Z! z - }
6 ^5 m7 x9 W! w/ }; S: j& m - 4 c( [$ U( d0 Z. A* x
- /**
, | H% F# t& i/ V& y u - * @brief Alarm callback 闹钟中断回调函数
% L2 X! K! E& T* C0 T- J; { - * @param hrtc : RTC handle# v3 e+ _: f3 L# C; O4 W5 Z
- * @retval None2 A, F3 x; p9 K0 R# m( e
- */
: H' h4 I5 j# x, @ - void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)) A- \ M: t3 F- r7 j6 Q6 Z$ ]
- {- ~7 ~2 t1 Z# ` D! C8 o4 M
- static uint16_t sEnterCnt = 0;
! R! w; ~, w; B+ r6 \" p - 9 y/ C0 ]8 Z( Q( p& ~
- setalarm(0,0,5); //设置 5s 后再次产生闹钟中断+ i# C- X6 Y* [+ w7 i' x
- printf("Alarm A Interrupt Cnt:%d \r\n",sEnterCnt++);
8 y- p) H* h$ z! O - }
复制代码 ' R) ^) Y( _ E3 K
main 函数
/ R$ w& T/ p9 Z5 J% k0 e g- /**$ r1 H3 U; B7 z |5 f( {
- * @brief The application entry point.
6 G0 |1 H& W& s# a7 f - * @retval int
+ I3 P/ l# T5 q - */
3 y0 Q1 X7 q; l6 ]2 W - int main(void)8 M; Z# w5 K. Q I7 V+ K E
- {8 Z, w) S) }0 t$ j( |3 i3 ^
- /* USER CODE BEGIN 1 */
& u- \$ n$ l7 n/ `2 X: q - 3 I, C% R" z I
- /* USER CODE END 1 */7 h/ [1 _3 k" \ N0 l
; W) s- c, {+ ]- /* MCU Configuration--------------------------------------------------------*/ B' M8 ?* ~: F& o7 I' z
* y8 @. P" K9 [9 ?% e- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
J) G1 S5 B- N' G) J - HAL_Init();& A5 @% L, D. o' M1 C7 r2 m
- - z* N+ w9 S8 K) _1 E
- /* USER CODE BEGIN Init */
' y8 J8 g4 S- h) P
' v @4 k/ K1 P: N6 t8 U0 ~' `- /* USER CODE END Init */
. [2 Q$ v5 r5 a7 t2 l% B
* R) I# ^' g) F- /* Configure the system clock */
1 g2 b. F9 N5 ^ - SystemClock_Config();& w2 Z# D$ c1 u, l
0 h! K0 m4 u. j' o1 ?- /* USER CODE BEGIN SysInit */
% ?9 G4 D/ L; C( M, n3 J - 6 I! @: p5 c4 ~2 `3 Q
- /* USER CODE END SysInit */
4 p; D( X7 H+ O* t8 m5 G - " A5 ^$ x, ?4 E4 T* y+ r
- /* Initialize all configured peripherals */, x- Q2 u; G0 U: R% j& b8 C
- MX_GPIO_Init();
9 V, E; ~* W. P, ?1 E1 ~/ f - MX_RTC_Init();
/ U3 y: O% \! F8 {- ^ x4 B - MX_USART1_UART_Init();
- m: N+ A; Y0 y# E5 O) c - /* USER CODE BEGIN 2 */9 T; D( P y0 x8 M+ U: [
- printf("stm32g030f6 rtc & alarm demo start......\n");
: e% k9 F" r: X8 H - RTCStatus = 0x01;
2 `6 u, W; T' |. E+ V - /* USER CODE END 2 */: ]' V( e* G8 s% b8 X% N6 a4 d) _
- 3 R7 y+ e$ S; I' K0 b
- /* Infinite loop */
: {# Z5 O8 C/ F. |* c6 S4 d) r - /* USER CODE BEGIN WHILE */4 z, h8 _. S, s) a* C! Y$ m
- while(1)9 o# A2 D }/ D7 X) w3 r; Q
- {3 O5 W/ Q2 s! ?- _0 X+ a( a
- HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
; y. s- m, h# A+ {% b% D! n% w - HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
/ t& M/ O6 I# R7 E - printf("20%d-%02d-%02d %02d:%02d:%02d \r\n", date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds);+ ?( C+ C/ _6 P N
- HAL_Delay(1000);
; ^* u" O: T" f# e! {6 w* }3 S2 L - /* USER CODE END WHILE */
8 y) D) H( e9 Y/ }, L; G - ' e) n0 J. e" w& [" P
- /* USER CODE BEGIN 3 */) c8 ^0 c4 _+ S- n6 }9 \
- }- B- R1 a9 ]+ M! |
- /* USER CODE END 3 */
6 K) f) z3 v# \6 ~5 s9 } - }
复制代码
# d0 P0 {3 v# _/ a主函数中每隔 1s 读取一次实时日期和时间,并打印出来.
2 [/ B6 ]# A: D3 N8 j$ H6 ]' y6 V5 a
3. 编译并下载测试9 S* _* W) ?6 L4 R: ` x
将程序下载到开发板中,连接串口助手并打开;( `# _% R+ q+ |: L) S% O/ ^+ O5 g+ L
实验现象: 打印实时时间,10s 后,第一次进入中断,并打印进入中断的次数,而后每隔 5s 打印一次进入中断的次数.
1 o0 d% ?3 c9 V* A1 W: x
8 _( {9 m8 b8 k( ~7 A. g; l& u0 s# r6 {1 G+ C5 s; m1 A7 ]% q' _) Z
G% k: m4 k% M& A |