使用74HC595移位寄存器控制4位7段LED显示

时间:2014-09-14 02:04:03

标签: arduino shift-register seven-segment-display

我在尝试使用两个595移位寄存器在4位7seg显示屏上输出数字时遇到了麻烦。

我已经到了正确显示数字的地步,但我现在遇到的问题是输出正在显示的数字之间闪烁一些垃圾。我该如何防止这种情况发生?

我非常确定问题在于,因为我使用字节发送到寄存器,所以它在显示的字节之间进行锁存。

这是我的代码

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);
}

1 个答案:

答案 0 :(得分:1)

所以看起来我有点正确,

shiftOut功能在功能结束时将时钟引脚设置为低电平,有效地强制锁存。

通过略微修改此页面上的代码,我能够阻止它,现在它完美无缺。 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);
}

简单地评论说最后一个digitalWrite修复了它。