import RPi.GPIO as GPIO import time LSBFIRST = 1 MSBFIRST = 2 dataPin = 11 #DS Pin of 74HC595(Pin14) latchPin = 13 #ST_CP Pin of 74HC595(Pin12) clockPin = 15 #CH_CP Pin of 74HC595(Pin11) def setup(): GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) GPIO.setup(dataPin, GPIO.OUT) GPIO.setup(latchPin, GPIO.OUT) GPIO.setup(clockPin, GPIO.OUT) def shiftOut(dPin,cPin,order,val): for i in range(0,8): GPIO.output(cPin,GPIO.LOW); if(order == LSBFIRST): GPIO.output(dPin,(0x01&(val>>i)==0x01) and GPIO.HIGH or GPIO.LOW) elif(order == MSBFIRST): GPIO.output(dPin,(0x80&(val<<i)==0x80) and GPIO.HIGH or GPIO.LOW) GPIO.output(cPin,GPIO.HIGH); def loop(): while True: # number 0x3f is 0 GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x3f) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x06 is number 1 GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x06) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x5b is number 2 GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x5b) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x4f is number 3 GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x4f) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x66 is number 4 GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x66) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 5 is 0xed GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0xed) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 6 is 0xfc GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0xfc) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x07 is number 7 GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x07) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x7F is number 8 GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x7F) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x67 is number 9 GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x67) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x77 is number 10 or A GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x77) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x7C is number 11 or b GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x7C) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x58 is 12 or c GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x58) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x5E is 13 or d GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x5E) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x79 is 14 or e GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x79) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x71 is 15 or f GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x71) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,MSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) def destroy(): GPIO.cleanup() if __name__ == '__main__': print ('Program is starting...' ) setup() try: loop() except KeyboardInterrupt: destroy()
Logic Table for MSBFirst. Most Significant Bit is H or DP. It would be the last bit and you do them in reverse order like the table.
H-E is the First Bit
D-A is the Second Bit
First Bit = 8*H + 4*G + 2*F + 1*E
Second Bit= 8*D + 4*C + 2*B + 1*A
It is done in Hex: 0-9 are just 0-9. 10 is A, 11 is B, 12 is C, 13 is D, 14 is E, 15 is F.