diff options
author | IhorNehrutsa <Ihor.Nehrutsa@gmail.com> | 2021-10-15 14:04:40 -0700 |
---|---|---|
committer | Damien George <damien@micropython.org> | 2021-12-03 23:58:52 +1100 |
commit | b491967bbd99470632b783ee3bf91914aa692047 (patch) | |
tree | 75a826d011ea6456fd586b17725a5a39dabaab8d /docs/esp32/quickref.rst | |
parent | a7fa18c203a241f670f12ab507aa8b349fcd45a1 (diff) | |
download | micropython-b491967bbd99470632b783ee3bf91914aa692047.tar.gz micropython-b491967bbd99470632b783ee3bf91914aa692047.zip |
esp32/machine_pwm: Implement duty_u16() and duty_ns() PWM methods.
The methods duty_u16() and duty_ns() are implemented to match the existing
docs. The duty will remain the same when the frequency is changed.
Standard ESP32 as well as S2, S3 and C3 are supported.
Thanks to @kdschlosser for the fix for rounding in resolution calculation.
Documentation is updated and examples expanded for esp32, including the
quickref and tutorial. Additional notes are added to the machine.PWM docs
regarding limitations of hardware PWM.
Diffstat (limited to 'docs/esp32/quickref.rst')
-rw-r--r-- | docs/esp32/quickref.rst | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/docs/esp32/quickref.rst b/docs/esp32/quickref.rst index 7391a4aa4b..97b6fba38d 100644 --- a/docs/esp32/quickref.rst +++ b/docs/esp32/quickref.rst @@ -218,20 +218,24 @@ range from 1Hz to 40MHz but there is a tradeoff; as the base frequency *increases* the duty resolution *decreases*. See `LED Control <https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/ledc.html>`_ for more details. -Currently the duty cycle has to be in the range of 0-1023. -Use the ``machine.PWM`` class:: +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) - pwm0.freq(1000) # set frequency - pwm0.duty() # get current duty cycle (default 512, 50%) - pwm0.duty(200) # set duty cycle - pwm0.deinit() # turn off PWM on the pin + pwm0 = PWM(Pin(0)) # create PWM object from a pin + 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%) + pwm0.duty(256) # set duty cycle from 0 to 1023 as a ratio duty/1023, (now 25%) + 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 + 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 + print(pwm2) # view PWM settings ESP chips have different hardware peripherals: @@ -251,6 +255,8 @@ but only 8 different PWM frequencies are available, the remaining 8 channels mus have the same frequency. On the other hand, 16 independent PWM duty cycles are possible at the same frequency. +See more examples in the :ref:`esp32_pwm` tutorial. + ADC (analog to digital conversion) ---------------------------------- |