Home Code Raspberry Pi and 74hc595 wiringPi example

Raspberry Pi and 74hc595 wiringPi example

by shedboy71

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

[codesyntax lang=”cpp”]

#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;
}

[/codesyntax]

Compile as follows

[codesyntax lang="bash"]

gcc shift.c –o shift -lwiringPi

[/codesyntax]

Run with this

[codesyntax lang=”bash”]

sudo./shift

[/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