LPCOpen Platform
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rtc.c
Go to the documentation of this file.
1 /*
2  * @brief RTC example
3  *
4  * @note
5  * Copyright(C) NXP Semiconductors, 2012
6  * All rights reserved.
7  *
8  * @par
9  * Software that is described herein is for illustrative purposes only
10  * which provides customers with programming information regarding the
11  * LPC products. This software is supplied "AS IS" without any warranties of
12  * any kind, and NXP Semiconductors and its licensor disclaim any and
13  * all warranties, express or implied, including all implied warranties of
14  * merchantability, fitness for a particular purpose and non-infringement of
15  * intellectual property rights. NXP Semiconductors assumes no responsibility
16  * or liability for the use of the software, conveys no license or rights under any
17  * patent, copyright, mask work right, or any other intellectual property rights in
18  * or to any products. NXP Semiconductors reserves the right to make changes
19  * in the software without notification. NXP Semiconductors also makes no
20  * representation or warranty that such application will be suitable for the
21  * specified use without further testing or modification.
22  *
23  * @par
24  * Permission to use, copy, modify, and distribute this software and its
25  * documentation is hereby granted, under NXP Semiconductors' and its
26  * licensor's relevant copyrights in the software, without fee, provided that it
27  * is used in conjunction with NXP Semiconductors microcontrollers. This
28  * copyright, permission, and disclaimer notice must appear in all copies of
29  * this code.
30  */
31 
32 #include "board.h"
33 
59 /*****************************************************************************
60  * Private types/enumerations/variables
61  ****************************************************************************/
62 
63 static volatile bool fIntervalReached;
64 static volatile bool fAlarmTimeMatched;
65 static volatile bool On0, On1;
66 
67 /*****************************************************************************
68  * Public types/enumerations/variables
69  ****************************************************************************/
70 
71 /*****************************************************************************
72  * Private functions
73  ****************************************************************************/
74 
75 /* Gets and shows the current time and date */
76 static void showTime(IP_RTC_TIME_T *pTime)
77 {
78  DEBUGOUT("Time: %.2d:%.2d:%.2d %.2d/%.4d\r\n", pTime->time[RTC_TIMETYPE_HOUR],
79  pTime->time[RTC_TIMETYPE_MINUTE],
80  pTime->time[RTC_TIMETYPE_SECOND],
81  pTime->time[RTC_TIMETYPE_MONTH],
82  pTime->time[RTC_TIMETYPE_YEAR]);
83 }
84 
85 /*****************************************************************************
86  * Public functions
87  ****************************************************************************/
88 
93 void RTC_IRQHandler(void)
94 {
95  uint32_t sec;
96 
97  /* Toggle heart beat LED for each second field change interrupt */
99  /* Clear pending interrupt */
101  On0 = (bool) !On0;
102  Board_LED_Set(0, On0);
103  }
104 
105  /* display timestamp every 5 seconds in the background */
107  if (!(sec % 5)) {
108  fIntervalReached = true; /* set flag for background */
109  if (fAlarmTimeMatched) {
110  fAlarmTimeMatched = false;
111  }
112  }
113 
114  /* Check for alarm match */
116  /* Clear pending interrupt */
118  fAlarmTimeMatched = true; /* set alarm handler flag */
119  }
120 }
121 
126 int main(void)
127 {
128  IP_RTC_TIME_T FullTime;
129 
130  Board_Init();
131  Board_LED_Init();
132 
133  fIntervalReached = 0;
134  fAlarmTimeMatched = 0;
135  On0 = On1 = false;
136  Board_LED_Set(2, false);
137 
138  DEBUGOUT("%s%s%s%s%s", "\r\n",
139  "The RTC operates on a 1 Hz clock.\r\n",
140  "Register writes can take up to 2 cycles.\r\n",
141  "It will take a few seconds to fully\r\n",
142  "initialize it and start it running.\r\n\r\n");
143 
144  DEBUGOUT("%s%s%s%s", "We'll print a timestamp every 5 seconds.\r\n",
145  "...and another when the alarm occurs.\r\n",
146  "Both LEDs steady for 5 seconds indicates\r\n",
147  "the alarm fired.\r\n\r\n");
148 
149  Chip_RTC_Init();
150 
151  DEBUGOUT("RTC Initialized.\r\n");
152 
153  /* Set current time for RTC 2:00:00PM, 2012-10-05 */
154  FullTime.time[RTC_TIMETYPE_SECOND] = 0;
155  FullTime.time[RTC_TIMETYPE_MINUTE] = 0;
156  FullTime.time[RTC_TIMETYPE_HOUR] = 14;
157  FullTime.time[RTC_TIMETYPE_MONTH] = 10;
158  FullTime.time[RTC_TIMETYPE_YEAR] = 2012;
159 
160  Chip_RTC_SetFullTime(&FullTime);
161 
162  /* Set ALARM time for 17 seconds from time */
163  FullTime.time[RTC_TIMETYPE_SECOND] = 17;
164  Chip_RTC_SetFullAlarmTime(&FullTime);
165 
166  /* Set the RTC to generate an interrupt on each second */
168 
169  /* Enable matching for alarm for second, minute, hour fields only */
171 
172  /* Enable RTC interrupt in NVIC */
173  NVIC_EnableIRQ((IRQn_Type) RTC_IRQn);
174 
175  /* Enable RTC (starts increase the tick counter and second counter register) */
177 
178  /* Loop forever */
179  while (1) {
180  if (fIntervalReached) { /* Every 5s */
181  /* read and display time */
182  Chip_RTC_GetFullTime(&FullTime);
183  showTime(&FullTime);
184 
185  On1 = (bool) !On1;
186  Board_LED_Set(1, On1);
187 
188  /* disable IRQ while modifying state flag */
189  NVIC_DisableIRQ((IRQn_Type) RTC_IRQn);
190  fIntervalReached = 0;
191  NVIC_EnableIRQ((IRQn_Type) RTC_IRQn);
192  }
193 
194  if (fAlarmTimeMatched) {
195  fAlarmTimeMatched = false;
196 
197  /* announce event */
198  DEBUGSTR("ALARM triggered!\r\n");
199  Board_LED_Set(2, true);
200 
201  /* read and display time */
202  Chip_RTC_GetFullTime(&FullTime);
203  showTime(&FullTime);
204  }
205  }
206 }
207