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

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

[复制链接]
caizhiwei 发布时间:2015-2-2 16:01
固件库中已经定义好了2个结构体,使用起来非常方便:
  1. RTC_DateTypeDef RTC_DateStructure;1 g; T" n/ k& ^' q% B2 K7 K
  2. RTC_TimeTypeDef RTC_TimeStructure;
复制代码
在STM32F30X_RTC.H文件中已定义:
" F5 Y3 w  N  `1 ?# @6 Y  C5 f# I

# B& ^) G. {- \- s- M  x
  1. /** 7 w+ P; v- L6 d* G  N, O
  2.   * @brief  RTC Date structure definition  . c+ D2 J5 h" D; y6 C, g
  3.   */
    0 b! E6 N2 [  p  X( a  u
  4. typedef struct
    4 F/ t! F" w/ |0 o% Q
  5. {( y- w" D7 T- T% o
  6.   uint8_t RTC_WeekDay; /*!< Specifies the RTC Date WeekDay.
    2 @; ]# U% O; i
  7.                         This parameter can be a value of @ref RTC_WeekDay_Definitions */( |2 B" S8 J& D' R
  8.   / s) i. ~% j8 c$ b: A
  9.   uint8_t RTC_Month;   /*!< Specifies the RTC Date Month (in BCD format).
    . F  `+ h; \/ K' M8 |2 S
  10.                         This parameter can be a value of @ref RTC_Month_Date_Definitions */
    : k# u0 s* \2 U* ~: b0 x! l
  11. 4 }. V5 w" @/ ^8 B
  12.   uint8_t RTC_Date;     /*!< Specifies the RTC Date.
    , A$ O, I0 I8 K1 k( N9 a2 ~
  13.                         This parameter must be set to a value in the 1-31 range. */8 L! z* U; X6 l. w6 n2 ]- W
  14.   % e  U9 `, T8 s4 d4 G+ H% w; f
  15.   uint8_t RTC_Year;     /*!< Specifies the RTC Date Year.
    ; k) T$ G3 Y  v0 u4 P
  16.                         This parameter must be set to a value in the 0-99 range. */
    # ?) V0 P3 l+ N% U2 O
  17. }RTC_DateTypeDef;7 ?+ ]- {8 H: u  t4 Z
复制代码
  1. typedef struct
    : [" S* \. H* ^5 m+ F' j6 @
  2. {( G% Q2 S- a7 E% m, E3 _- R
  3.   uint8_t RTC_Hours;    /*!< Specifies the RTC Time Hour.$ d; U; t' N! t* O! A
  4.                         This parameter must be set to a value in the 0-12 range
    3 w3 z& \4 i" y0 `
  5.                         if the RTC_HourFormat_12 is selected or 0-23 range if* s( w: w9 D! h% S: f  U* b( v+ p
  6.                         the RTC_HourFormat_24 is selected. */  c3 D# D; t% D  U3 v, I

  7. ! H5 j  i. b2 v6 C0 M: s
  8.   uint8_t RTC_Minutes;  /*!< Specifies the RTC Time Minutes.. ~) n+ w/ J0 N3 ?1 p7 v/ K
  9.                         This parameter must be set to a value in the 0-59 range. */( K  V1 {+ V  F
  10.   9 b: L+ i. d9 w  v+ ]
  11.   uint8_t RTC_Seconds;  /*!< Specifies the RTC Time Seconds.
    % v7 f5 r+ z+ K* n; o* c  J! s
  12.                         This parameter must be set to a value in the 0-59 range. */% Q5 C+ J, l$ n+ V% ]

  13. % a3 B; \; I. ?  ?. L7 D% \( E+ \
  14.   uint8_t RTC_H12;      /*!< Specifies the RTC AM/PM Time.1 G3 {. t6 e5 X
  15.                         This parameter can be a value of @ref RTC_AM_PM_Definitions */: t' \9 a& m" P; R/ k2 }
  16. }RTC_TimeTypeDef;
复制代码
在.C文件里做申明:
$ V+ j  E% z- w
  1. RTC_DateTypeDef RTC_DateStructure;
    - W' ~+ ]1 W% @) [( B6 }
  2. RTC_TimeTypeDef RTC_TimeStructure;
复制代码
在main.c中:5 W  N" |4 P; A$ W6 m+ z  U2 h4 e
  1. RTC_Config();) `/ F7 g% c1 R, T0 i5 j
  2.     while (1)
      p# z& P# O" z! c# p6 ^* L
  3.     {         
    0 V& Q* p# W& n+ }9 ^* h
  4.         /* Get the RTC current Time */
    3 `. c$ ]9 M! [6 w
  5.     RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
    ; n: q: W6 {$ C- Y+ S% N! {
  6.     /* Get the RTC current Date */
    8 z1 R) v2 i# L* ~
  7.     RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure);& ]' m. ?( w9 O! \
  8.     /* Display time Format : hh:mm:ss */
    " _% F0 i) R4 h! Z6 M- v
  9.     sprintf((char*)showtime,"%.2d:%.2d:%.2d",RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds);3 ^) x  [8 @7 l- E. V
  10.     /* Display date Format : mm-dd-yy */3 ]& H" m9 X+ ?  I+ ?1 F
  11.     sprintf((char*)showdate,"%.2d-%.2d-%.2d",RTC_DateStructure.RTC_Month, RTC_DateStructure.RTC_Date, 2000 + RTC_DateStructure.RTC_Year);   
    % P: D' U. I7 ^6 m0 ~& {
  12.     ) \5 ]* W( x3 s' {4 }
  13.     }
复制代码
在中断中描输出:7 Y& d$ z: u1 w# _4 [
  1. void RTC_Alarm_IRQHandler(void)
    + x* M8 T( R- p& c9 {. W' }
  2. {9 y2 R8 V  A8 g2 a* l

  3. # d+ r2 l3 Z3 q
  4.   if(RTC_GetITStatus(RTC_IT_ALRA) != RESET)5 H- k. p- o4 S6 V
  5.   {( G4 ^$ _5 m5 z" h/ V3 r; E
  6.    
    + y5 c; M1 G( n6 h1 U
  7.        if(GPIO_ReadOutputDataBit(GPIOA,  GPIO_Pin_5)==0)7 r/ _7 b, {2 b9 V
  8.       GPIO_SetBits(GPIOA, GPIO_Pin_5);  //Green LED9 y( F$ y2 `' h5 v4 n
  9.      else6 Z7 @) t% M, t# n# T2 J( u) r* w1 y: P
  10.         GPIO_ResetBits(GPIOA, GPIO_Pin_5);  //Green LED1 m; N  x  l' B# ]/ R

  11. & r' |' N, h, y! u7 r( M
  12.     RTC_ClearITPendingBit(RTC_IT_ALRA);
    7 Z! N8 s" |4 F# @% M
  13.     EXTI_ClearITPendingBit(EXTI_Line17);
    2 G8 A- F6 f% B: c- x
  14.   }
    0 y" K/ ]0 N, m& c
  15. }
复制代码
附件:
- l" |% t7 S9 E( f1 _8 E( H 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管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版