我正在寻找一个解决方案使用mcp23017的gpio扩展器与覆盆子皮作为引导-调光器,但每4-5秒有一个短暂的闪烁。我发现,如果我直接使用GPIO (如果您尝试使用代码中的注释/不注释相关部分),我也不能使用rpi.gpio软件pwm或pi-blaster,因为它不能通过i2c使用,如果您有一个使这个包为i2c做好准备的解决方案,那也很好--我认为问题就在某个困扰gpio的地方,但我不明白它。
-更新--不可能在raspberry pi上用软件获得稳定的时间。
#!/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)发布于 2015-01-28 06:32:14
根据评论,我为你的问题重新写了答案。
我通过删除I2C部分和注释简化了应用程序,并删除了睡眠函数。我这么做是为了向你们展示,对于这种精确的计时方式,Raspbery是多么的不可靠,你想用它的方式。我在代码中添加的是在for循环开始和结束时测量的时间,因此它现在测量的是"liste“数组的整个周期处理,而不是单个”值“的长度。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import RPi.GPIO as GPIO
import sys
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]
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7, GPIO.OUT)
while True:
ms = time.time() * 1000.0
for values in liste:
if values > 0:
values = True
else:
values = False
GPIO.output(7,values)
me = time.time() * 1000.0 - ms
sys.stdout.write("\r{0:4.4f}".format(me)),
sys.stdout.flush()我有一个香蕉派在家里帽子有相同的GPIO输出,但请运行它在一个覆盆子,你将有相同的结果(也许与更长的周期时间)。对我来说,结果是4-5ms和几个6毫秒长的脉冲,有时超过10毫秒。这就是为什么你一直在闪动。
对于基于I2C的解决方案,我建议使用专用的I2C驱动板来产生平滑的I2C信号,例如来自NXP的PCA9685。
https://stackoverflow.com/questions/25401469
复制相似问题