Menu

DS1307 to MCP7490N - Clock resets to +48s?

Help
Peter
2014-05-24
2014-05-29
  • Peter

    Peter - 2014-05-24

    I am using a Microchip MCP7490N RTC. It's very similar to the Maxim DS1307, the main changes are that the I2C bus address is different, and you write 1 to bit 7 of 0x00 to start the oscillator instead of writing 0. There are some extra features as well such as alarms which can be set.

    I'm using the DS1307.h library in GCB and I've modified it to include these changes and I am able to start the clock, set the time and read the time (I haven't gone any further yet).

    One problem I am having though, is when I enable the oscillator it adds 48 seconds to the clock.
    If I reset the clock (i.e. set everything to zero) and read the time it comes up with 00:00:00 as expected.
    If I set the time to say 20:18:10 and read the time back it gives 20:18:10.
    If I set the oscillator going, it will then read the time as 20:18:58 then continue running.

    Does anyone know what's going on here? Did the same thing happen on a DS1307? Has anyone else used the MCP7490N with GCB?

    This is the output of my LCD immediately after a power cycle (including of the MCP7940N) showing the values of initOsc and readReg as well as the clock value.
    LCD

    Extract of my MCP7940N.h (based on DS1307.h)

    <font color="AA0000">
     ' This is the address of the generic MCP7940N
     #define MCP7940N_AddrWrite 0xDE
     #define MCP7940N_AddrRead 0xDF
    
     '  Define some useful memory locations
     #define MCP7940N_time 0x00     'Start of the time registers - 0x00
     #define MCP7940N_date 0x04     'Start of the date registers - 0x04
     #define MCP7940N_control 0x07      'Control register
     #define MCP7940N_alarm0 0x0A       'Start of ALARM0 registers
     #define MCP7940N_alarm1 0x11       'Start of ALARM1 registers
     #define MCP7940N_powerDown 0x18    'Start of power loss timestamp registers
     #define MCP7940N_powerUp 0x1C      'Start of power restoration timestamp registers
    
    ;Variables
     Dim Sec As byte
     Dim Sec2 As byte
     Dim Min As byte
     Dim Hour As byte
     Dim DayOfWeek as byte
     Dim CDate as byte
     Dim CMonth as byte
     Dim CYear as Byte
     Dim va as byte
     va=0
     Sec=0
     Min=0
     Hour=0
     CDate=0
     CMonth=0
     CYear=0
    
     'On first application of power to the device the time and date registers
     'are typically reset to 01/01/00 01 00:00:00 (MM/DD/YY DOW HH:MM:SS).
     'The CH bit in the seconds register will be set to a 0
     'Set Bit 7 of 00h register. Oscillator is enabled, clock start
     Sub MCP7940N_Enab_Osc
    
         I2CSTART
         I2CSEND ( MCP7940N_AddrWrite )
         I2CSEND ( 0 )
         I2CSTART
         I2CSEND ( MCP7940N_AddrRead )
         I2CReceive ( sec, NACK )
         Sec=BcdToDec( Sec )
         Sec=  Sec | 128
         I2CSTART
         I2CSEND ( MCP7940N_AddrWrite )
         I2CSEND ( 0 )
         I2CSEND ( DecToBcd(sec) )
         I2CStop
    
     end sub
    </font>
    

    Copy of my code below:

    <font color="0000AA">
    ;Chip Settings
    #chip 16F914,8
    #config CP=OFF, MCLRE=ON, PWRTE=ON, WDT=OFF, OSC=INTRC_OSC_NOCLKOUT
    
    ;Include files (Libraries)
    #include <mcp7940n.h>
    
    ;Defines (Constants)
    #define LCD_IO 8
    #define LCD_RW PORTD.6
    #define LCD_RS PORTD.7
    #define LCD_Enable PORTD.5
    #define LCD_DATA_PORT PORTB
    #define I2C_MODE Master
    #define I2C_DISABLE_INTERRUPTS ON
    #define I2C_DATA PORTC.7
    #define I2C_CLOCK PORTC.6
    
    ;Variables
    Dim hr As byte
    Dim mn As byte
    Dim sc As byte
    Dim i As byte
    Dim initOsc As byte
    Dim readReg As byte
    
    MCP7940N_Enab_Osc
    initOsc = sec
    Do Forever
        CLS
        DisplayTime
        Locate 1, 0
        Print initOsc
        Wait 100 ms
    Loop
    END
    
    Sub DisplayTime
        MCP7940N_ReadTime hr, mn, sc
        readReg = sec
        Print hr
        Locate 0, 2
        Print ":"
        Locate 0, 3
        Print mn
        Locate 0, 6
        Print ":"
        Locate 0, 7
        Print sc
        Locate 1, 8
        Print readReg
        Exit Sub
    End Sub
    </font>
    
     

    Last edit: Peter 2014-05-24
  • Anonymous

    Anonymous - 2014-05-25

    Hi Peter,

    I just posted a new proposed DS1307 include file in the Contributors section. See if this doesn't fix your problem. I did in fact see the anomaly you mentioned earlier this week. But I have been through so many revisions now that I can't remember what the circumstances were. Anyway, the new code works well for me. Let me know if it works for you.

    Thomas Henry

     
    • Peter

      Peter - 2014-05-29

      Sorry for not replying sooner, tonight's been the first bit of free time I've had.

      I took your revised DS1307.h (26/05/2014 version), renamed the functions, did the basic alterations and it has solved the 48s problem, thanks very much for redoing the include file.

      I will plod on with converting the file and post a copy when it's good enough to use. If anyone else want to use the MCP7940N, all you need to change to get started is:
      - The device address to 0xDE and 0xDF
      - Swap 'on' and 'off' in the DS1307_Enable function

       
  • Anobium

    Anobium - 2014-05-25

    And, please let us know what version of the compiler and your GCB configuration. Are you you a standard 2007 install? GCB@Syn installation? or, a HOT Release? This helps us understand what code you have installed.

     
    • Peter

      Peter - 2014-05-29

      I'm using the Hot Release from 13/5/2014, the compiler is v0.9 11/5/2014.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.