使用RaspberryPi控制移位寄存器 - 不稳定的结果

时间:2016-06-10 19:22:19

标签: python raspberry-pi2

我试图通过使用RaspberryPi控制移位寄存器(74HC595)来闪烁某些LED。我在Python中基本上重写了this Arduino code,但我所预期的直接代码却充其量只是不稳定。

运行时,第一种模式工作正常,但第二种模式仅适用于LED 4-8。减少闪烁之间的延迟会产生更糟糕的结果。我在RPi 2模型B上运行Raspian Jessie .Python 2.7。寄存器和LED由单独的5V电源供电,所有器件都共用GND。

import RPi.GPIO as GPIO ## import GPIO lib
import time ## import time lib


GPIO.setmode(GPIO.BOARD) ## use board pin numbering
GPIO.setup(7, GPIO.OUT) ## SER
GPIO.setup(11, GPIO.OUT) ## RCLK
GPIO.setup(12, GPIO.OUT) ## SRCLK

num_74hc595s = int(1)
num_pins = num_74hc595s * 8

registers = [False]*num_pins

def setPin(index, value):
    registers[index] = value

def clearRegisters():
    for i in range(num_pins-1, -1, -1):
        registers[i] = False

def writeRegisters():
    GPIO.output(11, False)

    for i in range(num_pins-1, -1, -1):
        GPIO.output(12, False)

        val = registers[i]

        GPIO.output(7, val)
        GPIO.output(12, True)

    GPIO.output(11, True)

##turn on odd LEDs
clearRegisters()
writeRegisters()

setPin(0, True)
setPin(2, True)
setPin(4, True)
setPin(6, True)

writeRegisters()

print (registers)

time.sleep(1)

clearRegisters()
writeRegisters()

setPin(1, True)
setPin(3, True)
setPin(5, True)
setPin(7, True)

writeRegisters()

print (registers)

time.sleep(1)

clearRegisters()
writeRegisters()

for i in range(0, 8): #lights up LEDs sequentially

    clearRegisters()
    writeRegisters()

    setPin(i, True)

    writeRegisters()

    time.sleep(2)


GPIO.cleanup()
print ("Dunzo")

第一位

0 个答案:

没有答案