Jane Medium : This paper gives MicroPython Kernel Development Notes : Experimental tasks are embedded in the book Medium PWM Some contents of software use cases .
key word
:MicroPython,MM32F3277
Current version PWM All in all 6 Channels , Their corresponding pins are :
The following code initializes PWM passageway 1,2, Respectively take up PA0,PA2 Pin .
from machine import PWM
pwm0 = PWM(1, freq=10000, duty=200)
pwm1 = PWM(2, freq=10000, duty=500)
print(pwm0)
print(pwm1)
print('Test PWM.')
while True:
pass
Every PWM The output frequency is 10kHz, The duty cycles are 20%,50%. The following figure is collected by oscilloscope PWM1,PWM2 The output waveform of .
▲ PWM1,PWM2 wave form
Use pwm.duty() To change dynamically PWM Duty cycle of . Enter the value from 0 ~ 1000 Corresponding duty cycle from 0 To 100%.
below The sample program changes dynamically pwm Output duty cycle .
from machine import PWM
import time
pwm0 = PWM(1, freq=10000, duty = 1)
duty = 1
dutyinc = 50
incdir = 0
while True:
if incdir == 0:
duty += dutyinc
if duty >= 1000:
duty = 1000
incdir = 1
else:
if duty < dutyinc:
duty = 1
incdir = 0
else: duty -= dutyinc
pwm0.duty(duty)
time.sleep_ms(20)
You can observe pwm0 The duty cycle of the output is 0 ~ 100% Between cycles .
If dynamic changes are needed pwm frequency , Can be reused Define statement pairs PWM To initialize . such as
pwm0 = PWM(1, freq=10000, duty = 200)
pwm0 = PWM(1, freq=5000, duty = 500)
Final pwm0 The frequency of is defined as 5kHz, Duty ratio 500.
This paper gives MicroPython Kernel Development Notes : Experimental tasks are embedded in the book Medium PWM Some contents of software use cases .
call pwm.init() function , The program runs without error , But the corresponding pin waveform is not output . such as
pwm0 = PWM(1, freq=10000, duty = 200)
pwm0 = PWM(1, freq=5000, duty = 500)
pwm0.init(freq=5000)
while True:
pass
Corresponding PWM No waveform output .
Now in this version PWM There are only six channels .
■ Links to related literature :
● Related chart Links :