程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Software use cases in micropython kernel development notebook: PWM part of experimental use cases

編輯:Python

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

The contents of the manuscript Objective record
Contents
Basic experiments Output basic waveform Dynamically change the duty cycle Dynamically change the frequency total junction Existing problems call pwm.init error

 

  • The contents of this manuscript belong to MicroPython Kernel Development Notes : Experimental tasks are embedded in the book The content in .

 

§01 book Draft content


Current version PWM All in all 6 Channels , Their corresponding pins are :

  • PWM0:PA8
  • PWM1:PA0
  • PWM2:PA2
  • PWM3:PA3
  • PWM4:PA6
  • PWM5:PA7

One 、 Basic experiments

1、 Output basic waveform

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

2、 Dynamically change the duty cycle

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 .

3、 Dynamically change the frequency

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.

 

※ total junction ※


This paper gives MicroPython Kernel Development Notes : Experimental tasks are embedded in the book Medium PWM Some contents of software use cases .

One 、 Existing problems

1、 call pwm.init() error

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 .

2、PWM The number of channels is only 6 individual

Now in this version PWM There are only six channels .


■ Links to related literature :

  • MicroPython Kernel Development Notes : Experimental tasks are embedded in the book

● Related chart Links :

  • PWM1,PWM2 wave form

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved