七段显示使用595

时间:2017-11-21 04:23:05

标签: c arduino

使用移位寄存器和按钮编码七段显示器时遇到一些问题。目的是在按下按钮时显示屏计数。 0-9然后; A,B,C,D,E和F.我确实设法让这个工作起初但是任务已经改变,所以不允许使用“shiftOut”命令。

通过我调整后的代码,我的七段显示器现在似乎根本不起作用。我试图做很多自己的故障排除(即研究去抖动,数组和位敲击)。任何帮助,将不胜感激。谢谢。 PS:这是我的第一个问题,如果格式错误就很抱歉。

/* Pins to connect to common-cathode LED display via a 74HC595:
 DP-7, A-6, B-5, C-4, D-3, E-2, F-1, G-15
 */

const byte ledCharSet[128] = {
  // 00-0F: Hex digits
  B10000001, B11001111, B10010010, B10000110,   // 0123
  B11001100, B10100100, B10100000, B10001111,   // 4567
  B10000000, B10000100,                         // 89
  B10001000, B11100000, B10110001, B11000010,   // AbCd
  B10110000, B10111000,                         // EF
};

//// Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 8;
//// Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
//// Pin connected to Data in (DS) of 74HC595
const int dataPin  = 11;
//// Pin connected to display's common annode
const int faderPin = 10;
int _B = 7;
bool b;
int count=0;
int value;
long time = 0;
long debounce = 500;

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin,  OUTPUT);
  pinMode(_B, INPUT_PULLUP);
  b = true;
  toggleLatch();
  toggleCLK();
  Serial.begin(9600);
}

void toggleLatch() {
    digitalWrite(latchPin, HIGH); digitalWrite(latchPin, OUTPUT);
}

void toggleCLK() {
  digitalWrite(clockPin, HIGH); digitalWrite(clockPin, LOW);
}

void loop() {
  unsigned char count; // debounce counter
  if (b != digitalRead(_B)) {
    for(count = 0; count<20 && b != digitalRead(_B); count++);
    //register the change if we have confirmed it 20 consecutive times
    b=((count>=20 && b!=digitalRead(_B))?!b:b);
    if(!b) {
      count--;
    } // button is pushed
  }
  toggleLatch();

  void shiftBit(bool dataPin) { digitalWrite(ledCharSet,dataPin); toggleCLK(); }

  void shiftByteMSF(unsigned char b) {
    unsigned char b = ledCharSet[count];
    unsigned char m; // use m to select the bits in b one at a time
    m = 1;
    for (m = B10000000; m>0; m <<= 1) {
      if (b & m) {
        digitalWrite(dataPin, HIGH);
      } else {
        digitalWrite(dataPin, LOW);
      }
    }
    toggleLatch();
  }
  if (count==16) {
    count=0; //reset
  }
}

这是我使用的第一个代码,运行正常。

/* Pins to connect to common-cathode LED display via a 74HC595:
 DP-7, A-6, B-5, C-4, D-3, E-2, F-1, G-15 (shiftOut using MSBFIRST)
 */

const byte ledCharSet[128] = {
  // 00-0F: Hex digits
  B10000001, B11001111, B10010010, B10000110,   // 0123
  B11001100, B10100100, B10100000, B10001111,   // 4567
  B10000000, B10000100,                         // 89
  B10001000, B11100000, B10110001, B11000010,   // AbCd
  B10110000, B10111000,                         // EF
};

//// Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 8;
//// Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
//// Pin connected to Data in (DS) of 74HC595
const int dataPin  = 11;
//// Pin connected to display's common annode
const int faderPin = 10;
int _B = 7;
int reading;
int previous = LOW;
int counter=0;
long time = 0;
long debounce = 500;

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin,  OUTPUT);
  pinMode(_B, INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {
  reading = digitalRead(_B);
  Serial.print(counter);
  if (reading == HIGH && previous == LOW && millis() - time > debounce) {
    counter++;
    time = millis();    
  }
  previous = reading;
  byte bitsToSend = ledCharSet[counter];
  bitsToSend = bitsToSend ^ B11111111;
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
  digitalWrite(latchPin, HIGH);
  digitalWrite(11, 255);
  digitalWrite(11, 0);
  if (counter==16) {
    counter=0; //reset count
  }
}

0 个答案:

没有答案