Home ยป Raspberry PI and 74hc595 python example

Raspberry PI and 74hc595 python example

by shedboy71

In this example we expand the amount of outputs using a 74HC595

The 74HC595; 74HCT595 are 8-stage serial shift registers with a storage register and 3-state outputs. The registers have separate clocks. Data is shifted on the positive-going transitions of the shift register clock input (SHCP). The data in each register is transferred to the storage register on a positive-going transition of the storage register clock input (STCP). If both clocks are connected together, the shift register will always be one clock pulse ahead of the storage register.

The shift register has a serial input (DS) and a serial standard output (Q7S) for cascading. It is also provided with asynchronous reset (active LOW) for all 8 shift register stages. The storage register has 8 parallel 3-state bus driver outputs. Data in the storage register appears at the output whenever the output enable input (OE) is LOW.

 

Schematic

PI and 595 schematic

PI and 595 schematic

Code

The code was written in python, save this as shifter.py.

[codesyntax lang=”python”]

import RPi.GPIO as gpio
from time import sleep

class Shifter():

	inputB=11
	clock=12
	clearPin=13
	

	def __init__(self):
		self.setupBoard()
		self.pause=0
	def tick(self):
		gpio.output(Shifter.clock,gpio.HIGH)
		sleep(self.pause)
		gpio.output(Shifter.clock,gpio.LOW)
		sleep(self.pause)		

	def setValue(self,value):
		for i in range(24):
			bitwise=0x800000>>i
			bit=bitwise&value
			if (bit==0):
				gpio.output(Shifter.inputB,gpio.LOW)
			else:
				gpio.output(Shifter.inputB,gpio.HIGH)
			Shifter.tick(self)

	def clear(self):
		gpio.output(Shifter.clearPin,gpio.LOW)
		Shifter.tick(self)
		gpio.output(Shifter.clearPin,gpio.HIGH)

	def setupBoard(self):
		gpio.setup(Shifter.inputB,gpio.OUT)
		gpio.output(Shifter.inputB,gpio.LOW)
		gpio.setup(Shifter.clock,gpio.OUT)
		gpio.output(Shifter.clock,gpio.LOW)
		gpio.setup(Shifter.clearPin,gpio.OUT)
		gpio.output(Shifter.clearPin,gpio.HIGH)

def main():
	pause=0.5
	gpio.setmode(gpio.BOARD)
	shifter=Shifter()
	running=True
	while running==True:
        	try:
			shifter.clear()
			shifter.setValue(1)
			sleep(1)
			shifter.clear()
			shifter.setValue(0x0AAAAAA)
			sleep(pause)
			shifter.clear()
			shifter.setValue(0x0555555)
			sleep(pause)
	        except KeyboardInterrupt:
        		running=False


if __name__=="__main__":
    main()

[/codesyntax]

run this by typing in

[codesyntax lang=”dos”]

sudo python shifter.py

[/codesyntax]

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More