Arduino Forum

Using Arduino => LEDs and Multiplexing => Topic started by: Yigiter007 on Jul 10, 2015, 09:36 am

Title: Daisy chaining 74HC595 shift registers to control a Bi-Color LED bar
Post by: Yigiter007 on Jul 10, 2015, 09:36 am
I'm trying to drive a Bi-Color LED bar Bi-Color (Red/Green) 12-LED Bargraph  (http://www.adafruit.com/products/1719) and I followed this tutorial for the 74hc595 https://www.arduino.cc/en/Tutorial/ShiftOut (https://www.arduino.cc/en/Tutorial/ShiftOut) and I got it to work, the problem is the tutorial uses two arrays and sends to each register seperately, I want to give the shift register one big array of 16 digits and have it push the left over through the daisy chain. I tried modifying the code to shift 16 digits, but it didnt work. Here is my circuit circuit photo (https://db.tt/OzZCDBKX) and here is my working code with the two seperate arrays.
Code: [Select]
/*
  Shift Register Example
  Turning on the outputs of a 74HC595 using an array

 Hardware:
 * 74HC595 shift register
 * LEDs attached to each of the outputs of the shift register

 */
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;

//holders for infromation you're going to pass to shifting function
byte dataRED;
byte dataGREEN;

void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
}

void loop()
{

      dataRED = B10000000;
    dataGREEN = B00110000;
    //ground latchPin and hold low for as long as you are transmitting
    digitalWrite(latchPin, 0);
    //move 'em out
    shiftOut(dataPin, clockPin, dataGREEN);
    shiftOut(dataPin, clockPin, dataRED);
    //return the latch pin high to signal chip that it
    //no longer needs to listen for information
    digitalWrite(latchPin, 1);
    delay(300);

}



// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first,
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  //for each bit in the byte myDataOut�
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights.
  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {
      pinState= 0;
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin 
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}


here is the modified code I tried that doesn't give me the same result.
Code: [Select]
//  Shift Register Example
//  Turning on the outputs of a 74HC595 using an array

// Hardware:
// * 2 x 74HC595 shift register
// * Bi-Color Bar LED attached to each of the outputs of both shift registers

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
//holders for infromation you're going to pass to shifting function
int data;

void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
}

void loop()
{
    data = 1000000000110000;
    //ground latchPin and hold low for as long as you are transmitting
    digitalWrite(latchPin, 0);
    //move 'em out
    shiftOut(dataPin, clockPin, data);
    //shiftOut(dataPin, clockPin, dataRED);
    //return the latch pin high to signal chip that it
    //no longer needs to listen for information
    digitalWrite(latchPin, 1);
    delay(300);
}

// the heart of the program
void shiftOut(int myDataPin, int myClockPin, int myDataOut) {
  // This shifts 8 bits out MSB first,
  //on the rising edge of the clock,
  //clock idles low

  //internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

  //clear everything out just in case to
  //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  //for each bit in the byte myDataOut�
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights.
  for (i=15; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {
      pinState= 0;
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin 
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}

Thank you in advance for any help
Title: Re: Daisy chaining 74HC595 shift registers to control a Bi-Color LED bar
Post by: Grumpy_Mike on Jul 10, 2015, 10:34 am
The variable data is an int, you are putting a sacking big number into it. It is a decimal number not a binary number. Look up how th write in binary.

By the way that tutorial is wrong. The capacitor goes from ground to the 5V pin on the chip not the latch pin.

Shift out only shifts 8 bits even with an array you need two of them.
By the way you don't have an array you have a single variable. You must break any data into 8 bit chunks to send out using the shift out function.
Title: Re: Daisy chaining 74HC595 shift registers to control a Bi-Color LED bar
Post by: BirdBare on Jul 10, 2015, 04:44 pm
Hi,

I actually made a little piece of code like this a long time ago. I posted it on github and here is the link.

https://github.com/BirdBare/ArduinoLEDMatrix

It may seem complicated at first but I am confident if you read mine you can figure out how to program your own. Or figure out how to use mine.

With my program you can make the chain as small or as long as you want and you can even change colors on RGB LEDS.

If you need an example file I can post one, it does look complicated at first. Good luck!

Bare