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

【STM32-NUCLEO334R8学习笔记】3.RTC+日历,非UNIX时间戳

[复制链接]
caizhiwei 发布时间:2015-2-2 16:01
固件库中已经定义好了2个结构体,使用起来非常方便:
  1. RTC_DateTypeDef RTC_DateStructure;" O. p% Q; c. v& s
  2. RTC_TimeTypeDef RTC_TimeStructure;
复制代码
在STM32F30X_RTC.H文件中已定义:7 k7 Z$ N) z' c; [/ J

, V- K+ ^/ j8 S. m9 W# v" B/ F# b9 t2 Q
  1. /** - Y  O! d# O& Q0 T: t+ T: I
  2.   * @brief  RTC Date structure definition  5 }+ @! L% W! S- Q4 `
  3.   */0 I  c: F- g1 B
  4. typedef struct
    : v# P  Q. g9 S* {5 T/ Z
  5. {
    2 q8 ]# F# ~- |- `  i
  6.   uint8_t RTC_WeekDay; /*!< Specifies the RTC Date WeekDay.
    ; u; B, y& a: ]% x" L
  7.                         This parameter can be a value of @ref RTC_WeekDay_Definitions */
    & E% z) p$ f* M6 O6 _' K
  8.   ; G9 m% b& I2 _; p/ @
  9.   uint8_t RTC_Month;   /*!< Specifies the RTC Date Month (in BCD format).! I8 o2 z, V+ M  O& |
  10.                         This parameter can be a value of @ref RTC_Month_Date_Definitions */) t+ v  F5 Z9 @- r7 d8 V* Q
  11. 3 s9 I3 ?7 w; B' }, e6 ~
  12.   uint8_t RTC_Date;     /*!< Specifies the RTC Date.
    7 t! i: o  Y4 a; s) S5 S. L
  13.                         This parameter must be set to a value in the 1-31 range. */$ {8 I4 Y2 f, r/ C2 z0 T1 W4 Y
  14.   " G# F) j6 s4 S. i5 I" _
  15.   uint8_t RTC_Year;     /*!< Specifies the RTC Date Year.
    0 f2 N7 j# a2 D9 b/ s6 b" j
  16.                         This parameter must be set to a value in the 0-99 range. */
    9 f  o9 a3 T8 s  E7 n! n
  17. }RTC_DateTypeDef;
    3 X% S6 i- U& h# [% {7 |* P
复制代码
  1. typedef struct3 H6 c  ]+ c# b; @8 O
  2. {0 F' D" ~) U- A, X, y$ n# }( c
  3.   uint8_t RTC_Hours;    /*!< Specifies the RTC Time Hour.' k, S+ G7 j1 g" T# Q4 {
  4.                         This parameter must be set to a value in the 0-12 range0 u6 y: T. s4 f+ g2 i0 d; ?
  5.                         if the RTC_HourFormat_12 is selected or 0-23 range if
    0 {9 y  l5 U7 I
  6.                         the RTC_HourFormat_24 is selected. */
    , j/ P6 y+ G5 A- \
  7. 9 B8 m0 q5 _; U* t
  8.   uint8_t RTC_Minutes;  /*!< Specifies the RTC Time Minutes.  S" J: s  v; v& l
  9.                         This parameter must be set to a value in the 0-59 range. */; r9 _" p4 o+ x5 r$ M+ K
  10.   8 L3 e/ j$ c9 J3 ~
  11.   uint8_t RTC_Seconds;  /*!< Specifies the RTC Time Seconds.
    0 t: W1 }: n$ G$ x  h7 S
  12.                         This parameter must be set to a value in the 0-59 range. */; Q: n2 A. x/ h% x1 R: ~3 ^: _

  13. * H9 E1 k* _( x+ I5 P
  14.   uint8_t RTC_H12;      /*!< Specifies the RTC AM/PM Time.% _- ~+ L2 {" A$ T: r* P+ z
  15.                         This parameter can be a value of @ref RTC_AM_PM_Definitions */
      I/ _  ]& C% X; N
  16. }RTC_TimeTypeDef;
复制代码
在.C文件里做申明:
# Q+ _1 m0 _9 O
  1. RTC_DateTypeDef RTC_DateStructure;: W: p6 X0 r% z
  2. RTC_TimeTypeDef RTC_TimeStructure;
复制代码
在main.c中:4 Q- \- R4 z* `4 t9 L$ T
  1. RTC_Config();# U( X+ Y8 y: a
  2.     while (1)7 ^$ \+ q9 P3 \
  3.     {         
    ! c% C: O5 U6 c  ]! g. o4 z" Y
  4.         /* Get the RTC current Time */
    $ @& [! w: t/ j3 F
  5.     RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);& r5 t, I2 J3 U0 X
  6.     /* Get the RTC current Date */
    5 E: o+ D' s  |
  7.     RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure);' w' }) |" A# ~' k
  8.     /* Display time Format : hh:mm:ss */
    4 v5 \' T; `* T' T. ]! A
  9.     sprintf((char*)showtime,"%.2d:%.2d:%.2d",RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds);% D) j: I8 p! {5 b8 ?: g
  10.     /* Display date Format : mm-dd-yy */4 d* ~+ Z) Q  W8 {
  11.     sprintf((char*)showdate,"%.2d-%.2d-%.2d",RTC_DateStructure.RTC_Month, RTC_DateStructure.RTC_Date, 2000 + RTC_DateStructure.RTC_Year);   ' }6 C( r1 U; L! g' K6 _: ~
  12.     . U, D+ L2 @7 O! n# ?: m  x
  13.     }
复制代码
在中断中描输出:
0 x) V5 c. {$ U" f: t
  1. void RTC_Alarm_IRQHandler(void)
    + Y8 J; b  Q( w+ Q
  2. {  l1 t' T& n7 m3 U
  3.   g$ ~' C4 @" t: v
  4.   if(RTC_GetITStatus(RTC_IT_ALRA) != RESET)$ @) [! M# Z9 k# x* u, L- U
  5.   {
    & J% ]: ^( \* [
  6.    5 Z+ c/ q8 b7 H5 P
  7.        if(GPIO_ReadOutputDataBit(GPIOA,  GPIO_Pin_5)==0)  t# }6 I9 K3 h* P7 r
  8.       GPIO_SetBits(GPIOA, GPIO_Pin_5);  //Green LED+ a' n/ z) w- J: f" P7 {
  9.      else
    9 e- w% F6 y0 Z
  10.         GPIO_ResetBits(GPIOA, GPIO_Pin_5);  //Green LED
    ( p7 C: d3 O) U% ?6 w$ X+ f0 g
  11. 9 b; L! @' Q8 z% o  T
  12.     RTC_ClearITPendingBit(RTC_IT_ALRA);
    % d* U" Y( `4 e# h. T3 H
  13.     EXTI_ClearITPendingBit(EXTI_Line17);' `5 ?% {( Q/ F2 N$ b
  14.   }/ f7 c4 P9 g. ~4 |% Y
  15. }
复制代码
附件:+ `$ G& {3 |2 V' O8 v& B" |3 @
Nucleo_F334R8_RTC.zip (3.87 MB, 下载次数: 10)
收藏 评论7 发布时间:2015-2-2 16:01

举报

7个回答
wyxy163@126.com 回答时间:2015-2-2 18:23:07
提示: 作者被禁止或删除 内容自动屏蔽
moyanming2013 回答时间:2015-2-2 19:54:17
XUEXI学习了。。。
Emmanel丶 回答时间:2015-2-2 21:29:24
学习了。。。
STM32-366775 回答时间:2015-2-2 22:03:01
下载学习一下
左岸右岸 回答时间:2015-2-2 22:27:33
如果我也能申请到就能学习学习了
wgsxsm 回答时间:2015-2-3 21:27:56
不错,楼主加油
hanmcustm 回答时间:2015-2-3 21:45:02
不错不错

所属标签

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版