Skip to content

Commit 28f15c9

Browse files
committed
added examples for clock and timer
1 parent f1b8c60 commit 28f15c9

File tree

3 files changed

+198
-0
lines changed

3 files changed

+198
-0
lines changed

examples/pcf8523_clockout.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
8+
import time
9+
import board
10+
import busio
11+
import countio
12+
from digitalio import Pull
13+
from adafruit_pcf8523.clock import Clock
14+
15+
PIN_SDA = board.GP2 # connect to RTC
16+
PIN_SCL = board.GP3 # connect to RTC
17+
# use board.SCL and board.SDA if available
18+
19+
i2c = busio.I2C(PIN_SCL, PIN_SDA)
20+
# or i2c = board.I2C() if available
21+
clock = Clock(i2c)
22+
23+
# pin must support countio
24+
PIN_COUT = board.GP5
25+
counter = countio.Counter(pin=PIN_COUT, edge=countio.Edge.RISE, pull=Pull.UP)
26+
DURATION = 10
27+
28+
# Main loop:
29+
while True:
30+
# disable clockout
31+
print(f"testing disabled clock for {DURATION} seconds")
32+
clock.clockout_frequency = clock.CLOCKOUT_FREQ_DISABLED
33+
counter.reset()
34+
time.sleep(DURATION)
35+
print(f"clock-pulses: {counter.count}")
36+
print(f"clock-freq: {counter.count/DURATION}")
37+
38+
# test 32kHz
39+
print(f"testing 32 kHz clock for {DURATION} seconds")
40+
clock.clockout_frequency = clock.CLOCKOUT_FREQ_32KHZ
41+
counter.reset()
42+
time.sleep(DURATION)
43+
clock.clockout_frequency = clock.CLOCKOUT_FREQ_DISABLED
44+
print(f"clock-pulses: {counter.count}")
45+
print(f"clock-freq: {counter.count/DURATION}")
46+
47+
# test 4kHz
48+
print(f"testing 4 kHz clock for {DURATION} seconds")
49+
clock.clockout_frequency = clock.CLOCKOUT_FREQ_4KHZ
50+
counter.reset()
51+
time.sleep(DURATION)
52+
clock.clockout_frequency = clock.CLOCKOUT_FREQ_DISABLED
53+
print(f"clock-pulses: {counter.count}")
54+
print(f"clock-freq: {counter.count/DURATION}")
55+
56+
# test 1Hz
57+
print(f"testing 1 Hz clock for {DURATION} seconds")
58+
clock.clockout_frequency = clock.CLOCKOUT_FREQ_1HZ
59+
counter.reset()
60+
time.sleep(DURATION)
61+
clock.clockout_frequency = clock.CLOCKOUT_FREQ_DISABLED
62+
print(f"clock-pulses: {counter.count}")
63+
print(f"clock-freq: {counter.count/DURATION}")

examples/pcf8523_timer_flag.py

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

examples/pcf8523_timer_interrupt.py

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

0 commit comments

Comments
 (0)