-
-
Notifications
You must be signed in to change notification settings - Fork 24
pwm
- 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
Creates pwm object and reserves the pwm channel
channel pwm channel number
Range: 0 ~ 12
| Arg | Description |
|---|---|
pin |
mandatory; K210 gpio number to be used as pwm output |
invert |
optional; invert the pin output. Default: FalseCan be used to create pwm outputs with inverted phase |
freq |
optional; pwm frequeny in Hz Default 1000Range: 0.25 Hz ~ 100 MHz |
duty |
optional; pwm duty cycle Default: 0.5Range: 0.0 ~ 1.0
|
start |
optional; start the timer immediately Default: False
|
All arguments must be given as kw arguments (arg=value).
Stop pwm output, deinitialize and free the pwm channel, free the resources used.
The pwm can be reinitializeded using pwm.init().
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.
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)
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.
Resume the pwm , pwm output will be present on pwm pin.
Start all pwm in the same group at once.
All pwm outputs will have the same phase.
Stop all pwm in the same group at once.
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.
List the characteristics of all active pwm channels.
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)
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()
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()
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()
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()
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)
- Maix-M1 schematic
- Maix-Bit schematic
- Maix-Go schematic
- Kendryte K210 datasheet
- RISC-V ISA Design
- RISC-V ISA Manual
- Forum
- MicroPython documentation
If you find this project useful, you can contribute by making a donation