Real-time Clock

BitMasher features a clock which is displayed when the device is put to sleep. This clock is driven by the real-time clock and calendar module (RTCC). Functions to setup and query the RTCC module are found in BM_RTCModule. Clocking for the real-time clock module is derived from the external low-frequency (32.768 kHz) oscillator.

Querying the Time

The functions, BM_RTCModule_getCurrentTimeHMS and BM_RTCModule_getCurrentTimeBCD can be used to get the current time. There are two formats for representing time: Hour-Minute-Second (HMS) and Binary Coded Decimal (BCD).


In HMS representation, the hour, minute and second is assigned its own variable and is most convenient to work with:

typedef struct
    int32_t hour;
    int32_t minute;
    int32_t second;

Calling BM_RTCModule_getCurrentTimeHMS will return the current time in HMS. A pointer to an instance of BM_RTCModule_TimeHMS is required as a parameter input.


In BCD representation, each digit in the hour, minute and second is assigned its own variable:

BCD Representation


typedef struct
    int32_t hourT;
    int32_t hourU;
    int32_t minuteT;
    int32_t minuteU;
    int32_t secondT;
    int32_t secondU;

While this may seem inconvenient at first, the advantage of BCD is that each digit can be packed into a single 32-bit variable and easily transferred to the MCU's RTCC registers.

The time can be converted between different representations by using BM_RTCModule_convertBCDToHMS and BM_RTCModule_convertHMSToBCD functions.

Setting the Time

BM_RTCModule_setTime is used to set the time. This function expects the time in HMS format.


In addition to telling the time, the BM_RTCModule also features a counter that increments every minute (every minute, an interrupt is triggered which increments the counter). This counter is planned to be used in a timeout system that puts the device to sleep after a period of inactivity.