クラス PWM

# Import PWM class
from robot_hat import PWM

# Create PWM object with numeric pin numbering and default input pullup enabled
p0 = PWM(0)
# Create PWM object with named pin numbering
p1 = PWM('P1')


# Set frequency will automatically set prescaller and period
# This is easy for device like Buzzer or LED, which you care
# about the frequency and pulse width percentage.
# this usually use with pulse_width_percent function.
# Set frequency to 1000Hz
p0.freq(1000)
print(f"Frequence: {p0.freq()} Hz")
print(f"Prescaler: {p0.prescaler()}")
print(f"Period: {p0.period()}")
# Set pulse width to 50%
p0.pulse_width_percent(50)

# Or set prescaller and period, will get a frequency from:
# frequency = PWM.CLOCK / prescaler / period
# With this setup you can tune the period as you wish.
# set prescaler to 64
p1.prescaler(64)
# set period to 4096 ticks
p1.period(4096)
print(f"Frequence: {p1.freq()} Hz")
print(f"Prescaler: {p1.prescaler()}")
print(f"Period: {p1.period()}")
# Set pulse width to 2048 which is also 50%
p1.pulse_width(2048)

API

class robot_hat.PWM(channel, address=None, *args, **kwargs)

ベースクラス: I2C

パルス幅変調(PWM)

REG_CHN = 32

チャンネルレジスタの接頭辞

REG_PSC = 64

プリスケーラーレジスタの接頭辞

REG_ARR = 68

周期レジスタの接頭辞

CLOCK = 72000000.0

クロック周波数

__init__(channel, address=None, *args, **kwargs)

PWMの初期化

パラメータ

channel (int/str) -- PWMチャンネル番号(0-13/P0-P13)

freq(freq=None)

周波数の設定/取得、周波数を取得するには空白にしておく

パラメータ

freq (float) -- 周波数(0-65535)(Hz)

戻り値

周波数

戻り値の型

float

prescaler(prescaler=None)

プリスケーラーを設定/取得、プリスケーラーを取得するには空欄にしておく

パラメータ

prescaler (int) -- プリスケーラー(0-65535)

戻り値

プリスケーラー

戻り値の型

int

period(arr=None)

周期を設定/取得、周期を取得するには空欄にしておく

パラメータ

arr (int) -- 周期(0-65535)

戻り値

周期

戻り値の型

int

pulse_width(pulse_width=None)

パルス幅を設定/取得、パルス幅を取得するには空欄にしておく

パラメータ

pulse_width (float) -- パルス幅(0-65535)

戻り値

パルス幅

戻り値の型

float

pulse_width_percent(pulse_width_percent=None)

パルス幅のパーセンテージを設定/取得、パルス幅のパーセンテージを取得するには空欄にしておく

パラメータ

pulse_width_percent (float) -- パルス幅のパーセンテージ(0-100)

戻り値

パルス幅のパーセンテージ

戻り値の型

float