Allows easy usage of the LPC1768 RTC interrupts

Dependents:   Mini_DK_clk MakerBotServer GT-ACQUAPLUS_Consumo GT-ACQUAPLUS_Eficiencia ... more

The RTC library allows easy access to the LPC1768s interrupt sources on its RTC. There are two different interrupt sources: periodic interrupts, for example every new second, hour, etc, and an alarm, which is called at a specified time.

Time initialization

This library only allows easy usage of the RTC interrupts. You have to initialize the time functions (so the RTC) the normal way specified here: http://mbed.org/handbook/Time?action=view&revision=11592

Hello World!

#include "mbed.h"
#include "RTC.h"

DigitalOut led(LED1);

void ledFunction( void )
{
    led = 1;
    RTC::detach(RTC::Second);
}

void displayFunction( void )
{
    time_t seconds = time(NULL);
    printf("%s", ctime(&seconds));
}

void alarmFunction( void )
{
    error("Not most useful alarm function");
}

int main()
{
    set_time(1256729737); // Set time to Wed, 28 Oct 2009 11:35:37

    tm t = RTC::getDefaultTM();
    t.tm_sec = 5;
    t.tm_min = 36;

    RTC::alarm(&alarmFunction, t);
    RTC::attach(&displayFunction, RTC::Second);
    RTC::attach(&ledFunction, RTC::Minute);

    while(1);
}

Periodic interrupts

Periodic interrups can be attached by using:

RTC::attach([function], [TimeUnit]);

The TimeUnit specifies which unit should increase for the function to be called. This function is useful if you are making for example a clock: You can simply connect the display update to an RTC interrupt. Of course you can do something similar by using timer objects, but they aren't timed exactly correct, and it is nicer to do it directly on the RTC.

Alarm function

The LPC1768's RTC also allows for one alarm to be activated. The alarm goes off the first time there is a compare match on a specified time and the current time. All fields of the normal C tm structure are supported (http://www.cplusplus.com/reference/ctime/tm/), set a field at -1 for it to be ignored. The RTC::getDefaultTM() function helps with that, it returns a tm structure with every field initialized to -1, so don't care.

So if you want to make an alarm that gets called every monday, every 5 minutes and 30 seconds after the hour, you would use:

tm t = RTC::getDefaultTM();
t.tm_sec = 30;     //30 seconds
t.tm_min = 5;      //5 minute
t.tm_wday = 1;     //monday
RTC::alarm([yourFunction], t);

Attaching member functions

For compactness of the documentation attaching member functions of objects isn't included. However it works exactly the same as for example attaching one to a Ticker object: http://mbed.org/users/mbed_official/code/mbed/docs/63cdd78b2dc1/classmbed_1_1Ticker.html#af92f41ff11906b1f96fa6bbe0b17aa29

Disclaimer

Believe it or not, but I didn't test every possible combination of alarm settings / interrupts. So especially for the larger timescales there is an increased chance on bugs.

Committer:
Sissors
Date:
Wed Dec 05 21:03:44 2012 +0000
Revision:
0:39767ffe05e6
Child:
1:be9d058ee5c7
Version 1, lacks comments, seems to work

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 0:39767ffe05e6 1 #ifndef RTC_H
Sissors 0:39767ffe05e6 2 #define RTC_H
Sissors 0:39767ffe05e6 3
Sissors 0:39767ffe05e6 4 #include "mbed.h"
Sissors 0:39767ffe05e6 5
Sissors 0:39767ffe05e6 6
Sissors 0:39767ffe05e6 7 class RTC {
Sissors 0:39767ffe05e6 8 public:
Sissors 0:39767ffe05e6 9 enum TimeUnit {Second, Minute, Hour,
Sissors 0:39767ffe05e6 10 Day, Month, Year};
Sissors 0:39767ffe05e6 11
Sissors 0:39767ffe05e6 12 static void attach(void (*function)(void), TimeUnit interval);
Sissors 0:39767ffe05e6 13
Sissors 0:39767ffe05e6 14 static void alarm(void (*function)(void), tm time);
Sissors 0:39767ffe05e6 15
Sissors 0:39767ffe05e6 16 static void alarmOff( void );
Sissors 0:39767ffe05e6 17
Sissors 0:39767ffe05e6 18 private:
Sissors 0:39767ffe05e6 19 static void IRQHandler( void );
Sissors 0:39767ffe05e6 20
Sissors 0:39767ffe05e6 21 static FunctionPointer attachCB[6];
Sissors 0:39767ffe05e6 22 static FunctionPointer alarmCB;
Sissors 0:39767ffe05e6 23
Sissors 0:39767ffe05e6 24 //If someone knows a nicer way to do this, please tell me
Sissors 0:39767ffe05e6 25 static bool initialRun;
Sissors 0:39767ffe05e6 26
Sissors 0:39767ffe05e6 27
Sissors 0:39767ffe05e6 28 };
Sissors 0:39767ffe05e6 29
Sissors 0:39767ffe05e6 30 #endif