2

我在尝试使用两个 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);
}
4

1 回答 1

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 即可解决所有问题。

于 2014-09-14T04:37:47.903 回答