您好!
我很久以前问过这个问题,并以某种方式解决了问题,但我失去了解决方案,因为窗户问题,我回来了相同的问题。
我正在使用从 GitHub 下载的给定代码。 并在代码中添加链接。
它对我工作正常、但最后6个键、它显示双输出、如串行监视器数据中所示。
/* * TCA8418 Driver * This code is adapted from the phishman's driver for arduino, github.com/.../TCA8418 * The difference is that it is written to be easily ported to another microcontroller * Author: Henry Troutman */ /* * Keyboard PCB Hookup Guide: * 1 -> 3.3V * 2 -> GND * 3 -> SDA * 4 -> SCL * 5 -> INT_PIN * 6 -> RTC_INT (ds3231) */ // Arduino I2C Library #include <Wire.h> // keyboard Constants #define KEYBOARD_ADDRESS 0x34 #define KEYBOARD_INT_PIN 1 #define KEYBOARD_STATE_REGISTER 0x04 #define KEYBOARD_FLAG_REGISTER 0x02 #define KEYBOARD_KEY_DOWN 0x80 #define KEYBOARD_KEY_MASK 0x7F // configuration commands (configures the 3x9 matrix as inputs with pullups, interrupts ... // For more information look at the code in the repo linked above const uint8_t KEYBOARD_CONFIG_COMMANDS[] = {0x1D,0x07,0x1E,0xFF,0x1F,0x01,0x01,0xB9,0x02,0x09}; // Define our functions before use void keyboard_configure(void); void keyboard_clearFlag(void); uint8_t keyboard_getState(void); // keyInt allows our event to ultimately be handled in the loop volatile bool keyInt = false; void setup() { Serial.begin(9600); Wire.begin(); keyboard_configure(); pinMode(KEYBOARD_INT_PIN,INPUT); // There is an external pullup on the pin // Set up a falling edge pin interrupt on pin 1 attachInterrupt(digitalPinToInterrupt(KEYBOARD_INT_PIN), keyboard_ISR, FALLING); } void loop() { if(keyInt) { uint8_t key_code = keyboard_getState(); // Get first keycode from FIFO if(key_code & KEYBOARD_KEY_DOWN) { // This is where we can write to a screen or handle control keys Serial.print(key_code&KEYBOARD_KEY_MASK); // print the keycode Serial.print(","); } keyInt = false; keyboard_clearFlag(); delay(100); } } void keyboard_configure(void){ for(uint8_t i = 0;i < 9;i+=2){ Wire.beginTransmission(KEYBOARD_ADDRESS); Wire.write(KEYBOARD_CONFIG_COMMANDS[i]); Wire.write(KEYBOARD_CONFIG_COMMANDS[i+1]); Wire.endTransmission(); } } uint8_t keyboard_getState(void){ uint8_t key; Wire.beginTransmission(KEYBOARD_ADDRESS); Wire.write(KEYBOARD_STATE_REGISTER); Wire.endTransmission(); Wire.requestFrom(KEYBOARD_ADDRESS, 1); // request 1 bytes from slave device 0x34 while (Wire.available()) { // slave may send less than requested key = Wire.read(); // receive a byte as character } return key; } void keyboard_clearFlag(void){ Wire.beginTransmission(KEYBOARD_ADDRESS); Wire.write(KEYBOARD_FLAG_REGISTER); Wire.write(0x0F); Wire.endTransmission(); } void keyboard_ISR(void){ keyInt = true; } /* I2C Command Summary Setup board (3x9 keypad) write to Register, value 0x1D, 0x07 0x1E, 0xFF 0x1F, 0x01 0x01, 0xB9 0x02, 0x09 To get keys read 1 byte from 0x04 To clear interrupt flag write to register,value 0x02, 0x0F */
如果您能帮助我、我有两个请求。
- 为 SW1到 SW9分配与 SW10到 SW18顺序相同的数字。 例如、SW1应为1、而不是 SW1为21。 SW10至 SW18的序列是完美的。
- 从 SW19到 SW27分配一个唯一数字、而不是双数字
串行监视器数据
21至29对于 SW1至 SW9按下
11至19对于 SW11至 SW18、按下
在给我一个唯一的 ID 之前、我是可以接受的。
当我按下 SW19时、问题就开始了、然后它会生成一对输出、即1、2
SW20 2、3
SW21 3、4等
我需要知道最后9个开关的操作错误。
我使用3X9键盘矩阵。