Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
rtc.c@17:927fc1eceb9d, 2018-01-11 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Jan 11 17:39:36 2018 +0000
- Revision:
- 17:927fc1eceb9d
- Parent:
- rtc.cpp@0:33686e88f09a
- Child:
- 18:207dd1474cd9
Removed dependence on Mbed OS
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| andrewboyson | 17:927fc1eceb9d | 1 | #include <time.h> |
| andrewboyson | 17:927fc1eceb9d | 2 | #include "peripherals.h" |
| andrewboyson | 0:33686e88f09a | 3 | |
| andrewboyson | 17:927fc1eceb9d | 4 | int RtcIsSet() { return !(LPC_RTC->RTC_AUX & 0x10); } //27.6.2.5 RTC Auxiliary control register - RTC Oscillator Fail detect flag |
| andrewboyson | 0:33686e88f09a | 5 | |
| andrewboyson | 0:33686e88f09a | 6 | void RtcGetTm(struct tm* ptm) |
| andrewboyson | 0:33686e88f09a | 7 | { |
| andrewboyson | 0:33686e88f09a | 8 | ptm->tm_sec = LPC_RTC->SEC; |
| andrewboyson | 0:33686e88f09a | 9 | ptm->tm_min = LPC_RTC->MIN; |
| andrewboyson | 0:33686e88f09a | 10 | ptm->tm_hour = LPC_RTC->HOUR; |
| andrewboyson | 0:33686e88f09a | 11 | ptm->tm_mday = LPC_RTC->DOM; |
| andrewboyson | 0:33686e88f09a | 12 | ptm->tm_mon = LPC_RTC->MONTH - 1; |
| andrewboyson | 0:33686e88f09a | 13 | ptm->tm_year = LPC_RTC->YEAR - 1900; |
| andrewboyson | 0:33686e88f09a | 14 | ptm->tm_wday = LPC_RTC->DOW; |
| andrewboyson | 0:33686e88f09a | 15 | ptm->tm_yday = LPC_RTC->DOY - 1; |
| andrewboyson | 0:33686e88f09a | 16 | ptm->tm_isdst = -1; // -ve should signify that dst data is not available but it is used here to denote UTC |
| andrewboyson | 0:33686e88f09a | 17 | } |
| andrewboyson | 0:33686e88f09a | 18 | void RtcSetTm(struct tm* ptm) |
| andrewboyson | 0:33686e88f09a | 19 | { |
| andrewboyson | 0:33686e88f09a | 20 | LPC_RTC->CCR = 2; //Stop and reset the divider (CTCRST bit 1 = 1) |
| andrewboyson | 0:33686e88f09a | 21 | LPC_RTC->SEC = ptm->tm_sec; // 0 --> 59 |
| andrewboyson | 0:33686e88f09a | 22 | LPC_RTC->MIN = ptm->tm_min; // 0 --> 59 |
| andrewboyson | 0:33686e88f09a | 23 | LPC_RTC->HOUR = ptm->tm_hour; // 0 --> 23 |
| andrewboyson | 0:33686e88f09a | 24 | LPC_RTC->DOM = ptm->tm_mday; // 1 --> 31 |
| andrewboyson | 0:33686e88f09a | 25 | LPC_RTC->MONTH = ptm->tm_mon + 1; // rtc 1 --> 12; tm 0 --> 11 |
| andrewboyson | 0:33686e88f09a | 26 | LPC_RTC->YEAR = ptm->tm_year + 1900; // rtc 1900 --> 2100; tm 0 --> 200 |
| andrewboyson | 0:33686e88f09a | 27 | LPC_RTC->DOW = ptm->tm_wday; // 0 --> 6 where 0 == Sunday |
| andrewboyson | 0:33686e88f09a | 28 | LPC_RTC->DOY = ptm->tm_yday + 1; // rtc 1 --> 366; tm 0 --> 365 |
| andrewboyson | 0:33686e88f09a | 29 | LPC_RTC->RTC_AUX = 0x10; //27.6.2.5 RTC Auxiliary control register - RTC Oscillator Fail detect flag - Writing a 1 to this bit clears the flag. |
| andrewboyson | 0:33686e88f09a | 30 | LPC_RTC->CCR = 1; //Release the divider (CTCRST bit 1 = 0) |
| andrewboyson | 0:33686e88f09a | 31 | } |
| andrewboyson | 0:33686e88f09a | 32 | void RtcInit() |
| andrewboyson | 0:33686e88f09a | 33 | { |
| andrewboyson | 0:33686e88f09a | 34 | LPC_RTC->CCR = 1; //27.6.2.2 Clock Control Register - enable the time counters (CLKEN bit 0 = 1); no reset (CTCRST bit 1 = 0); enable calibration counter (CCALEN bit 4 = 0) |
| andrewboyson | 0:33686e88f09a | 35 | LPC_RTC->CIIR = 0; //27.6.2.3 Counter Increment Interrupt Register - set to not interrupt |
| andrewboyson | 0:33686e88f09a | 36 | LPC_RTC->AMR = 0xFF; //27.6.2.4 Alarm Mask Register - mask all alarms |
| andrewboyson | 0:33686e88f09a | 37 | LPC_RTC->RTC_AUXEN = 0; //27.6.2.6 RTC Auxiliary Enable register - mask the oscillator stopped interrupt |
| andrewboyson | 0:33686e88f09a | 38 | LPC_RTC->ILR = 3; //27.6.2.1 Interrupt Location Register - Clear all interrupts |
| andrewboyson | 0:33686e88f09a | 39 | |
| andrewboyson | 0:33686e88f09a | 40 | } |