8000 ports/PWM: Change the PWM examples. · micropython/micropython@8547fe0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8547fe0

Browse files
committed
ports/PWM: Change the PWM examples.
Adding the freq and duty_u16 keyword settings to the constructor. For SAMD more changes were made to keep up with the changed Pin naming scheme.
1 parent 4c8cc2c commit 8547fe0

File tree

4 files changed

+31
-28
lines changed

4 files changed

+31
-28
lines changed

docs/esp32/quickref.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ Use the :ref:`machine.PWM <machine.PWM>` class::
305305

306306
from machine import Pin, PWM
307307

308-
pwm0 = PWM(Pin(0)) # create PWM object from a pin
309-
freq = pwm0.freq() # get current frequency (default 5kHz)
308+
pwm0 = PWM(Pin(0), freq=5000, duty_u16=32768) # create PWM object from a pin
309+
freq = pwm0.freq() # get current frequency
310310
pwm0.freq(1000) # set PWM frequency from 1Hz to 40MHz
311311

312312
duty = pwm0.duty() # get current duty cycle, range 0-1023 (default 512, 50%)

docs/mimxrt/quickref.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,12 @@ handling signal groups. ::
145145

146146
from machine import Pin, PWM
147147

148-
pwm2 = PWM(Pin(2)) # create PWM object from a pin
149-
pwm2.freq() # get current frequency
150-
pwm2.freq(1000) # set frequency
151-
pwm2.duty_u16() # get current duty cycle, range 0-65535
152-
pwm2.duty_u16(200) # set duty cycle, range 0-65535
148+
# create PWM object from a pin and set the frequency and duty cycle
149+
pwm2 = PWM(Pin(2), freq=2000, duty_u16=32768)
150+
pwm2.freq() # get the current frequency
151+
pwm2.freq(1000) # set/change the frequency
152+
pwm2.duty_u16() # get the current duty cycle, range 0-65535
153+
pwm2.duty_u16(200) # set the duty cycle, range 0-65535
153154
pwm2.deinit() # turn off PWM on the pin
154155
# create a complementary signal pair on Pin 2 and 3
155156
pwm2 = PWM((2, 3), freq=2000, duty_ns=20000)

docs/rp2/quickref.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,12 @@ Use the ``machine.PWM`` class::
153153

154154
from machine import Pin, PWM
155155

156-
pwm0 = PWM(Pin(0)) # create PWM object from a pin
157-
pwm0.freq() # get current frequency
158-
pwm0.freq(1000) # set frequency
159-
pwm0.duty_u16() # get current duty cycle, range 0-65535
160-
pwm0.duty_u16(200) # set duty cycle, range 0-65535
156+
# create PWM object from a pin and set the frequency and duty cycle
157+
pwm0 = PWM(Pin(0), freq=2000, duty_u16=32768)
158+
pwm0.freq() # get the current frequency
159+
pwm0.freq(1000) # set/change the frequency
160+
pwm0.duty_u16() # get the current duty cycle, range 0-65535
161+
pwm0.duty_u16(200) # set the duty cycle, range 0-65535
161162
pwm0.deinit() # turn off PWM on the pin
162163

163164
ADC (analog to digital conversion)

docs/samd/quickref.rst

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ Use the :ref:`machine.Pin <machine.Pin>` class::
132132
print(p2.value()) # get value, 0 or 1
133133

134134
p4 = Pin('D4', Pin.IN, Pin.PULL_UP) # enable internal pull-up resistor
135-
p7 = Pin("PA07", Pin.OUT, value=1) # set pin high on creation
135+
p7 = Pin('PA07', Pin.OUT, value=1) # set pin high on creation
136136

137137
Pins can be denoted by a string or a number. The string is either the
138138
pin label of the respective board, like "D0" or "SDA", or in the form
@@ -157,7 +157,7 @@ See :ref:`machine.UART <machine.UART>`. ::
157157
# Use UART 3 on a ItsyBitsy M4 board
158158
from machine import UART
159159

160-
uart3 = UART(3, tx=Pin(1), rx=Pin(0), baudrate=115200)
160+
uart3 = UART(3, tx=Pin('D1'), rx=Pin('D0'), baudrate=115200)
161161
uart3.write('hello') # write 5 bytes
162162
uart3.read(5) # read up to 5 bytes
163163

@@ -178,11 +178,12 @@ It supports all basic methods listed for that class. ::
178178

179179
from machine import Pin, PWM
180180

181-
pwm = PWM(Pin(7)) # create PWM object from a pin
182-
pwm.freq() # get current frequency
183-
pwm.freq(1000) # set frequency
184-
pwm.duty_u16() # get current duty cycle, range 0-65535
185-
pwm.duty_u16(200) # set duty cycle, range 0-65535
181+
# create PWM object from a pin and set the frequency and duty cycle
182+
pwm = PWM(Pin('D7'), freq=2000, duty_u16=32768)
183+
pwm.freq() # get the current frequency
184+
pwm.freq(1000) # set/change the frequency
185+
pwm.duty_u16() # get the current duty cycle, range 0-65535
186+
pwm.duty_u16(200) # set the duty cycle, range 0-65535
186187
pwm.deinit() # turn off PWM on the pin
187188

188189
pwm # show the PWM objects properties
@@ -245,9 +246,9 @@ Use the :ref:`machine.ADC <machine.ADC>` class::
245246

246247
from machine import ADC
247248

248-
adc0 = ADC(Pin("A0")) # create ADC object on ADC pin, average=16
249+
adc0 = ADC(Pin('A0')) # create ADC object on ADC pin, average=16
249250
adc0.read_u16() # read value, 0-65536 across voltage range 0.0v - 3.3v
250-
adc1 = ADC(Pin("A1"), average=1) # create ADC object on ADC pin, average=1
251+
adc1 = ADC(Pin('A1'), average=1) # create ADC object on ADC pin, average=1
251252

252253
The resolution of the ADC is 12 bit with 12 bit accuracy, irrespective of the
253254
value returned by read_u16(). If you need a higher resolution or better accuracy, use
@@ -341,7 +342,7 @@ Software SPI (using bit-banging) works on all pins, and is accessed via the
341342
# construct a SoftSPI bus on the given pins
342343
# polarity is the idle state of SCK
343344
# phase=0 means sample on the first edge of SCK, phase=1 means the second
344-
spi = SoftSPI(baudrate=100000, polarity=1, phase=0, sck=Pin(7), mosi=Pin(9), miso=Pin(10))
345+
spi = SoftSPI(baudrate=100000, polarity=1, phase=0, sck=Pin('D7'), mosi=Pin('D9'), miso=Pin('D10'))
345346

346347
spi.init(baudrate=200000) # set the baud rate
347348

@@ -388,7 +389,7 @@ accessed via the :ref:`machine.SoftI2C <machine.SoftI2C>` class::
388389

389390
from machine import Pin, SoftI2C
390391

391-
i2c = SoftI2C(scl=Pin(10), sda=Pin(11), freq=100000)
392+
i2c = SoftI2C(scl=Pin('D10'), sda=Pin('D11'), freq=100000)
392393

393394
i2c.scan() # scan for devices
394395

@@ -424,7 +425,7 @@ The OneWire driver is implemented in software and works on all pins::
424425
from machine import Pin
425426
import onewire
426427

427-
ow = onewire.OneWire(Pin(12)) # create a OneWire bus on GPIO12
428+
ow = onewire.OneWire(Pin('D12')) # create a OneWire bus on GPIO12
428429
ow.scan() # return a list of devices on the bus
429430
ow.reset() # reset the bus
430431
ow.readbyte() # read a byte
@@ -454,12 +455,12 @@ The DHT driver is implemented in software and works on all pins::
454455
import dht
455456
import machine
456457

457-
d = dht.DHT11(machine.Pin(4))
458+
d = dht.DHT11(machine.Pin('D4'))
458459
d.measure()
459460
d.temperature() # eg. 23 (°C)
460461
d.humidity() # eg. 41 (% RH)
461462

462-
d = dht.DHT22(machine.Pin(4))
463+
d = dht.DHT22(machine.Pin('D4'))
463464
d.measure()
464465
d.temperature() # eg. 23.6 (°C)
465466
d.humidity() # eg. 41.3 (% RH)
@@ -474,7 +475,7 @@ The APA102 on some Adafruit boards can be controlled using SoftSPI::
474475

475476
from machine import SoftSPI, Pin
476477
# create the SPI object. miso can be any unused pin.
477-
spi=SoftSPI(sck=Pin(25), mosi=Pin(26), miso=Pin(14))
478+
spi=SoftSPI(sck=Pin('D25'), mosi=Pin('D26'), miso=Pin('D14'))
478479

479480
# define a little function that writes the data with
480481
# preamble and postfix
@@ -499,7 +500,7 @@ with the Neopixel driver from the MicroPython driver library::
499500
import machine
500501

501502
# 1 LED connected to Pin D8 on Adafruit Feather boards
502-
p = machine.Pin(8, machine.Pin.OUT)
503+
p = machine.Pin('D8', machine.Pin.OUT)
503504
n = neopixel.NeoPixel(p, 1)
504505

505506
# set the led to red.

0 commit comments

Comments
 (0)
0