【发布时间】:2014-10-13 14:51:40
【问题描述】:
我正在寻找使用 mcp23017 gpio-expander 和树莓派作为 LED 调光器的解决方案,但每 4-5 秒就会出现短暂的闪烁。如果我直接使用 gpio,我执行了闪烁也存在(如果您尝试,请注释/取消注释代码中的相关部分) 我不能使用 rpi.gpio software-pwm 或 pi-blaster 因为它不能通过 i2c 使用,如果你有一个解决方案可以为 i2c 准备好这个包,那也很棒 我认为问题出在寻址 GPIO 的某个地方,但我不明白
--update-- 在树莓派上使用软件无法获得稳定的时序
#!/usr/bin/python
# -*- coding: utf-8 -*-
# uncomment line 14-20 for using I2C and comment line 24-35, switch for using GPIO directly
import smbus
import time
import RPi.GPIO as GPIO
liste = [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0]
# print liste #debugging ...
periodendauer = 0.001 # means 1000Hz
# # to send data to mcp23017
# b = smbus.SMBus(1) # 0 indicates /dev/i2c-0, muss auf 1 stehen (für rev2)
# while True:
# for values in liste:
# b.write_byte_data(0x20,0x14,values) #send data via smbus(I2C) to mcp23017
# # print values #debugging only
# time.sleep(periodendauer)
# to send data direct to gpio-pin
GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
while True:
for values in liste:
if values > 0:
values = True
else:
values = False
GPIO.output(7,values)
# print values #debugging only
time.sleep(periodendauer)
【问题讨论】:
-
编辑:此代码也可通过 github 获得 github.com/herrxyz/software-pwm-raspberry-python/blob/master/…
标签: python raspberry-pi i2c pwm