1. 使用 CubeMx 创建 RTC 工程
2 O6 @2 `& }0 B! O9 ^' [打开 CubeMx 软件,选中我们此次使用的单片机型号 STM32G030F6P6 ,点击 StartProject.
. y2 K+ W7 u( g. R
, \( U/ }' }% \* N* E2 h先配置一下串口,用来打印相关信息( }/ C% ~# f: Z# Z. J
: S' x9 L) _4 {" q; A# ?
3 q( f5 v' G6 E8 r! A% i5 p9 l6 J
再来配置 RTC
0 l' E# w( _# o6 B( w' A
4 ]; Z+ n4 q5 b5 J) L% s7 ?! d7 q
6 U: \9 E( t* }: V; P7 a5 r# f; y. n c& k5 ?
- y3 c: h. p. f# Q; w5 K+ n* @4 A, Z3 m* R
配置时钟8 n9 K9 M! h7 R3 I5 w
3 B4 ~% `% \" M' j- ], z" V
+ e& ^8 u8 g% d2 x3 T' a
& n: T5 i4 Z v1 W" m Dps:本实验使用内部低速时钟测试,未使用外部晶振.
8 r# m: M4 E* Y9 C. S$ N- y+ ?) f- ], t+ T, r! j/ j5 p
配置工程相关选项
+ E5 x" A, U: u3 G/ U7 q
4 X! q( V2 r& }! Q8 h0 C5 y% c& P6 h0 K7 _: Z5 z& N
' u. p5 h% f9 B3 d% v9 M6 M配置完成后点击右上角 GENERATE CODE完成工程的创建5 N2 f# r5 y3 w, v/ R
% [- d' b4 ]7 Z3 I2. 编程# z+ f. t' l* g3 Q) `
2.1 熟悉一下 Hal 库中 RTC 使用到的相关 API 接口
; d5 J5 L4 _ L! ?' U- /*设置系统时间*/
! Z$ z6 z! g* [5 ]7 V7 N% G - HAL_StatusTypeDef HAL_RTC_SetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format) - f: \. `0 X4 A2 m; y
- /*读取系统时间*/& L: Z. T% Y' L7 h1 {. R( \4 F
- HAL_StatusTypeDef HAL_RTC_GetTime(RTC_HandleTypeDef *hrtc, RTC_TimeTypeDef *sTime, uint32_t Format)
( a. u0 D3 z+ S7 z, {' a- i - /*设置系统日期*/7 r. `. h$ Y9 Z- P8 M3 y7 ^
- HAL_StatusTypeDef HAL_RTC_SetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)) h, `8 u- X, e" n# X
- /*读取系统日期*/5 A! n& E. `3 G$ c, X! Y
- HAL_StatusTypeDef HAL_RTC_GetDate(RTC_HandleTypeDef *hrtc, RTC_DateTypeDef *sDate, uint32_t Format)
" `- p2 Q/ H p7 S- m - /*启动报警功能*/
' S' a" m; X; f - HAL_StatusTypeDef HAL_RTC_SetAlarm(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format)
1 [4 Y9 [8 _( ~* h - /*设置报警中断*/
( q2 m2 s; \# c; u7 O - HAL_StatusTypeDef HAL_RTC_SetAlarm_IT(RTC_HandleTypeDef *hrtc, RTC_AlarmTypeDef *sAlarm, uint32_t Format): _7 {% ]. Z: k. ~& f
- /*报警时间回调函数*/
2 K4 C# c; |5 o) [ - __weak void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)! Q( q0 o6 t q0 z; o
- /*写入后备储存器*/
( j2 O) Z. k$ q1 M$ s - void HAL_RTCEx_BKUPWrite(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister, uint32_t Data)5 ?! i1 l- f4 t; ~
- /*读取后备储存器*/' A% m% {% c) B1 C
- uint32_t HAL_RTCEx_BKUPRead(RTC_HandleTypeDef *hrtc, uint32_t BackupRegister
复制代码
7 f, q7 Z" D" c, a/ u6 u0 I2.2 串口相关的代码/ h6 V C& m0 K( p
因打印相关信息需要使用 printf ,需要包含 stdio.h 的头文件,且需要重新设 fputc 的函数( y {$ J, x$ m* e
- /* Includes ------------------------------------------------------------------*/
3 S" I; s C" n, f# R+ v7 S" ^! B - #include "main.h") e% k1 f$ Q* f# {$ W
3 _/ P; y }# v$ g6 ^- /* Private includes ----------------------------------------------------------*/
7 p: c+ Q+ B; f3 G8 Y5 j/ ~4 D4 u; _5 q - /* USER CODE BEGIN Includes */7 ~1 I" J% }# Z: b$ j
- #include "stdio.h"" p$ a+ V# D8 |7 Z+ e1 R9 U4 U
- /* USER CODE END Includes */5 s* y2 g% i. P$ d
- . b5 V) C7 g8 A2 |$ j
- /* Private typedef -----------------------------------------------------------*/$ N$ m7 Y5 ]( y4 |& `$ y' ^& O
- /* USER CODE BEGIN PTD */# r" y, W% K2 u
6 R+ P7 I7 l7 Q% L& F- /* USER CODE END PTD */
: ~8 S' @( ~; N4 \0 u
) t( z8 H( H) u' ^- /* Private define ------------------------------------------------------------*/
1 n8 R/ `6 e! T; P5 z4 V( @ - /* USER CODE BEGIN PD */
8 U( g; `* U) ?7 V' ] - /* USER CODE END PD */( o/ M+ |! O* ?, l* k; C1 P: f: k
- . I1 E7 Y# q+ ~+ D; Q
- /* Private macro -------------------------------------------------------------*/; k6 u/ i+ u" O6 [3 b* K. b
- /* USER CODE BEGIN PM */
6 T9 O& C- d* a$ u: D. {* j - & [9 Z% x3 B# J* g" ]0 U9 q# {3 ]
- /* USER CODE END PM */, O9 f: C6 a1 T, n! l4 a& @' Q+ L
% R U# T0 B# s7 A( G4 q- /* Private variables ---------------------------------------------------------*/% V8 h2 K* Q2 U0 g, |
- RTC_HandleTypeDef hrtc;
7 Q5 o- d7 P! Y) W. e. d P! i
7 n( n4 T' Q# w1 e0 y- UART_HandleTypeDef huart1;
% A, S. U. z e
1 K/ ^, y7 k( ~/ g- /* USER CODE BEGIN PV */+ `, M K$ P6 W0 [8 }: H( T# I, g
- RTC_DateTypeDef date; //读取日期
3 Y. |) h, i+ t! A9 k; D - RTC_TimeTypeDef time; //读取时间6 c8 o3 l9 U2 n/ L2 {7 S
- uint8_t RTCStatus = 0; //指示RTC状态
& k% q- }6 o) X4 V; n - 8 a" |+ F$ v8 I/ e# ^6 N- R! X/ H
- /* USER CODE END PV */0 d* r/ ]0 s. ~7 B; w+ E4 N. `
' ?8 K& q! K$ d- /* Private function prototypes -----------------------------------------------*/
- r% W6 D+ Q" W( a7 S$ l - void SystemClock_Config(void);/ e& R5 \0 n/ q: H0 Z( S* z! F
- static void MX_GPIO_Init(void);4 W3 j- M3 p7 |( s
- static void MX_RTC_Init(void);" p8 s2 M* E& Y% J X# A4 A- w4 {
- static void MX_USART1_UART_Init(void);. ^% v. u; o2 q8 a& t1 x
- /* USER CODE BEGIN PFP */
4 m8 `0 i6 O+ ~" s. q& w - // fputc 函数重映射
# q/ c l3 o8 n/ | - #ifdef __GNUC_
* c3 {6 }. l0 {3 x' w* z - #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)% C( f, z, C6 T* b% u8 Y
- #else# m; `& r1 Z) R7 L4 h; K$ ^9 g
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
, G5 m1 Z" d9 e* x - #endif; r$ v& {' T: e; w
- PUTCHAR_PROTOTYPE
) o/ S+ d6 \1 s, }9 V4 { - {) Y+ ? s+ W7 B. e. Z6 i9 H
- HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, 0xFFFF);& l4 v% y9 w$ u
- . X/ s# B. M6 _! c1 O" J, t4 b
- return ch;
# n% F5 c0 j) `( [& G% j - }
/ }, I* X2 a& U - /* USER CODE END PFP */
复制代码 " \0 b# ~/ T# O S' A5 j, [" z
9 q% {2 {% `7 uRTC 初始化. d$ c# k. ?( N8 Y/ y7 f
- static void MX_RTC_Init(void)
, i |) K; q; [ - {9 p) n* T/ L9 i. l0 Q0 ~
6 T+ H# r- r: w! b" Q6 l" e- /* USER CODE BEGIN RTC_Init 0 */* z8 D* o: u# B
; Y9 T8 S& G! ]- /* USER CODE END RTC_Init 0 */
1 E1 I1 I1 [5 O' M
0 o+ Q" x9 V1 o$ P( @8 E0 e- RTC_TimeTypeDef sTime = {0};
5 \; K ~# q. @3 P4 W8 d - RTC_DateTypeDef sDate = {0};& p$ S4 Y0 L& ^/ I y( H
- RTC_AlarmTypeDef sAlarm = {0};
% Q8 {5 K1 c# P8 L4 i - , H3 C9 M( { h" }5 r* {$ G. P, g( }8 u7 h
- /* USER CODE BEGIN RTC_Init 1 */0 ?* p9 A: Y, Y9 ?# ? z7 E0 f
- 6 Y2 W6 F* I$ e$ t0 v2 o2 x
- /* USER CODE END RTC_Init 1 */5 q2 R F4 A7 U( l5 g
- /** Initialize RTC Only. C! o7 y9 P3 P6 P
- */
- Y' ?8 v5 G: c& s - hrtc.Instance = RTC;% @2 j9 ?" ?( `, n E4 D$ \
- hrtc.Init.HourFormat = RTC_HOURFORMAT_24;; I, t1 t& s! _& c6 Y& G8 N
- hrtc.Init.AsynchPrediv = 127;
; ]' \" y( d8 ^8 ]+ G" J - hrtc.Init.SynchPrediv = 255;
. w. }( q/ P9 } - hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
( g( }& Q9 Q! `' Q - hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;0 L2 ~- \( X$ j
- hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;: I6 P; x1 ~- I5 g3 `/ D" F0 v
- hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
+ t; _) v; ]3 Z - hrtc.Init.OutPutPullUp = RTC_OUTPUT_PULLUP_NONE;7 ?3 S# e# v( f. R5 ^
- if (HAL_RTC_Init(&hrtc) != HAL_OK). ~% j0 q7 }% d: C
- {9 x" K4 [ n1 U7 u- P& b% k# B
- Error_Handler();
! n; |( p7 P- F - }
1 f- B8 v0 _3 H" H. K1 u
% `/ {! v- x, {0 n% q- /* USER CODE BEGIN Check_RTC_BKUP */1 \( a1 U5 ?0 @: U
- // //重启后判断该寄存器是否有值,判定是不是第丿次初始化,是否要装载初始倿
$ A# A- c. M* i) c8 E - // if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR1) == 0xAA). ]( K( c4 t( t [" A
- // {
3 d. a0 k, A) |& F% @* X - // //已经初始化过了,直接跳出初始化函- x" G, p8 d5 v* G
- // return;
( U i% `$ V! |1 J/ [ - // }
+ [% X+ J. x2 q; W. y - // //第一次初始化,将任意后备寄存器写任意值,做个标记,标记已经初始化过了,下次系统复位时不用初始匿
3 L/ J, ]9 P8 O2 b" u' I& M. u - // HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0xAA);
( n2 a2 X. c( a% ` - /* USER CODE END Check_RTC_BKUP */. N$ U: g) F6 N( I! e3 A
3 ?% h! p0 E }: M- /** Initialize RTC and set the Time and Date; F+ |1 M( a t7 A9 C8 ]3 m5 j6 T
- */
4 S$ o1 Y+ f0 x: W4 M* Y4 U - sTime.Hours = 14;2 u+ a: r& C6 j! g! x1 U( G
- sTime.Minutes = 50;+ [5 j4 }1 D$ g- y
- sTime.Seconds = 0;
$ a) t3 s& E1 x - sTime.SubSeconds = 0; [, Z+ g7 Y/ b& [# i
- sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
a3 v3 s) p1 Y+ j; C% ?" h - sTime.StoreOperation = RTC_STOREOPERATION_RESET;) d2 J9 B8 `/ A" N& [( M0 A) e# w
- if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
7 N: P' ^1 u" }' X% ^0 b - {
/ }9 Q5 S" l: g( B+ a w1 { - Error_Handler();
8 t6 O& t( g7 J8 S( m# n) t) [ - }1 c5 G% T6 c% O2 }- p
- sDate.WeekDay = RTC_WEEKDAY_THURSDAY;
6 `& I* q) r3 F- e - sDate.Month = RTC_MONTH_JANUARY;! L% ^ k( L0 Y! C6 W" ~, Q
- sDate.Date = 28; @: I- M* _2 C
- sDate.Year = 21;
+ T$ t5 Z4 P3 ?$ W! U! H3 B3 [ - # ]: k L7 i T7 e- e E
- if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
* j: z4 l. n" h0 s, R3 M# w$ G: A - {( e, }, r/ s/ W, x* c
- Error_Handler();9 T. z1 a! S7 G2 O
- }+ }5 {0 B6 b' V/ @4 {$ b
- /** Enable the Alarm A( k* ~# @. p- _% M& ^/ Q" s* d& H; Q6 q
- */
$ z/ [: D S. l! L* M1 @1 s! N: ] - sAlarm.AlarmTime.Hours = 14;
7 Y) S! ^+ M$ B) ]5 M8 d - sAlarm.AlarmTime.Minutes = 50;
' a& t+ m$ r8 a, ` e& _8 H - sAlarm.AlarmTime.Seconds = 10; //设置 10s 后产生闹钟中断7 I7 g: W( E% B- N9 S! N B4 i
- sAlarm.AlarmTime.SubSeconds = 0; D! ]6 P F' {
- sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;! r/ R0 m1 h% L9 P7 \
- sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;7 n# v" f8 z$ V a$ K: s# ^6 e
- sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
" Q" h4 I6 d$ C6 N7 X - sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;
; d. L/ ]8 b6 C' m5 W/ X - sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;# ^* \1 v5 v( s
- sAlarm.AlarmDateWeekDay = 28;7 J; _/ o# M' d' U8 v
- sAlarm.Alarm = RTC_ALARM_A;
# S0 e1 j5 a) D e, U) T - if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
# ]: R. }1 E4 H, l. z8 U - {
% d+ w9 k& N' s- W S& l) z - Error_Handler();
& n; x: n! _! i' L. ~+ s - }
. E$ W+ t. _8 i( B/ i: c9 f7 b5 n - /* USER CODE BEGIN RTC_Init 2 */
3 F" U6 [4 d* c* {
- R% ]0 y, \. S1 y9 m- /* USER CODE END RTC_Init 2 */
% j1 J, N6 q4 Q4 R& _0 q; Y - ( T, R6 \: w. Q7 I0 p6 o' c
- }
复制代码
1 i4 G k j( L$ |) R% Y- X9 N此初始化函数中 设置了当前的时间和日期,并使能了闹钟中断在 10s 后产生.
/ M/ B1 Z2 h* ?) C. T( @6 ~1 F7 S4 k3 ?& v, n: C
配置 RTC 闹钟中断的回调函数及设置下次产生闹钟中断的时间函数
8 `/ s+ n# v. K' x) F; E( [- /* 设置闹钟下次产生的时间 */
# G+ Z+ s( s/ Y - void setalarm(int hours, int minutes, int seconds)* S0 R5 i& f* U; x% n
- {
; W( O/ n3 T+ i' d/ i - RTC_AlarmTypeDef sAlarm = {0};; J# ~5 j3 |) g9 h5 Y+ o* y- D
- RTC_DateTypeDef sdate = {0}; , ]1 p3 k. J9 L( M6 A
- RTC_TimeTypeDef stime = {0};
! b0 K E+ Y% u# ~' L - 8 ^% g' Y& ^' e
- //读取当前时间 - F) X5 ^5 C4 M
- HAL_RTC_GetTime(&hrtc, &stime, RTC_FORMAT_BIN);
0 ?+ R3 @6 Z1 S3 m( f, I% Q9 ] - HAL_RTC_GetDate(&hrtc, &sdate, RTC_FORMAT_BIN);* y# b0 R; |8 Q- B$ \
- //获取设置中断时的时间, H4 v2 E" A/ Z/ b1 R+ P4 ^$ d, @
- sAlarm.Alarm = RTC_ALARM_A;, z& J$ I0 {0 R# P, F6 V
- sAlarm.AlarmTime.Hours = hours + stime.Hours;
0 W& c. Y* @9 q5 }$ p7 O, I5 L$ q - sAlarm.AlarmTime.Minutes = minutes + stime.Minutes;
; q# V& x* N( T" K - sAlarm.AlarmTime.Seconds = seconds + stime.Seconds;: X9 _9 Y1 ]( f6 r
- if(sAlarm.AlarmTime.Seconds >= 60)
) D4 w% ]# p t h7 U% }4 _3 g. m8 b* \ - {! O! C5 w. z% V$ o3 `* I# r
- sAlarm.AlarmTime.Seconds = sAlarm.AlarmTime.Seconds % 60;/ \$ S. h7 k# u
- sAlarm.AlarmTime.Minutes += 1;
0 p3 k. O1 ?, d - if(sAlarm.AlarmTime.Minutes >= 60)
( G) W; G! K8 F6 E - {
) O( D$ c8 v, Y" Y w* r - sAlarm.AlarmTime.Minutes = sAlarm.AlarmTime.Minutes % 60;
6 ^( R4 ~+ s4 ? - sAlarm.AlarmTime.Hours += 1;
, g5 k. U: r( L" H - if(sAlarm.AlarmTime.Hours >= 24)
2 k# i' Q$ o0 J' ]( A4 l( m+ r! _ - {3 l: c" N& P3 x' V' b6 S# }
- sAlarm.AlarmTime.Hours = sAlarm.AlarmTime.Hours % 24;
+ g2 T, z: x! p - sdate.Date += 1;
6 w3 Q1 N; p0 b* R3 R2 L& A - }
1 s6 d( |' D) P2 O* \2 \: U - }6 Q# Z6 U t$ `, r q7 D
- }$ I% r5 ^# I- R+ u/ {! ^
- sAlarm.AlarmTime.SubSeconds = 0;# P( ]9 P" |7 z8 z$ E
- sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
( o7 Z% y8 z6 _ - sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
' J3 c/ v }+ {- N; u, ` P - sAlarm.AlarmMask = RTC_ALARMMASK_NONE;2 i w; @# x7 ?- j5 P
- sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;* {' ]( X9 y: `" w: k7 E
- sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;+ n# d( Q6 c( n" C$ z' p; r
- sAlarm.AlarmDateWeekDay = sdate.Date; //跨天数定时,需要注意
7 n9 m% k# n" H+ _3 ]* P - % p- Y+ U$ A2 W4 f6 |
" w, ?- [* d3 p( c9 f7 N- if(HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
" a$ B; c+ G$ Z8 L% S% ` - {; i# Z$ N L6 Y! K: \* |8 f2 ?
- Error_Handler();
& K. l/ S9 \5 z - }% ~7 m7 ~: m, K; r
- }
2 ^! m& g% D3 f: @% C/ T# U" s - 2 F" v7 I' Q& u9 {7 m- X
- /**
" l, Q7 J% o: }4 s2 B8 g) B: w - * @brief Alarm callback 闹钟中断回调函数3 f- o8 ]0 G1 f" k; v2 x
- * @param hrtc : RTC handle1 p. p4 i$ l! e8 G7 E
- * @retval None3 g; q/ ^2 Q5 Y e7 L: y
- */( x& U- B5 \& L2 ]% M, U7 l
- void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)% d; k! P' e, \# F# A# ~* Z; [" o9 [
- {
' v3 H1 A2 A7 h6 W8 j - static uint16_t sEnterCnt = 0;
/ V) b3 b V% W. x - 8 j8 C* O" y, i% D
- setalarm(0,0,5); //设置 5s 后再次产生闹钟中断, q& r2 \( H8 t, ]* ~& ?- Z2 m
- printf("Alarm A Interrupt Cnt:%d \r\n",sEnterCnt++);2 @7 [+ p1 X& i
- }
复制代码
* o6 Y3 f* L* ^# U; ~; G! B9 lmain 函数
) U. o; f9 X: \) T8 b0 K, [- /**
@' _3 Z; A; M+ v% i - * @brief The application entry point.
+ e: M; W: m) d$ g - * @retval int, ~1 z# N/ Q* O* U
- */
) i. h9 U) T& F9 _ - int main(void)
4 @( Z, h4 ^4 Q7 k; p8 l/ _! ` - {8 ~# W2 ^& _# z. Q& f
- /* USER CODE BEGIN 1 */
8 R$ w1 \$ _' Y, N; Q9 | - 5 B. m+ E4 F6 w& W2 f, _7 M
- /* USER CODE END 1 */
! X# ~$ _0 P$ D: ^+ B9 h: J e0 p
& D n) t: b+ k/ r- /* MCU Configuration--------------------------------------------------------*/
/ j8 x& w$ X( F: e2 q - ' f x; { B4 t6 i* L
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
. n& Q A E N3 o& ~# @ - HAL_Init();
X% K# _1 ~" e5 P: M7 H. k6 i6 X - 1 g1 ]8 T! D7 H' w; U* M& f
- /* USER CODE BEGIN Init */
0 X) }6 L% x& p( m' F& C
/ U( A: M4 N& Z- /* USER CODE END Init */
7 a+ j; r& z( F0 i# Q. B - . ?/ h) r9 \% P8 _' m' ^8 f3 h
- /* Configure the system clock */; l4 p, c# d. P3 d$ u( p
- SystemClock_Config();. A4 J2 n6 ~! C. v% t; @, r2 a
9 V2 \2 \6 s# X- /* USER CODE BEGIN SysInit */) K" D/ J1 z8 J% B
- % v, H& @) y9 C6 C4 a. l( F
- /* USER CODE END SysInit */
8 z2 i! j0 E) s8 |% b6 W - $ M h$ A+ n: w& |+ E6 y6 W( e
- /* Initialize all configured peripherals */
* z/ o( d- v2 N - MX_GPIO_Init();
' w1 A0 Y' o2 |9 q - MX_RTC_Init();
; {+ L. S% y0 B& T( [( V - MX_USART1_UART_Init();
- U4 [$ ?4 ], v" B0 W" Q0 J# s/ ^ - /* USER CODE BEGIN 2 */
2 K; W* e+ m/ ~1 S4 L& ]' h/ o7 |$ } - printf("stm32g030f6 rtc & alarm demo start......\n");
0 T( ` O: t* p8 y. S - RTCStatus = 0x01;' A2 I0 p, Z5 s; x* z6 J, ?3 v. k
- /* USER CODE END 2 */% c4 a5 J: j( E7 F0 w: X
- % s! F# l& S+ w& z3 d+ Y1 ` [
- /* Infinite loop */
- |+ F) J* ]* r0 g: J* e; H5 _ - /* USER CODE BEGIN WHILE */& M6 n% f* q5 U+ D# t
- while(1)* O( v' H! u( i$ m2 I
- {
1 Y5 g) x1 f" u# B9 X# z: o - HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
( `0 n: h0 V* b( m - HAL_RTC_GetDate(&hrtc, &date, RTC_FORMAT_BIN);
% ~$ E n+ V' r3 j$ ]. l - printf("20%d-%02d-%02d %02d:%02d:%02d \r\n", date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds);
. P/ q% _$ ~+ `9 E - HAL_Delay(1000);
) W/ B& G i6 |4 Q- r1 k - /* USER CODE END WHILE */ G; V: n- u! [+ O, J8 n4 \2 Q
% |) E; x0 x4 B7 i3 c- /* USER CODE BEGIN 3 */$ h' n6 M; _* R% j% p* S
- }
4 p5 t6 O5 K Y - /* USER CODE END 3 */5 V' {4 ? X+ R) o n
- }
复制代码
; L* F h/ j! d主函数中每隔 1s 读取一次实时日期和时间,并打印出来. f! \3 t- v8 F) }: @0 G
0 Q. o0 g7 g& s% `
3. 编译并下载测试0 A; F3 N9 o9 D0 m2 c2 q& ]6 d3 e
将程序下载到开发板中,连接串口助手并打开;
( b% v; i* m. B5 q; v4 S8 u; l2 X2 z* V实验现象: 打印实时时间,10s 后,第一次进入中断,并打印进入中断的次数,而后每隔 5s 打印一次进入中断的次数.
& V! K: i" [" ^5 W# W3 q: M
* e. u+ x) ]% y* I1 O$ G8 ~
) n: S5 d0 C+ F: `; D: d3 N' ]4 U4 E h6 Y# K2 U! x% o5 U
|