| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

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