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 cacle
  • 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 synchroneousely (all have the same phase)
  • timer in the same group can be started with some phase shift

Create the pwm instance object

pwm = machine.PWM(channel)

Creates pwm object and reserves the pwm channel channel pwm channel number
Range: 0 ~ 12

Methods


pwm.init(args)

Arg 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
Default: 0.5
Range: 0.0 ~ 1.0
start optional; start the timer 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 the pwm timer, 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 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.

pwm.stop_group()

Stop all pwm in the same group at once.


pwm.sync_group(delay)

Start all pwm in the same group, each delayed by delay.
Each next channel in the group will be delayed by 1/pwm_frequency * delay
delay range: 0.01 ~ 0.99.

machine.PWM.list()

List the characteristics of all active pwm channels.


Examples


Start 4 pwm channels, all of 100 kHz and 50% duty cycle.
PWM channels have different phased 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.

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