LPCOpen Platform  v1.03
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 
67 /*****************************************************************************
68  * Private types/enumerations/variables
69  ****************************************************************************/
70 
71 static volatile bool fIntervalReached;
72 static volatile bool fAlarmTimeMatched;
73 static volatile bool On0, On1;
74 
75 /*****************************************************************************
76  * Public types/enumerations/variables
77  ****************************************************************************/
78 
79 /*****************************************************************************
80  * Private functions
81  ****************************************************************************/
82 
83 /* Gets and shows the current time and date */
84 static void showTime(IP_RTC_TIME_T *pTime)
85 {
86  DEBUGOUT("Time: %.2d:%.2d:%.2d %.2d/%.2d/%.4d\r\n", pTime->time[RTC_TIMETYPE_HOUR],
87  pTime->time[RTC_TIMETYPE_MINUTE],
88  pTime->time[RTC_TIMETYPE_SECOND],
89  pTime->time[RTC_TIMETYPE_MONTH],
91  pTime->time[RTC_TIMETYPE_YEAR]);
92 }
93 
94 /*****************************************************************************
95  * Public functions
96  ****************************************************************************/
97 
102 void RTC_IRQHandler(void)
103 {
104  uint32_t sec;
105 
106  /* Toggle heart beat LED for each second field change interrupt */
108  /* Clear pending interrupt */
110  On0 = (bool) !On0;
111  Board_LED_Set(0, On0);
112  }
113 
114  /* display timestamp every 5 seconds in the background */
116  if (!(sec % 5)) {
117  fIntervalReached = true; /* set flag for background */
118  }
119 
120  /* Check for alarm match */
122  /* Clear pending interrupt */
124  fAlarmTimeMatched = true; /* set alarm handler flag */
125  }
126 }
127 
132 int main(void)
133 {
134  IP_RTC_TIME_T FullTime;
135 
136  Board_Init();
137 
138  fIntervalReached = 0;
139  fAlarmTimeMatched = 0;
140  On0 = On1 = false;
141  Board_LED_Set(2, false);
142 
143  DEBUGSTR("The RTC operates on a 1 Hz clock.\r\n" \
144  "Register writes can take up to 2 cycles.\r\n" \
145  "It will take a few seconds to fully\r\n" \
146  "initialize it and start it running.\r\n\r\n");
147 
148  DEBUGSTR("We'll print a timestamp every 5 seconds.\r\n" \
149  "...and another when the alarm occurs.\r\n");
150 
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_DAYOFMONTH] = 5;
158  FullTime.time[RTC_TIMETYPE_DAYOFWEEK] = 5;
159  FullTime.time[RTC_TIMETYPE_DAYOFYEAR] = 279;
160  FullTime.time[RTC_TIMETYPE_MONTH] = 10;
161  FullTime.time[RTC_TIMETYPE_YEAR] = 2012;
162 
163  Chip_RTC_SetFullTime(LPC_RTC, &FullTime);
164 
165  /* Set ALARM time for 17 seconds from time */
166  FullTime.time[RTC_TIMETYPE_SECOND] = 17;
168 
169  /* Set the RTC to generate an interrupt on each second */
171 
172  /* Enable matching for alarm for second, minute, hour fields only */
174 
175  /* Clear interrupt pending */
177 
178  /* Enable RTC interrupt in NVIC */
179  NVIC_EnableIRQ((IRQn_Type) RTC_IRQn);
180 
181  /* Enable RTC (starts increase the tick counter and second counter register) */
183 
184  /* Loop forever */
185  while (1) {
186  if (fIntervalReached) { /* Every 5s */
187  fIntervalReached = 0;
188 
189  On1 = (bool) !On1;
190  Board_LED_Set(1, On1);
191 
192  /* read and display time */
193  Chip_RTC_GetFullTime(LPC_RTC, &FullTime);
194  showTime(&FullTime);
195  }
196 
197  if (fAlarmTimeMatched) {
198  fAlarmTimeMatched = false;
199 
200  /* announce event */
201  DEBUGSTR("ALARM triggered!\r\n");
202  Board_LED_Set(2, true);
203 
204  /* read and display time */
205  Chip_RTC_GetFullTime(LPC_RTC, &FullTime);
206  showTime(&FullTime);
207  }
208  }
209 }
210