1. 使用 CubeMx 创建 RTC 工程
8 d$ X5 {0 T1 `* H, D3 N打开 CubeMx 软件,选中我们此次使用的单片机型号 STM32G030F6P6 ,点击 StartProject.! ~; C; R6 s" Y7 y
9 c+ ?1 Y# n) R# v+ t先配置一下串口,用来打印相关信息% d: w: A5 m y. F% M1 R. m6 h
0 m. P/ h+ }$ I% @) K3 E
% P+ b7 R# B- {
( f/ z: t+ u' | v% J再来配置 RTC1 T K' z/ l1 Z+ a5 E
+ N( m( k' W) R
: Q* T0 V( q; `
1 ]+ l! y a/ @
0 Q9 H; F# J& O- a
% s6 `. p E8 o; b( t( |3 D; A配置时钟
- P4 d$ S5 s' X# n4 n8 j0 Q3 R B1 h8 _$ h7 X0 M V
' X c1 p, r2 K" D6 R: o
% k b# a2 v) Q0 N5 d8 r+ hps:本实验使用内部低速时钟测试,未使用外部晶振.
8 f9 u" I# V6 u6 O0 E- e
& v7 G5 U& r4 B; X) X2 Z) O配置工程相关选项. e* ^+ {( m3 P8 k
+ t L+ D `9 L) f+ k" v% ^
- ~% c+ k# B% z9 ^* \0 Q4 ]3 v
6 n9 @) W" M) C( {配置完成后点击右上角 GENERATE CODE完成工程的创建
! ~- f0 c2 S, X9 P9 j: R
, @$ m) J( r& O0 R2. 编程
6 Y% n$ P& f: l2.1 熟悉一下 Hal 库中 RTC 使用到的相关 API 接口
9 }; W2 o+ j/ F2 E- /*设置系统时间*/0 p$ x9 n6 @0 p* d4 e
- HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
7 b P; [% j1 Q5 G# Y& Q- I: t - /*读取系统时间*/ O# o/ e9 H9 @; F! O0 v
- HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
) V, u. M5 D8 E1 \0 ?6 W9 B - /*设置系统日期*/
+ v' Q' E2 y, |2 Q3 t - HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)3 y+ K( V+ _/ K/ Z& P
- /*读取系统日期*/( N9 ~* Y4 h- j" F
- HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
1 P3 P& n% N- F0 ~* N) @ x, F - /*启动报警功能*/
- {4 {9 F' Z0 C, _ - HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format): n# C- A8 }* V3 j* ~ U
- /*设置报警中断*/4 h1 t" ?6 d6 z* `4 a! Q2 ^% b8 I
- HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
% Q9 T1 ~5 W# C7 G# W" f& R - /*报警时间回调函数*/+ V1 |8 ^) |) i: |: X
- __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
. X) p, H! T( D' e$ Z- U, a; n3 M - /*写入后备储存器*/
- a3 U% B9 O2 K# h" ~ - void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)$ J& k, T/ T0 ~1 y2 e
- /*读取后备储存器*/. ^) m1 F, G6 e$ I9 O8 m
- uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister
复制代码 0 a! C# l- F( {" L2 s. K+ N; s5 j
2.2 串口相关的代码
; o0 H, [% r2 r3 Q+ G. V/ U C因打印相关信息需要使用 printf ,需要包含 stdio.h 的头文件,且需要重新设 fputc 的函数3 d& R j; L: ^' C2 Z5 i& a
- /* Includes ------------------------------------------------------------------*/
1 G2 W0 Q0 P( E. k( Z0 J - #include "main.h"0 m0 B, R' I& K1 ^2 y
* _) N8 e' D9 B9 G3 P- /* Private includes ----------------------------------------------------------*/: C( b4 K+ X" h5 R( b* E" w5 F
- /* USER CODE BEGIN Includes */
2 X; f/ o& W7 O: K+ L: i3 M8 [0 } - #include "stdio.h"
- T! b# O- N1 _, U - /* USER CODE END Includes */1 b9 c' x0 v3 m9 B9 j
- 2 K* i% E3 t; l9 i# l' _% e" F
- /* Private typedef -----------------------------------------------------------*/: j. U0 F" K: p- [! E0 M/ y- ^
- /* USER CODE BEGIN PTD */0 W4 J8 \8 R' K) ?* L9 Z
# i: g0 X0 y9 I, c2 ~8 a$ Y% ~- /* USER CODE END PTD */* t% b! f" ^7 z
- q7 u3 _+ A4 U5 l- @) U L. J0 T
- /* Private define ------------------------------------------------------------*/4 x; ?6 T( c; t: i) N/ R4 z; l
- /* USER CODE BEGIN PD */) r( F1 O' T1 i$ o- W5 Z; k
- /* USER CODE END PD */
" B) T& h9 W& a ^4 V$ y - 1 D' E0 M; G1 j$ H, X d" M1 l
- /* Private macro -------------------------------------------------------------*/( E9 b* N; G' ~' p( `8 D. i
- /* USER CODE BEGIN PM */# w' ^( _+ p5 c' U6 J
- ; e; v* S' D* c3 A' x
- /* USER CODE END PM */0 [) i) m4 s. l9 f! M) h" a1 v
- * u5 |7 Q9 k/ x) }
- /* Private variables ---------------------------------------------------------*/) K) U) W$ U( T9 y: L
- RTC_HandleTypeDef hrtc;7 |% ~. n* u2 |
1 Z' d; `0 h' D' u+ [3 f* c- Z- UART_HandleTypeDef huart1;0 {* y" K& Z' t y
- , E1 V" Q9 D' j+ b' c2 U
- /* USER CODE BEGIN PV */
9 V' }( }; v( k/ d - RTC_DateTypeDef date; //读取日期
7 Q/ ~, b# `& l* | - RTC_TimeTypeDef time; //读取时间8 {; Z% o' I3 F. ?
- uint8_t RTCStatus = 0; //指示RTC状态
" h: X3 v6 T1 Q9 g" ^ - + q3 V: i5 M3 \! [1 z% `8 H: |
- /* USER CODE END PV */8 T% M# I1 e: `( {! z
( p; I. L. `" W- /* Private function prototypes -----------------------------------------------*/
7 n; d" p. D; x9 u - void SystemClock_Config(void);
+ \) N0 f6 P9 K. @$ P/ z; ]' B - static void MX_GPIO_Init(void);
% ?, u& N+ {( }, e7 s' K# B - static void MX_RTC_Init(void);
$ v; f& U- P3 V9 Y - static void MX_USART1_UART_Init(void);
9 q0 K4 T+ _$ O - /* USER CODE BEGIN PFP */
! P5 l. Y5 j: W7 y% K; `2 p - // fputc 函数重映射" @5 ]4 x5 w" \4 G) B* p
- #ifdef __GNUC_! G$ L3 y9 H) ^/ p0 L6 S
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)9 O- s% k# K' A% Z( C
- #else
: p, T8 i0 Y5 `7 V8 p! ~ G - #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
7 ?4 \( C' z9 Q, T# x4 \# C J9 P - #endif' d9 m1 x/ j1 m% u: |; c+ t
- PUTCHAR_PROTOTYPE9 F) o7 _) q) N! [; T
- {" B9 C1 w5 Y P7 j
- HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, 0xFFFF);
6 {& z% @, P% S) {0 l3 C3 K
0 M; A3 h; J% ?4 ?: ]1 C- return ch;" W& x: b' |+ P5 u
- }
" _4 w6 @, Q% ?8 }; u( q& A1 J& G$ V - /* USER CODE END PFP */
复制代码 0 B( Y3 G) s9 D1 T, V3 r! [. Q
' m% H8 T% z7 X# N
RTC 初始化# h$ k0 v; t% g; ^" e
- static void MX_RTC_Init(void)+ Y+ d3 c1 p/ N6 ^; ^, E
- {7 X% y4 P: g, E6 {0 M
- # g4 `8 H' i0 ?( @8 K
- /* USER CODE BEGIN RTC_Init 0 */1 d) }! ]. }6 c5 b' c
+ N9 _; U! q- e7 c- /* USER CODE END RTC_Init 0 */
" X2 L# O% M* L2 z - ' m7 ^# Q9 p0 Q8 N/ O
- RTC_TimeTypeDef sTime = {0};
/ N7 N( D# A7 T; y - RTC_DateTypeDef sDate = {0};8 |4 c( B7 o; a* ?9 r+ F% q' e- e2 Q
- RTC_AlarmTypeDef sAlarm = {0};! ^+ E5 H1 O. C* W
6 S0 I: T7 Y& J- /* USER CODE BEGIN RTC_Init 1 */ p4 `' b1 }6 L4 P k0 w8 \
/ R! K1 g$ f9 |8 N- /* USER CODE END RTC_Init 1 */
4 V# r {6 d8 l B4 J- [ - /** Initialize RTC Only
6 r. `+ {) r* I3 h& o; c - */2 K& u0 Q6 Q# P" Q* y: V
- hrtc.Instance = RTC;' N) E4 P6 b% G1 q& ^6 c+ F
- hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
/ R+ d Z* \" @4 ^( m - hrtc.Init.AsynchPrediv = 127;
( y# {8 R* p% a( U; z I - hrtc.Init.SynchPrediv = 255;
7 c% f6 c$ p( `! |5 L; J - hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;: D# ]; S" r1 s
- hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;% L" }* c$ D @. ^# M2 R2 ~
- hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
? P: f* x1 K9 L3 N - hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;% {* F, \* L. S# L' j
- hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;# t, O$ E* `7 Z- N# _
- if (HAL_RTC_Init(&hrtc) != HAL_OK)
7 Z7 {! Y* b0 o - {
) Y6 |3 F! \6 q" ]. p+ e - Error_Handler();9 U4 S {; \8 I3 G/ |" \
- }
) v4 U3 @! ^; [' Q9 E1 O+ G
5 Z+ l8 ]0 \. J! X2 `$ B- B/ y- /* USER CODE BEGIN Check_RTC_BKUP */6 ^9 s9 H0 K! I! v& [6 L ]' X3 |, _
- // //重启后判断该寄存器是否有值,判定是不是第丿次初始化,是否要装载初始倿6 q& v: s2 W) R; L: V4 p1 t
- // if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) == 0xAA)
@, X0 K- ?7 x% i i# B' o# ` - // {- L6 n% m9 w+ d5 `- y
- // //已经初始化过了,直接跳出初始化函! l/ R- ~! }) }: A- F
- // return;8 @+ u) p2 X4 X Y3 K( m+ C
- // }
* ~: v$ a( ]5 C# k/ W0 k - // //第一次初始化,将任意后备寄存器写任意值,做个标记,标记已经初始化过了,下次系统复位时不用初始匿0 a1 u$ f" `: D5 R1 ?$ b
- // HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0xAA);
8 B- F1 {3 p/ M9 g- @2 J' L - /* USER CODE END Check_RTC_BKUP *// U( K3 ~9 s2 c, c+ `2 r8 i/ ?
: j2 E6 E& a; t4 p5 F ~: @: }- /** Initialize RTC and set the Time and Date
/ x% y% J; Y( h! z. Y; j7 S* i+ b - */: C" s' c) q B
- sTime.Hours = 14;
5 x% z/ J( E; ^, p9 m - sTime.Minutes = 50;- s; s0 w5 x) J" `. H
- sTime.Seconds = 0;. z5 l2 b6 t" b( r" e2 _
- sTime.SubSeconds = 0;
2 J: i( E9 {* b( _, L# _ - sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
# z0 b, ~3 `2 z$ B7 U* | - sTime.StoreOperation = RTC_STOREOPERATION_RESET;
7 |# k( Z5 q! Z8 f! x - if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK): A/ X9 s0 E2 n i
- {
9 j- F3 Z% `! f& S n - Error_Handler();
( {2 y" X; H4 E1 m' e5 j: H - }) ~9 M* C' A# m( x3 z+ O8 j
- sDate.WeekDay = RTC_WEEKDAY_THURSDAY;
* z* t: Q8 H! z) _% l$ ]0 f - sDate.Month = RTC_MONTH_JANUARY;9 W6 L( I' }7 P; U
- sDate.Date = 28;/ N4 P1 w% N5 I8 t; Z) p
- sDate.Year = 21;# c+ p/ @; H0 T; K4 z+ S
; A7 }0 Z4 I! E/ a' B+ r. f6 A- if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)0 [# X/ L; ?0 M# R
- {$ ~( D. A4 B, W3 J0 v9 _
- Error_Handler();7 F$ M% Z8 T! i! Y7 ^" ]
- }, @% M! G/ K0 c
- /** Enable the Alarm A+ K$ ^$ j1 n2 R2 q! I. w ?
- */& w9 G0 N; k: Y8 p+ s1 i% d& ?
- sAlarm.AlarmTime.Hours = 14; t' s) s% i T0 \! r1 R, \
- sAlarm.AlarmTime.Minutes = 50;! v+ f, c) Z. n0 |
- sAlarm.AlarmTime.Seconds = 10; //设置 10s 后产生闹钟中断6 z7 B5 N8 T( U
- sAlarm.AlarmTime.SubSeconds = 0;
7 ^; c: ~! s$ Q) t - sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
7 u' J5 z' q9 k4 t( a - sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;4 M0 v0 _1 R) }3 h9 l
- sAlarm.AlarmMask = RTC_ALARMMASK_NONE;$ d1 n a- j8 \- e! F: p
- sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;
" [4 B j8 @. J% ^4 F - sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;1 r' h+ ] n: y1 Q
- sAlarm.AlarmDateWeekDay = 28;
6 c- U; [4 a3 M1 @ - sAlarm.Alarm = RTC_ALARM_A;
) ]( Z8 `* v$ W2 T - if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)# {+ P e% g, e0 ?% o0 y& x: Z
- {0 q$ X' I g) e- ?1 F n+ _4 W* I
- Error_Handler();
: K- C& U5 B+ P3 n4 O - }
1 N& n# E' ?3 y* V1 S5 ]6 `5 `% O7 g - /* USER CODE BEGIN RTC_Init 2 */
8 ~7 _. q0 {' G/ N! g9 } - ) m5 F( P0 M' P3 @: ]- L. u
- /* USER CODE END RTC_Init 2 */9 q4 V* K1 P! ^. l5 ]/ O
- # P, N3 {6 c1 ^2 ], ~5 d( f+ L5 ~
- }
复制代码 ; v0 k8 x$ K' T
此初始化函数中 设置了当前的时间和日期,并使能了闹钟中断在 10s 后产生.
% x/ R7 _5 A3 O2 z4 K8 S/ J
# ^" P8 [" t1 C0 y8 ~1 _; f4 @# Z配置 RTC 闹钟中断的回调函数及设置下次产生闹钟中断的时间函数5 E [2 ]8 n2 W, w/ p9 D
- /* 设置闹钟下次产生的时间 */
$ P2 o. J- d4 b: Y$ i4 A5 k% ]4 @ - void setalarm(int hours, int minutes, int seconds)
* m2 y8 [: v- K7 S% p. W - {) g" [ o8 T+ P
- RTC_AlarmTypeDef sAlarm = {0};
7 h( z! d! r. l - RTC_DateTypeDef sdate = {0}; R8 v! N* o) f; B
- RTC_TimeTypeDef stime = {0};0 u) o0 E6 @; l$ x' `$ u" C* Y
- & x6 C& i; G0 b2 w0 e
- //读取当前时间
6 S1 M5 ?& P. a' e8 j0 Q1 f - HAL_RTC_GetTime(&hrtc, &stime, RTC_FORMAT_BIN);
3 ?8 D i H4 _ - HAL_RTC_GetDate(&hrtc, &sdate, RTC_FORMAT_BIN);
9 L# O% ^: i* W - //获取设置中断时的时间5 X5 _* z1 [: ], S* i) D
- sAlarm.Alarm = RTC_ALARM_A;+ H' y) l7 o! [5 b n3 L/ x: @
- sAlarm.AlarmTime.Hours = hours + stime.Hours;
/ N/ n+ g5 r" y* _( Q# Z - sAlarm.AlarmTime.Minutes = minutes + stime.Minutes;
1 [' J3 {- ^% u2 `$ n$ Z/ \6 W. B0 m - sAlarm.AlarmTime.Seconds = seconds + stime.Seconds;0 N/ l# d4 A! d8 n* @ k
- if(sAlarm.AlarmTime.Seconds >= 60)9 @2 `$ U$ Q W
- {
) T" S4 n) Z/ F/ u - sAlarm.AlarmTime.Seconds = sAlarm.AlarmTime.Seconds % 60;4 k; Z. W" t0 e4 d
- sAlarm.AlarmTime.Minutes += 1;; T4 W d0 h5 N9 {) [
- if(sAlarm.AlarmTime.Minutes >= 60)
# J; L" }3 F) e' E% U - {7 w$ \5 d3 R$ ?( q/ P$ c
- sAlarm.AlarmTime.Minutes = sAlarm.AlarmTime.Minutes % 60;
$ e5 J& h$ S* m: D J7 @8 z J - sAlarm.AlarmTime.Hours += 1;
; H2 n2 `8 B& b - if(sAlarm.AlarmTime.Hours >= 24)- a8 z8 m2 K% x8 i+ ^
- {
5 {" N- o, _, a3 V. H# c) W( r - sAlarm.AlarmTime.Hours = sAlarm.AlarmTime.Hours % 24;
% ]5 P4 Z* ?0 `6 }4 N( s) g/ @ - sdate.Date += 1;' @- I: h5 F9 ]' K! _1 U
- }- D* O! d, s& Z! e
- }
! n+ Z* G4 V0 M3 ? - }# r: E7 L) S: j) U K
- sAlarm.AlarmTime.SubSeconds = 0;
A9 U4 J8 C# z0 u- U0 q6 \ - sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
, V2 G, D# w5 A0 z8 _& D - sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;+ L2 j+ Y6 z9 \0 {/ K
- sAlarm.AlarmMask = RTC_ALARMMASK_NONE;4 `# l6 U: a% U& L
- sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;
$ f2 @" v( P0 t0 j7 ^ - sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;" }# ^. A8 ]) C2 y3 X) j; h q
- sAlarm.AlarmDateWeekDay = sdate.Date; //跨天数定时,需要注意
, U7 d+ U. ?1 U$ [- v+ s$ M - + f; |7 p$ {0 G; s5 c Z0 P) B9 P
' s6 B1 b2 X; @: E+ e: I6 K- if(HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
$ F. V' v& c3 h' t9 ~2 ` - {
t2 H$ _' e( h: R8 x, A - Error_Handler();7 U' g7 G0 M! {) k5 t
- }, O# P4 s: k- d( x, h+ o, }
- }& n6 H" @0 E; v
% r0 F% I1 l6 l% k. E4 d- /**
, H1 }# U3 U- _4 u - * @brief Alarm callback 闹钟中断回调函数
* n# [3 a1 `6 M# Z - * @param hrtc : RTC handle4 a0 S" {& S' |/ y0 S/ {& F$ I5 I
- * @retval None) M8 S. D1 [2 |4 J1 x2 S
- */" q( W B" }2 s% Z) A. K
- void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
_; F, @1 C& ]( l3 I( l - {7 ]5 C& @/ m4 {- y/ W; O4 x+ B, s
- static uint16_t sEnterCnt = 0;
( X) \ E/ |$ h, u
. f% q H/ H1 s- j$ {- setalarm(0,0,5); //设置 5s 后再次产生闹钟中断* i. F% T- J& v7 Y
- printf("Alarm A Interrupt Cnt:%d \r\n",sEnterCnt++);& r( k* @5 _" l2 |+ G) W0 V" a* ^
- }
复制代码
0 G A3 ]& T ]% f& W' u6 l$ Fmain 函数/ @! {7 W4 P4 H6 ]
- /**
6 X: I( o4 j0 O! X4 L* g - * @brief The application entry point.
6 `9 W1 ]$ f3 e! s; |0 B* l - * @retval int
, z" N7 p, z- H3 k - */
( J+ T3 ? Z! r1 ?6 W - int main(void)4 W. p' [1 `$ }( H4 Z2 u$ J# u# @
- {
0 o6 `, q4 J' g - /* USER CODE BEGIN 1 */' X% L7 e3 M+ @" T& ?8 \7 {& s
- 7 T- s# [- Q: K+ E, l4 U
- /* USER CODE END 1 */6 f5 J5 x' c) L" i8 [' F. N9 @: ]
- ) E: N/ M' U8 Q8 E& V7 N3 I
- /* MCU Configuration--------------------------------------------------------*/
& G. c; j9 ^8 Y' t9 D
6 q1 S2 J4 F- P8 E3 F3 k- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
2 I) i( H, B3 E( i9 U - HAL_Init();
) P! D" d( E0 L9 Q8 S; c0 _' ] - 5 w+ n; ], |, b
- /* USER CODE BEGIN Init */& ?& \7 N1 e6 V0 p
- & C. `6 G) ?" |: a
- /* USER CODE END Init */
- A; Q2 l- D9 K+ d* o" q! I {
8 n6 n% j' j+ d7 n( |$ q4 X- /* Configure the system clock */" j3 J$ U) w/ V% s! M( `4 f3 ^
- SystemClock_Config();( ]3 t3 s1 H, b
. r t. O7 c6 K- /* USER CODE BEGIN SysInit */8 M* K; d) d& B3 T- e0 A, i# R
$ @) r* v) v3 m% y( o, T- /* USER CODE END SysInit */+ k. L5 l% |; }$ g. }2 Y
- + @2 V' p, i2 j1 ?
- /* Initialize all configured peripherals */0 r: P) h& Y, c: S7 J/ X( I+ R0 @ V+ y
- MX_GPIO_Init();
: |: e" ?2 V! ?4 S: j$ I, M - MX_RTC_Init();
1 L/ v" C! x9 T+ S5 f - MX_USART1_UART_Init();. g: `& L1 F: n: ~, U2 G
- /* USER CODE BEGIN 2 */
1 U8 N9 f& D1 ? {1 D4 ^4 m. X - printf("stm32g030f6 rtc & alarm demo start......\n");5 ^# D% s& b7 G( Q
- RTCStatus = 0x01;; m; e7 x+ p2 |5 v- U+ M
- /* USER CODE END 2 */ ]" J3 R+ U* e' M
- 6 b" [) s' E7 V/ _
- /* Infinite loop */& r/ c0 L+ O: Z# K _8 S3 X
- /* USER CODE BEGIN WHILE */ t5 _! |5 ]" x6 j7 x' X+ i1 J
- while(1)% q/ R8 M1 ~+ E# D
- {
. {' Q Y" b) m5 w |$ U, W# O - HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
* q1 U% [5 U" w4 r - HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
* \! u, h; G- c - printf("20%d-%02d-%02d %02d:%02d:%02d \r\n", date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds);& ?, g6 c, n/ Y
- HAL_Delay(1000);3 n7 K- g9 I$ l) L9 |% m& W
- /* USER CODE END WHILE */
0 Q/ k/ _; ^5 K3 o u( K - r: Q/ E) p p& W
- /* USER CODE BEGIN 3 *// d& k' S9 P2 I
- }
+ c3 Z. ~* z4 G3 c. [9 R/ p. | - /* USER CODE END 3 */- [& V' c+ y$ J: }; R* b( t
- }
复制代码 3 G& F6 {9 B9 R
主函数中每隔 1s 读取一次实时日期和时间,并打印出来.
! S' }0 {, C. B4 D. ]1 `( l9 \3 B
1 p% W% S8 \! [, D" @3. 编译并下载测试
2 i/ v. i6 q# i: ?' o: G) U' l+ t将程序下载到开发板中,连接串口助手并打开;! k# h5 A' w, d L% \, F/ m- X1 K
实验现象: 打印实时时间,10s 后,第一次进入中断,并打印进入中断的次数,而后每隔 5s 打印一次进入中断的次数.
: b+ e' @4 H) ?+ u) }6 H
. G6 ?. G4 A: u, n# W+ @
: N4 V- d" N# v( `6 Z! c2 \% Q* s3 V& R3 u4 s5 z% _; J% z, t
|