leonardo加矩阵键盘

    xiaoxiao2021-03-25  327

    leonardo采用了34U4,相比于328p来说将usb整合进入了mcu中,单价也略高

     

     

     

    共有这几个函数

    Keyboard.print();

    Keyboard.println();

    Keyboard.release();

    Keyboard.releaseAll();

    Keyboard.begin();

    Keyboard.end();

     

    我们可以通过使用矩阵键盘函数和物理矩阵键盘输入字符并识别,然后使用函数Keyboard.print()来反映到PC上

     

    例如:Keyboard.print(“s”);

    Keyboard.print(steel);//这里的steel是一个已经被定义的值

     

    除了abc012这类的单个字符还有些其他的按键:

     

    按键名称

    对应十六进制

    对应的十进制

    KEY_LEFT_CTRL

    0x80

    128

    KEY_LEFT_SHIFT

    0x81

    129

    KEY_LEFT_ALT

    0x82

    130

    KEY_LEFT_GUI

    0x83

    131

    KEY_RIGHT_CTRL

    0x84

    132

    KEY_RIGHT_SHIFT

    0x85

    133

    KEY_RIGHT_ALT

    0x86

    134

    KEY_RIGHT_GUI

    0x87

    135

    KEY_UP_ARROW

    0xDA

    218

    KEY_DOWN_ARROW

    0xD9

    217

    KEY_LEFT_ARROW

    0xD8

    216

    KEY_RIGHT_ARROW

    0xD7

    215

    KEY_BACKSPACE

    0xB2

    178

    KEY_TAB

    0xB3

    179

    KEY_RETURN

    0xB0

    176

    KEY_ESC

    0xB1

    177

    KEY_INSERT

    0xD1

    209

    KEY_DELETE

    0xD4

    212

    KEY_PAGE_UP

    0xD3

    211

    KEY_PAGE_DOWN

    0xD6

    214

    KEY_HOME

    0xD2

    210

    KEY_END

    0xD5

    213

    KEY_CAPS_LOCK

    0xC1

    193

    KEY_F1

    0xC2

    194

    KEY_F2

    0xC3

    195

    KEY_F3

    0xC4

    196

    KEY_F4

    0xC5

    197

    KEY_F5

    0xC6

    198

    KEY_F6

    0xC7

    199

    KEY_F7

    0xC8

    200

    KEY_F8

    0xC9

    201

    KEY_F9

    0xCA

    202

    KEY_F10

    0xCB

    203

    KEY_F11

    0xCC

    204

    KEY_F12

    0xCD

    205

     

    更多的按键对应数值请查看ASCII码表: http://www.asciitable.com/ 

     

     

    这些按键的用法

    例子:

    const char ctrlKey = KEY_LEFT_GUI;

    void setup()

    {

    Keyboard.print(ctrlKey);

    }

     

    这样就如同按下了左ctrl按键

     

     

    如果要组合键比如说ctrl+C则

    Keyboard.press(ctrlKey);

    Keyboard.press(‘C’);

    delay(10);

    Keyboard.releaseAll();//映射press函数的所有按键到PC

     

     

     

     

     

     

    示例代码:

    //基于arduino 莱昂纳多

    //the die ver is 1.6.7

    //this suite include a serial keyboard

     

    #include "Keypad.h"

    #include "Keyboard.h"

    int counter = 1;

    const byte ROWS = 6; //four rows

    const byte COLS = 4; //four columns

    //define the cymbols on the buttons of the keypads

    char hexaKeys[ROWS][COLS] = {

      {'1', '2', '3', 'A'},

      {'4', '5', '6', 'B'},

      {'7', '8', '9', 'C'},

      {'H', '0', 'L', 'L'},

      {'O', 'C', 'K', 'E'},

      {'*', '0', 13, ' '}//13 is enter button

    };

    const char String1[] = {"sh"};//演示用

    byte rowPins[ROWS] = {4, 5, 6, 7, 8, 9}; //connect to the row pinouts of the keypad

    byte colPins[COLS] = {10, 16, 14, 15}; //connect to the column pinouts of the keypad

     

    //initialize an instance of class NewKeypad

    Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);//矩阵实体键盘的初始化函数

     

    void setup() {

      Serial.begin(9600);

    }

     

    void loop() {

      char customKey = customKeypad.getKey();

      int k, i, j;

      if (customKey) {

        Serial.print(customKey);

        Keyboard.print(customKey);

        if (customKey ==  ' ')

        { for (k = 0; k < 15; k++)

          {

          

            for (j = 0; j < 5; j++)

            {

              Keyboard.println(String1);

              delay(10);

              for(i=0;i

                Keyboard.println(".");

              Keyboard.println(counter);

              counter ++;

              delay(10);

              if (counter == 6)

                counter = 1;

     

     

            }

          }

     

        }

    if(customKey =='*')

     Keyboard.println(String2);

       

      }

    }

     

     

     

    关于机械键盘部分:

     

    这里使用一块切割下的PCB和和黑轴以及KBC的白色按键

     

    利用杜邦线直接连接到Arduino 莱昂纳多pro micro 上

     

    基本电路就是矩阵键盘的常用电路,因为十分常见就不附带贴图

     

     有兴趣可以尝试一下n线达到nxn键盘的扫描电路,这样利用能达到最多18x18个按键

     

     

     

     

     

    转载请注明原文地址: https://ju.6miu.com/read-441.html

    最新回复(0)