0

我需要帮助。我做了一些研究,我对键盘扫描的一点了解是,当按下键盘按钮时,输入列的 ShiftIn 值应该返回零 (0)。我的 BIN 只返回 255(或 11111111)。我只需要在按下一个键时跟踪零值,然后扫描键矩阵以显示按下的键。我将不胜感激。我已经添加了我的代码和原理图。

在此处输入图像描述] 1

const int kbdRows = 4;
const int kbdCols = 4;
int LatchIn = 2; //165 pin1
int ClockPin = 3; // 595 pin11 & 165 pin2
int DataIn = 4; //165 pin9
int LatchOut = 5; // 595 pin12
int DataOut = 6; //595 pin14
int led = 7;
int PinState = 0;

char keys[kbdRows][kbdCols] = {
  { '1','2','3','4' },
  { '5','6','7','8' },
  { '9','0','A','B' },
  { 'C','D','E','F' }
};

byte KeyIsDown() {
  int row;
  int col;
  int rowBits;
  int colBits;
  rowBits = 0X10;
  for (row = 0; row < kbdRows; row++) {
    digitalWrite(ClockPin, LOW);
    digitalWrite(LatchOut, LOW);
    shiftOut(DataOut, ClockPin, LSBFIRST, rowBits);
    digitalWrite(LatchOut, HIGH);
    delay(5);
    digitalWrite(ClockPin, HIGH);
    digitalWrite(LatchIn, LOW);
    delay(5);
    digitalWrite(LatchIn, HIGH);
    colBits = shiftIn(DataIn, ClockPin, LSBFIRST);
    for (col = 0; col < kbdCols; col++) {
      if (colBits==0) {
        // not sure what condition to put here
        byte keypressed = keys[kbdRows][kbdCols]; here
        // I know this is the right stuff to return here
      }
      return colBits;
      colBits = colBits >> 1;
    }
    rowBits = rowBits << 1;
  }
}

void setup() {
  pinMode(ClockPin, OUTPUT);
   pinMode(DataOut, OUTPUT);
   pinMode(DataIn, INPUT_PULLUP);
   pinMode(LatchOut, OUTPUT);
   pinMode(LatchIn, OUTPUT);
  digitalWrite(LatchOut, HIGH);
  digitalWrite(LatchIn, HIGH);
  Serial.begin(9600);
  digitalWrite(led, HIGH);
}

void loop() {
  byte retColBit = KeyIsDown();
  Serial.print("ColBit: ");
  Serial.println(retColBit,BIN);
  delay(500);
  PinState = digitalRead(DataOut);
  Serial.print("DataOut: ");
  Serial.println(PinState,BIN);
  delay(500);
}
4

0 回答 0