Skip to content

Commit 57b96a7

Browse files
author
danicampora
committed
docs: Correct machine.Timer code examples related to duty cycle.
1 parent 8e1fdf2 commit 57b96a7

2 files changed

Lines changed: 12 additions & 14 deletions

File tree

docs/library/machine.Timer.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ class Timer -- control internal timers
4040
Further examples::
4141

4242
from machine import Timer
43-
tim1 = Timer(2, mode=Timer.ONE_SHOT) # initialize it in one shot mode
44-
tim2 = Timer(1, mode=Timer.PWM) # initialize it in PWM mode
43+
tim1 = Timer(1, mode=Timer.ONE_SHOT) # initialize it in one shot mode
44+
tim2 = Timer(2, mode=Timer.PWM) # initialize it in PWM mode
4545
tim1_ch = tim1.channel(Timer.A, freq=10, polarity=Timer.POSITIVE) # start the event counter with a frequency of 10Hz and triggered by positive edges
46-
tim2_ch = tim2.channel(Timer.B, freq=10000, duty_cycle=50) # start the PWM on channel B with a 50% duty cycle
46+
tim2_ch = tim2.channel(Timer.B, freq=10000, duty_cycle=5000) # start the PWM on channel B with a 50% duty cycle
4747
tim2_ch.freq(20) # set the frequency (can also get)
48-
tim2_ch.duty_cycle(30) # set the duty cycle to 30% (can also get)
49-
tim2_ch.duty_cycle(30, Timer.NEGATIVE) # set the duty cycle to 30% and change the polarity to negative
48+
tim2_ch.duty_cycle(3010) # set the duty cycle to 30.1% (can also get)
49+
tim2_ch.duty_cycle(3020, Timer.NEGATIVE) # set the duty cycle to 30.2% and change the polarity to negative
5050
tim2_ch.period(2000000) # change the period to 2 seconds
5151

5252
.. note::
@@ -185,7 +185,9 @@ Methods
185185

186186
.. method:: timerchannel.duty_cycle([value])
187187

188-
Get or set the duty cycle of the PWM signal (in the range of 0-100).
188+
Get or set the duty cycle of the PWM signal. It's a percentage (0.00-100.00). Since the WiPy
189+
doesn't support floating point numbers the duty cycle must be specified in the range 0-10000,
190+
where 10000 would represent 100.00, 5050 represents 50.50, and so on.
189191

190192
Constants
191193
---------

docs/wipy/quickref.rst

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,12 @@ PWM (pulse width modulation)
6363
See :ref:`machine.Pin <machine.Pin>` and :ref:`machine.Timer <machine.Timer>`. ::
6464

6565
from machine import Timer
66-
from machine import Pin
67-
68-
# assign GP25 to alternate function 9 (PWM)
69-
p_out = Pin('GP25', mode=Pin.AF, alt=9)
7066

71-
# timer 2 in PWM mode and width must be 16 buts
72-
tim = Timer(2, mode=Timer.PWM, width=16)
67+
# timer 1 in PWM mode and width must be 16 buts
68+
tim = Timer(1, mode=Timer.PWM, width=16)
7369
74-
# enable channel A @1KHz with a 50% duty cycle
75-
tim_a = tim.channel(Timer.A, freq=1000, duty_cycle=50)
70+
# enable channel A @1KHz with a 50.55% duty cycle
71+
tim_a = tim.channel(Timer.A, freq=1000, duty_cycle=5055)
7672

7773
ADC (analog to digital conversion)
7874
----------------------------------

0 commit comments

Comments
 (0)