RTC not changing time values.

Thread Starter

matthew798

Joined Jan 16, 2013
38
Hello all!

I am using a pic 18f24j11, and I am trying to get the Real-Time clock going. I can't seem to however. Here is my code:

Code:
char sec;

void main(){
     EECON2 = 0x55;
     EECON2 = 0xAA;

     RTCCFG.RTCEN = 1;
     RTCCFG.RTCWREN = 1;
     RTCCFG.RTCSYNC = 0;
     RTCCFG.HALFSEC = 0;
     RTCCFG.RTCOE = 1;
     RTCCFG.RTCPTR0 = 0;
     RTCCFG.RTCPTR1 = 0;

     RTCCAL = 0x00;

     ALRMCFG.ALRMEN = 0;

     TRISB = 0x00;
     
     RTCVALL = 0x05;
     
     do{
        sec = RTCVALL;
        if(sec % 2 == 0){
        PORTB = 0xff;
        }
        PORTB = 0x00;
        RTCCFG.RTCPTR0 = 0;
        RTCCFG.RTCPTR1 = 0;
       
     }while(1);
}
I'm using mikroC, so the relevant config bits are: RTCC Clock Select : INTRC

Basically, I want all of port B to flash as the seconds go by, but nothing happens. In debug mode, the RTCVALL and RTCVALH do not change and on the circuit port B does not flash.

I'm thinking I may have not done something write as far as the internal oscillator goes, but I really can't figure out what it is. If anyone has any insight t would be greatly appreciated!!
 

Thread Starter

matthew798

Joined Jan 16, 2013
38
Well, I've figured it out. If this can ever help anyone, GOOD!

I changed my code to:

Code:
char sec;

void main(){
     EECON2 = 0x55;
     EECON2 = 0xAA;

     RTCCFG.RTCEN = 0;       //Disable RTC
     RTCCFG.RTCWREN = 1;     //Enable writing to RTC timer
     RTCCFG.RTCPTR0 = 1;     //Set pointers before setting date
     RTCCFG.RTCPTR1 = 1;
     
     RTCVALL = 0x14;         //Minutes
     RTCVALL = 0x1F;         //Hours
     RTCVALL = 0x09;         //Day
     RTCVALL = 0x00;         //Year
     
     RTCCFG.RTCPTR0 = 1;     //Set pointers before setting date
     RTCCFG.RTCPTR1 = 1;
     
     RTCVALH = 0x00;         //RESERVED Always 00
     RTCVALH = 0x08;         //Month
     RTCVALH = 0x00;         //Weekday
     RTCVALH = 0x0D;         //Minutes

     RTCCFG.RTCEN = 1;       //Enable RTC
     RTCCFG.RTCWREN = 0;     //Disable writing to RTC timer
     
     RTCCAL = 0x00;

     ALRMCFG.ALRMEN = 0;

     TRISB = 0x00;
     
     RTCVALL = 0x05;
     
     do{
        sec = RTCVALL;
        if(sec % 2 == 0){
        PORTB = 0xff;
        }
        PORTB = 0x00;
        RTCCFG.RTCPTR0 = 0;
        RTCCFG.RTCPTR1 = 0;
       
     }while(1);
}
What's different? First, i disabled the RTC BEFORE writing to it. Second, I wrote to it! Before, I was starting it off from 0. Perhaps it requires a value to start from?

Anyways, now it works. I had a hard time getting this to work. Usually, I'm a wizard with google and can find anything. This is something I had a really hard time figuring out. Here is a link to a resource that REALLY helped me: http://minhdanh2002.blogspot.ca/2014/03/using-real-time-clock-and-calendar-rtcc.html

Good luck!
 

DaveLong

Joined Nov 28, 2017
1
Hi Mathew,

I was very pleased to find that your code helped me out - I was stuck with exactly the same problem. One question:- in my case it wasn't the enable that was causing the problem. Adding these two lines:

EECON2 = 0x55;
EECON2 = 0xAA;

Fixed my problem and got the clock to run. One problem - I don't understand why these lines are needed, in the data sheet I only find reference to these registers in a sequence used to erase the program memory. Can you shed any light on this for me if you still happen to read this forum ?

Kind regards,
Dave Long
 
Top