13 Oct 2011

Finally, a three some (74HC595)!

As a follow up to the first tests with the 74HC594 and 74HC595 with Arduino I got some more 595 delivered, finally chaining them together. This example uses the 74HC595 chip.



Code:

First, you need to install (extract) the MM74HC595 library I wrote into your "<\libraries>" folder.

/**
 * Board: Pro Mini 5V
 * 
 * Chained 74HC595 8 bit shift register demo. Knight Rider!
 *
 * $Id: test_74HC595.pde 319 2011-10-13 20:48:10Z wunderlins $
 */

// how many shift registers are we using in serie? (must be >=1)
#define NUM_SHIFT_REGISTERS 3

// include library (must be installed unter <sketches>/libraries/MM74HC595/)
// http://spliffy.freeshell.net/hardware/MM74HC595.zip
#include <MM74HC595.h>

/**
 * Arduino pins:
 * 
 * PIN_SER: is the arduino serial pin
 * PIN_SCK: is the arduino pin for the shift register
 * PIN_RCK: is the arduino pin for the output register
 */
int PIN_SER = 8;  // pin 14 on the 75HC595
int PIN_SCK = 9;  // pin 12 on the 75HC595
int PIN_RCK = 10; // pin 11 on the 75HC595

int i = 0;
int direction = 1; // 1 = L2R, -1 R2L

// shift register instance
MM74HC595 registers(PIN_SER, PIN_SCK, PIN_RCK, NUM_SHIFT_REGISTERS);

// module pins already initialzed
void setup() {;}               

void loop(){
 
 // reset all pins (set to LOW)
 registers.reset();
 
 // set pin(s) to high
 registers.set(i, true);
 
 // activate outputs
 registers.update();
 
 // check if we have to change direction
  if (i+1 == NUM_SHIFT_REGISTERS * 8) {
   direction = -1;
  } else if (i == 0) {
   direction = 1;
  }
  i += direction;

  delay(100);
}