-
Notifications
You must be signed in to change notification settings - Fork 2
PWM API
onionys edited this page Mar 15, 2019
·
3 revisions
Pulse width modulation (PWM) 脈衝寬度調變。形式為 machine 模組下的一個 名為 PWM 的 class,提供使用者
PWM 訊號輸出功能,mt7697 hdk 開發板上所有 Pin 都有提供該功能。
PWM 頻率。可設定範圍為 610 ~ 39,062 Hz。
PWM duty cycle。方波寬度比例設定,可設定範圍為 0 ~ 1024 (0 ~ 100%)。
pwm = machine.PWM(pin_no, freq, *, duty=0)
產生一個 PWM 物件以從指定 Pin 腳輸出 PWM 訊號。
參數 pin_no 為指定 Pin 腳的號碼(整數 type)或是Pin 物件。
參數 freq 設定 PWM 訊號的輸出頻率,以整數形式輸入,範圍為 610 ~ 39,062 Hz。
參數 duty 設定 PWM 訊號的輸出寬度,以整數形式輸入,範圍為 0 ~ 1024 (0 ~ 100%),預設值為 0 。
如下範例。產生 PWM 物件,指定 Pin 7 為 PWM 訊號輸出 Pin 腳,輸出頻率設定為 1000 Hz,使用預設 duty = 0 ()
from machine import PWM
pwm7 = PWM(7, 1000)
或是
from machine import PWM, Pin
p7 = Pin(7)
pwm7 = PWM(p7, 1000)
如果要在 PWM 物件產生的同時就開始輸出 50% 的 PWM 訊號,就指定 duty 值:
from machine import PWM, Pin
p7 = Pin(7)
pwm7 = PWM(p7, 1000,duty=512)
x 值有代入整數值,則設定輸出頻率。如 x 無代入值則為取得目前輸出頻率。
pwm7.freq(1000) # 設定 PWM 頻率為 1000 Hz
print(pwm7.freq()) # print 目前PWM 的頻率設定值
pwm7.freq(2000) # 設定 PWM 頻率為 1000 Hz
print(pwm7.freq()) # print 目前PWM 的頻率設定值
x 值有代入整數值,則設定輸出 PWM 訊號寬度比例。如 x 無代入值則回傳取得目前設定值。
設定值範圍為 0 ~ 1024 (0 ~ 100%)
pwm7.duty(25 * 1024 // 100) # 設定 PWM 頻率為 25%
print(pwm7.duty()) # print 目前PWM 的 duty 設定值
pwm7.duty(50 * 1024 // 100) # 設定 PWM 頻率為 50%
print(pwm7.duty()) # print 目前PWM 的 duty 設定值
關閉 PWM 訊號。
pwm7.deinit() # 關閉 PWM 訊號輸出
from machine import PWM
pwm7 = PWM(7, 1000, duty=512)
pwm7.duty(1024)
pwm7.duty(512)
pwm7.duty(256)
pwm7.freq(1000)
pwm7.freq(2000)
pwm7.freq(19000)
pwm7.deinit()

- Pin (machine module)
- I2C (machine module)
- PWM (machine module)
- WDT (machine module)
- RTC (machine module)
- Timer (machine module)
- ADC (machine module)
- SPI (machine module)
- UART (machine module)
- BLE (Peripheral)
- LinkIt Remote
- cmath (Builtin functions)
- gc (Builtin functions)
- math (Builtin functions)
- sys (Builtin functions)
- uarray (Builtin functions)
- ubinascii (Builtin functions)
- ucollections (Builtin functions)
- uerrno (Builtin functions)
- uhashlib (Builtin functions)
- uheapq (Builtin functions)
- uio (Builtin functions)
- ujson (Builtin functions)
- uos (Builtin functions)
- ure (Builtin functions)
- uselect (Builtin functions)
- usocket (Builtin functions)
- ussl (Builtin functions)
- ustruct (Builtin functions)
- utime (Builtin functions)
- uzlib (Builtin functions)
- []