Skip to content

Commit 95b5284

Browse files
author
Thomas Preston
committed
Working on pwm
1 parent d43b7bd commit 95b5284

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

codebug_tether/core.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,30 @@ def set_leg_io(self, leg_index, direction):
165165
self.or_mask(CHANNEL_INDEX_IO_DIRECTION_EXT, direction_mask)
166166

167167
def pwm_on(self, t2_prescale, full_period, on_period):
168-
"""Turns on the PWM generator with the given settings."""
169-
# self.set
170-
pass
168+
"""Turns on the PWM generator with the given settings.
169+
170+
:param t2_prescale: One of T2_PS_1_1, T2_PS_1_4, T2_PS_1_16
171+
Scales down the 4MHz instruction clock by
172+
1, 4 or 16.
173+
:param full_period: 8-bit value - which is scaled up to 10-bits
174+
(<< 2) - to which timer 2 will count up to
175+
before resetting PWM output to 1.
176+
:param on_period: 10-bit value to which timer 2 will count up to
177+
before setting PWM output to 0. Use this with
178+
full_period to control duty cycle. For
179+
example:
180+
181+
# 4MHz / 16 with 50% duty cycle
182+
codebug.pwm_on(T2_PS_1_16, 0xff, 0x200)
183+
184+
"""
185+
# full period
186+
self.set(CHANNEL_INDEX_PWM_CONF_0, full_period)
187+
self.set(CHANNEL_INDEX_PWM_CONF_1, on_period | 0xff)
188+
go_busy = 1
189+
top_two_bit_on_period = (on_period >> 8) & 0b11
190+
conf = go_busy << 4 | t2_prescale << 2 | top_two_bit_on_period
191+
self.set(CHANNEL_INDEX_PWM_CONF_2, conf)
171192

172193
def pwm_freq(self, frequency):
173194
"""Turns on the PWM generator with the given frequency."""
@@ -176,7 +197,8 @@ def pwm_freq(self, frequency):
176197
pass
177198

178199
def pwm_off(self):
179-
pass
200+
go_busy_off_mask = 0xff ^ (1 << 4)
201+
self.and_mask(CHANNEL_INDEX_PWM_CONF_2, go_busy_off_mask)
180202

181203
def clear(self):
182204
"""Clears the pixels on CodeBug.

0 commit comments

Comments
 (0)