Monday, March 23, 2015

PIC18f USB-HID

I always want to try the USB communication but USB is a notorious protocol. I read a lot about USB from online resources like waitingforfriday, engscope, wikipedia. I was confused.

And finally the author from engscope says that you dont need to understand USB software stacks to implement USB, so I decided to give it a try. The simplest way is to run existing projects offered by Microchip.

First, download the Microchip Libraries for Applications (MLA) from here and install it.
Just install it in the default location.

Also, I have MPLAB IDE v8.92 and c18 preinstalled, these can be downloaded from Microchip software achieve. Took me some times to find it.

For the hardware part, I use the development kit from cytron and the PIC I used is PIC18f4553.

In order to use the USB port, two pins (USB, SEL) inside the red circle need to be short.
Also I use the switches and leds on board to run the MLA demo projects.
1. SW1   - RB0
2. SW2   - RB1
3. LED1 - RB6
4. LED2 - RB7
We will use these in the source code initialization later.
Okay, the preparation is done.

--------------------------------------------------------------------------------------------------------------------------
The USB also have a few protocols, HID and CRC are the most commonly used.
HID - dont need driver, slower transfer rate.
CDC - need driver, act as serial, faster transfer rate.
There is a discussion "HID vs CDC"at Microchip forum.

Since HID is the simplest (cause no driver needed), so I decided to try the "Device - HID - Custom Demos" of MLA.
It can be found in "C:\microchip_solutions_v2013-06-15\USB\Device - HID - Custom Demos".

Now to go the "Firmware" in "Device - HID - Custom Demos" folder, you can see many Microchip MPLAB.Project files which are made for Microchip development kits.

I use the "USB Device - HID - Simple Custom Demo - C18 - PICDEM FSUSB" because it is the closet resemble of cytron SK40C. Need some modifications on the code later.
After you open the file, you should see something like this at the workspace.
Usually I would create a new project and copy the files to another folder to avoid the modification of original files.

Also I use pic18f4553, but this project is for pic18f4550. So it is more convenient to create a new project with pic18f4553 using project wizard.

The hard part of creating a new project "pic18f4553 usb hid" is to find all the header files and source files needed.

So the easiest way is copying all related files and folders into your project location.
From "C:\microchip_solutions_v2013-06-15\USB\Device - HID - Custom Demos\Firmware", 
copy out -
main, HardwareProfile, usb_config, and usb_descriptors.

Goto "C:\microchip_solutions_v2013-06-15\Microchip\Include"
copy out -
Compiler, GenericTypeDefs. And the entire folder "USB".

After that, you project folder should contain all these.

Finally, add the header files,  source files, and subfolder according to the second picture shown.
Phew, one of the crucial parts is done.

--------------------------------------------------------------------------------------------------------------------------

Modifying the source code.
First, go to github and download the "HardwareProfile - PIC SK40C".
Open "HardwareProfile.h" and modify the section shown at picture below:

or just copy the following lines and paste it.
        #if (defined(__18F4550) || defined(__18F4553))
            #include "HardwareProfile - PIC SK40C.h"

Some desciptions on the SK40C is shown at picture below

it is modified according to onboard leds and switches.

Last is the linker files.
If you are planning to use the pic18f4550, dont need to modify the linker file.
For the pic18f4553 user, open the linker file and modified the below section.


--------------------------------------------------------------------------------------------------------------------------

Testing part:
After you burn the program to pic, Led1 and Led2 will be blinking alternative when you plug it into computer.

Open the "GenericHIDSimpleDemo" at "C:\microchip_solutions_v2013-06-15\USB\Device - HID - Custom Demos".

Click "Connect". Clicks "Toggle LED(s)", the blinking will stop.
Press "Toggle LED(s)" again, both leds will on.
Now, press the "SW1" on SK40C, in the same time, press "Get Pushbutton State", you will get "State: Pressed".
Let go the "SW1", press "Get Pushbutton State" again, you will get "State: Not Present".

Implementation of other work is easy, without theory and understanding of USB, you can get it to run.
But modifying it for your own use is difficult.



3 comments: