====== rtcRead ======
''u8 rtcRead (u8 rtcAdr)''
|rtcAdr : Address of the RTC data field (e.g. day, month, year, etc...) from which to read. |
Reads the RTC value from the RTC data field's address, ''rtcAdr''. The value returned is in [[https://en.wikipedia.org/wiki/Binary-coded_decimal|Binary
Coded Decimal (BCD)]].
The following table illustrates the format in which the RTC data is stored in memory. Data is stored in [[https://en.wikipedia.org/wiki/Binary-coded_decimal|Binary
Coded Decimal (BCD)]].
^rtcAdr ^Value ^Range ^bit7 ^bit6 ^bit5 ^bit4 ^bit3 ^bit2 ^bit1 ^bit0^
|0 |Second |0 to 59 | | 2nd digit place ||| 1st digit place ||||
|1 |Minute |0 to 59 | | 2nd digit place ||| 1st digit place ||||
|2 |Hour |0 to 23 | | | 2nd digit place || 1st digit place ||||
|3 |Day |1 to 7 | | | | | | 1st digit place |||
|4 |Date |1 to 31 | | | 2nd digit place || 1st digit place ||||
|5 |Month |1 to 12 | | | | 2nd digit | 1st digit place ||||
|6 |Year |00 to 99 | 2nd digit place |||| 1st digit place ||||
The MOACON CPU Module has a built in Real-Time Clock (RTC). The chip contains a temperature sensor to
compensate for drift due to temperature fluctuations. Please note, however, that although the MOACON's
RTC is better than most, it is not 100% accurate, and should be synchronized to a time source periodically.
The RTC is powered by a battery that keeps the RTC counting when no power is supplied to the MOACON.
The battery's life span is approximately 10 years.
Days of the week (the "Day" field) are coded as shown in the following table
|Sunday |1|
|Monday |2|
|Tuesday |3|
|Wednesday| 4|
|Thursday| 5|
|Friday| 6|
|Saturday| 7|
The hour field ranges from 0 to 23 so one can distinguish between AM and PM.
#include "moacon500.h"
void cmain(void)
{
//Set the current date and time
rtcWrite(6, 0x11); //2011-03-15 (March 15, 2011)
rtcWrite(5, 0x03);
rtcWrite(4, 0x15);
rtcWrite(2, 0x13); //01:17:23 pm
rtcWrite(1, 0x17);
rtcWrite(0, 0x23);
while(1) //Run forever
{
//Print the current date and time to the debug console
printf("The current date and time is:\r\n");
printf("20%02X-%02X-%02X %02X:%02X:%02X\r\n", //yyyy-mm-dd hh:mm:ss
rtcRead(6), rtcRead(5), rtcRead(4), //year, month, day
rtcRead(2), rtcRead(1), rtcRead(0)); //hour, minute, second
delay(1000);
}
}
{{ :moacon:rtcread:rtcdisplay.png?nolink |}}
To convert between [[https://en.wikipedia.org/wiki/Binary-coded_decimal|Binary
Coded Decimal (BCD)]] and ordinary binary, consider using the following functions.
u8 bcd2bin(u8 x)
{
return ((x >> 4) * 10) | (x & 0x0F);
}
u8 bin2bcd(u8 b)
{
return ((b / 10) << 4) | (b % 10);
}
[[MOACON:index#System_Library:|MOACON home]]