Demo
examples/pcf8563_simpletest.py
1# SPDX-FileCopyrightText: 2019 Sommersoft
2# SPDX-FileCopyrightText: Copyright (c) 2021 Jeff Epler for Adafruit Industries
3#
4# SPDX-License-Identifier: Unlicense
5
6# Simple demo of reading and writing the time for the PCF8563 real-time clock.
7# Change the if False to if True below to set the time, otherwise it will just
8# print the current date and time every second. Notice also comments to adjust
9# for working with hardware vs. software I2C.
10
11import time
12
13import board
14import busio
15
16from adafruit_pcf8563.pcf8563 import PCF8563
17
18# Change to the appropriate I2C clock & data pins here!
19i2c_bus = busio.I2C(board.SCL, board.SDA)
20
21# Create the RTC instance:
22rtc = PCF8563(i2c_bus)
23
24# Lookup table for names of days (nicer printing).
25days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
26
27
28if False: # change to True if you want to set the time!
29 # year, mon, date, hour, min, sec, wday, yday, isdst
30 t = time.struct_time((2017, 10, 29, 10, 31, 0, 0, -1, -1))
31 # you must set year, mon, date, hour, min, sec and weekday
32 # yearday is not supported, isdst can be set but we don't do anything with it at this time
33 print("Setting time to:", t) # uncomment for debugging
34 rtc.datetime = t
35 print()
36
37
38# Main loop:
39while True:
40 if rtc.datetime_compromised:
41 print("RTC unset")
42 else:
43 print("RTC reports time is valid")
44 t = rtc.datetime
45 # print(t) # uncomment for debugging
46 print(f"The date is {days[int(t.tm_wday)]} {t.tm_mday}/{t.tm_mon}/{t.tm_year}")
47 print(f"The time is {t.tm_hour}:{t.tm_min:02}:{t.tm_sec:02}")
48 time.sleep(1) # wait a second