• AVR Freaks
Reply to post

Hot!MCP79520 - issue setting date to the 31st of the month

Author
otisman37
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2019/10/31 17:08:01
  • Location: 0
  • Status: offline
2019/11/05 15:00:26 (permalink)
0

MCP79520 - issue setting date to the 31st of the month

Hello forum, this is my first post.  I've come across a strange problem that I cannot solve.  We use the MCP79520 RTCC in a product we recently released.  On the 31st of October we had a few calls where customers could not set the date correctly.  When setting the date to the 31st of October, it would revert to the 1st of October.  The data being written to the chip seems to be correct, and setting to (most) any other date works.
ie. Setting the date to 31st of January works, and 31st of March works.  The issue only occurs for 31st of May, July, August, October and December.  Also 29th of February on a leap year does not work.
The clock will roll from the 30th to 31st without issue - but we just can't set the date to the 31st, it always come back as the 1st.  Does anyone have any ideas?!

15 Replies Related Threads

    ric
    Super Member
    • Total Posts : 27979
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: MCP79520 - issue setting date to the 31st of the month 2019/11/07 21:41:17 (permalink)
    0
    How are you writing the new date?
    Are you doing all the registers in a single transfer, or one register per transfer?
     

    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!
    NKurzman
    A Guy on the Net
    • Total Posts : 18852
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: MCP79520 - issue setting date to the 31st of the month 2019/11/07 21:56:54 (permalink)
    0
    What number are you using for October?
    Is January 0 or 1 on you chip?
    NKurzman
    A Guy on the Net
    • Total Posts : 18852
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: MCP79520 - issue setting date to the 31st of the month 2019/11/07 22:08:12 (permalink)
    0
    Duplicate
    ric
    Super Member
    • Total Posts : 27979
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: MCP79520 - issue setting date to the 31st of the month 2019/11/07 22:09:43 (permalink)
    0
    According to TABLE 5-3: in the datasheet, January is 01.
     

    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!
    ric
    Super Member
    • Total Posts : 27979
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: MCP79520 - issue setting date to the 31st of the month 2019/11/11 12:28:11 (permalink)
    0
    Any news otisman37?
     

    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!
    otisman37
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2019/10/31 17:08:01
    • Location: 0
    • Status: offline
    Re: MCP79520 - issue setting date to the 31st of the month 2019/11/11 20:46:48 (permalink)
    0
    Hi, Thanks for the replies, and sorry for my late response.
     
    I have not been able to work it out yet.  The behavior is very odd.  A few days ago, setting the 30th of November worked no problem, but today it did not work (reverting to the 1st)... until I set the time to 23:59 on the 29th of November, and let it roll over by itself to the 30th.... now I can set the 30th of November no problem...
     
    @ric we send everything in one transfer, so the day of the month is somewhere in the middle.
     
    @NKurzman, January is 01 - October is 10.  BCD format.
     
    ric
    Super Member
    • Total Posts : 27979
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: MCP79520 - issue setting date to the 31st of the month 2019/11/11 20:53:27 (permalink)
    0
    What if you set it twice?
    That would ensure it had the correct month selected when the date was written in the second transfer.
     

    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!
    otisman37
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2019/10/31 17:08:01
    • Location: 0
    • Status: offline
    Re: MCP79520 - issue setting date to the 31st of the month 2019/11/11 20:55:14 (permalink)
    0
    This is the function that writes to the RTCC, called in the main loop of the program.
    It will write to the RTCC when "writertccreq" is true.
     
    int writeXRTCCseq(void) {
    auto unsigned char rd_data, wr_data;
    auto unsigned char secs;
    switch (WXState) {
    case 0:
    if (spimode == 1) {
    if (writertccreq && (RXState == 0)) {
    WXState = 1;
    writertccreq = 0;
    }
    }
    break;
    case 1:
    rtcc_st = 1;
    rtcc_24 = 0;
    rtcc_vbaten = 1;
    rtcc_control = 0x40; // SQWE = 1
    CSRTCC = 0; // select the external RTCC
    SPI2BUFL = RTCC_READ; // send read command
    WXState++;
    break;
    case 2:
    if (SPI2STATLbits.SPIRBF) { // SPI transfer complete?
    rd_data = SPI2BUFL; // ignore this data
    SPI2BUFL = 0x01; // RTCC/SRAM address 0x01
    WXState++;
    }
    break;
    case 3:
    if (SPI2STATLbits.SPIRBF) { // SPI transfer complete?
    rd_data = SPI2BUFL; // ignore this data
    SPI2BUFL = 0; // send dummy, read data at address 0x01
    WXState++;
    }
    break;
    case 4:
    if (SPI2STATLbits.SPIRBF) { // SPI transfer complete?
    rd_data = SPI2BUFL;
    CSRTCC = 1; // de-select external RTCC
    secs = rd_data & 0x7F;
    if (secs == 0x59) { // wait if in 59th second
    WXState = 1;
    } else {
    WXState++;
    }
    }
    break;
    case 5:
    CSRTCC = 0; // select the external RTCC
    SPI2BUFL = RTCC_WRITE; // send write command
    WXState++;
    break;
    case 6:
    if (SPI2STATLbits.SPIRBF) { // SPI transfer complete?
    rd_data = SPI2BUFL; // ignore this data
    SPI2BUFL = 0x01; // send RTCC/SRAM address 0x01
    WXState++;
    }
    break;
    case 7:
    if (SPI2STATLbits.SPIRBF) { // SPI transfer complete?
    rd_data = SPI2BUFL; // ignore this data
    wr_data = set_tm.sec ;
    wr_data |= (rtcc_st << 7);
    SPI2BUFL = wr_data; // send data for address 0x01
    WXState++;
    }
    break;
    case 8:
    if (SPI2STATLbits.SPIRBF) { // SPI transfer complete?
    rd_data = SPI2BUFL; // ignore this data
    wr_data = set_tm.min;
    SPI2BUFL = wr_data; // send data for address 0x02
    WXState++;
    }
    break;
    case 9:
    if (SPI2STATLbits.SPIRBF) { // SPI transfer complete?
    rd_data = SPI2BUFL; // ignnore this data
    wr_data = set_tm.hour;
    wr_data |= (rtcc_calsgn << 7);
    wr_data |= (rtcc_24 << 6);
    SPI2BUFL = wr_data; // send data for address 0x03
    WXState++;
    }
    break;
    case 10:
    if (SPI2STATLbits.SPIRBF) { // SPI transfer complete?
    rd_data = SPI2BUFL; // ignore this data
    wr_data = set_dt.wday;
    wr_data |= (rtcc_vbat <<4);
    wr_data |= (rtcc_vbaten << 3);
    SPI2BUFL = wr_data; // send data for address 0x04
    WXState++;
    }
    break;
    case 11:
    if (SPI2STATLbits.SPIRBF) { // SPI transfer complete?
    rd_data = SPI2BUFL; // ignore this data
    wr_data = set_dt.mday;
    SPI2BUFL = wr_data; // send data for address 0x05
    WXState++;
    }
    break;
    case 12:
    if (SPI2STATLbits.SPIRBF) { // SPI transfer complete?
    rd_data = SPI2BUFL; // ignore this data
    wr_data = set_dt.mon;
    wr_data |= (rtcc_lp << 5);
    SPI2BUFL = wr_data; // send data for address 0x06
    WXState++;
    }
    break;
    case 13:
    if (SPI2STATLbits.SPIRBF) { // SPI transfer complete?
    rd_data = SPI2BUFL; // ignnore this data
    wr_data = set_dt.year;
    SPI2BUFL = wr_data; // send data for address 0x07
    WXState++;
    }
    break;
    case 14:
    if (SPI2STATLbits.SPIRBF) { // SPI transfer complete?
    rd_data = SPI2BUFL; // ignnore this data
    wr_data = rtcc_control;
    SPI2BUFL = wr_data; // send data for address 0x08
    WXState++;
    }
    break;
    case 15:
    if (SPI2STATLbits.SPIRBF) { // SPI transfer complete?
    rd_data = SPI2BUFL; // ignnore this data
    wr_data = rtcc_calibration;
    SPI2BUFL = wr_data; // send data for address 0x09
    WXState++;
    }
    break;
    case 16:
    if (SPI2STATLbits.SPIRBF) { // SPI transfer complete?
    rd_data = SPI2BUFL; // ignore this data
    CSRTCC = 1; // de-select external RTCC
    WXState = 0;
    }
    break;
    default:
    WXState = 0;
    }
    return WXState;
    }
    otisman37
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2019/10/31 17:08:01
    • Location: 0
    • Status: offline
    Re: MCP79520 - issue setting date to the 31st of the month 2019/11/11 20:59:05 (permalink)
    0
    // FUNCTION DECLARATIONS
    // union/structure for read/write of time into the RTCC device
    typedef union
    {
    struct
    {
    BYTE rsvd; // reserved for future use
    BYTE sec; // BCD codification for seconds, 00-59
    BYTE min; // BCD codification for minutes, 00-59
    BYTE hour; // BCD codification for hours, 00-24
    }; // field access //this was anonymous in xc32...
    BYTE b[4]; // BYTE access
    unsigned short int w[2]; // 16 bits access
    unsigned long int l; // 32 bits access
    }rtccTime;

    // union/structure for read/write of date into the RTCC device
    typedef union
    {
    struct
    {
    BYTE wday; // BCD codification for day of the week, 00-06
    BYTE mday; // BCD codification for day of the month, 01-31
    BYTE mon; // BCD codification for month, 01-12
    BYTE year; // BCD codification for years, 00-99
    }; // field access
    BYTE b[4]; // BYTE access
    unsigned short int w[2]; // 16 bits access
    unsigned long int l; // 32 bits access
    }rtccDate;
    otisman37
    New Member
    • Total Posts : 5
    • Reward points : 0
    • Joined: 2019/10/31 17:08:01
    • Location: 0
    • Status: offline
    Re: MCP79520 - issue setting date to the 31st of the month 2019/11/11 21:05:15 (permalink)
    0
    @ric, It doesn't work if you set it twice...  I tried that!   
     
    Earlier in development there was a bug where the month could be set to zero - and it accepted that.  I can't remember the exact behaviour, but I think it automatically fixed itself (to January) when it rolled over to the next day.
     
    LdB_ECM
    Super Member
    • Total Posts : 404
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: MCP79520 - issue setting date to the 31st of the month 2020/02/15 23:05:30 (permalink)
    0
    Personally rewrite the code that is long and convoluted and hard to follow for debugging much less you have not given us enough of the code to work with.
     
    The single most likely bug given the way you have written the code is you forgot to convert a value to BCD or similar. All we know is you are writing things to the RTC from something called >> set_tm <<
     
    So it begs the question before you attempt to write do you verify that set_tm is carrying valid BCD values, I mean it's a no brainer to scan those values before you even attempt to write it given your bug.
     
    At the moment you seem to jump to the conclusion it is the write to the MCP79520 that isn't working, call me Doubting Thomas but your code leaves way more options than that open and likely.
    nigelwright7557
    Super Member
    • Total Posts : 447
    • Reward points : 0
    • Joined: 2006/11/06 08:15:51
    • Location: 0
    • Status: offline
    Re: MCP79520 - issue setting date to the 31st of the month 2020/02/16 11:04:46 (permalink)
    0
    LdB_ECM
     
     
    At the moment you seem to jump to the conclusion it is the write to the MCP79520 that isn't working, call me Doubting Thomas but your code leaves way more options than that open and likely.




    I have had what I was convinced were problems with IC's and compilers many times and in the end it was just a silly in my code.
    The first thing to do is write BCD numbers for 31st into the IC without any conversions anywhere and see if that works.
     
     
     
    LdB_ECM
    Super Member
    • Total Posts : 404
    • Reward points : 0
    • Joined: 2019/04/16 22:01:25
    • Location: 0
    • Status: offline
    Re: MCP79520 - issue setting date to the 31st of the month 2020/02/16 18:25:36 (permalink)
    0
    As a strange aside I pulled the datasheets on this part  ... have you read Errata 2?
     
    2. Issue:Date Reset after Month Write If the application writes a new month value to the timekeeping registers, some combinations of date and month values may result in the date being reset to 1 or 21, even if the date is also written with the month.

    You are getting a 1 and your sequence does not confirm to the fix sequence
     
    The patch looks fun to implement given your programming code, sorry I smiled when I read it :-)
    Work around:
    Disable the oscillator by clearing the ST bit, and wait for the OSCON bit to clear. Write new Month value as a separate Write command. Then, write the new valid Date value as a separate Write command. Write the Month value again. Then, restart the oscillator and wait until the clock starts or monitor the OSCON bit until it is set. For the special case of February 29, first ensure that the year is a leap year and that the LY bit is set

    post edited by LdB_ECM - 2020/02/16 18:27:06
    ric
    Super Member
    • Total Posts : 27979
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: MCP79520 - issue setting date to the 31st of the month 2020/02/16 19:26:10 (permalink)
    5 (1)
    LdB_ECM
    As a strange aside I pulled the datasheets on this part  ... have you read Errata 2?

    Blimey!
    Microchip have known about this since 2016, but still haven't updated the silicon.
    Gimmie a DS3231M any day. All it lacks is a "hundredths of seconds" facility.

    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!
    simong123
    Lab Member No. 003
    • Total Posts : 1391
    • Reward points : 0
    • Joined: 2012/02/07 18:21:03
    • Location: Future Gadget Lab (UK Branch)
    • Status: offline
    Re: MCP79520 - issue setting date to the 31st of the month 2020/02/17 16:46:04 (permalink)
    0
    LdB_ECM
    As a strange aside I pulled the datasheets on this part  ... have you read Errata 2?

    Wow!
    I agree with ric.
    Glad I gave up on MCHP RTC's after my first try as I found they were very picky with SMD crystals. Also, external caps (!) & no analogue trim so the 32kHz output is much less usefull.
    Moved to Intersil for where size/low power mattered, and DS3231M for everything else.
    Guest
    Quick Reply: (Open Full Version)
      Enter the random characters shown
    Submit Post
    Some restrictions apply to prevent link (URL) Spam.
    URLs in messages, signatures, and PM's are removed unless you have ...
    • been a member for at least 0 day(s);
    • made a total of 0 post(s);
    • earned at least 0 point(s) for post scores (based on the ratings on your posts);
    • earned at least 0 reward point(s);
    Jump to:
    © 2020 APG vNext Commercial Version 4.5