V-USB HID mouse example with Arduino Nano === V-USB 是透過軟體的方式使 Atmel 的 AVR® 系列微處理器作為 USB 裝置工作。本範例是使用該函式庫內提供的滑鼠範例,成功的話,將電路接上電腦就會讓滑鼠轉圈圈囉。 ### Required 為了編譯原始碼,需要一些工具,雖然這些工具 Arduino IDE 內都有, 不過還是透過安裝套件省去設定 bin (執行檔)和 include (標頭檔)的麻煩。 ``` sudo apt install gcc-avr avr-libc avrdude make ``` ### Download 先至 [V-USB](https://www.obdev.at/products/vusb/index.html) 的網站下載程式碼或是直接使用指令下載: ```shell wget https://www.obdev.at/downloads/vusb/vusb-20121206.tar.gz tar xvzf vusb-20121206.tar.gz cd vusb-20121206 ``` ### 電路圖 這是依照範例程式的腳位接線的電路圖[^v_usb_mouse], 如果要修改接腳定義記得修改程式碼,詳見「[設定韌體腳位](#設定韌體腳位)」。  USB 的資料腳位使用的是較低的電壓 (3.3 V)[^low_volt] , 因此需要稽納二極體來使輸出的 TTL 降壓。 R1 與 R2 的電阻則是為了進行阻抗匹配[^resistor]的終端電阻 (termination resistors) ,USB 在高速模式(high-speed) 下訊號使用的是電流源,這時阻抗的匹配便顯得十分重要,但是在低速模式下就沒這麼重要了[^non-important],本人手邊沒有剛好的電阻,使用的是手邊有的零件 (33 ohm) 。 ### 設定韌體腳位 預設是使用 `PD2` 和 `PD4` 接腳分別作為作為 `Data+` 和 `Data-`, 如要修改接腳定義記得到原始碼的 `usbconfig.h` 當中修改: ```c++ #define USB_CFG_IOPORTNAME D #define USB_CFG_DMINUS_BIT 4 #define USB_CFG_DPLUS_BIT 2 ``` `USB_CFG_IOPORTNAME` 是選擇指定的埠口, `USB_CFG_DMINUS_BIT ` 則是設定 `Data-` 到埠口編號; `USB_CFG_DPLUS_BIT ` 則是設定 `Data+` 到埠口編號。 不過別忘了,範例程式碼的註解中有特別提及 `Data+` 必須連接至 MCU 的 `INT0`。 本人是使用 Arduino nano 測試,因此附上一張接腳對照圖:  ### Compile 記得先安裝必備的工具 ``` cd examples/hid-mouse/firmware make hex ``` ### Upload 就像平常上傳 Arduino 程式一樣,從開發板原本的 USB 接到電腦就可以了, 指令是參考網路上的文章[^command-upload], 請依照實際的環境調整參數。 ```shell avrdude -C/opt/arduino-1.8.5/hardware/tools/avr/etc/avrdude.conf -v -v -v -v -patmega328p -carduino -P/dev/ttyUSB0 -b57600 -D -Uflash:w:main.hex:i ``` `-C` 參數的 `avrdude.conf` 要根據實際 Arduino IDE 安裝的路徑去確認, `-P` 根據實際自己的 COM port 路徑設定, `-U` 內的檔案依照剛剛編譯出的 `hex` 檔路徑填寫(`-U flash:w:<put-hex-file-path-here>:i`)。 ### 運作結果 {%youtube 6iHsnILbZd4 %} [^v_usb_mouse]:Cooper Maa: V-USB hid-mouse (cooper Maa). Retrieved 2019-10-30, from http://coopermaa2nd.blogspot.com/2011/09/v-usb-hid-mouse.html [^low_volt]:Switching in USB Consumer Applications | Analog Devices. (n.d.). Retrieved 2019-10-30, from https://www.analog.com/en/analog-dialogue/articles/switching-in-usb-consumer-applications.html [^resistor]:關於USB匹配電阻的一些疑問和解答 - IT閱讀. (n.d.). Retrieved 2019-10-30, from https://www.itread01.com/content/1542633982.html [^non-important]:What's the purpose of 68ohm resistors on D+ and D- lines? - Objective Development Forums. (n.d.). Retrieved 2019-10-30, from https://forums.obdev.at/viewtopic.php?t=1394 [^command-upload]:Compile `.hex` files on Linux - Arduino Stack Exchange. (n.d.). Retrieved 2019-10-30, from https://arduino.stackexchange.com/questions/23209 ###### tags: `learning note` `2019-10-30`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up