mega2560 USB第二季KEYBOARD HID

作者:邓帅哥——大人

不要崇拜哥,哥为人民服务,哥已不在社区,但社区的每一个角落都流传着哥的传说。

Now,镜头转入正题,mega2560 USB第二季KEYBOARD HID

首先,你必须有一片arduino主板,而且主板上必须有一片atmega8U2/atmega16U2/支持usb的芯片+mega主芯片(mega328,128,1280,2560.......),操作前请导入附件的USB库

step1:

将下列arduino代码通过arduion下载到arduino板子上,它的功能是,当你按键NumLock三次的时候,它会模拟按键输入一排信息,改变Keyboard.print内的操作即可以改变你想要的按键。ARDUINO 代码复制打印

/* Arduino USB Business Card 1. Upload this program to Arduino 2. Upgrade atmega8u2 with firmware/Arduino-keyboard-0.3.hex3. Re-plug Arduino to usb port, and pressed Num Lock button 3 times in 2 seconds Arduino will type your contact text information into the text editor/#include <USBKeyboard.h>const uint8_t KEYBOARD_LED = LED_NUMLOCK; // Num Lock LEDconst int ledPin =13;       uint8_t lastState = 0, currentState = 0;void setup() {pinMode(ledPin, OUTPUT);Keyboard.init();}// return true if KEYBOARD_LED (Num Lock button) is pressedboolean buttonPressed(){uint8_t ledStatus;ledStatus = Keyboard.readLedStatus();// Arduino Led pin as an indicator  // If Num Lock led is on, the led indicator will be onif (ledStatus & KEYBOARD_LED) {  digitalWrite(ledPin, HIGH);  } else {  digitalWrite(ledPin, LOW);}  lastState = currentState;currentState = ledStatus & KEYBOARD_LED;return (currentState != lastState); }void sendContactInfo(){Keyboard.print("dengshuaige");Keyboard.sendKeyStroke(KEY_ENTER);Keyboard.print("Email: [email]mkandy44@gmail.com[/email]");Keyboard.sendKeyStroke(KEY_ENTER);Keyboard.print("dengshuaige.com");Keyboard.sendKeyStroke(KEY_ENTER);}void loop() {int counter;delay(100);// Check whether KEYBOARD_LED is pressed 3 times in 2 secondsif (buttonPressed()) {  counter = 1;  for (int i = 0; i < 20; i++) {  delay(100);  if (buttonPressed()) counter = counter + 1;    // Send my information to computer  if (counter >= 3) {  sendContactInfo();  break;  }  }    counter = 0;      // reset counter}}
/ Arduino USB Business Card 

  1. Upload this program to Arduino
  2. Upgrade atmega8u2 with firmware/Arduino-keyboard-0.3.hex
  3. Re-plug Arduino to usb port, and pressed Num Lock button 3 times in 2 seconds

    Arduino will type your contact text information into the text editor

    */

    #include <USBKeyboard.h>



    const uint8_t KEYBOARD_LED = LED_NUMLOCK; // Num Lock LED

    const int ledPin =13;

    uint8_t lastState = 0, currentState = 0;



    void setup()

    {

    pinMode(ledPin, OUTPUT);

    Keyboard.init();

    }



    // return true if KEYBOARD_LED (Num Lock button) is pressed

    boolean buttonPressed()

    {

    uint8_t ledStatus;



    ledStatus = Keyboard.readLedStatus();



    // Arduino Led pin as an indicator

    // If Num Lock led is on, the led indicator will be on

    if (ledStatus & KEYBOARD_LED) {

    digitalWrite(ledPin, HIGH);

    } else {

    digitalWrite(ledPin, LOW);

    }



    lastState = currentState;

    currentState = ledStatus & KEYBOARD_LED;



    return (currentState != lastState);

    }



    void sendContactInfo()

    {

    Keyboard.print("dengshuaige");

    Keyboard.sendKeyStroke(KEY_ENTER);

    Keyboard.print("Email: [email]mkandy44@gmail.com[/email]");

    Keyboard.sendKeyStroke(KEY_ENTER);

    Keyboard.print("dengshuaige.com");

    Keyboard.sendKeyStroke(KEY_ENTER);

    }



    void loop()

    {

    int counter;



    delay(100);



    // Check whether KEYBOARD_LED is pressed 3 times in 2 seconds

    if (buttonPressed()) {

    counter = 1;

    for (int i = 0; i < 20; i++) {

    delay(100);

    if (buttonPressed()) counter = counter + 1;



    // Send my information to computer

    if (counter >= 3) {

    sendContactInfo();

    break;

    }

    }

    counter = 0; // reset counter

    }

    }
下载到mega2560后,板子还是不能工作的,请继续往下看,
step2:将Arduino-keyboard-0.3-mega2560.hex通过烧录器烧到mega8U2上面

未命名.jpg

2012-6-14 09:38 上传
(23.93 KB)





哥用的是USBasp,淘宝15——20元一个。

aspflash.jpg

2012-6-14 09:41 上传
(130.85 KB)



OK,将mega8u2烧好之后,

连接arduino的USB线,你可以看见在键盘下方多了一个HID keyboard device(不需要安装任何驱动),哈哈,这时,你可以用你自已做的键盘了。

HIDKeyboard.jpg

2012-6-14 09:50 上传
(36.83 KB)





测试一下,新建一个文本,打开,光标输入到文本,按三次NumLock键,得到如下:

test.jpg

2012-6-14 09:52 上传
(20.47 KB)




libraries.rar

(335.03 KB, 下载次数: 184)

2012-8-31 21:34 上传
点击文件名下载附件

Arduino-keyboard-0.3-mega2560.rar

(4.23 KB, 下载次数: 139)

2012-8-31 21:34 上传
点击文件名下载附件



USB第三季预告(AVR篇)用mega8单独打造USB键盘...掌声~~~~
via - 极客工坊

标签: Arduino教程