User Tools

Site Tools




void rtcWrite(u8 rtcAdr, u8 rtcData)

rtcAdr : Address of the RTC data field (e.g. day, month, year, etc…) from which to read.
rtcData: Value to set in Binary Coded Decimal (BCD)

Sets the RTC data field at address rtcAdr to the value rtcData. rtcData should be in Binary Coded Decimal (BCD)

The following table illustrates the format in which the RTC data is stored in memory. Data is stored in 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.

The following program sets the current date and time, and then prints the current date and time to the debug console.

#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

To convert between 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/rtcwrite/index.txt · Last modified: 2021/10/18 09:53 by COMFILE Technology