Using the BM8563 RTC with MaixPy
Update history
| Date | Version | Author | Update content |
|---|---|---|---|
| 2024-08-27 | 1.0.0 | iawak9lkm | Initial document |
Introduction to BM8563
BM8563 is a real-time clock (RTC) chip that stores the time and date. With a backup battery, it continues keeping time while the device is powered off.
Using BM8563 in MaixPy
When creating a BM8563 object, specify the I2C bus connected to the chip. The onboard BM8563 on MaixCAM Pro uses I2C-4.
Example:
from maix import ext_dev, pinmap, err, time
### Enable I2C
# ret = pinmap.set_pin_function("PIN_NAME", "I2Cx_SCL")
# if ret != err.Err.ERR_NONE:
# print("Failed in function pinmap...")
# exit(-1)
# ret = pinmap.set_pin_function("PIN_NAME", "I2Cx_SDA")
# if ret != err.Err.ERR_NONE:
# print("Failed in function pinmap...")
# exit(-1)
BM8563_I2CBUS_NUM = 4
rtc = ext_dev.bm8563.BM8563(BM8563_I2CBUS_NUM)
### 2020-12-31 23:59:45
t = [2020, 12, 31, 23, 59, 45]
# Set time
rtc.datetime(t)
while True:
rtc_now = rtc.datetime()
print(f"{rtc_now[0]}-{rtc_now[1]}-{rtc_now[2]} {rtc_now[3]}:{rtc_now[4]}:{rtc_now[5]}")
time.sleep(1)
When using the onboard BM8563 on MaixCAM Pro, you do not need to configure the I2C-4 pins separately.
The example sets the BM8563 time and then reads it once per second.
The next example synchronizes the BM8563 time and the system time.
from maix import ext_dev, pinmap, err, time
### Enable I2C
# ret = pinmap.set_pin_function("PIN_NAME", "I2Cx_SCL")
# if ret != err.Err.ERR_NONE:
# print("Failed in function pinmap...")
# exit(-1)
# ret = pinmap.set_pin_function("PIN_NAME", "I2Cx_SDA")
# if ret != err.Err.ERR_NONE:
# print("Failed in function pinmap...")
# exit(-1)
BM8563_I2CBUS_NUM = 4
rtc = ext_dev.bm8563.BM8563(BM8563_I2CBUS_NUM)
### Update RTC time from system
rtc.systohc()
### Update system time from RTC
# rtc.hctosys()
while True:
rtc_now = rtc.datetime()
print(f"{rtc_now[0]}-{rtc_now[1]}-{rtc_now[2]} {rtc_now[3]}:{rtc_now[4]}:{rtc_now[5]}")
time.sleep(1)
The BM8563 API protects concurrent access to the same chip. You can create objects in different parts of a program without causing a data race during simultaneous reads or writes.
The timetuple passed to a BM8563 object uses (year, month, day[, hour[, minute[, second]]]). Year, month, and day are required; omitted time fields are left unchanged. A successful read returns a six-item list, (year, month, day, hour, minute, second). An empty result indicates an error.
See the BM8563 API documentation for all parameters.