|
H753_CPP_I2C_DS3231_RTC_01 1.0
STM32H753 DS3231 real-time clock over I2C, with an AT24C32 EEPROM marker that stops the clock being set twice, built on the STM32_DS3231 and STM32_AT24C32 libraries
|
This documentation covers a STM32 NUCLEO-H753ZI tutorial project that reads a DS3231 real-time clock over I2C1 and prints the date and time on USART1. The clock is set once from the time the firmware was compiled, and a small marker kept in the AT24C32 EEPROM on the same ZS-042 module remembers that it was set, so a reset or a power cycle never sets it back. It builds on two reusable plain-C STM32 HAL libraries: STM32_DS3231 (the clock) and STM32_AT24C32 (the EEPROM).
The firmware demonstrates:
A ZS-042 DS3231 module (the AT24C32 EEPROM is on the same board and shares SCL and SDA) on I2C1 of the NUCLEO-H753ZI: SCL on PB8 (Arduino D15), SDA on PB9 (Arduino D14). The terminal is USART1 at 19200 baud.
Power the module from the Nucleo's 3.3 V, not 5 V. The ZS-042 has a charging path from VCC through a 200 ohm resistor and a diode to the battery pin: at 5 V it pushes current into a non-rechargeable CR2032, which can leak or swell, and at 3.3 V the diode stays reverse-biased. Its pull-ups also go to the module's VCC, so at 5 V the I2C lines idle at 5 V, which many MCU pins and other bus devices do not tolerate. Both chips work from 3.3 V.
The behaviour is controlled by a few macros at the top of main.c: FORCE_SET_TIME (set the clock again from the build time even though the EEPROM marker exists, then set it back to 0), SET_TIME_OFFSET_SECONDS (seconds added to the build time to cover the compile-and-flash delay), CLOCK_ROW (terminal row of the clock), ANSI_HIDE_CURSOR (escape sequence that hides the cursor) and the three marker constants CLOCK_MARKER_ADDRESS, CLOCK_MARKER_MAGIC and CLOCK_MARKER_SIZE. Each is documented with its value in the file reference of main.c.
The chip holds the local time of the computer that compiled the project; there is no time-zone conversion. Do not flash an unchanged build with FORCE_SET_TIME at 1 later: the build time is the time main.c was compiled, so it would set a stale time.