Tuesday, 9 September 2014

Using RTC (Real Time Clock) in GR - Sakura


Here is a simple program to enable the inbuilt Real Time Clock (RTC), write the current timing and read the RTC registers in GR-Sakura… This can be used as a reference for your applications.
                The date and time used are 26th August 2014, 8:10:00. The time is displayed via 16 x 2 LCD.

Code:


#include<rxduino.h>
#include<iodefine_gcc63n.h>

#include<liquidcrystal.h>
LiquidCrystal lcd (22,23,24,25,26,27,28);

void init_rtc();
void getvalue();

char year10, year1, month10, month1, day10, day1, hour10, hour1, min10, min1, sec10, sec1, pm;
char g[2][5]={"AM","PM"};

main()
{
                lcd.begin(16,2);  // begin 16 x 2 lcd

                init_rtc();  // initialize RTC
                char line1[10],line2[10];

                while(1)
                {

                getvalue();

                lcd.setCursor(0,0);

                sprintf((char *)line1," %d%d - %d%d - 20%d %d",day10,day1,month10,month1,year10,year1);
                lcd.print(line1);

                lcd.setCursor(0,1);
                sprintf((char*)line2," %d%d:%d%d:%d%d %s",hour10,hour1,min10,min1,sec10,sec1,g[pm]);

                lcd.print(line2);
                }             
}

 
void init_rtc()
{
                RTC.RCR2.BIT.START=0;  // Stop RTC

                while(RTC.RCR2.BIT.START==1){} // wait for START bit to be written as 0 – this is necessary since the RTC’s clock source speed is less than that of Sakura’s clock
                RTC.RCR2.BIT.HR24=0; // 12 hour format

                RTC.RCR2.BIT.RESET=1;
                RTC.RCR3.BIT.RTCEN=0;

                RTC.RCR4.BIT.RCKSEL=1;
                RTC.RYRCNT.WORD=0x0014;  // year

                RTC.RMONCNT.BYTE=0x08;   // month
                RTC.RDAYCNT.BYTE=0x26;   // day

                RTC.RWKCNT.BYTE=0x02;  //week
                RTC.RHRCNT.BYTE=0x48;  // Hour

                RTC.RMINCNT.BYTE=0x10;  // minute
                RTC.RSECCNT.BYTE=0x00;  // second

                RTC.RCR2.BIT.START=1;   // Start RTC
                while(RTC.RCR2.BIT.START==0){} // wait for START bit to be written as 1
  
                IEN(RTC,COUNTUP)=0; 

                RTC.RCR1.BIT.CIE=1;  // carry interrupt is enabled
}

 
void getvalue()   // read time from RTC registers
{
                do
                {

                IR(RTC,COUNTUP)=0;   // interrupt flag is cleared
                year10 = RTC.RYRCNT.BIT.YR10;

                year1 = RTC.RYRCNT.BIT.YR1;
                month10 = RTC.RMONCNT.BIT.MON10;

                month1 = RTC.RMONCNT.BIT.MON1;
                day10 = RTC.RDAYCNT.BIT.DATE10;

                day1 = RTC.RDAYCNT.BIT.DATE1;
                hour10 = RTC.RHRCNT.BIT.HR10;

                hour1 = RTC.RHRCNT.BIT.HR1;
                min10 = RTC.RMINCNT.BIT.MIN10;

                min1 = RTC.RMINCNT.BIT.MIN1;
                sec10 = RTC.RSECCNT.BIT.SEC10;

                sec1 = RTC.RSECCNT.BIT.SEC1;
                pm=RTC.RHRCNT.BIT.PM;

                }while(IR(RTC,COUNTUP)==1);  // an interrupt flag is set when there is a change in RTC registers
}

 

 

Note:

                RTC in Gr-Sakura uses only 64/128k HZ speed whereas Sakura uses 48 Mhz speed. Hence there should be a wait for the RTC register to change their state. This is given in init_rtc() function as,

while(RTC.RCR2.BIT.START==1){}
                        while(RTC.RCR2.BIT.START==0){}

Reference:

Book : Embedded Systems - An introduction using the Renesas RX63N Microcontroller
                                                  By James M. Conrad, Alexander G. Dean

No comments:

Post a Comment