Skip to content

Class DS3231M

ClassList > DS3231M

Class for DS3231M RTC communication.

  • #include <DS3231M.h>

Public Attributes

Type Name
uint8_t rtc
Array to store RTC time and date data.

Public Functions

Type Name
DS3231M (TwoWire * pWire=&Wire)
Constructor of the DS3231M class.
bool begin (void)
Initializes I2C communication.
void clearAlarm ()
Clears the alarm flags in the status register.
void disable32k ()
Disables the 32kHz square wave output on the SQW/INT pin.
void enable32k ()
Enables the 32kHz square wave output on the SQW/INT pin.
DateTime getRTCTime ()
Gets the current RTC time.
float getTemp ()
Gets the current temperature from the DS3231M sensor.
bool isAlarm ()
Checks if any alarm has been triggered.
bool lostPower (void)
Checks if the RTC lost backup power.
void setAlarm1 (const DateTime & dt, eAlarmType_t type)
Sets the first RTC alarm (alarm 1).
void setAlarm2 (const DateTime & dt, eAlarmType_t type)
Sets the second RTC alarm (alarm 2).
void setRTCTime (uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
Sets the RTC time and date.
~DS3231M ()
Destructor of the DS3231M class.

Protected Functions

Type Name
uint8_t dayOfTheWeek (uint16_t y, uint8_t m, uint8_t d) const
Calculates the day of the week for a given date.
virtual uint8_t readReg (uint8_t reg, const void * pBuf, size_t size)
Reads data from a specific DS3231M register via I2C.
virtual void writeReg (uint8_t reg, const void * pBuf, size_t size)
Writes data to a specific DS3231M register via I2C.

Protected Static Functions

Type Name
uint8_t bcd2bin (uint8_t val)
Converts a BCD (Binary-Coded Decimal) value to BIN (Binary).
uint8_t bin2bcd (uint8_t val)
Converts a BIN (Binary) value to BCD (Binary-Coded Decimal).

Public Attributes Documentation

variable rtc

Array to store RTC time and date data.

uint8_t DS3231M::rtc[7];


Public Functions Documentation

function DS3231M

Constructor of the DS3231M class.

inline DS3231M::DS3231M (
    TwoWire * pWire=&Wire
) 

Parameters:

  • pWire Peripheral used for I2C communication.

function begin

Initializes I2C communication.

bool DS3231M::begin (
    void
) 

Returns:

True if communication with the module succeeds, false if it fails.


function clearAlarm

Clears the alarm flags in the status register.

void DS3231M::clearAlarm () 

This is necessary after an alarm has been triggered so it can be activated again.


function disable32k

Disables the 32kHz square wave output on the SQW/INT pin.

void DS3231M::disable32k () 


function enable32k

Enables the 32kHz square wave output on the SQW/INT pin.

void DS3231M::enable32k () 


function getRTCTime

Gets the current RTC time.

DateTime DS3231M::getRTCTime () 

Returns:

DateTime object containing the current time and date.


function getTemp

Gets the current temperature from the DS3231M sensor.

float DS3231M::getTemp () 

Returns:

Temperature in degrees Celsius (float).


function isAlarm

Checks if any alarm has been triggered.

bool DS3231M::isAlarm () 

Returns:

True if alarm 1 or alarm 2 has been triggered, false otherwise.


function lostPower

Checks if the RTC lost backup power.

bool DS3231M::lostPower (
    void
) 

Returns:

True if backup power was lost.


function setAlarm1

Sets the first RTC alarm (alarm 1).

void DS3231M::setAlarm1 (
    const DateTime & dt,
    eAlarmType_t type
) 

Parameters:

  • dt DateTime object with the alarm time and date.
  • type Alarm type (e.g. every minute, every hour, every day, etc.).

function setAlarm2

Sets the second RTC alarm (alarm 2).

void DS3231M::setAlarm2 (
    const DateTime & dt,
    eAlarmType_t type
) 

Parameters:

  • dt DateTime object with the alarm time and date.
  • type Alarm type (e.g. every minute, every hour, every day, etc.).

function setRTCTime

Sets the RTC time and date.

void DS3231M::setRTCTime (
    uint16_t year,
    uint8_t month,
    uint8_t day,
    uint8_t hour,
    uint8_t minute,
    uint8_t second
) 

Parameters:

  • year Year (e.g. 2024).
  • month Month (1-12).
  • day Day (1-31).
  • hour Hour (0-23).
  • minute Minute (0-59).
  • second Second (0-59).

function ~DS3231M

Destructor of the DS3231M class.

inline DS3231M::~DS3231M () 


Protected Functions Documentation

function dayOfTheWeek

Calculates the day of the week for a given date.

uint8_t DS3231M::dayOfTheWeek (
    uint16_t y,
    uint8_t m,
    uint8_t d
) const

Parameters:

  • y Year.
  • m Month.
  • d Day.

Returns:

Day of the week (1 = Sunday, 2 = Monday, ..., 7 = Saturday).


function readReg

Reads data from a specific DS3231M register via I2C.

virtual uint8_t DS3231M::readReg (
    uint8_t reg,
    const void * pBuf,
    size_t size
) 

Parameters:

  • reg Register address to read from.
  • pBuf Pointer to the buffer where read data will be stored.
  • size Number of bytes to read.

Returns:

The number of bytes read (0 in case of error or if no bytes were read).


function writeReg

Writes data to a specific DS3231M register via I2C.

virtual void DS3231M::writeReg (
    uint8_t reg,
    const void * pBuf,
    size_t size
) 

Parameters:

  • reg Register address to write to.
  • pBuf Pointer to the data buffer to write.
  • size Number of bytes to write.

Protected Static Functions Documentation

function bcd2bin

Converts a BCD (Binary-Coded Decimal) value to BIN (Binary).

static uint8_t DS3231M::bcd2bin (
    uint8_t val
) 

Parameters:

  • val Value in BCD format.

Returns:

Value in BIN format.


function bin2bcd

Converts a BIN (Binary) value to BCD (Binary-Coded Decimal).

static uint8_t DS3231M::bin2bcd (
    uint8_t val
) 

Parameters:

  • val Value in BIN format.

Returns:

Value in BCD format.



The documentation for this class was generated from the following file firmware/DS3231M.h