微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决连接到键盘的 Arduino 引脚 4 始终为高电平,导致键盘无法工作

我正在尝试将 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",14);
      LCD.update();
      Serial.print('2');
      break;
    case 4:  // 0x004
      LCD.clrscr();
      LCD.print("3",14);
      LCD.update();
      Serial.print('3');
      break;
    case 8:  // 0x008
      LCD.clrscr();
      LCD.print("4",14);
      LCD.update();
      Serial.print('4');
      break;
    case 16:  // 0x010
      LCD.clrscr();
      LCD.print("5",14);
      LCD.update();
      Serial.print('5');
      break;
    case 32:  // 0x020
      LCD.clrscr();
      LCD.print("6",14);
      LCD.update();
      Serial.print('6');
      break;
    case 64:  // 0x040
      LCD.clrscr();
      LCD.print("7",14);
      LCD.update();
      Serial.print('7');
      break;
    case 128:  // 0x080
      LCD.clrscr();
      LCD.print("8",14);
      LCD.update();
      Serial.print('8');
      break;
    case 256:  // 0x100
      LCD.clrscr();
      LCD.print("9",14);
      LCD.update();
      Serial.print('9');
      break;
    case 512:  // 0x200
      LCD.clrscr();
      LCD.print("Star",14);
      LCD.update();
      Serial.print('*');
      break;
    case 1024:  // 0x400
      LCD.clrscr();
      LCD.print("0",14);
      LCD.update();
      Serial.print('0');  // Sends a keyboard '0'
      break;
      case 2048:  // 0x800
      LCD.clrscr();
      LCD.print("Raute",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”。

键盘本身不可能有问题,因为我测量了每个按钮的电阻并且工作正常。

有人有什么建议吗?

解决方法

最好使用 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);
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?