• AVR Freaks

Hot!PIC24F RTCC --How Do I program it

Page: 12 > Showing page 1 of 2
Author
catshot
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2010/09/30 16:45:47
  • Status: offline
2010/09/30 17:38:56 (permalink)
0

PIC24F RTCC --How Do I program it

I am new (like 2 weeks ago) to PIC programming, I design motor controls, first time to use a MCU.
I have chosen a PIC24FJ64GB002 to use in a stand alone app.
I am not an experienced C programmer, so I need help in setting up the the date and time of the RTCC.
I have looked at all the examples I could find at Microchip.
Is there a simple,not elegant, method of programming the RTCC?
At this point I will be happy to brute force repetitive lines  of  code to get it done.
I have studied the  ".h" files and could not decipher how to write/read  the RTCVAL registers, Pg 29-7, reg 29-4 of Section 29, Real-Time Clock and Calender.
I do know that I need to use the RCFGCAL reg bits 9-8 to point to the RTCVAL reg.
I ask again,until my skill increases, simple, straight to the point and clearly understandable to the "not  intuitively obvious" person that I am.
I have not attached any code because they are just snipetts of examples I don't understand fully.
Thanks
Catshot
#1

23 Replies Related Threads

    Jim Nickerson
    User 452
    • Total Posts : 6584
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2010/09/30 18:27:25 (permalink)
    0
    You might find Devices, PIC24FJ64GB004Family, PIC24FJ64GB002, RTCC of use. in "Microchip PIC24F Peripheral Library.chm", mine is located at "C:\Program Files (x86)\Microchip\MPLAB C30\docs\periph_lib"
    #2
    Jim Nickerson
    User 452
    • Total Posts : 6584
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2010/09/30 18:42:45 (permalink)
    0
    Attached is the Rtcc  example from the help file.
    Rename the .txt to 24FRtcc.zip and expand it.
    The forum does not allow .zip files as attachments.
    #3
    catshot
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2010/09/30 16:45:47
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2010/09/30 19:07:58 (permalink)
    0
    Thanks Jim.
    It will take time to digest this, back to the books and learn about structures and such. I have looked at this a while a back and didn't understand it well enough to use it.
    Are the RTCVAL regs (date and time) out of reach of simple programming, Is there something I have missed while reading the manual?
     
    #4
    Jim Nickerson
    User 452
    • Total Posts : 6584
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2010/09/30 19:14:24 (permalink)
    0
    I will look up the names to use to touch them directly.
    Using the library calls will make your life much easier.
    I have attached a new zip that sets the structures to something meaningful.
    Unfortunately the simulator does not include the RTCC module so I could not test this on a part.
    Maybe tomorrow I can find a 24F to run the code on.

    #5
    Jim Nickerson
    User 452
    • Total Posts : 6584
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2010/09/30 20:00:32 (permalink)
    0

    This compiles, I do not have a 24F with a Rtcc to test.
         
        RCFGCALbits.RTCPTR = 3;;
        asm volatile("push w7");
        asm volatile("push w8");
        asm volatile("disi #5");
        asm volatile("mov #0x55, w7");
        asm volatile("mov w7, _NVMKEY");
        asm volatile("mov #0xAA, w8");
        asm volatile("mov w8, _NVMKEY");
        asm volatile("bset _RCFGCAL, #13"); //set the RTCWREN bit
        asm volatile("pop w8");
        asm volatile("pop w7");
        RTCVAL  = 0x0010;   // year
        RTCVAL = 0x0930;    // month, day
        RTCVAL = 0x0510;    // week day, hour
        RTCVAL = 0x0509;    // mins, seconds
     





    post edited by JANickerson - 2010/09/30 20:41:19
    #6
    catshot
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2010/09/30 16:45:47
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2010/09/30 21:02:28 (permalink)
    0
    Jim,
    I have downloaded the example. It builds fine. I will transport it to my code in the morning(central time) and test it out in the MPLab sim.
    Thanks a lot for the help and your time, frustration level is down considerablely......
    I will let you know how it turns out.
    #7
    Jim Nickerson
    User 452
    • Total Posts : 6584
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2010/10/01 07:05:00 (permalink)
    0

    My bad.
    In 24Frtcc2
     
    RtccTime.f.hour = 0x19; // change, it is stored in bcd

    RtccDate.f.mday = 0x30; // change, it is stored in bcd




    #8
    catshot
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2010/09/30 16:45:47
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2010/10/01 08:39:14 (permalink)
    0
    I tried your suggestion using the reg write of RTCVAL.
    This works fine in simulation.
    I am about to try the other example and the fix as per the last post.
    It is now off to get clocks clocking and ticks ticking.
    Thanks again for all the help and I will let you know how it turns out.
    Catshot
    #9
    aschen0866
    Super Member
    • Total Posts : 4546
    • Reward points : 0
    • Joined: 2006/01/08 22:18:32
    • Location: San Diego
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2010/10/01 09:19:45 (permalink)
    0
    JANickerson
          
       ...
       asm volatile("push w7");
       asm volatile("push w8");
       asm volatile("disi #5");
       asm volatile("mov #0x55, w7");
       asm volatile("mov w7, _NVMKEY");
       asm volatile("mov #0xAA, w8");
       asm volatile("mov w8, _NVMKEY");
       asm volatile("bset _RCFGCAL, #13"); //set the RTCWREN bit
       asm volatile("pop w8");
       asm volatile("pop w7");
     

     
    There is nothing wrong with your inline assembly, but I think C30 already provides the mechanism to do so, i.e., DISI(5) and __builtin_write_RTCWEN(). 


    #10
    Jim Nickerson
    User 452
    • Total Posts : 6584
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2010/10/01 11:35:47 (permalink)
    0
    aschen0866

    JANickerson
           
      ...
     asm volatile("push w7");
     asm volatile("push w8");
     asm volatile("disi #5");
     asm volatile("mov #0x55, w7");
     asm volatile("mov w7, _NVMKEY");
     asm volatile("mov #0xAA, w8");
     asm volatile("mov w8, _NVMKEY");
     asm volatile("bset _RCFGCAL, #13"); //set the RTCWREN bit
     asm volatile("pop w8");
     asm volatile("pop w7");


     
    There is nothing wrong with your inline assembly, but I think C30 already provides the mechanism to do so, i.e., DISI(5) and __builtin_write_RTCWEN(). 

    Thank you, I copied from the manual.
    You are quite correct, as the op did ask

    At this point I will be happy to brute force repetitive lines  of  code to get it done.


    I thought I would provide it.
    #11
    howardtang85
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2015/01/23 00:51:29
    • Location: 0
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2015/04/16 20:02:01 (permalink)
    0
    Hi,
     
    May I know how do I set the RTCC to use the internal oscillator or external tuning fork?
     
    (*I initialize the RTCC using the library in C30 compiler as suggested by the earlier post)
    #12
    jack@kksound
    code tags!
    • Total Posts : 3224
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2015/04/17 11:09:09 (permalink)
    0
    From the data sheet:
     

    20.1 RTCC Source Clock
    The user can select between the SOSC crystal oscillator
    or the LPRC Low-Power Internal RC Oscillator as the
    clock reference for the RTCC module. This is configured
    using the RTCOSC (CW4) Configuration bit. This
    gives the user an option to trade off system cost,
    accuracy and power consumption, based on the overall
    system needs

     
     
    #13
    howardtang85
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2015/01/23 00:51:29
    • Location: 0
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2015/04/19 18:30:58 (permalink)
    0
    Hi Jack,
     
    I read this from the datasheet as well. However, I am not very familiar with PIC programming; hence, I have no clue how to set the CW4 register and thus the RTCOSC.
     
    Could you kindly advise on that? Thanks!
    #14
    Aussie Susan
    Super Member
    • Total Posts : 3686
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2015/04/19 20:01:31 (permalink)
    0
    The CW4 register reference is to one of the configuration registers that are set using the #pragma or special macros  (depending on the compiler) and use various names something like "_CONFIG1". These config bits determine how the MCU powers up.
    Look in the data sheet and there is generally a section named "Special Features" that describes all of the config settings.
    Susan
    #15
    howardtang85
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2015/01/23 00:51:29
    • Location: 0
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2015/04/19 20:43:40 (permalink)
    0
    Hi Susan,
     
    I used some codes which I found in one of the forums (as shown below) to configure the clock source:
     
    /////////////////////////////////////////////////
    asm("mov #OSCCON,W1");
    asm("mov.b #0x02, W0");
    asm("mov.b #0x46, W2");
    asm("mov.b #0x57, W3");
    asm("mov.b W2, [W1]");
    asm("mov.b W3, [W1]");
    asm("mov.b W0, [W1]");
    ////////////////////////////////////////////////
     
    However, I am unable to find the value of W1 - W3, and I can't read what value is being copied to which register.
     
    From the datasheet, the clock source for the RTCC can be configured by the BIT5 of CW4, the address of CW4 is ABF8h, may I know any part of the code above try to configure the BIT5 of CW4?
     
    Thanks!
     
     
    Howard 
    #16
    ric
    Super Member
    • Total Posts : 26159
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re:PIC24F RTCC --How Do I program it 2015/04/19 20:51:25 (permalink)
    3 (1)
    Howard, please read Susan's reply again.
    You can NOT write to those registers in running code, they are part of the power-up configuration, set using #pragma statements in your source code.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #17
    howardtang85
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2015/01/23 00:51:29
    • Location: 0
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2015/04/19 23:00:23 (permalink)
    0
    Hi Ric,
     
    I have finally managed to setup the config (in _CONFIG4) such that the rtcc takes the osc value from the LP OSC.
     
    _CONFIG1(WDTPS_PS1 & FWPSA_PR32 & WINDIS_OFF & FWDTEN_OFF & ICS_PGx1 & GWRP_OFF & GCP_OFF & JTAGEN_OFF)
    _CONFIG2(POSCMOD_NONE & I2C1SEL_PRI & IOL1WAY_OFF & OSCIOFNC_ON & FCKSM_CSDCMD & FNOSC_FRCPLL & PLLDIV_NODIV & IESO_OFF)
    _CONFIG3(WPFP_WPFP0 & WPDIS_WPDIS & WPCFG_WPCFGDIS & WPEND_WPENDMEM & SOSCSEL_IO) // SOSCSEL_IO: get RA4 and RB4 as digital I/O
    _CONFIG4(DSWDTPS_DSWDTPS3 & DSWDTOSC_LPRC & RTCOSC_LPRC & DSBOREN_OFF & DSWDTEN_OFF)

     
    However, I still can't get the clock ticking. I used the codes from C30 (V3.31) peripheral library, and my codes are as follow:
    #define USE_AND_OR /* To enable AND_OR mask setting */
    #include <rtcc.h>
    #include "logger.h"


    rtccTime RtccTime; /* Initialize time struct with required values */
    rtccTime RtccTimeVal; /* Initialize time struct with required values */
    BOOL update_time;
    BOOL Time_Input_out_of_range;
    //BOOL Date_Input_out_of_range;
    rtccDate RtccDate;

    void rtccInit(void)
    {
      RtccTime.f.sec = 1;
      RtccTime.f.min = 7;
      RtccTime.f.hour = 0x19;

      RtccDate.f.mday = 0x30;
      RtccDate.f.mon = 9;
      RtccDate.f.wday = 5;
      RtccDate.f.year = 0x10;
        
      RtccInitClock(); //turn on clock source
      
      
      RtccWrOn(); //enable RTCC peripheral
     

     
      Time_Input_out_of_range = (!RtccWriteTime(&RtccTime, TRUE));

      Date_Input_out_of_range = (!RtccWriteDate(&RtccDate, TRUE));

      RCFGCALbits.RTCEN=1;

    }

     
    Then in my main program, I code it this way:
     
    #include <rtcc.h>
    extern rtccTime RtccTime;
    extern rtccTime RtccTimeVal;

    int main (void)
    {
      rcctInit();

      while(1)
      {
        RtccReadTime(&RtccTimeVal);
            
        dbgLog("%hhx:%hhx:%hhx\n", RtccTime.f.hour, RtccTime.f.min, RtccTime.f.sec); //dbgLog is a printf command module
      }

    }



     
    The dbgLog command prints the hour, min and sec to a virtual terminal, however, the time is frozen at 19:07:01 in every loop. May I know if you can advise on that?
    #18
    Aussie Susan
    Super Member
    • Total Posts : 3686
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2015/04/20 19:49:39 (permalink)
    0
    I have not ever bothered with the library routines for the RTCC so I *assume* that it is working correctly.
    If you are using the LPRC, then note the following from section 9.2.4.8 of the data sheet:

    Running the RTCC from LPRC will result in a loss of accuracy in the RTCC of approximately 5 to 10%.

    I have never used that source, always a 32.768kHz crystal on the SOSC and I know that works very reliably. Using the LPRC, you will probably want to play with the CAL bits in the RCFGCAL register.
    Have you tried routing the clock source or the 1 second timer signal to the RTCC pin and making sure that the the clock source is working and the RTCC peripheral is responding?
    If that works then there could be an issue with the way the way the data is being read.
    I note in the 'rtccInit' function that the last statement looks like you are wanting to enable the RTCC peripheral. Two things; 1) is that not what the 'rtccWrOn' function call is supposed to do (and I'm guessing here as I don't know the library functions but the comment implies this) and 2) your last statement will not work without the  RTCWREN bit begin set and section 20.2.2 shows how to do that. (Personally I prefer to use the builtin macro but I use the XC16 compiler and I'm not sure the name of the equivalent in C30.)
    Susan
     
    #19
    howardtang85
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2015/01/23 00:51:29
    • Location: 0
    • Status: offline
    Re:PIC24F RTCC --How Do I program it 2015/04/20 20:45:05 (permalink)
    0
    Hi Susan,
     
    Thanks for the reply! Do you mind to share with me the macro you used in XC16 compiler? It's because I can switch to XC16 compiler as well.
     
     
    Howard
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5