Skip to content

Instantly share code, notes, and snippets.

@sbchisholm
Last active December 14, 2015 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbchisholm/5033331 to your computer and use it in GitHub Desktop.
Save sbchisholm/5033331 to your computer and use it in GitHub Desktop.
/* ---------------------------------------------------------
* | Arduino Experimentation Kit Example Code |
* | CIRC-05 .: 8 More LEDs :. (74HC595 Shift Register) |
* ---------------------------------------------------------
*
* We have already controlled 8 LEDs however this does it in a slightly
* different manner. Rather than using 8 pins we will use just three
* and an additional chip.
*
*
*/
#include<Arduino.h>
class ShiftRegister {
// :: Public Constants
public:
static const byte bits[];
static const byte masks[];
// :: Constructor ----------------------------------------
public:
ShiftRegister(short data, short clock, short latch)
: m_data(data)
, m_clock(clock)
, m_latch(latch)
, m_state(B00000000)
{}
// :: Interface -----------------------------------------
public:
// valid pin values are 0,1,2,...,7.
void setPin(byte pin, short pinState) {
// Make sure that the input values are valid.
// TODO: Add error handling.
if (pin < 0 or pin > 7) return;
if (not pinState == LOW or not pinState == HIGH) return;
// Set the pin we are addressing to LOW.
m_state = m_state & masks[pin];
// If the bit is set to HIGH turn it on.
if (pinState == HIGH)
m_state == pinState | bits[pin];
}
// Override the current state with the given one.
void setState(byte state) { m_state = state; }
// Write the current state out to the shift register.
void writeState() {
// Copy the current state to a temporary value
byte state = m_state;
// Pulls the chips latch low
digitalWrite(m_latch, LOW);
// Will repeat 8 times (once for each bit)
for(byte i = 0; i < 8; i++) {
// We use a "bitmask" to select only the eighth
// bit in our number (the one we are addressing this time through
byte bit = state & B10000000;
// Shift the state by one so that the next value is in the 8th bit
// position.
state = state << 1;
// If the 8th bit is set then set our data pin to HIGH otherwise set
// it LOW.
digitalWrite(m_data, bit ? HIGH : LOW);
// The next three lines pulse the clock pin.
digitalWrite(m_clock, HIGH);
delay(1);
digitalWrite(m_clock, LOW);
}
// Pulls the latch high shifting our data into being displayed.
digitalWrite(m_latch, HIGH);
}
// Write the current state to the shift register using the built in shiftOut
// function.
void writeStateShort() {
//Pulls the chips latch low
digitalWrite(m_latch, LOW);
//Shifts out the 8 bits to the shift register
shiftOut(m_data, m_clock, MSBFIRST, m_state);
//Pulls the latch high displaying the data
digitalWrite(m_latch, HIGH);
}
// :: Members --------------------------------------------
private:
short m_data;
short m_clock;
short m_latch;
byte m_state;
};
//These are used in the bitwise math that we use to change individual LEDs
//For more details http://en.wikipedia.org/wiki/Bitwise_operation
const byte ShiftRegister::bits[] =
{B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000};
const byte ShiftRegister::masks[] =
{B11111110, B11111101, B11111011, B11110111, B11101111, B11011111, B10111111, B01111111};
//Pin Definitions
//The 74HC595 uses a serial communication
//link which has three pins
short data = 2;
short clock = 3;
short latch = 4;
/*
* setup() - this function runs once when you turn your Arduino on
* We set the three control pins to outputs
*/
void setup()
{
pinMode(data, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(latch, OUTPUT);
}
/*
* loop() - this function will start after setup finishes and then repeat
* we set which LEDs we want on then call a routine which sends the states to the 74HC595
*/
void loop()
{
ShiftRegister shiftRegister(data, clock, latch);
shiftRegister.setState(B01010101);
shiftRegister.writeState();
delay(1000);
shiftRegister.setState(B10101010);
shiftRegister.writeState();
delay(1000);
short delayTime = 100;
for (byte i = B11111111 ; i >= B00000000; i--) {
shiftRegister.setState(i);
shiftRegister.writeState();
delay(delayTime);
}
}
@sbchisholm
Copy link
Author

TODO: Make it work for shift register classes with 8 bits, 16 bits, 24 bits... etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment