Demo

examples/pcf8523_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# Simple demo of reading and writing the time for the PCF8523 real-time clock.
 5# Change the if False to if True below to set the time, otherwise it will just
 6# print the current date and time every second.  Notice also comments to adjust
 7# for working with hardware vs. software I2C.
 8
 9import time
10
11import board
12
13from adafruit_pcf8523.pcf8523 import PCF8523
14
15i2c = board.I2C()  # uses board.SCL and board.SDA
16# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
17rtc = PCF8523(i2c)
18
19# Lookup table for names of days (nicer printing).
20days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
21
22
23if False:  # change to True if you want to set the time!
24    #                     year, mon, date, hour, min, sec, wday, yday, isdst
25    t = time.struct_time((2017, 10, 29, 10, 31, 0, 0, -1, -1))
26    # you must set year, mon, date, hour, min, sec and weekday
27    # yearday is not supported, isdst can be set but we don't do anything with it at this time
28    print("Setting time to:", t)  # uncomment for debugging
29    rtc.datetime = t
30    print()
31# pylint: enable-msg=using-constant-test
32
33# Main loop:
34while True:
35    t = rtc.datetime
36    # print(t)     # uncomment for debugging
37    print(f"The date is {days[int(t.tm_wday)]} {t.tm_mday}/{t.tm_mon}/{t.tm_year}")
38    print(f"The time is {t.tm_hour}:{t.tm_min:02}:{t.tm_sec:02}")
39    time.sleep(1)  # wait a second

Square wave generation

Simple demo for clockout-mode (square-wave generation)

examples/pcf8523_clockout.py
 1# SPDX-FileCopyrightText: 2023 Bernhard Bablok
 2# SPDX-License-Identifier: MIT
 3
 4# Simple demo for clockout-mode (square-wave generation)
 5# Note that for 32kHz, the duty-cycle is from 60:40 to 40:60, thus
 6# it is not a perfect square wave (see datasheet 8.9.1.2)
 7
 8import time
 9
10import board
11import busio
12import countio
13from digitalio import Pull
14
15from adafruit_pcf8523.clock import Clock
16
17PIN_SDA = board.GP2  # connect to RTC
18PIN_SCL = board.GP3  # connect to RTC
19# use board.SCL and board.SDA if available
20
21i2c = busio.I2C(PIN_SCL, PIN_SDA)
22# or i2c = board.I2C() if available
23clock = Clock(i2c)
24
25# pin must support countio
26PIN_COUT = board.GP5
27counter = countio.Counter(pin=PIN_COUT, edge=countio.Edge.RISE, pull=Pull.UP)
28DURATION = 10
29
30# Main loop:
31while True:
32    # disable clockout
33    print(f"testing disabled clock for {DURATION} seconds")
34    clock.clockout_frequency = clock.CLOCKOUT_FREQ_DISABLED
35    counter.reset()
36    time.sleep(DURATION)
37    print(f"clock-pulses: {counter.count}")
38    print(f"clock-freq:   {counter.count / DURATION}")
39
40    # test 32kHz
41    print(f"testing 32 kHz clock for {DURATION} seconds")
42    clock.clockout_frequency = clock.CLOCKOUT_FREQ_32KHZ
43    counter.reset()
44    time.sleep(DURATION)
45    clock.clockout_frequency = clock.CLOCKOUT_FREQ_DISABLED
46    print(f"clock-pulses: {counter.count}")
47    print(f"clock-freq:   {counter.count / DURATION}")
48
49    # test 4kHz
50    print(f"testing 4 kHz clock for {DURATION} seconds")
51    clock.clockout_frequency = clock.CLOCKOUT_FREQ_4KHZ
52    counter.reset()
53    time.sleep(DURATION)
54    clock.clockout_frequency = clock.CLOCKOUT_FREQ_DISABLED
55    print(f"clock-pulses: {counter.count}")
56    print(f"clock-freq:   {counter.count / DURATION}")
57
58    # test 1Hz
59    print(f"testing 1 Hz clock for {DURATION} seconds")
60    clock.clockout_frequency = clock.CLOCKOUT_FREQ_1HZ
61    counter.reset()
62    time.sleep(DURATION)
63    clock.clockout_frequency = clock.CLOCKOUT_FREQ_DISABLED
64    print(f"clock-pulses: {counter.count}")
65    print(f"clock-freq:   {counter.count / DURATION}")

Timer Flag Operation

Simple demo for timer operation using the timer-flag

examples/pcf8523_timer_flag.py
 1# SPDX-FileCopyrightText: 2023 Bernhard Bablok
 2# SPDX-License-Identifier: MIT
 3
 4# Simple demo for timer operation using the timer-flag
 5
 6import time
 7
 8import board
 9import busio
10
11from adafruit_pcf8523.clock import Clock
12from adafruit_pcf8523.timer import Timer
13
14LOW_FREQ_TIMER = 10
15HIGH_FREQ_TIMER = 0.02
16HIGH_FREQ_TIME = 10
17PIN_SDA = board.GP2
18PIN_SCL = board.GP3
19# use board.SCL and board.SDA if available
20
21i2c = busio.I2C(PIN_SCL, PIN_SDA)
22# or i2c = board.I2C() if available
23timer = Timer(i2c)
24clock = Clock(timer.i2c_device)
25clock.clockout_frequency = clock.CLOCKOUT_FREQ_DISABLED
26
27# Main loop:
28while True:
29    print("low-frequency timer: checking timer-flag")
30    timer.timer_enabled = False
31    timer.timer_status = False
32    timer.timer_frequency = timer.TIMER_FREQ_1HZ
33    timer.timer_value = LOW_FREQ_TIMER
34    start = time.monotonic()
35    timer.timer_enabled = True
36    while not timer.timer_status and time.monotonic() - start < LOW_FREQ_TIMER + 1:
37        pass
38    if not timer.timer_status:
39        # shoud not happen!
40        print(f"error: timer did not fire within {LOW_FREQ_TIMER + 1} seconds!")
41    else:
42        elapsed = time.monotonic() - start
43        print(f"elapsed: {elapsed}")
44
45    print("high-frequency timer: checking timer-flag")
46    timer.timer_enabled = False
47    timer.timer_status = False
48    timer.timer_frequency = timer.TIMER_FREQ_4KHZ
49    timer.timer_value = min(round(HIGH_FREQ_TIMER * 4096), 255)
50    counter = 0
51    start = time.monotonic()
52    end = start + HIGH_FREQ_TIME
53    timer.timer_enabled = True
54    while time.monotonic() < end:
55        if not timer.timer_status:
56            continue
57        timer.timer_status = False
58        counter += 1
59    if counter > 0:
60        mean_interval = (time.monotonic() - start) / counter
61        print(f"interval requested: {HIGH_FREQ_TIMER}")
62        print(f"interval observed:  {mean_interval} (mean of {counter} alarms)")
63    else:
64        print(f"error: timer did not fire within {HIGH_FREQ_TIME} seconds!")
65        print("error: timer did not fire")

Timer Interrupt

Simple demo for timer operation, using the interrupt-pin

examples/pcf8523_timer_interrupt.py
 1# SPDX-FileCopyrightText: 2023 Bernhard Bablok
 2# SPDX-License-Identifier: MIT
 3
 4# Simple demo for timer operation, using the interrupt-pin
 5
 6import time
 7
 8import board
 9import busio
10from digitalio import DigitalInOut, Direction, Pull
11
12from adafruit_pcf8523.clock import Clock
13from adafruit_pcf8523.timer import Timer
14
15LOW_FREQ_TIMER = 10
16HIGH_FREQ_TIMER = 0.02
17HIGH_FREQ_TIME = 10
18PIN_INT = board.GP5
19PIN_SDA = board.GP2
20PIN_SCL = board.GP3
21# use board.SCL and board.SDA if available
22
23i2c = busio.I2C(PIN_SCL, PIN_SDA)
24# or i2c = board.I2C() if available
25timer = Timer(i2c)
26clock = Clock(timer.i2c_device)
27clock.clockout_frequency = clock.CLOCKOUT_FREQ_DISABLED
28
29# interrupt pin
30intpin = DigitalInOut(PIN_INT)
31intpin.direction = Direction.INPUT
32intpin.pull = Pull.UP
33
34# Main loop:
35timer.pulsed = False
36timer.timer_interrupt = True
37while True:
38    print("low-frequency timer: checking interrupt")
39    timer.timer_enabled = False
40    timer.timer_status = False
41    timer.timer_frequency = timer.TIMER_FREQ_1HZ
42    timer.timer_value = LOW_FREQ_TIMER
43    start = time.monotonic()
44    timer.timer_enabled = True
45    while intpin.value and time.monotonic() - start < LOW_FREQ_TIMER + 1:
46        pass
47    if intpin.value:
48        # shoud not happen!
49        print(f"error: timer did not fire within {LOW_FREQ_TIMER + 1} seconds!")
50    else:
51        elapsed = time.monotonic() - start
52        print(f"elapsed: {elapsed}")
53
54    print("high-frequency timer: checking interrupt")
55    timer.timer_enabled = False
56    timer.timer_status = False
57    timer.timer_frequency = timer.TIMER_FREQ_4KHZ
58    timer.timer_value = min(round(HIGH_FREQ_TIMER * 4096), 255)
59    counter = 0
60    start = time.monotonic()
61    end = start + HIGH_FREQ_TIME
62    timer.timer_enabled = True
63    while time.monotonic() < end:
64        if intpin.value:
65            continue
66        timer.timer_status = False
67        counter += 1
68    if counter > 0:
69        mean_interval = (time.monotonic() - start) / counter
70        print(f"interval requested: {HIGH_FREQ_TIMER}")
71        print(f"interval observed:  {mean_interval} (mean of {counter} alarms)")
72    else:
73        print(f"error: timer did not fire within {HIGH_FREQ_TIME} seconds!")
74        print("error: timer did not fire")

DisplayIO Simpletest

This is a simple test for boards with built-in display.

examples/pcf8523_displayio_simpletest.py
 1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
 2# SPDX-FileCopyrightText: 2024 Jose D. Montoya
 3#
 4# SPDX-License-Identifier: MIT
 5
 6# Simple demo of the PCF8523 real-time clock using a built-in display.
 7import time
 8
 9import board
10from adafruit_display_text.bitmap_label import Label
11from displayio import Group
12from terminalio import FONT
13
14from adafruit_pcf8523.pcf8523 import PCF8523
15
16# create a main_group to hold anything we want to show on the display.
17main_group = Group()
18# Initialize I2C bus and sensor.
19i2c = board.I2C()  # uses board.SCL and board.SDA
20rtc = PCF8523(i2c)
21
22# Lookup table for names of days (nicer printing).
23days = (
24    "Monday",
25    "Tuesday",
26    "Wednesday",
27    "Thursday",
28    "Friday",
29    "Saturday",
30    "Sunday",
31)
32
33# Set the time
34t = time.struct_time((2024, 12, 12, 10, 31, 0, 0, -1, -1))
35rtc.datetime = t
36print("Setting time to:", t)
37
38# Create two Labels to show the readings. If you have a very small
39# display you may need to change to scale=1.
40date_output_label = Label(FONT, text="", scale=2)
41time_output_label = Label(FONT, text="", scale=2)
42
43# place the label in the middle of the screen with anchored positioning
44date_output_label.anchor_point = (0, 0)
45date_output_label.anchored_position = (4, board.DISPLAY.height // 2)
46time_output_label.anchor_point = (0, 0)
47time_output_label.anchored_position = (4, 20 + board.DISPLAY.height // 2)
48
49# add the label to the main_group
50main_group.append(date_output_label)
51main_group.append(time_output_label)
52
53# set the main_group as the root_group of the built-in DISPLAY
54board.DISPLAY.root_group = main_group
55
56# begin main loop
57while True:
58    # Update the label.text property to change the text on the display
59    t = rtc.datetime
60    date_output_label.text = (
61        f"The date is {days[int(t.tm_wday)]} {t.tm_mday}/{t.tm_mon}/{t.tm_year}"
62    )
63    time_output_label.text = f"The time is {t.tm_hour}:{t.tm_min:02}:{t.tm_sec:02}"
64    # wait for a bit
65    time.sleep(1)