標(biāo)題: 絕對(duì)原創(chuàng)micropython生成50HZ正弦波 [打印本頁]

作者: lg28702426    時(shí)間: 2019-1-21 14:52
標(biāo)題: 絕對(duì)原創(chuàng)micropython生成50HZ正弦波
開發(fā)板采用PYBOARD, 芯片為STM32F405VET。軟件采用micropython 非常簡(jiǎn)單,micropython 與python 一樣,解釋性語言。
把程序通過模擬串口上傳到開發(fā)板。 上傳軟件采用uPyCraft v1.1 其它軟件也可以,串口調(diào)試組手都可以。開發(fā)板可以采用普通串口,這里我采用的是USB 模擬串口。
  1. import math
  2. import pyb
  3. from pyb import Timer,Pin
  4. import stm
  5. class pwm_sin:
  6.     def __init__(self):
  7.         self.buf = bytearray(100)
  8.         for i in range(len(self.buf)):
  9.             self.buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(self.buf)))
  10.         pa0 = Pin('PA0', Pin.OUT_PP)
  11.         self.t6 = Timer(6, prescaler=83, period=19999)
  12.         t2 = Timer(2, prescaler=83, period=254)
  13.         self.ch1 = t2.channel(1, Timer.PWM, pin=pa0)
  14.         t2.callback(self.timer_callback)
  15.     def __del__(self):
  16.         pass

  17.     @micropython.native
  18.     def timer_callback(self,timer):
  19.         t = self.t6.counter() // 200
  20.         self.ch1.pulse_width(self.buf[t])
  21. p=pwm_sin()
復(fù)制代碼

未濾波前波形



經(jīng)阻容低通濾波器濾波后的波形


50HZ是不是非常完美,改變參數(shù)可以調(diào)整波形頻率。


作者: tangdou    時(shí)間: 2019-8-5 09:03
有人在嗎
作者: tangdou    時(shí)間: 2019-8-5 09:03
想問一下如何用microPython產(chǎn)生一個(gè)SPWM波形

作者: redeyeroc    時(shí)間: 2019-8-5 14:21
最近一直在學(xué)micropython,感謝提供素材~~~~~~~




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1