Controling 4 digit 7segment LED Display using 74HC595 shift register

JamesD picture JamesD · Sep 14, 2014 · Viewed 11.8k times · Source

I'm having troubles trying to use two 595 shift registers to output numbers on a 4 digit 7seg display.

I've gotten to the point of displaying numbers correctly, but I'm now having the issue that the output is flashing some garbage between the digits being displayed. How do I prevent this from happening?

I'm pretty sure the issue is that as I'm using bytes to send to the registers it is latching between bytes being displayed.

Here is my code

int latchPin = 5;
int clockPin = 6;
int dataPin = 4;

int i = 0;

int waitTime = 500;

// digits from the right
byte colDig[4] = 
{
    B00001000, // digit 1
    B00000100, // digit 2
    B00000010, // digit 3
    B00000001, // digit 4
};

const byte digit[10] =      //seven segment digits in bits
{
    B11000000, // 0
    B11111001, // 1
    B10100100, // 2
    B10110000, // 3
    B10011001, // 4
    B10010010, // 5
    B10000010, // 6
    B11111000, // 7
    B10000000, // 8
    B10011000, // 9
};
void setup()
{
    pinMode(latchPin, OUTPUT);
    pinMode(dataPin, OUTPUT);
    pinMode(clockPin, OUTPUT);
}

void loop()
{
    // step through each digit then increment 
    // the counter by one, until nine
    for(int j = 0;j<9;j++){
        updateShiftRegister(0, j);
        delay(waitTime);
        updateShiftRegister(1, j);
        delay(waitTime);
        updateShiftRegister(2, j);
        delay(waitTime);
        updateShiftRegister(3, j);
        delay(waitTime);
    }
}

void updateShiftRegister(int col, int num)
{
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, colDig[col]);
    shiftOut(dataPin, clockPin, MSBFIRST, digit[num]);
    digitalWrite(latchPin, HIGH);
}

Answer

JamesD picture JamesD · Sep 14, 2014

So it looks like I was sort of correct,

The shiftOut function sets the clock pin to low at the end of the function, effectively forcing a latch.

By slightly modifying the code on this page I was able to stop this and it works perfect now. http://arduino.cc/en/Tutorial/ShftOut23

// the heart of the program
void shiftItOut(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);
}

Simply commenting out that last digitalWrite fixed it all.