This extension supports the DS3231 (see product page) and provides several functions for setting and reading:
- the Real Time Clock (RTC) time registers
- the 2 Alarm register sets (times and modes)
- Temperature
NOTE: The DS3231 chip allows alarm 1 to have seconds set, in addition to minutes, hours etc. This extension does NOT (yet) provide for that. In other words, both alarms can have minutes, hours etc set ONLY.
Also, functions are provided for handling the alarms:
- read status register (includes the alarm flags AnF)
- read control register (includes the alarm interrupt enables AnE and the INTCN bit)
- enable/disable alarm (Status register)
- clear alarm flags (Status register)
- enable/disable INT pin (Control register)
- PXT/microbit
- xxx - read clock register where xxx is: year month date (week)day hour minute second
- status - read reg 0x0f
- control - read reg 0x0e
- set - all clock registers together: year month date (week)day hour minute second
- set Alarm time: alarm (1 | 2), mode (Minute | HourMinute | DateHourMinute | DayHourMinute) and date day hour minute
- alarm interrupt enable (1 | 2) (Enable | Disable) AnIE => 1 | 0
- clear alarm flag: alarm (1 | 2) - resets AnF
- configure INTCN (Enable | Disable) - INTCN => 1 | 0
- temperature (upper) - signed (2's complement) of integer part of the on-chip temperature measurement
- temperature (lower) - the upper 2 bits are the fractional portion
Set an alarm:
DS3231.setAlarm(alarmNum.A1,mode.Minute,0,0,alarmHour,alarmMinute)
Record (over USB) a temperature measurement:
serial.writeLine("Temperature: " + TwosCompToDec(DS3231.temperatureUpper()) + "." + Math.floor(DS3231.temperatureLower() / 64) * 25)
This is assumed to have an i2c address of 0x68.
I used this one. It has a holder for a button cell for battery backup.
You can set one or both alarms in several different modes. To avoid having to poll the RTC to detect an alarm you would normally:
- enable the RTC interrupt, and
- connect the interrupt pin (which is marked SQW on my RTC board) to a microbit pin - e.g. P0
- when the microbit detects that P0 has gone low then read the RTC's status register to see which alarm's flag bit has gone high
- then take whatever action you want, including (possibly) clearing that alarm flag.
I connected the 3V, GND, SCL and SCK pins to the corresponding microbit pins. The DS3231 board's interrupt (SQW) pin was connected to the microbit's P0 pin. The photos below show the board and the microbit.
Set time and alarm registers in "all" bit combinations, read back and verify
Functional tests - read temperature, then set and wait for alarms. Clear interrupts.