Initialize RTC without set_time, and Retrieve Current Time

20 Mar 2011

I have a 3V coin cell battery connected to the mbed, I would like to set the time once and then have the battery keep the RTC running, and then retrieve the current time upon reset

I have tried the following

#include "mbed.h"

int main() {
    while(1) {
        time_t t = time(NULL);
        printf("%s\n", ctime(&t));
        wait(1);
    }
}

however, the time does not increment at all

but if I do the following

#include "mbed.h"

int main() {
    set_time(0);
    while(1) {
        time_t t = time(NULL);
        printf("%s\n", ctime(&t));
        wait(1);
    }
}

then the RTC is set to 0 at every reset, which is NOT what I want to happen at reset, I would like the mbed to continue using the current RTC time since I have a back up battery

How can I achieve this using the mbed library's API? I know I can do it manually but I want to know if there's an established method used by the mbed community first. Forum, Handbook, and Cookbook search did not return any information that is useful.

edit:

View full screen if you want to see 640x480

The code shown in the video is

#include "mbed.h"

Serial pc(USBTX, USBRX);

int main() {
    while(1) {
        time_t t = time(NULL);    
        printf("%s\n", ctime(&t));
        wait(1);
        while (pc.readable()) {
            set_time(1);
            pc.getc();
        }
    }
}
20 Mar 2011

I would do it like this: read the time; if it's not set/not valid, read the initial setting from the flash drive and set it.

20 Mar 2011

Igor Skochinsky wrote:

I would do it like this: read the time; if it's not set/not valid, read the initial setting from the flash drive and set it.

I can't read the time because the RTC isn't initialized, and the only way to initialize the RTC via mbed.h is to call "set_time" which would ruin the actual time.

20 Mar 2011

I had a similar problem, and it turned out the coin cell I was using was not making contact with the PCB. Once I added a thin layer of solder to that pad area (made it stand up just a bit above the solder mask), and then reinstalled the battery, I could set the time. I could then power down and up and retrieve the clock w/o first altering it. It does drift some seconds a day, and I know there is an adjustment capability for this, but I haven't pursued that yet.

Here's a quick bit of code I applied:

static int SignOf(const int i) {
    return (i >= 0) ? 1 : -1;
}

void ShowTime(int hOffset, int mOffset) {
    ShowTime(0, hOffset, mOffset);
}

void ShowTime(time_t tValue, int hOffset, int mOffset) {
    time_t ctTime;
    char timbuf[70];

    if (tValue == 0)
        tValue = time(NULL);
    ctTime = tValue + hOffset * 3600 + SignOf(hOffset) * mOffset * 60;
    strcpy(timbuf, ctime(&ctTime));
    timbuf[strlen(timbuf)-1] = '\0';
    printf("    %s (offset: %02i:%02i)\r\n", timbuf, hOffset, mOffset);
}

Sample Output:

Quote:

Sat Mar 19 21:26:43 2011 (offset: -6:00)

20 Mar 2011

I can get the RTC to continue if the MCU is reset (via reset button). This works with or without the battery.

It still loses time (resets to 0) when the main power source is lost even when there is a battery. I've checked the voltage on V-BAT and it is enough, I've even tried a 3.7V cell, a 3V cell, and two AAA Duracell batteries in series.

I wonder if this has anything to do with the mbed mystery interface chip since I'm mainly testing by unplugging and then replugging the USB cable.

Since you have it working and I do not, it's something wrong with my end I guess, I would like for you to confirm that you are testing by completely removing all power (except the battery) and then re-applying power, correct?

20 Mar 2011

I have both types of MBED, and do not see these problems,

I have a project I am working on that is normaly battery power, and sometimes from USB. In my code,I read in the time, calculate year,house etc. And if year < 2010 then error, or user intervention.

Don't know if this real helps :)

Ceri

20 Mar 2011

I found this http://mbed.org/forum/mbed/topic/452/ The diode mentioned on http://mbed.org/users/chris/notebook/rtc-battery-backup-current/ is not installed

I'm now connecting V-BAT with a fixed 3.3V regulated source. It seems to be drawing 1.118 mA at all times, with or without the main power source connected.

Is this my cheap multimeter's fault? It's set at the 2 mA range, the readings are roughly the same at 20 mA and 200 mA range.

20 Mar 2011

This snippet works very well for me, I have not had any real problems with retention of RTC

<> Code

void RTC_Get_Time () { seconds = time (NULL);

t = localtime (&seconds);

RTC_H = t->tm_hour; RTC_M = t->tm_min; RTC_S = t->tm_sec;

RTC_Y = (1900 + t->tm_year); RTC_Mt = t->tm_mon; RTC_Dt = t->tm_mday;

RTC_Mt ++; 0 = Jan } code

By the way you all should treat yourselfs to a good Digital Multi Meter (>£30) A good one will save you hours of aggrivation.

Enjoy Ceri

20 Mar 2011

My multimeter should be fine, it can measure current lower than this

View full screen if you want to see 640x480

The code shown in the video is

#include "mbed.h"

Serial pc(USBTX, USBRX);

int main() {
    while(1) {
        time_t t = time(NULL);    
        printf("%s\n", ctime(&t));
        wait(1);
        while (pc.readable()) {
            set_time(1);
            pc.getc();
        }
    }
}
13 Apr 2012

I had the same exact problem. I think if the voltage on VB ever drops a bit low with power off, the RTC clock locks up at Jan 1900 until a new set_time(). I don't know the exact low voltage that kills it, but it seems to be a bit higher than the spec. I was using a .1f supercapacitor and until I got it charged up all the way to 3.3V (initially took several minutes) and tried it again the same thing happened to me over and over. At 3.3V, it now works every time and does not kill the RTC.

14 Apr 2012

I tried a bit more to see what happens. The clock ran for several minutes until the Vb voltage dropped to around 1.8V. Then the clock stopped running, but the last time value would hold and it would start counting again, if power was turned on. Once the voltage dropped to around 0.7V on VB it went to that Jan 1 1900 value and the clock did not run once power was turned back on.

21 May 2019

Guys, how do you exactly power the RTC with the battery? Do you power ALL device with that coin battery or there is some dedicated pin only to uphold the RTC?