连接到键盘的 Arduino 引脚 4 始终为高电平,导致键盘无法正常工作

Posted

技术标签:

【中文标题】连接到键盘的 Arduino 引脚 4 始终为高电平,导致键盘无法正常工作【英文标题】:Arduino Pin 4 connected to keypad alsways HIGH resulting in not working keypad 【发布时间】:2021-02-04 12:23:37 【问题描述】:

我正在尝试将keypad 集成到我的项目中,并希望首先在一个简单的程序中对其进行测试。 键盘连接到 Arduino Uno (ATmega328P),如下所示:

C1 -> 引脚 6

C2 -> 引脚 8

C3 -> 引脚 4

R1 -> 引脚 7

R2 -> 引脚 2

R3 -> 引脚 3

R4 -> 引脚 5

我为此键盘下载了一个模板程序并相应地调整了我的引脚配置。

代码如下:

#include <LCD5110_Graph.h> 
/* keyPadHiduino Example Code
   by: Jim Lindblom
   date: January 5, 2012
   license: MIT license. If you find this code useful, please
   feel free to use this code however you'd like, commercially 
   or otherwise. Just keep this license on there whatever you do.

   This code implements a 12-key USB keypad. You can type 0-9,
   * is the + sign and the # key is enter. I'm using SparkFun's
   12-button keypad, your pinouts may vary. Multi-touch is
   not supported.

   SparkFun Keypad Pinout:
   Rows and columns are connected as such:
   -------------
 R1  | 1 | 2 | 3 | - 7 (grün)
 R2  | 4 | 5 | 6 | - 2 (braun)
 R3  | 7 | 8 | 9 | - 3 (schwarz)
 R4  | * | 0 | # | - 5 (violett)
   -------------
       |C1 |C2 |C3
 6(blau) 8(gelb) 4(weiß)
*/
// Pins 1-7 of the keypad connected to the Arduino respectively: 
uint8_t keypadPins[7] = 2, 3, 4, 5, 6, 7, 8;
int keypadStatus;  // Used to monitor which buttons are pressed.
int timeout;  // timeout variable used in loop
LCD5110 LCD(9, 10, 11, 12, 13);
extern uint8_t SmallFont[]; //charactersize 6x8 pixels
extern uint8_t TinyFont[];  //charactersize 4x6 pixels
extern uint8_t MediumNumbers[];  //charactersize 12x16 pixels

void setup()

  LCD.InitLCD();
  LCD.setFont(TinyFont);
  
  for (uint8_t i=0; i<7; i++)
  
    pinMode(keypadPins[i], INPUT);  // Set all keypad pins as inputs 
    digitalWrite(keypadPins[i], HIGH);  // pull all keypad pins high
  
  
  keypadStatus = 0;
  Serial.begin(9600);


void loop()

  
  keypadStatus = getKeypadStatus();  // read which buttons are pressed
  if (keypadStatus != 0)  // If a button is pressed go into here
  
    Serial.println("A button was pressed");
    sendKeyPress(keypadStatus);  // send the button over USB
    timeout = 2000;  // top of the repeat delay
    while ((getKeypadStatus() == keypadStatus) && (--timeout))  // Decrement timeout and check if key is being held down
      delayMicroseconds(1);
    while (getKeypadStatus() == keypadStatus)  // while the same button is held down
    
      sendKeyPress(keypadStatus);  // continue to send the button over USB
      delay(50);  // 50ms repeat rate
    
  


/* sendKeyPress(int key): This function sends a single key over USB
   It requires an int, of which the 12 LSbs are used. Each bit in
   key represents a single button on the keypad.
   This function will only send a key press if a single button
   is being pressed */
void sendKeyPress(int key)

  switch(key)
  
    case 1:  // 0x001
      LCD.clrScr();
      LCD.print("1",CENTER,14);
      LCD.update();
      Serial.print('1');  // Sends a keyboard '1'
      break;
    case 2:  // 0x002
      LCD.clrScr();
      LCD.print("2",CENTER,14);
      LCD.update();
      Serial.print('2');
      break;
    case 4:  // 0x004
      LCD.clrScr();
      LCD.print("3",CENTER,14);
      LCD.update();
      Serial.print('3');
      break;
    case 8:  // 0x008
      LCD.clrScr();
      LCD.print("4",CENTER,14);
      LCD.update();
      Serial.print('4');
      break;
    case 16:  // 0x010
      LCD.clrScr();
      LCD.print("5",CENTER,14);
      LCD.update();
      Serial.print('5');
      break;
    case 32:  // 0x020
      LCD.clrScr();
      LCD.print("6",CENTER,14);
      LCD.update();
      Serial.print('6');
      break;
    case 64:  // 0x040
      LCD.clrScr();
      LCD.print("7",CENTER,14);
      LCD.update();
      Serial.print('7');
      break;
    case 128:  // 0x080
      LCD.clrScr();
      LCD.print("8",CENTER,14);
      LCD.update();
      Serial.print('8');
      break;
    case 256:  // 0x100
      LCD.clrScr();
      LCD.print("9",CENTER,14);
      LCD.update();
      Serial.print('9');
      break;
    case 512:  // 0x200
      LCD.clrScr();
      LCD.print("Star",CENTER,14);
      LCD.update();
      Serial.print('*');
      break;
    case 1024:  // 0x400
      LCD.clrScr();
      LCD.print("0",CENTER,14);
      LCD.update();
      Serial.print('0');  // Sends a keyboard '0'
      break;
      case 2048:  // 0x800
      LCD.clrScr();
      LCD.print("Raute",CENTER,14);
      LCD.update();
      Serial.print('#');  // Sends the 'ENTER' key
      break; 
  


/* getKeypadStatus(): This function returns an int that represents
the status of the 12-button keypad. Only the 12 LSb's of the return
value hold any significange. Each bit represents the status of a single
key on the button pad. '1' is bit 0, '2' is bit 1, '3' is bit 2, ..., 
'#' is bit 11.

This function doesn't work for multitouch.
*/
int getKeypadStatus()

  uint8_t rowPins[6] = keypadPins[5], keypadPins[0], keypadPins[1], keypadPins[2];  // R1 = KeypadPin[5], R2 = ...
  uint8_t columnPins[3] = keypadPins[4], keypadPins[6], keypadPins[2];  // C1 = KeypadPin[4], C2 = ...
  int keypadStatus = 0;  // this will be what's returned
  
  /* initialize all pins, inputs w/ pull-ups */  
   for (uint8_t i=0; i<7; i++)
  
    pinMode(keypadPins[i], INPUT);
    digitalWrite(keypadPins[i], HIGH);
  
  
  for (uint8_t row=0; row<4; row++)
    // initial for loop to check all 4 rows
    pinMode(rowPins[row], OUTPUT);  // set the row pin as an output
    digitalWrite(rowPins[row], LOW);  // pull the row pins low
    for (int col=0; col<3; col++)
      // embedded for loop to check all 3 columns of each row
      if (!digitalRead(columnPins[col]))
      
        keypadStatus |= 1 << ((row+1)*3 + (col+1) - 4);  // set the status bit of the keypad return value
        Serial.print("keypadStatus: ");Serial.println(keypadStatus);
      
    
    pinMode(rowPins[row], INPUT);  // reset the row pin as an input
    digitalWrite(rowPins[row], HIGH);  // pull the row pin high
  
  
  return keypadStatus;

为了检查按下的数字是否返回正确的值,我通过串行通信将其值打印到 arduino 控制台并将其显示在我的 LCD 上。

我的问题是,当我上传代码并且 µcontroller 开始运行时,我从我的键盘获得了很多输入而没有触摸任何东西,在 getKeypadStatus() 中计算的值是 2048”,它转换为“#”按钮。我尝试在计算时显示值 keypadStatus,结果我通过串行通信获得了大量的“2048”。

此外,我在 arduino 的引脚 4 处测量了 5V。我切换了一些电线以检查问题是否出在键盘或 arduino 上。切换电线时,我仍然在 arduino 的引脚 4 上测量了 5V,但打印的值(keypadStatus)仍然是“2048”。

键盘本身不会有故障,因为我测量了每个按钮的电阻并且工作正常。

有人有什么建议吗?

【问题讨论】:

拉电阻在哪里? 那“测量5V是什么意思”,你是用示波器还是电压表检查的? 我尝试使用 INPUT_PULLUP 它没有用,我使用了电压表 【参考方案1】:

最好使用 Mark Stanley 和 Alexander Brevig 编写的 Keypad 库。该库负责设置引脚并轮询不同的列和行。要安装键盘库,请转到 Sketch > Include Library > Manage Libraries 并搜索“keypad”。单击库,然后单击安装。 3x4小键盘的代码如下

#include <Keypad.h>

const byte ROWS = 4; 
const byte COLS = 3; 

char hexaKeys[ROWS][COLS] = 
  '1', '2', '3',
  '4', '5', '6',
  '7', '8', '9',
  '*', '0', '#'
;

byte rowPins[ROWS] = 7, 2, 3, 5; 
byte colPins[COLS] = 6, 8, 4; 

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

void setup()
  Serial.begin(9600);

  
void loop()
  char customKey = customKeypad.getKey();
  
  if (customKey)
    Serial.println(customKey);
  

【讨论】:

它并没有直接解决我的问题,但是这个库工作正常并且减少了代码量。谢谢分享:)

以上是关于连接到键盘的 Arduino 引脚 4 始终为高电平,导致键盘无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

怎样用Arduino和手机控制LED

怎样用Arduino和手机控制LED

ESP8266和Arduino接口

怎么在arduino串口监视器上实现输入一个字符一次某引脚为高电平,再输入一次变为低电平?

4x4薄膜键盘提供意外信号

Arduino 上的模拟读数返回错误值