Skip to content
Boris Lovosevic edited this page Aug 6, 2019 · 13 revisions

machine Module

Class PWM


Features

  • up to 12 pwm channels can be used
  • pwm output on any K210 pin
  • pwm output pin can be inverted
  • each pwm cnannel can have its own frequency and duty cycle
  • pwm frequency range 0.25 Hz ~ 100 MHz
  • K210 timer peripheral is used (in pwm mode)
  • 12 pwm channels are divided into 3 groups of 4 channels, each group using separate hw timer
  • all timers in the same group can be started synchronously (all have the same phase)
  • timer in the same group can be started with some phase shift

PWM and Timer clases uses the same K210 timers.
If some channel is used for Timer it cannot be used for PWM and vice versa.

Note:
For high PWM frequencies (>1 MHz) the acctual pwm frequency may be different than requested.
Duty cycle resolution (duty increment step) can also be small.
Both are reported by printing the PWM instance or as a result of pwm.freq() and pwm.duty() functions.



Create the PWM instance object

pwm = machine.PWM(channel)

Creates pwm object and reserves the pwm channel.
PWM must be started with pwm.init() or pwm.start()
channel - pwm channel number; Range: 0 ~ 11

Methods


pwm.init(args)

Argument Description
pin mandatory; K210 gpio number to be used as pwm output
invert optional; invert the pin output.
Default: False
Can be used to create pwm outputs with inverted phase
freq optional; pwm frequeny in Hz
Default 1000
Range: 0.25 Hz ~ 100 MHz
duty optional; pwm duty cycle
Value: duty percentage / 100
Default: 0.5
Range: 0.0 ~ 1.0 (0% ~ 100%)
start optional; start the PWM immediately
Default: False

All arguments must be given as kw arguments (arg=value).

pwm.deinit()

Stop pwm output, deinitialize and free the pwm channel, free the resources used.
The pwm can be reinitializeded using pwm.init().

pwm.freq([freq])

With no argument, returns the current pwm channel frequency.
Set the new pwm frequency to ḟreq Hz.
Range: 0.25 Hz ~ 100 MHz
Returns the acctual frequency set.

pwm.duty([duty])

With no argument, returns the current pwm channel duty cycle.
Set the new pwm duty cycle to duty_perc.
. Range: 0.0 ~ 1.0
Returns the tuple of acctual duty and maximal duty step (dependant on pwm frequency)

pwm.pause()

pwm.stop()

Pause (stop) the PWM, no pwm output will be present on pwm pin.
For 'normal' pwm, pin state will be low, for inverted pwm high.

pwm.resume()

pwm.start()

Resume (start) the PWM , pwm output will be present on pwm pin.

pwm.start_group()

Start all pwm in the same group at once.
All pwm outputs will have the same phase.
Returns the bitmask of started channels or 0 if failed.

pwm.stop_group()

Stop all pwm in the same group at once.
Returns the bitmask of started channels or 0 if failed.

pwm.sync_group(delay)

Start all pwm in the same group, each delayed by delay argument value.
Each next channel in the group will be delayed by 1/pwm_frequency * delay
Returns the bitmask of started channels or 0 if failed.
delay - range: 0.01 ~ 0.99.

machine.PWM.list()

List the characteristics of all active pwm channels.

import machine

freq = 100000

p1=machine.PWM(0)
p2=machine.PWM(1)
p3=machine.PWM(2)
p4=machine.PWM(9)
p1.init(pin=20, freq=10000, duty=0.2)
p2.init(pin=21, freq=20000, duty=0.4)
p3.init(pin=18, freq=40000, duty=0.6)
p4.init(pin=19, freq=10e6, duty=0.25, start=True)

p3.start_group()
machine.PWM.list()
PWM(0 [0:0]) pin=20, freq=10000.000 Hz, duty=0.200 (step=<0.001), active=True)
PWM(1 [0:1]) pin=21, freq=20000.000 Hz, duty=0.400 (step=<0.001), active=True)
PWM(2 [0:2]) pin=18, freq=40000.000 Hz, duty=0.600 (step=<0.001), active=True)
PWM(9 [2:1]) pin=19, freq=10081632.653 Hz, duty=0.245 (step=0.020), active=True)



Examples


Start 4 pwm channels, all of 100 kHz and 50% duty cycle.
PWM channels have different phase as they are all started at different times.

import machine

freq = 100000

p1=machine.PWM(0)
p2=machine.PWM(1)
p3=machine.PWM(2)
p4=machine.PWM(3)
p1.init(pin=20, freq=freq, start=True)
p2.init(pin=21, freq=freq, start=True)
p3.init(pin=18, freq=freq, start=True)
p4.init(pin=19, freq=freq, start=True)

pwm1


Start 4 pwm channels, all of 100 kHz and 50% duty cycle, syncronized in phase.
Invert the p2 output

import machine

freq = 100000

p1=machine.PWM(0)
p2=machine.PWM(1)
p3=machine.PWM(2)
p4=machine.PWM(3)
p1.init(pin=20, freq=freq)
p2.init(pin=21, freq=freq, invert=True)
p3.init(pin=18, freq=freq)
p4.init(pin=19, freq=freq)

p1.start_group()

pwm1


Start 4 pwm channels, all with different frequencies and 50% duty cycle, syncronized in phase.

import machine

freq = 100000

p1=machine.PWM(0)
p2=machine.PWM(1)
p3=machine.PWM(2)
p4=machine.PWM(3)
p1.init(pin=20, freq=10000)
p2.init(pin=21, freq=20000)
p3.init(pin=18, freq=30000)
p4.init(pin=19, freq=40000)

p4.start_group()

pwm1


Start 4 pwm channels, 100 kHz frequency and different duty cycles, syncronized in phase.

import machine

freq = 100000

p1=machine.PWM(0)
p2=machine.PWM(1)
p3=machine.PWM(2)
p4=machine.PWM(3)
p1.init(pin=20, freq=freq, duty=0.2)
p2.init(pin=21, freq=freq, duty=0.4)
p3.init(pin=18, freq=freq, duty=0.6)
p4.init(pin=19, freq=freq, duty=0.8)

p1.start_group()

pwm1


Start 4 pwm channels, all with different frequencies and different duty cycles, syncronized in phase.

import machine

freq = 100000

p1=machine.PWM(0)
p2=machine.PWM(1)
p3=machine.PWM(2)
p4=machine.PWM(3)
p1.init(pin=20, freq=10000, duty=0.2)
p2.init(pin=21, freq=20000, duty=0.4)
p3.init(pin=18, freq=40000, duty=0.6)
p4.init(pin=19, freq=60000, duty=0.8)

p4.start_group()

pwm1


Start 4 pwm channels, all with 100 kHz frequency and 50% duty cycles, shifted in phase by 10% of the p1 period.

import machine

freq = 100000

p1=machine.PWM(0)
p2=machine.PWM(1)
p3=machine.PWM(2)
p4=machine.PWM(3)
p1.init(pin=20, freq=freq)
p2.init(pin=21, freq=freq)
p3.init(pin=18, freq=freq)
p4.init(pin=19, freq=freq)

p1.sync_group(0.1)

pwm1

Clone this wiki locally