RTC not changing time values.

Discussion in 'Embedded Systems and Microcontrollers' started by matthew798, Aug 31, 2014.

  1. matthew798

    Thread Starter Member

    Jan 16, 2013
    38
    2
    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 (Text):
    1.  
    2. char sec;
    3.  
    4. void main(){
    5.      EECON2 = 0x55;
    6.      EECON2 = 0xAA;
    7.  
    8.      RTCCFG.RTCEN = 1;
    9.      RTCCFG.RTCWREN = 1;
    10.      RTCCFG.RTCSYNC = 0;
    11.      RTCCFG.HALFSEC = 0;
    12.      RTCCFG.RTCOE = 1;
    13.      RTCCFG.RTCPTR0 = 0;
    14.      RTCCFG.RTCPTR1 = 0;
    15.  
    16.      RTCCAL = 0x00;
    17.  
    18.      ALRMCFG.ALRMEN = 0;
    19.  
    20.      TRISB = 0x00;
    21.      
    22.      RTCVALL = 0x05;
    23.      
    24.      do{
    25.         sec = RTCVALL;
    26.         if(sec % 2 == 0){
    27.         PORTB = 0xff;
    28.         }
    29.         PORTB = 0x00;
    30.         RTCCFG.RTCPTR0 = 0;
    31.         RTCCFG.RTCPTR1 = 0;
    32.        
    33.      }while(1);
    34. }
    35.  
    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!!
     
  2. matthew798

    Thread Starter Member

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

    I changed my code to:

    Code (Text):
    1.  
    2. char sec;
    3.  
    4. void main(){
    5.      EECON2 = 0x55;
    6.      EECON2 = 0xAA;
    7.  
    8.      RTCCFG.RTCEN = 0;       //Disable RTC
    9.      RTCCFG.RTCWREN = 1;     //Enable writing to RTC timer
    10.      RTCCFG.RTCPTR0 = 1;     //Set pointers before setting date
    11.      RTCCFG.RTCPTR1 = 1;
    12.      
    13.      RTCVALL = 0x14;         //Minutes
    14.      RTCVALL = 0x1F;         //Hours
    15.      RTCVALL = 0x09;         //Day
    16.      RTCVALL = 0x00;         //Year
    17.      
    18.      RTCCFG.RTCPTR0 = 1;     //Set pointers before setting date
    19.      RTCCFG.RTCPTR1 = 1;
    20.      
    21.      RTCVALH = 0x00;         //RESERVED Always 00
    22.      RTCVALH = 0x08;         //Month
    23.      RTCVALH = 0x00;         //Weekday
    24.      RTCVALH = 0x0D;         //Minutes
    25.  
    26.      RTCCFG.RTCEN = 1;       //Enable RTC
    27.      RTCCFG.RTCWREN = 0;     //Disable writing to RTC timer
    28.      
    29.      RTCCAL = 0x00;
    30.  
    31.      ALRMCFG.ALRMEN = 0;
    32.  
    33.      TRISB = 0x00;
    34.      
    35.      RTCVALL = 0x05;
    36.      
    37.      do{
    38.         sec = RTCVALL;
    39.         if(sec % 2 == 0){
    40.         PORTB = 0xff;
    41.         }
    42.         PORTB = 0x00;
    43.         RTCCFG.RTCPTR0 = 0;
    44.         RTCCFG.RTCPTR1 = 0;
    45.        
    46.      }while(1);
    47. }
    48.  
    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!
     
    ErnieM and absf like this.
Loading...