在所有gpio上调制复杂信号

时间:2017-09-03 04:33:20

标签: python raspberry-pi pwm

我需要在GIPO输出端有一个大约这个形状的信号。(脉冲中的子脉冲)enter image description here

如何在PI上使用PWM实现?我尝试用RPIO做,但他的古老GPIO引脚可能不适用于我的Rpi 3 b +。

from RPIO import PWM
servo = PWM.Servo()
servo.set_servo(12, 10000)
PWM.add_channel_pulse(0, 12, start=200, width=2000)

引脚上没有信号。 enter image description here 我对此感到困惑,并希望尝试使用内置库来处理PWM,但我没有发现子循环的可能性。我怎么能从不同的GPIO输出这种形式的信号?

3 个答案:

答案 0 :(得分:2)

documentation表明只需将一个频道列表作为GPIO.setup和GPIO.output的第一个参数传递即可完成您的要求。

chan_list = [11,12]    # add as many channels as you want!
                       # you can tuples instead i.e.:
                       #   chan_list = (11,12)
GPIO.setup(chan_list, GPIO.OUT)
GPIO.output(chan_list, GPIO.LOW)                # sets all to GPIO.LOW

答案 1 :(得分:1)

与RPi.GPIO相比,我对pigpio有更好的PWM体验。 Wiringpi也很好,但是Pigpio的PWM支持是更好的IMO。

该文档具有一些在任何引脚上生成PWM的功能:

http://abyz.co.uk/rpi/pigpio/python.html#set_servo_pulsewidth http://abyz.co.uk/rpi/pigpio/python.html#set_PWM_dutycycle

*有* 使用RPi.GPIO吗?我知道这不是一个确切的答案,但我希望它至少能指出你正确的方向。

答案 2 :(得分:1)

看来,您应该使用这样的代码。不幸的是,由于我没有频率计或示波器,我没有机会测试它。

import time
import pigpio

GPIO=12

pulse = []

#                          ON       OFF    MICROS
pulse.append(pigpio.pulse(1<<GPIO, 0,       5))
pulse.append(pigpio.pulse(0,       1<<GPIO, 5))
pulse.append(pigpio.pulse(1<<GPIO, 0,       5))
pulse.append(pigpio.pulse(0,       1<<GPIO, 1e7))

pi = pigpio.pi() # connect to local Pi

pi.set_mode(GPIO, pigpio.OUTPUT)

pi.wave_add_generic(pulse)

wid = pi.wave_create()

if wid >= 0:    
    pi.wave_send_repeat(wid)
    time.sleep(60)   # or another condition for stop processing
    pi.wave_tx_stop()
    pi.wave_delete(wid)

pi.stop()