Thread: RTC problem

  1. #1
    Registered User
    Join Date
    May 2011
    Location
    Toronto
    Posts
    20

    RTC problem

    Hi Friends, I'm using a real time clock chip (DS1307) which has back up battery when power goes out. My problem is the chip doesn't keep up time when on B/U power, it just stops there. When power is back on, it keep time from there OK. Problem is power is not always on, but I need accurate time stamps. Chip communicates via I2C serial. Just wondering if there is any line of code I have to add to initialize the chip start up...

    _______________

    Quote from Data sheet:

    CLOCK AND CALENDAR
    The time and calendar information is obtained by reading the appropriate register bytes. The RTC
    registers are illustrated in Figure 3. The time and calendar are set or initialized by writing the appropriate
    register bytes. The contents of the time and calendar registers are in the BCD format. Bit 7 of register 0
    is the clock halt (CH) bit. When this bit is set to a 1, the oscillator is disabled. When cleared to a 0, the
    oscillator is enabled.
    Please note that the initial power-on state of all registers is not defined. Therefore, it is important
    to enable the oscillator (CH bit = 0) during initial configuration.
    The DS1307 can be run in either 12-hour or 24-hour mode. Bit 6 of the hours register is defined as the
    12- or 24-hour mode select bit. When high, the 12-hour mode is selected. In the 12-hour mode, bit 5 is
    the AM/PM bit with logic high being PM. In the 24-hour mode, bit 5 is the second 10 hour bit (20-
    23 hours).
    _____________________

    DS1307 PDF Datasheet

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If the chip does not keep time when the power is off, then you need to assume every time the power comes on that the clock is wrong and find a way to acquire the correct time (perhaps you can ask the thing that the chip is communicating with what time it is?)

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Well, considering we don't have a schematic or single line of code from you, it's hard to say whether you need to add a line of code. The chip is supposed to have an automatic fail-over to battery power, and it should keep time if configured properly. Maybe you send it all the right commands, but have a design/wiring problem. Maybe your battery is bad. Maybe the fail-over circuitry in your RTC is bad. We need more info from you to actually provide any useful help. Also note that this is a C programming forum not an embedded or electronics specific forum. Nevertheless, we have some embedded- and electronics-savvy people here and we'll do our best to help you, if we can get some useful info from you.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    The DS1307 is a well known SRTC and it operates with a backup battery. If yours is not, this must be because either the battery is dead or it's too low voltage (get a new 3V one), excluding any defect to the chip.

    >> Just wondering if there is any line of code I have to add to initialize the chip start up...

    No. If it is not keeping time between Vbat and Vcc swaps there's nothing you can do other than setting the time as tabstop suggests above. This is how you will do it (taken a long time ago from some PIC blog I don't really remember anymore):

    Code:
    void set_rtc(unsigned short addr, unsigned short data) {
    
        I2C_Start(); // Start signal. Obligatory!
    
        I2C_Wr(0xD0); // I2C send byte: device address(0xD) + Write (0))
        I2C_Wr(addr); // send byte
        I2C_Wr(data); // send data
    
        I2C_Stop(); // Stop signal. Obligatory!
    
    }
    
    // Set the NOW time (my local time)
    set_rtc(0,0x80); // 0 second reset and stop oscillator
    set_rtc(1,0x15); // 15 minutes
    set_rtc(2,0x18); // 18 hours
    set_rtc(3,0x02); // Monday
    set_rtc(4,0x01); // Day 1
    set_rtc(5,0x08); // Month 8th
    set_rtc(6,0x11); // Year 2011
    set_rtc(7,0x10); // Control. Always the same.
    set_rtc(0,0x00); // Start oscillator
    Last edited by Mario F.; 08-01-2011 at 11:18 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by RobertD View Post
    Hi Friends, I'm using a real time clock chip (DS1307) which has back up battery when power goes out. My problem is the chip doesn't keep up time when on B/U power, it just stops there.
    According to the datasheet only the memory is battery backed. You should probably look into using a larger battery and keeping the chip itself powered during power off...
    Last edited by CommonTater; 08-01-2011 at 11:44 AM.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by CommonTater View Post
    According to the datasheet only the memory is battery backed. You should probably look into using a larger battery and keeping the chip itself powered during power off...
    From the data sheet, p6, Pin Description table, pin 8:
    Primary Power Supply. When voltage is applied within normal limits, the device is fully
    accessible and data can be written and read. When a backup supply is connected to the
    device and VCC is below VTP, read and writes are inhibited. However, the timekeeping
    function continues unaffected by the lower input voltage.

    It should continue to keep time just fine. I'm with Mario on battery issues.

    @OP: Can you hook up a steady 3V to the battery terminal and see if that works?

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Indeed. The backup battery will power timekeeping too. I would have known by now if it didn't.
    I believe the problem here is that his oscillator is stopping when changing to battery backup mode while the SRAM retains the last time. This is indicative of a defective/failing battery or a low power battery because the oscillator will stop at a higher DC than that required by SRAM to persist data. Essentially the OP's SRTC is operating in data retention-only mode when switching to Vbat. A mode actually supported by the DS1307.

    And you are absolutely right. The OP needs to provide a schematic or let us know the exact board model. Other than the possibility above, it could be this is by design, or if he built it himself he fumbled somewhere.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  2. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  3. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  4. Visual Studio Linker problem or my problem?
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-13-2004, 12:32 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM