Friday, May 3, 2013

Setting up an alarm on the RTC module




Reading the MCP79410 data sheet, I found the purpose of the MFP pin on the RTC module (see this post). I promised to come back in the near future to dig more about this. This pin is used for special functions like setting an alarm or getting a square wave signal.

MCP79410 Alarms

This chip provides two alarms: alarm 0 and 1. They are both structured as the main time/date registers, just the address change. Alarm 0 uses the addresses ranging from 0x0A to 0x0F, while Alarm 1 is from 0x11 to 0x16.

In order to set an alarm one should first set bits 4 and 5 of the configuration register according to the following table

Bit 4 Bit 5 Meaning
0 0 No alarm
0 1 Alarm 0 is set
1 0 Alarm 1 is set
1 1 Unused

The configuration register can be found at address 0x7.

Library

I developed a small library for playing with the RTC module. Successively, I decided to expand the library to cover all sort of sensors, modules, etc that I was cumulating so far. It can be found on github at the following address: https://github.com/maczinga/ASOM. Commands related to RTC module are in RTC.h and RTC.cpp. Below we briefly resume those relative to alarms.



Alarm 0 and 1 can be referenced using the two predefined constants:


 static const uint8_t RTC_ALM0=0x0A;
 static const uint8_t RTC_ALM1=0x11;

Please use the variables to reference alarms and not directly the values 0xsomething since they can change in future releases. The following instruction is used to setup criteria to trigger an alarm:


 void setAlarmMatch(const uint8_t target, const char *format, ...);

where target is one of the two alarms and format is a (case insensitive) string containing the characters s, m, h, d, with the following meaning:

s = match seconds
m = match minutes
h = match hours
d = match day (alarm triggered at 12:00:00 AM)
a = match seconds, minutes, hours, day, date, month

To know which match criteria are set for the target alarm just use


 uint8_t getAlarmMatch(const uint8_t target);

the encoding of the return value is the same as the format chars of the previous instruction. Now we just need a method to test if an alarm is triggered or not:


boolean isAlarmTriggered(const uint8_t target)

Remark that when an alarm is triggered, the hardware sets an internal flag. The above function just tests
if this flag is on or not. Once triggered, the flag stays on until reset by the software. This can be done using the following instruction:


void alarmFlagReset(const uint8_t target);

That's all for the moment. Happy playing!

Future developments

The MCP79410 offers other interesting surprises. I will present them in a future post. Stay tuned!

No comments :

Post a Comment