docs: Correct machine.Timer code examples related to duty cycle.

This commit is contained in:
danicampora 2016-02-23 20:18:45 +01:00
parent 8e1fdf2eb3
commit 57b96a7be2
2 changed files with 12 additions and 14 deletions

View File

@ -40,13 +40,13 @@ class Timer -- control internal timers
Further examples::
from machine import Timer
tim1 = Timer(2, mode=Timer.ONE_SHOT) # initialize it in one shot mode
tim2 = Timer(1, mode=Timer.PWM) # initialize it in PWM mode
tim1 = Timer(1, mode=Timer.ONE_SHOT) # initialize it in one shot mode
tim2 = Timer(2, mode=Timer.PWM) # initialize it in PWM mode
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
tim2_ch = tim2.channel(Timer.B, freq=10000, duty_cycle=50) # start the PWM on channel B with a 50% duty cycle
tim2_ch = tim2.channel(Timer.B, freq=10000, duty_cycle=5000) # start the PWM on channel B with a 50% duty cycle
tim2_ch.freq(20) # set the frequency (can also get)
tim2_ch.duty_cycle(30) # set the duty cycle to 30% (can also get)
tim2_ch.duty_cycle(30, Timer.NEGATIVE) # set the duty cycle to 30% and change the polarity to negative
tim2_ch.duty_cycle(3010) # set the duty cycle to 30.1% (can also get)
tim2_ch.duty_cycle(3020, Timer.NEGATIVE) # set the duty cycle to 30.2% and change the polarity to negative
tim2_ch.period(2000000) # change the period to 2 seconds
.. note::
@ -185,7 +185,9 @@ Methods
.. method:: timerchannel.duty_cycle([value])
Get or set the duty cycle of the PWM signal (in the range of 0-100).
Get or set the duty cycle of the PWM signal. It's a percentage (0.00-100.00). Since the WiPy
doesn't support floating point numbers the duty cycle must be specified in the range 0-10000,
where 10000 would represent 100.00, 5050 represents 50.50, and so on.
Constants
---------

View File

@ -63,16 +63,12 @@ PWM (pulse width modulation)
See :ref:`machine.Pin <machine.Pin>` and :ref:`machine.Timer <machine.Timer>`. ::
from machine import Timer
from machine import Pin
# assign GP25 to alternate function 9 (PWM)
p_out = Pin('GP25', mode=Pin.AF, alt=9)
# timer 2 in PWM mode and width must be 16 buts
tim = Timer(2, mode=Timer.PWM, width=16)
# timer 1 in PWM mode and width must be 16 buts
tim = Timer(1, mode=Timer.PWM, width=16)
# enable channel A @1KHz with a 50% duty cycle
tim_a = tim.channel(Timer.A, freq=1000, duty_cycle=50)
# enable channel A @1KHz with a 50.55% duty cycle
tim_a = tim.channel(Timer.A, freq=1000, duty_cycle=5055)
ADC (analog to digital conversion)
----------------------------------