带有I / O移位寄存器的Arduino-4X4矩阵键盘

时间:2017-10-01 09:55:45

标签: arduino arduino-uno shift-register

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

enter image description here] 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);
}

0 个答案:

没有答案