Click here to Skip to main content
15,883,896 members
Articles / Desktop Programming / Win32
Article

Microchip PIC 16F1455 USB Stack

Rate me:
Please Sign up or sign in to vote.
4.85/5 (27 votes)
20 Oct 2014CPOL5 min read 121.2K   7K   44   64
This article is about a HID USB Stack for Microchip PIC 16F1455 and the way of communicating with it on Windows Platform.

Introduction

This article is about a HID USB Stack for Microchip PIC 16F1455 and the way of communicating with it on Windows Platform.

Background

This is an expansion to my first Article: C# USB HID Interface with slight improvement on both the USB Stack and C# USB HID Device Classes.

The main inspiration for this was this post on Codewrite: USB interface using PIC16F1455 which makes entire problem of USB-ing with 16F1455 look easy, However it is based on a stack provided by Microchip which as we know is not free, it is tedious to read and takes by far too much memory (almost half of device).

This stack is free to use, as I wrote it mainly for educational purpose and it is by no means complete. It contains only whatever is necessary for communication as per examples further down in this article, and a lot of call-backs and other unnecessary bumf have been removed to improve the readability of the code for those, who are willing to learn about USB.

The major improvement is made to the Stack where entire functionality of the management of USB has been moved to work via Interrupts, leaving the actual program space to the programmer, without having to worry about the USB. In other words, you just do what you need to with your device as USB has been taken care of in the background.

This article covers the following:

  1. Creation of an USB HID Device based on PIC 16f1455
  2. Connecting to the device from windows via C#
  3. Sending Commands to the device
  4. Parsing those commands by the device and acting on them
  5. Sending Status or status changes by the device back to the host
  6. Parsing those statuses by the host and reflecting them on form in C#

Essentially we will create a device, a program in c# under visual studio and get those talking to each other.

This stack has been written under MPLAB-X 2.10 for Compiler XC8 v1.30 which was the latest at the time.

Without any further Ado, Download the following files

  1. 16F1455GenericHid.zip         -> This is the MPLABX Project that you can build yourself
  2. 16F1455GenericHidHex.zip   -> In case the build of above fails for some reason this is a built Hex
  3. USB16F1455HidTest.zip        -> This is a Visual Studio Project for the Windows Side.

Using the code

First we will need a device to work with so I will start with the schematic:

Schematic

And the built prototype:

Built Prototype

 

Apparently there should be a resistor between RA3 and VCC for programming but I have discovered that with PICKIT3 it actually works better without it.

Once you have it made it is time to build our stack and program it to the chip.

This is when I would like to point out that this Stack takes very little resources from the chip, compared to what Microchip has to offer:

Memory 8

When I first built it with microchip, both memory consumption indicators were at almost half...

 

After programming connect the USB cable to the computer.

Here are some screenshots of what happened on mine:

Device Discovery 1

Device discovery 2

Device discovery 3

After having a look into the properties under device manager I have discovered this:

Hid Windows Properties

At this stage I knew we are ready to start talking to it.

Now on the host side fire up visual studio (express version is enough), load the project and build it. It only requires .net 2.0 so it is safe to assume that the framework is already installed on your machine, and it will build just fine. Once it is built, start it.

Visual Studio Test 8

The two indicators on top left of the form show current status of the button and the led. If you press the button the status gets updated pretty much immediately. The "Get Status" button sends the first command to the device for it to resend its current status, like a manual refresh. The second button on the form toggles the led on or off depending on the current state. The "Clear" button clears the raw data boxes.

One thing I would like to point is that none of the buttons on this form have any control over the indicators. Those are driven purely by the PIC.

When you click the first button a "Get Status" command message is sent to the chip and the chip executes that command by reporting its current status back to the host, by sending a status message. Host upon reception of the status message updates both indicators on top left accordingly to the third and fourth byte in the received message.

The second button sends the "Toggle Led" command message to the PIC, which processes it in a similar way to the above with the difference of changing the state of the led before reporting status back to the host.

At this stage we have just went through a full cycle of communication between our device and the computer.

This is just a Generic device and thanks to HID this chip can become pretty much whatever we want like a Keyboard or Mouse. I have written the stack with this in mind, so it is time to demonstrate how to build Hybrid devices like a HID, Mouse and Keyboard on a single chip with proper implementation on both sides.

Continue to Part 2

Points of Interest

A small Tip...

As we know in today's environment a single 8 Byte message may not be enough for what we want to send in one go, so here is a tip:

In MPLABX open File "UsbDescriptors.h" and change the following line:

Hid Line 8

To:

Hid Line 64

This will increase the length of messages from 8 Bytes to 64. Bear in mind that longer message means bigger buffers and therefore more memory consumption:

Memory 64

Rebuild the MPLAB project and burn it to the chip. You don't need to do anything on the Visual studio side, as the message length is taken from the device, and the windows side automatically adjusts itself to what it is given.

Once burned start everything again and here is what I had:

VS Test 64

From now on the messages flying between will be 64 Bytes Long and we achieved it by changing a single variable in the code.

So to wrap it up I hope you will find this useful, perhaps learn something from it and who knows what happens after.... Your imagination is the only limit :)

History

Not much in history but this is a second article of a kind. To have a look at the first one click here:

C# USB HID Interface

Thanks.

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionError Pin
Member 1584999812-Dec-22 17:07
Member 1584999812-Dec-22 17:07 
AnswerRe: Error Pin
Member 1616690220-Dec-23 20:55
Member 1616690220-Dec-23 20:55 
QuestionReceive button push from PIC Pin
rftestman14-Jan-18 10:37
rftestman14-Jan-18 10:37 
GeneralMy vote of 5 Pin
Robert_Dyball25-Apr-17 23:50
professionalRobert_Dyball25-Apr-17 23:50 
QuestionPerfect article, immediately works, how to detect multiple devices with same VID and PID Pin
Harald Sonntag27-Mar-17 22:24
Harald Sonntag27-Mar-17 22:24 
QuestionWhy does the received event handler happen twice? Pin
Member 1294589915-Jan-17 15:27
Member 1294589915-Jan-17 15:27 
AnswerRe: Why does the received event handler happen twice? Pin
Sod Almighty12-Feb-21 12:44
Sod Almighty12-Feb-21 12:44 
QuestionApplication freeze after click to send command Pin
antonioclj6-Jan-17 7:47
antonioclj6-Jan-17 7:47 
QuestionHow to now when the buffer is ready for more data? (HIDTxBuffer) Pin
danove_b17-Sep-16 7:06
danove_b17-Sep-16 7:06 
QuestionResume Host Pin
danove_b10-Sep-16 5:18
danove_b10-Sep-16 5:18 
QuestionMultiple devices with same VID/PID Pin
Member 934117010-Mar-16 20:12
Member 934117010-Mar-16 20:12 
AnswerRe: Multiple devices with same VID/PID Pin
Szymon Roslowski26-Apr-16 2:18
professionalSzymon Roslowski26-Apr-16 2:18 
QuestionThe Program Pin
Member 1204848510-Oct-15 5:58
Member 1204848510-Oct-15 5:58 
AnswerRe: The Program Pin
Szymon Roslowski10-Dec-15 1:56
professionalSzymon Roslowski10-Dec-15 1:56 
QuestionPIC16F1455 I²C Pin
Member 408330925-Sep-15 2:09
Member 408330925-Sep-15 2:09 
AnswerRe: PIC16F1455 I²C Pin
Szymon Roslowski29-Sep-15 0:45
professionalSzymon Roslowski29-Sep-15 0:45 
Question18F4550 Pin
Member 1199231317-Sep-15 9:25
Member 1199231317-Sep-15 9:25 
AnswerRe: 18F4550 Pin
Member 1199168217-Sep-15 21:22
Member 1199168217-Sep-15 21:22 
AnswerRe: 18F4550 Pin
Szymon Roslowski17-Sep-15 22:19
professionalSzymon Roslowski17-Sep-15 22:19 
GeneralRe: 18F4550 Pin
Member 1199231329-Sep-15 8:18
Member 1199231329-Sep-15 8:18 
GeneralRe: 18F4550 Pin
Szymon Roslowski29-Sep-15 8:24
professionalSzymon Roslowski29-Sep-15 8:24 
Try this out:

18F2550GenericHid.zip (301.9 KB)

It was written for 18F2550 but should work.

Please let me know how it went.

Thanks.
GeneralRe: 18F4550 Pin
rnorman35-Jan-17 9:36
rnorman35-Jan-17 9:36 
GeneralRe: 18F4550 Pin
rnorman36-Jan-17 3:49
rnorman36-Jan-17 3:49 
QuestionExcellent project/tutorial! Pin
Member 1155877324-Jul-15 6:34
Member 1155877324-Jul-15 6:34 
AnswerRe: Excellent project/tutorial! Pin
Szymon Roslowski29-Sep-15 0:49
professionalSzymon Roslowski29-Sep-15 0:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.