Raspberry Pi and 74hc595 wiringPi example

This is a follow on from the Raspberry PI and python example – http://www.pibits.net/code/raspberry-pi-and-74hc595-python-example.php

This example uses wiringPi

Schematic

PI and 595 schematic
PI and 595 schematic

Code

This example is C++, 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
SHARE
Previous articleRaspberry PI and 74hc595 python example
Next articleRaspberry PI RGB example in C