summaryrefslogtreecommitdiff
path: root/docs/esp32
diff options
context:
space:
mode:
authorIhorNehrutsa <Ihor.Nehrutsa@gmail.com>2021-12-12 14:16:53 +0200
committerDamien George <damien@micropython.org>2021-12-22 00:05:58 +1100
commit09fe80d0912b2418fb704cf8e80db1712fb7523f (patch)
tree7100ea5b310fdf91c3d196e07d0c8a009be58a01 /docs/esp32
parent4189c64869bfe9054df7126ef33b80d718f08038 (diff)
esp32/machine_pwm: Keep duty constant when changing frequency.
Save and restore the same duty cycle when the frequency (or frequency resolution) is changed. This allows a smooth frequency change. Also update the esp32 PWM quickref to be clearer.
Diffstat (limited to 'docs/esp32')
-rw-r--r--docs/esp32/quickref.rst14
1 files changed, 9 insertions, 5 deletions
diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst
index 41c2fd6c6..e74d3d81f 100644
--- a/docs/esp32/quickref.rst
+++ b/docs/esp32/quickref.rst
@@ -224,14 +224,18 @@ Use the :ref:`machine.PWM <machine.PWM>` class::
from machine import Pin, PWM
pwm0 = PWM(Pin(0)) # create PWM object from a pin
- pwm0.freq() # get current frequency (default 5kHz)
+ freq = pwm0.freq() # get current frequency (default 5kHz)
pwm0.freq(1000) # set PWM frequency from 1Hz to 40MHz
- pwm0.duty() # get current duty cycle, range 0-1023 (default 512, 50%)
+
+ duty = pwm0.duty() # get current duty cycle, range 0-1023 (default 512, 50%)
pwm0.duty(256) # set duty cycle from 0 to 1023 as a ratio duty/1023, (now 25%)
+
+ duty_u16 = pwm0.duty_u16() # get current duty cycle, range 0-65535
pwm0.duty_u16(2**16*3//4) # set duty cycle from 0 to 65535 as a ratio duty_u16/65535, (now 75%)
- pwm0.duty_u16() # get current duty cycle, range 0-65535
+
+ duty_ns = pwm0.duty_ns() # get current pulse width in ns
pwm0.duty_ns(250_000) # set pulse width in nanoseconds from 0 to 1_000_000_000/freq, (now 25%)
- pwm0.duty_ns() # get current pulse width in ns
+
pwm0.deinit() # turn off PWM on the pin
pwm2 = PWM(Pin(2), freq=20000, duty=512) # create and configure in one go
@@ -246,7 +250,7 @@ Number of groups (speed modes) 2 1
Number of timers per group 4 4 4
Number of channels per group 8 8 6
----------------------------------------------------- -------- -------- --------
-Different of PWM frequencies (groups * timers) 8 4 4
+Different PWM frequencies (groups * timers) 8 4 4
Total PWM channels (Pins, duties) (groups * channels) 16 8 6
===================================================== ======== ======== ========