1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| #include <Wire.h> #include <LiquidCrystal\_I2C.h>//点击这里会自动打开管理库页面: http://librarymanager/All#LiquidCrystal\_I2C #include <Key.h> #include <Keypad.h> #include <Keypad\_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);//Address PCF8574 :A0 A1 A2-->VCC #define I2CADDR 0x26 // Set the Address of the PCF8574 :A1 A2-->VCC;A0-->GND
const byte ROWS = 4; // Set the number of Rows const byte COLS = 4; // Set the number of Columns
// 4×4矩阵键盘 char keys [ROWS] [COLS] = { {'7', '8', '9', 'C'}, {'4', '5', '6', 'B'}, {'1', '2', '3', 'A'}, {'\*', '0', '#', 'D'} };
// define active Pin (4x4) byte rowPins [ROWS] = {0, 1, 2, 3}; // Connect to Keyboard Row Pin byte colPins [COLS] = {4, 5, 6, 7}; // Connect to Pin column of keypad.
// makeKeymap (keys): Define Keymap // rowPins:Set Pin to Keyboard Row // colPins: Set Pin Column of Keypad // ROWS: Set Number of Rows. // COLS: Set the number of Columns // I2CADDR: Set the Address for i2C // PCF8574: Set the number IC Keypad_I2C keypad (makeKeymap (keys), rowPins, colPins, ROWS, COLS, I2CADDR, PCF8574);
//void LCD\_display(void); void setup () { lcd.init(); lcd.backlight(); Wire .begin (); // Call the connection Wire keypad.begin (makeKeymap (keys)); // Call the connection Serial.begin (9600); lcd.setCursor(2,0); lcd.print("Key BUTTON");
} void loop () { char key = keypad.getKey (); // 获取按键 if (key) { // Serial.println (key); // 串口输出 lcd.setCursor(0,1);//显示位置 lcd.print(key); } }
|