Mastodon Politics, Power, and Science: Shift register using 74HC595

Thursday, November 20, 2014

Shift register using 74HC595

I used two 74HC595 chips hooked to the Serial Peripheral Interface Bus of an Arduino.   This bus will runs at 4MHz so the updates to the LEDs were very snappy.





A single chip can only control 8 LED's.  In order to control another 8 chips you have to connect a second chip to the first chip in series.   


/*
 Shift Register Example
 for 74HC595 shift register

 This sketch turns turns your arduino into a cylon.

 Hardware:
 * 74HC595 shift register attached to hardware SPI
 Arduino               74HC959
  pin# name            pin#  name
  10 SS        --->    12 RCK
  11 MOSI      --->    14 SI
  13 SCK       --->    11 SCK
 
 * LEDs attached to each of the outputs of the shift register

 Created 02 Oct 2014
 by James M. Rogers

 Based on work of others found in following locations:
 http://forum.arduino.cc/index.php/topic,149470.0.html
 http://arduino.cc/en/tutorial/ShiftOut
 http://arduino.cc/en/Tutorial/SPIDigitalPot

 */

#include <SPI.h>

int pinSS=10; //latch

int d=40;

void setup() {
  SPI.begin(); 
}

//#define d 40
void loop() {

    int i;
    for (i=0 ; i<16 ; i++) {
      registerWrite(i, HIGH);
       delay (d);
    }
    for (i=15 ; i>=0 ; i--) {
      registerWrite(i, HIGH);
       delay (d);
    }

  if (d>10)
    d-=5;
  else 
    d--;
    
    if (d==0)
      d=40;
}

// This method sends bits to the shift register:
void registerWrite(int whichPin, int whichState) {
// the bits you want to send
  byte lowbitsToSend = 0;
  byte highbitsToSend = 0;

  if (whichPin <8) {
    // turn on the next highest bit in bitsToSend:
    bitWrite(lowbitsToSend, whichPin, whichState);
  } else {
    bitWrite(highbitsToSend, whichPin-8, whichState);
  }
  
  SPI.transfer(highbitsToSend);
  SPI.transfer(lowbitsToSend);

  digitalWrite(pinSS,LOW);
  delayMicroseconds(1);
  digitalWrite(pinSS,HIGH);
}

No comments:

Post a Comment

The 8th-Grade Crutch: How the 2-Body Problem Broke Modern Physics

J. Rogers, SE Ohio If you want to understand why fundamental physics has been stagnant for 70 years, don’t look at String Theory or the Mult...