I don't know if you are interested about the STP16CP05 which contains already the current driver.
This way you don't need driver and level shifter.
It is similar like the 74HC595 but it has 16 bits I/O with current adjustable from 0 to 100 ma. The only bug is that it cames on SOIC or TSSOP version. but you could by soic/tssop adapter.
But at least it is way easier . No need to put driver. The cost of the chip is a little more than the 74HC595 so it is cheap.
I use it in multiplexing mode with a TB6612 driver.
https://dl.dropboxusercontent.com/s/g4u ... tiplex.mov
And the code.
Code: Select all
import RPi.GPIO as GPIO
import time
import threading
import spidev
class LEDs:
def __init__(self, bus, A1=18,A2=23,B1=24,B2=25,ENABLE=4):
self.A1 = A1
self.A2 = A2
self.B1 = B1
self.B2 = B2
self.ENABLE = ENABLE
# digit segment
A = 1
B = 2
C = 4
D = 8
E = 16
F = 32
G = 64
DP = 128
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(A1, GPIO.OUT)
GPIO.setup(A2, GPIO.OUT)
GPIO.setup(B1, GPIO.OUT)
GPIO.setup(B2, GPIO.OUT)
GPIO.setup(ENABLE, GPIO.OUT)
GPIO.output(ENABLE,1)
GPIO.output(A1,0)
GPIO.output(B1,0)
GPIO.output(A2,0)
GPIO.output(B2,0)
# creation d'un dictionaire pour convertir le chiffre en segment
# segment
#
#
# -A-
# | |
# F B
# | |
# -G-
# | |
# E C
# | |
# -D- DP
#
#
# creation de la correspondance digit versus segment
#
D0 = (A | B | C | D | E | F)
D1 = (B | C)
D2 = (A | B | G | E | D)
D3 = (A | B | G | C | D)
D4 = (F | B | G | C)
D5 = (A | F | G | C |D)
D6 = (A | F | G | C |D |E)
D7 = (A | B | C)
D8 = (A | B | C | D | E | F | G)
D9 = (A | B | C | D | F | G)
DMOINS = G
DBLANK = 0
DQUESTION = ( A | B | G | E)
# dictionaire des valeurs
self.Chiffre = {0: D0, 1: D1, 2: D2, 3: D3, 4: D4,
5: D5, 6: D6, 7: D7, 8: D8, 9: D9,
'-': DMOINS, ' ': DBLANK, '?': DQUESTION,
'SEGA': A, 'SEGB': B, 'SEGC': C,
'SEGD': D, 'SEGE': E, 'SEGF': F,
'SEGG': G, 'SEGDP': DP}
self.digits = [ 0, 0, 0, 0, 0, 0, 0, 0 ]
self.digitsIdx = 0
self.spi = spidev.SpiDev()
self.speed=2000000
self.spi.open(0,bus)
self.spi.max_speed_hz=self.speed
self.Exit = False
self.timer = threading.Thread(target=self.nextDigits)
self.timer.start()
def __del__(self):
self.Exit = True
def nextDigits(self):
while not self.Exit:
# Desactive sortie
GPIO.output(self.ENABLE,1)
# Active les bons digits
if self.digitsIdx == 0:
GPIO.output(self.B2, 0)
GPIO.output(self.A1, 1)
elif self.digitsIdx == 1:
GPIO.output(self.A1, 0)
GPIO.output(self.A2, 1)
elif self.digitsIdx == 2:
GPIO.output(self.A2, 0)
GPIO.output(self.B1, 1)
else:
GPIO.output(self.B1, 0)
GPIO.output(self.B2, 1)
# Ecrire deux prochains digits sur SPI
# Swap MSB byte LSB Byte
Data = [ self.digits[self.digitsIdx*2+1],
self.digits[self.digitsIdx*2]]
self.spi.xfer(Data)
#activer la sortie
GPIO.output(self.ENABLE,0)
self.digitsIdx = self.digitsIdx + 1
if self.digitsIdx > 3 :
self.digitsIdx=0
time.sleep(0.005)
GPIO.output(self.ENABLE,1)
def GetByte(self, Dval=' ', DP=False):
if DP:
BData = DP
else:
BData = 0
if Dval in self.Chiffre:
BData = BData | self.Chiffre[Dval]
else:
BData = BData | self.Chiffre['?']
return BData
def numero(self,valeur):
ivaleur = int(valeur)
d = [ 0 , 0 , 0 , 0 , 0 , 0, 0 , 0]
for i in range(8):
d[i] = self.Chiffre[ivaleur % 10]
ivaleur = ivaleur // 10
if ivaleur == 0:
break;
self.digits=d
def stop(self):
for i in range(8):
dp.digits[i] = 0
self.Exit= True
def ShiftDisplay(dp,n):
for i in range(8):
dp.digits[i]= dp.GetByte(n);
time.sleep(0.1)
if __name__ == "__main__":
dp = LEDs(0)
try:
i = 79999990
while True:
dp.numero(i)
i = i + 1
time.sleep(0.1)
except KeyboardInterrupt:
dp.stop()