USB HID host (USB keyboard interfacing) with PIC32 code and schematic
This post will provide a tutorial of how to interface a USB keyboard with PIC32 microcontroller as USB HID host. I called this application as Mini Computer based on PIC32 (according to definition of computer). In this example I used PIC32 USB host application on my custom designed board. The key pressed in keyboard is shown on LCD 20x4. I implemented almost every key except the functions keys and document editing keys. You may download code and schematic from the download section at the bottom of this page. I am using custom designed PIC32 I/O board in this particular example for simulating code practically. The code is written in C language using XC32 compiler and MPLAB X IDE.
In this post I assumed that you must know following if not then please read them before proceeding further:
4. USB HID Host.
As mentioned above you may download code and schematic from the download section at the bottom of this page. I am using MPLAB X IDE, XC32 Compiler. This code is written in C Language and will work on all PIC32 (32bit microcontroller by Microchip) by slightly changing the code according to their features.
I have an external high speed crystal of 8MHz. My custom board have a USB host connector. To be honest I actually understand an example of USB HID host keyboard from microchip libraries and changed it according to my requirement. Being an industrial engineer I strongly recommend that if libraries are available to you. You should use them, changed them according to your application. DONOT try to write them by your own. I didn’t mean to discourage you but logically speaking this is 10,000 plus lines of code. It would take at least 6+ months to write from scratch. Then may be it takes another 6 months to fix bugs. And in a year we have so much advancements in this field to learn. So start from libraries and improve them according to your application needs but with proper understanding. There is another reason why I wrote this is to make you understand how to use APIs (Application programming interface).
As this microcontroller is not available for simulation in Proteus, therefore I go for practical simulation. I am using PIC32MX460F256L in this particular example. In this PIC32 I/O board I have placed a 16 pin connector at one side of this board to interface LCD which is directly one to one connected to LCD as shown in figure 1.
|Figure 1. USB HID host keyboard.|
As mentioned earlier in this post the code is written in C language using MPLAB X IDE and XC32 compiler and can be downloaded from the downloads section at the bottom of this page. In the following figure 2. We have a main C function in which I initialize the USB layers as a host by calling a function named “USBInitialize(0)”. And in the while loop we have a state machine. You need to download this HID host example for understanding this, not all the things are explained in details here. Remember this is not an ordinary code you need to be strong C programmer to understand this.
|Figure 2. main C file USB HID host keyboard.|
In code line no. 164 the function named “USBHostHID_ApiDeviceDetect()” detects the device is present or not this returns true if it is an HID keyboard device. So state machine changes here to device connected state. Actually according to this code we have two functions named “App_ProcessInputReport()” and “App_PrepareOutputReport()”. The first function is used to get HID data from keyboard in HID Hex value second one is used to send data to keyboard if the numlock or capslock key is pressed to indicate the light on keyboard. App_ProcessInputReport() has actually the values of HID data which is then passed to another function named “LCD_Display_Routine()” which actually used to display the HID data to on LCD. You may get HID codes for keys on keyboard (like asci codes) just google USB HID keyboard scan codes you will get a table of all hid keyboard codes. In this function I implemented some of the most common keys such as space to generate space on LCD when pressed, on pressing escape key from keyboard this USB host application clears the screen likewise when we press the backspace it clears the last pressed key. As we have 20x4 LCD so I also implement carriage return key so when Enter button is pressed we have next line. You may implements all keys here like I didn’t implement function keys etc.
|Figure 2. Display characters on LCD (USB HID host keyboard).|
The schematic with code is also available for download, in the download section. In this schematic some of the basic connections recommended for running this code are shown in the following figure 3.
|Figure 3. Schematic USB HID host keyboard.|
For having a better idea of how it works here is a video.
Downloads: if you learned something please consider donating so that I will continue producing such content for young engineers thank you.
You can download Code (MPLAB X and XC32 compiler Schematic is also given). Click here
For all topics Click here:
That’s all for this post hope you will learn please comment if you have any questions for upcoming posts please subscribe or follow.
All the text and graphics contained on this blog belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission.
© Copyright 2013 IbrahimLabs. All rights reserved.