哪位大佬幫忙解釋下面的代碼
開發板采用PYBOARD, 芯片為STM32F405VET。軟體采用micropython 非常簡單,micropython 與python 一樣,解釋性語言。
把程式通過模擬串口上傳到開發板。 上傳軟體采用uPyCraft v1.1 其它軟體也可以,串口除錯組手都可以。開發板可以采用普通串口,這里我采用的是USB 模擬串口。
import math
import pyb
from pyb import Timer,Pin
import stm
class pwm_sin:
def __init__(self):
self.buf = bytearray(100)
for i in range(len(self.buf)):
self.buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(self.buf)))
pa0 = Pin('PA0', Pin.OUT_PP)
self.t6 = Timer(6, prescaler=83, period=19999)
t2 = Timer(2, prescaler=83, period=254)
self.ch1 = t2.channel(1, Timer.PWM, pin=pa0)
t2.callback(self.timer_callback)
def __del__(self):
pass
@micropython.native
def timer_callback(self,timer):
t = self.t6.counter() // 200
self.ch1.pulse_width(self.buf[t])
p=pwm_sin()
未濾波前波形
uj5u.com熱心網友回復:
self.buf = bytearray(100)for i in range(len(self.buf)):
self.buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(self.buf)))
這三行是在self.buf里,100個點構成一個周期的正弦波,值范圍128到255
其他建議參考
https://docs.singtown.com/micropython/zh/latest/openmvcam/library/pyb.Timer.html
https://docs.singtown.com/micropython/zh/latest/openmvcam/library/pyb.Pin.html
檔案寫的很詳細
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/82698.html
