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 0xfc is 0 GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0xfc) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x60 is number 1 GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x60) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0xda is number 2 GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0xda) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0xf2 is number 3 GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0xf2) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x66 is number 4 GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x66) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 5 is 0xb6 GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0xb6) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 6 is 0xbe GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0xbe) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0xe0 is number 7 GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0xe0) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0xfe is number 8 GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0xfe) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0xe6 is number 9 GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0xe6) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0xee is number 10 or A GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0xee) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x3e is number 11 or b GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x3e) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x1a is 12 or c GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x1a) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x7a is 13 or d GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x7a) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x9e is 14 or e GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x9e) GPIO.output(latchPin,GPIO.HIGH) time.sleep(2) # blank GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x00) GPIO.output(latchPin,GPIO.HIGH) time.sleep(0.1) # number 0x8e is 15 or f GPIO.output(latchPin,GPIO.LOW) shiftOut(dataPin,clockPin,LSBFIRST,0x8e) 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 LSBFirst. Least Significant Bit is A. It would be thefirst bit: A to H
A-D is the First Bit
E-H is the Second Bit
First Bit= 8*D + 4*C + 2*B + 1*A
Second Bit = 8*H + 4*G + 2*F + 1*E
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.