Go Down

Topic: 74HC595 Shift Register and ShiftOut() - Pins Will Only Fire ... [SOLVED] (Read 1 time) previous topic - next topic

bbureau12

I'm attempting to use a 74H595 Shift Register to control 24 total LED's.  Would like to get them to perform a variety of tricks including chase, sparkle (fire random LED, pause for a random burst, fire another,) pulse to music etc.

As a proof of concept, started with a single 74H595, wired it up per the Arduino shiftout tutorial with the exception of clockpin being 3, datapin 2, and latchpin 4.  Fired them up with a basic for loop that increments up a byte array from B00000001 to B10000000, and it fires pins 1-8 in sequence as expected.

Tried the same thing with reversing the loop (7 to 0) and, mysteriously, and it continues firing pins 1-8.


Things I've tried:
  • Troubleshooting the loop and the pulled both the counter and the array value using Serial.print().  It goes from 7-0 and 128-1 as expected.
  • Checking the circuit, completely rewiring it.
  • Changing the delay speed.  Again nothing
  • Writing 0 to all 3 pins every time I ShiftOut.  No noticeable effect.
  • Clearing all pins by sending 0 to ShiftOut().  No effect even if performed every time.
  • Tried changing the values in the array... making B00000001 B10101010 for example.  There was no effect.  While Serial.Print showed the expected value, the lights continued to chase from 1-8.
  • Ran the instructables "Knight Rider" code with tweaked pin #'s.  It runs perfectly.
  • Putting ShiftOut in its own function (as anticipated, this looked more orderly, but made no difference.)


Early on, I suspected that the clock may only run up and was incapable of firing in reverse order.  That doesn't make any sense, however, as Instructables.com code fired as expected; it went up, then down.  At this point I'm a little baffled and am assuming that either there's something fundamentally wrong with my code or I'm trying to perform something that is not possible with the 74HC595.

Code below.  Any help is appreciated.
Code: [Select]

byte dataPin = 2;              // The Serial Data Pin to the Shift Register
byte latchPin = 3;             // The Latch Pin to the Shift Register
byte clockPin = 4;             // The Clock Pin to the Shift Register

//my array of patterns
byte patterns[8] = { B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000 };


void setup()
{
    pinMode(dataPin, OUTPUT);    // Configure Digital Pins
    
    pinMode(latchPin, OUTPUT);
    
    pinMode(clockPin, OUTPUT);  

    Serial.begin(9600);
}


void loop()
{

  //thisWorks();
  thisDoesNot();


}

// This works just fine
void thisWorks()
{

  do {
  for (int i=0;i<8;i++)
  {
 
    digitalWrite(latchPin,LOW);
     shiftOut(dataPin,clockPin,MSBFIRST,patterns[i]);  
     digitalWrite(latchPin, HIGH);
    delay(100);
  }
  } while (true);
}

// this does the same as above, despite the fact that it should go in reverse.
void thisDoesNot()
{

  do {
  for (int i=7;i>=0;i--)
  {
     Serial.print(i);
     Serial.print("=");
     Serial.println(patterns[i]);
     digitalWrite(latchPin,LOW);
     shiftOut(dataPin,clockPin,MSBFIRST,patterns[i]);  
     digitalWrite(latchPin, HIGH);
    delay(100);
  }
  } while (true);
}

bbureau12

Figured it out at last - a simple switch of clockpin and latchpin did the trick.  Makes loads of sense and explains it pretty handily.  Always the little things!

Go Up