74HC595 connected to Arduino
Fig. 1

Interfacing Raspberry Pi 74HC595 Serial Shift Register

by Lewis Loflin

  
  

Here we look into connecting a 74HC595 8-bit serial shift register with 8-bit latch to a Raspberry Pi. Fig. 1 shows the internal configuration.

# MSB out first!
def ssrWrite(value):
    for  x in range(0,8):
        temp = value & 0x80
        if temp == 0x80:
           GPIO.output(dataBit, 1) # data bit HIGH
        else:
           GPIO.output(dataBit, 0) # data bit LOW
        pulseCLK()        
        value = value <<  0x01 # shift left
    serLatch() # output byte
    return 

Above is our serial shift subroutine ssrWrite(). It's nothing more than a for loop, a bitwise AND, and left-shift that sets or clears the dataBit pin, pulses HIGH then LOW the shift clock (pulseCLK), then after doing that 8 times pulses HIGH then LOW (serLatch) the latch pin. Here we shift out the most significant bit first.

74HC595 connected to 8 LEDs.
Fig. 2

Fig. 2 shows the LED connections. The LED pattern is simply a reproduction of the binary pattern shifted into the 8-bit register.

while 1:
    temp = 1
    for j in range(0, 8 ):
        ssrWrite(temp)
        temp = temp << 1
        time.sleep(.2)

    for j in range(0, 8 ):
        temp = temp >> 1
        ssrWrite(temp)
        time.sleep(.2)  

The main program turns on a single LED and moves it back and forth using left and right shift commands. This consists of an infinite while loop and two for loops.



#!/usr/bin/env python
# File rpi_74HC595_cylon.py
# Use left and right shift resister functions.
# 8 LEDs connected to 74HC595
# http://www.bristolwatch.com/index.htm
# by Lewis Loflin lewis@sullivan-county.com

# access to GPIO must be through root
import RPi.GPIO as GPIO
import time


LATCH = 11 # Pin 12 Latch clock
CLK = 12 # Pin 11 shift clock
dataBit = 7 # Pin 14 A


GPIO.setup(LATCH, GPIO.OUT) # P0 
GPIO.setup(CLK, GPIO.OUT) # P1 
GPIO.setup(dataBit, GPIO.OUT) # P7 


# Setup IO
GPIO.output(11, 0)
GPIO.output(CLK, 0)




def pulseCLK():
    GPIO.output(CLK, 1)
   # time.sleep(.01) 
    GPIO.output(CLK, 0)
    return

def serLatch():
    GPIO.output(LATCH, 1)
   # time.sleep(.01)
    GPIO.output(LATCH, 0)
    return

# MSB out first!
def ssrWrite(value):
    for  x in range(0,8):
        temp = value & 0x80
        if temp == 0x80:
           GPIO.output(dataBit, 1) # data bit HIGH
        else:
           GPIO.output(dataBit, 0) # data bit LOW
        pulseCLK()        
        value = value << 0x01 # shift left
    serLatch() # output byte
    return 

    



# convert an 8-bit number to a binary string
def convBinary(value):
    binaryValue = '0b'
    for  x in range(0,8):
        temp = value & 0x80
        if temp == 0x80:
           binaryValue = binaryValue + '1'
        else:
            binaryValue = binaryValue + '0'
        value = value << 1
    return binaryValue


while 1:
    temp = 1
    for j in range(0, 8 ):
        ssrWrite(temp)
        temp = temp << 1
        time.sleep(.2)

    for j in range(0, 8 ):
        temp = temp >> 1
        ssrWrite(temp)
        time.sleep(.2)  

print "Good by!"
exit


Optical Isolation of H-Bridge Motor Controls YouTube
Optical Isolation of H-Bridge Motor Controls

Opto-Couplers Theory and Circuits YouTube
Opto-Isolated Transistor Drivers for Micro-Controllers

All NPN Transistor H-Bridge Motor Control YouTube
All NPN Transistor H-Bridge Motor Control

Pulse-Width Modulation Tutorial YouTube
Pulse-Width Modulation Tutorial

PIC12F683 Microcontroller and Circuits YouTube
PIC12F683 Microcontroller and Circuits

 


donate