1 | p = GPIO.PWM(channel, frequency) |
1 | p.start(dc) # dc 代表占空比(范围:0.0 <= dc >= 100.0) |
1 | p.ChangeFrequency(freq) # freq 为设置的新频率,单位为 Hz |
1 | p.ChangeDutyCycle(dc) # 范围:0.0 <= dc >= 100.0 |
1 | p.stop() |
1 2 3 4 5 6 7 8 9 | import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.OUT) p = GPIO.PWM(12, 0.5) p.start(1) input('点击回车停止:') # 在 Python 2 中需要使用 raw_input p.stop() GPIO.cleanup() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(12, GPIO.OUT) p = GPIO.PWM(12, 50) # 通道为 12 频率为 50Hz p.start(0) try: while 1: for dc in range(0, 101, 5): p.ChangeDutyCycle(dc) time.sleep(0.1) for dc in range(100, -1, -5): p.ChangeDutyCycle(dc) time.sleep(0.1) except KeyboardInterrupt: pass p.stop() GPIO.cleanup() |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |