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

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

[复制链接]
caizhiwei 发布时间:2015-2-2 16:01
固件库中已经定义好了2个结构体,使用起来非常方便:
  1. RTC_DateTypeDef RTC_DateStructure;& U# y5 W7 n) b4 F7 X% G
  2. RTC_TimeTypeDef RTC_TimeStructure;
复制代码
在STM32F30X_RTC.H文件中已定义:* X- `; ^5 q& l0 K) l+ g! \
. w! d$ o4 U' Y% [
6 n6 T: a1 E4 U$ W- S$ L, g% `( A
  1. /**
    ) ?9 r( q2 b8 F
  2.   * @brief  RTC Date structure definition  
    $ }' _; v1 M" M/ C& U- N4 ^, Z7 |. d
  3.   */! E4 n0 A6 z$ P% e% z
  4. typedef struct
      L* o6 u* \! K+ g" ]0 [/ D
  5. {* s  j7 b9 Z: V
  6.   uint8_t RTC_WeekDay; /*!< Specifies the RTC Date WeekDay.
    0 o: [  S1 i# l5 P# D$ |
  7.                         This parameter can be a value of @ref RTC_WeekDay_Definitions */* _7 i0 x" x( Z, ~% T# J- b
  8.   " A+ g6 k% S6 m$ R% R! Z: k
  9.   uint8_t RTC_Month;   /*!< Specifies the RTC Date Month (in BCD format)./ ]9 F3 M" S2 H- z& Z- D- @
  10.                         This parameter can be a value of @ref RTC_Month_Date_Definitions */
    8 _4 |& P! U# t: P! Y3 ]! o5 i
  11. - V1 U, S4 a* n% P
  12.   uint8_t RTC_Date;     /*!< Specifies the RTC Date.4 k# U# h2 K4 K+ q, G+ i
  13.                         This parameter must be set to a value in the 1-31 range. */. C- ~  j9 U: y2 a, T% J) x* E
  14.   
    ! f- e) x" h* B' `" @: p9 l* L
  15.   uint8_t RTC_Year;     /*!< Specifies the RTC Date Year." r* S( x4 ]# ~6 \9 m+ B
  16.                         This parameter must be set to a value in the 0-99 range. */
    3 z  Z2 x5 \( p+ [/ u) w+ ^3 P; L
  17. }RTC_DateTypeDef;7 i( S+ I; q2 J5 }( m& d
复制代码
  1. typedef struct
    1 n9 s% u  D8 Q
  2. {
    5 u' _% S: o. z
  3.   uint8_t RTC_Hours;    /*!< Specifies the RTC Time Hour.
    7 {7 R5 w! m* Z' i8 O8 F5 v
  4.                         This parameter must be set to a value in the 0-12 range- g2 r9 l5 m7 r% T- ^# U+ L
  5.                         if the RTC_HourFormat_12 is selected or 0-23 range if" t, u6 e2 ?% G; V0 H
  6.                         the RTC_HourFormat_24 is selected. */
    5 ~9 ]; c& x, r  O$ K
  7. 7 Z' d1 F( L2 A+ g+ H
  8.   uint8_t RTC_Minutes;  /*!< Specifies the RTC Time Minutes.
    , a9 g/ P+ Q% L, ?
  9.                         This parameter must be set to a value in the 0-59 range. */0 k# u- S3 x' s& P+ H  R, @2 V, d
  10.   
    5 m' `1 f  j9 N: S* N+ ^
  11.   uint8_t RTC_Seconds;  /*!< Specifies the RTC Time Seconds.2 v; \0 P4 Y5 y; c' o' s
  12.                         This parameter must be set to a value in the 0-59 range. */( u) t* X1 A; E% U

  13. + j: c; f) o8 ~' r+ k
  14.   uint8_t RTC_H12;      /*!< Specifies the RTC AM/PM Time.
    ' |+ D- D" n$ @) |
  15.                         This parameter can be a value of @ref RTC_AM_PM_Definitions *// u( p6 I1 u( T6 H$ o! \
  16. }RTC_TimeTypeDef;
复制代码
在.C文件里做申明:* e- g6 b5 E1 ~4 W
  1. RTC_DateTypeDef RTC_DateStructure;5 w4 F  i8 }* A: P% H. ^
  2. RTC_TimeTypeDef RTC_TimeStructure;
复制代码
在main.c中:+ |' H- h/ Y# U) t+ {0 E  T- u
  1. RTC_Config();
    & x3 I9 u0 e$ @  H1 s, a7 q; C9 u
  2.     while (1)
    2 l, o0 ]8 I2 S1 a5 _/ m5 |. ^0 g
  3.     {          . N! p/ B+ u# |$ ?( J
  4.         /* Get the RTC current Time */
    . p" j! X( d6 z1 e1 L& x! C: ?% p
  5.     RTC_GetTime(RTC_Format_BIN, &RTC_TimeStructure);
    % ?3 o/ ?1 r; x1 l
  6.     /* Get the RTC current Date */8 j$ B" R, n! ~2 S( m6 n4 d% X
  7.     RTC_GetDate(RTC_Format_BIN, &RTC_DateStructure);& K& S; `* r; o$ x& C3 M4 F
  8.     /* Display time Format : hh:mm:ss */& S8 N$ h* j. ~+ s
  9.     sprintf((char*)showtime,"%.2d:%.2d:%.2d",RTC_TimeStructure.RTC_Hours, RTC_TimeStructure.RTC_Minutes, RTC_TimeStructure.RTC_Seconds);
    2 O4 D- a' R: R' {2 y* [8 U
  10.     /* Display date Format : mm-dd-yy */2 Z1 e$ k( U+ f" _! [6 ?$ W3 Y. c
  11.     sprintf((char*)showdate,"%.2d-%.2d-%.2d",RTC_DateStructure.RTC_Month, RTC_DateStructure.RTC_Date, 2000 + RTC_DateStructure.RTC_Year);   
    6 c) B9 z4 ?$ U, d
  12.    
    ! N* j$ S4 j, w2 C$ ^
  13.     }
复制代码
在中断中描输出:! j" W$ n* |0 l' Q* w* z* }
  1. void RTC_Alarm_IRQHandler(void)3 i; y) y. O* G& [
  2. {
    8 k: Q+ m. M7 V! g! \$ F9 E
  3. * D$ ?5 s% _9 }2 |" }) h8 J
  4.   if(RTC_GetITStatus(RTC_IT_ALRA) != RESET)% ~8 h, t& u* O+ }
  5.   {; _# M& b% v1 Z& l3 B% C
  6.    
    / n, ]. \6 R9 f# D* ^# F  M. ]
  7.        if(GPIO_ReadOutputDataBit(GPIOA,  GPIO_Pin_5)==0)* j) s3 z5 L, p
  8.       GPIO_SetBits(GPIOA, GPIO_Pin_5);  //Green LED
    0 O6 C/ P# E# R6 a# |
  9.      else
    : M, l. B. _0 W; J! y0 ?( Z
  10.         GPIO_ResetBits(GPIOA, GPIO_Pin_5);  //Green LED
    1 h" Y8 s$ ?6 v! o& n7 P' \) i0 d6 V

  11. 2 D$ P0 _1 \" X* i
  12.     RTC_ClearITPendingBit(RTC_IT_ALRA);
    ' M* a- G  Q9 |( C* C( k
  13.     EXTI_ClearITPendingBit(EXTI_Line17);; M* M7 t. E' W* B
  14.   }% d3 V3 @2 R) ~, Q" q7 @) w
  15. }
复制代码
附件:
/ p, O* [5 X2 ?0 I9 l# A 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 手机版