開發(fā)板采用PYBOARD, 芯片為STM32F405VET。軟件采用micropython 非常簡單,micropython 與python 一樣,解釋性語言。
把程序通過模擬串口上傳到開發(fā)板。 上傳軟件采用uPyCraft v1.1 其它軟件也可以,串口調試組手都可以。開發(fā)板可以采用普通串口,這里我采用的是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()
復制代碼
未濾波前波形
1.jpg (3.65 MB, 下載次數(shù): 66)
下載附件
未濾波前波形
2019-1-21 14:50 上傳
經阻容低通濾波器濾波后的波形
2.jpg (4.04 MB, 下載次數(shù): 60)
下載附件
低通濾波后波形
2019-1-21 14:51 上傳
50HZ是不是非常完美,改變參數(shù)可以調整波形頻率。
|