Raspberry Pi and 74HCT595 examples

0
1579

In this example we will connect a 74hc595 to a Raspberry Pi and then flash LEDs on and off using python and C++

Lets look at the 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.

 

Parts List

Name Link
Raspberry Pi 3 2018 new original Raspberry Pi 3 Model B+ (plug) Built-in Broadcom 1.4GHz quad-core 64 bit processor Wifi Bluetooth and USB Port
74HC595 Shift Register Breakout 74HC595 Shift Register Breakout CJMCU-595 8 bit shift register

Schematic

Pi and 595 schematic
Pi and 595 schematic

Code Examples

Python

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

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()

run this by typing in

sudo python shifter.py

C++ Example

This example is C++ and uses wiringPi, it will flash an LED one at a time connected to the 74HC595

Call this shift.c

#include <wiringPi.h>
#include <stdio.h>
 
#define   SDI   0   //serial data input
#define   RCLK  1   //memory clock input
#define   SRCLK 2   //shift register clock input
 
unsigned char LED[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; 
 
void pulse(int pin)
{
    digitalWrite(pin, 0);
    digitalWrite(pin, 1);
}
 
void ShiftOUT(unsigned char byte)
{
    int i; 
    for(i=0;i<8;i++)
	{
        digitalWrite(SDI, ((byte & (0x80 >> i)) > 0));
        pulse(SRCLK);
    }
}
 
void init(void)
{
    pinMode(SDI, OUTPUT);
    pinMode(RCLK, OUTPUT);
    pinMode(SRCLK, OUTPUT);
    digitalWrite(SDI, 0);
    digitalWrite(RCLK, 0);
    digitalWrite(SRCLK, 0);
}
 
int main(void)
{
    int i;
 
    if(wiringPiSetup() == -1)
	{ 
        printf("setup wiringPi failed !");
        return 1;
    }
 
    init();
 
    while(1)
	{
        for(i=0;i<8;i++)
		{
            ShiftOUT(LED[i]);
            pulse(RCLK);
            delay(150);
        }
        delay(500);
    }
 
    return 0;
}

Compile as follows

gcc shift.c –o shift -lwiringPi

Run with this

sudo./shift

 

Link

sn74hc595 datasheet

LEAVE A REPLY

Please enter your comment!
Please enter your name here

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.