RTC of ARM MBED LPC1768

Introduction

Real Time Clock (RTC) is used for tracking time and maintaining a calendar.

Many applications require to keep a record of time/date for occurrence of certain events. RTCs are useful in such applications.

RTCs come handy in data logging applications. They are also used in devices like computers, laptops, and mobile phones.

RTCs are powered by external batteries so that they can maintain time and date even in case of power failures.

RTCs have several registers that keep a track of time and date.

 

RTC on ARM MBED

ARM MBED board has RTC present on it which provides a hardware clock that can keep track of the date and time.

It provides a UNIX timestamp means returns the time in seconds since January 1st, 1970.

RTC keeps the track of time even if power down mode or power failure is occur because a secondary battery source is provided.

 

Battery Connections for RTC

The connection of the secondary battery (Vb) is shown in the below diagram.

Fig: CMOS Battery interfacing to MBED board

 

RTC Functions for MBED LPC1768

set_time()

This function are used to set the time of RTC in MBED

In this function you can only provide unix timestamp.

You can check current unix timestamp from here.

e.g.

set_time(1256729737)

 

ctime()

This is the basic time function which is convert unix timestamp to UTC time

In this function pass current unix timestamp value in second

e.g.

ctime()

 

localtime()

This function is used to convert the current unix time to local custom formatted time

e.g.

localtime()

 

Program for UTC time

#include "mbed.h"

int main() {
    set_time (1256729737);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
    while (true) {
        time_t seconds = time(NULL);
        printf ("Time as seconds since January 1, 1970 = %d\n", seconds);        
        printf ("Time as a basic string = %s", ctime(&seconds));
        char buffer [32];
        strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds));
        printf ("Time as a custom formatted string = %s", buffer);        
        wait (1);
    }
}

 

How to set clock to a country’s local time zone?

E.g. Let’s set clock to India’s Time zone.

Indias time is +5.30 GMT i.e. 5.30 hours ahead from universal time. So, add this time i.e. +5.30 GMT in unix timestamp which will provide us India’s current unix time.

1 hour = 3600 seconds

5.30 hour = 19800 seconds

 

Program for local time zone of India

#include "mbed.h"

int main() {
    set_time(1519655300+19800);  // Add UNIX time + Indian time zone in second (i.e +5.30)
    while (true) {
        time_t seconds = time(NULL);
        char buffer[32];	//create buffer of 32 
        strftime(buffer, 32, "%I:%M:%S %p\n", localtime(&seconds));
        printf("Time as a custom formatted string = %s", buffer); 
        wait(1);		//wait for 1second
    }
}

Components Used

ARM mbed 1768 LPC1768 Board
ARM mbed 1768 Board
1
Ad