A Simple USB Application using HID and VB

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply

Please rate this article out of 5: (1 being the worst and 5 being the best)

1
0
No votes
2
0
No votes
3
0
No votes
4
0
No votes
5
3
100%
 
Total votes: 3

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

A Simple USB Application using HID and VB

Post by Benj »

Image

One of the most portable types of USB device is a Human Interface Device, as it does not require a driver file to connect and interface to your computer. The computer driver information for a HID device is actually stored inside the memory of the USB device. This means that your device will run without modification on pretty much any of today's computer systems: PC, MAC and even Linux. Though the PC side of the software will need porting to the various operating systems.

The HID descriptor is the piece of code that controls how the computer sees the embedded USB device so it is important to get this correct. Using the custom descriptor property of the HID component I pasted in the following section of code.

//HID Descriptor, 8 bytes Tx, 8 bytes Rx
0x06,0xA0,0xFF, //Usage Page - Vendor Defined
0x09,0x01, //Usage Vendor Defined
0xA1,0x01, //Collection - Application
0x09,0x02, //Usage Vendor Defined
0xA1,0x00, //Collection - Linked
0x06,0xA1,0xFF, //Usage Page - Buttons
0x09,0x03, //Usage Vendor Defined
0x09,0x04, //Usage Vendor Defined
0x15,0x00, //Logical Minimum - 0
0x26,0xFF,0x00, //Logical Maximum - 255
0x35,0x00, //Physical Minimum - 0
0x45,0xFF, //Physical Maximum - 255
0x75,0x08, //Report Size – 8bits
0x95,0x08, //Report Count – 8fields
0x81,0x02, //Input Data Variable Absolute
0x09,0x05, //Usage Vendor Defined
0x09,0x06, //Usage Vendor Defined
0x15,0x00, //Logical Minimum - 0
0x26,0xFF,0x00, //Logical Maximum - 255
0x35,0x00, //Physical Minimum - 0
0x45,0xFF, //Physical Maximum - 255
0x75,0x08, //Report Size 8bits
0x95,0x08, //Report Count 8fields
0x91,0x02, //Output Data Variable Absolute
0xC0, //End Collection
0xC0 //End Collection

The first highlighted line configures the amount of data going from the PC to the embedded USB device.

The second highlighted line configures the amount of data going back from the Embedded USB device to the PC.

The value 0x08 specifies that 8 bytes will be transferred. If you wish to change this then you will also need to change the HID Options property page to match.

Here is an example that will send and receive 8 bytes to match the above HID descriptor.
Image

Also shown in the HID options property page is the rate of throughput of the data. At the moment the device is configured to throughput 8 bytes of data in and out of the system at a maximum rate of 100Hz. This can be changed by editing the interrupt periods inside the USB HID properties. Remember that this sets the maximum throughput for your application but your application can actually run with a much lower data throughput with no ill effects. If you try and go above the data throughput then the PC will terminate communications with the device and stop further communications from taking place. So if you are having problems getting your application to work once the device has appeared in your device manager then this could be the cause.

Now we have configured the device to run as a generic HID it is possible to use a purpose built application to talk to your device. There are a number of example applications provided at the following link in the Host Applications section.

http://www.lvr.com/hidpage.htm

The PC demo files supplied with this article were generated using the generic HID VB code and were compiled using the free trial version of VB 2008 Express edition. Other examples available from the link above include: VB.net, Visual C#, VB6, Visual C++ 6. All of the examples on the main HID page are using Microsoft compilers so if you have your own favourite compiler then there is bound to be HID examples available for that via a Google search.

The example Flowcode file simply takes the value from the first data byte and outputs to PortB. Once it has done this it copies its incoming buffer to its outgoing buffer and echos the data back to the PC.
Attachments
Generic HID.zip
(245.13 KiB) Downloaded 2323 times

knighthawk
Posts: 8
Joined: Sun Dec 11, 2011 10:19 pm
Has thanked: 3 times
Been thanked: 2 times
Contact:

Re: A Simple USB Application using HID and VB

Post by knighthawk »

Hi Benj,

I have been experimenting with a simple PIC18F2550 project but for the life of me I cannot get the PC to recognize the chip after programming.

I am using version 5.2 and am using the USB HID, also tried the examples adjusted for the above chip, but I am guessing I am missing something.

I am using the QL-200 Development board for programming and testing.
Project1.JPG
Project1.JPG (40.36 KiB) Viewed 25934 times
Project2.JPG
Project2.JPG (135.35 KiB) Viewed 25934 times

I am clocking at 4Mhz and the Chip settings and code are below.

I have also tried your above example to no avail. I have a flashing LED in both the True and False of the initialize but the program never gets past the initialize. If I take it out it flashes the LEDS.

Hope you can help.

Mike
Attachments
ZX Spectrum.fcf
(13 KiB) Downloaded 840 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: A Simple USB Application using HID and VB

Post by Benj »

Hi Mike,

I have edited your config settings a bit and hopefully everything should work correctly now. The device needs to be setup to run at 12MIPs to allow USB to work.
ZX Spectrum.fcf
(13 KiB) Downloaded 890 times
Let me know how you get on.

knighthawk
Posts: 8
Joined: Sun Dec 11, 2011 10:19 pm
Has thanked: 3 times
Been thanked: 2 times
Contact:

Re: A Simple USB Application using HID and VB

Post by knighthawk »

Hi Benj,

Thank you, I will try this when I get home and let you know.

Mike

knighthawk
Posts: 8
Joined: Sun Dec 11, 2011 10:19 pm
Has thanked: 3 times
Been thanked: 2 times
Contact:

Re: A Simple USB Application using HID and VB

Post by knighthawk »

Hi Benj,

That works great thank you, I also have to do the Cap 470nf between RC3 and Ground.

The only thing is now is when I SendDataDirect("0x61") it seems to never stop sending and also send the wrong character.

You can here the PC beeping as if you where holding a key down, Notepad displays allsorts, but seems to be {{{{{{{{{{{{{{[[[[[[[[[[[[[[[, even though I am send only once every second.

Working on that now.

Thanks Again

Mike

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: A Simple USB Application using HID and VB

Post by Benj »

Hi Mike,

Are you using the standard Keyboard profile? If so then the attached might help.
HID Keyboard Scan Codes.xls
(37.5 KiB) Downloaded 1030 times
There should be a good keyboard example in the Flowcode v5 examples pack which should help to show you what is required to send the 8-byte buffer.

knighthawk
Posts: 8
Joined: Sun Dec 11, 2011 10:19 pm
Has thanked: 3 times
Been thanked: 2 times
Contact:

Re: A Simple USB Application using HID and VB

Post by knighthawk »

Hi Benj,

Thank you,

That was my next step to try the example.

Mike

knighthawk
Posts: 8
Joined: Sun Dec 11, 2011 10:19 pm
Has thanked: 3 times
Been thanked: 2 times
Contact:

Re: A Simple USB Application using HID and VB

Post by knighthawk »

Hi Benj,

All working great.

Thank you for your help.

Mike

knighthawk
Posts: 8
Joined: Sun Dec 11, 2011 10:19 pm
Has thanked: 3 times
Been thanked: 2 times
Contact:

Re: A Simple USB Application using HID and VB

Post by knighthawk »

Hi,

All working great until I try Bus Powered.

What is the Setting for the PIC18F2550 for bus powered ?

I have set the Max Current, but there is no Bus Powered option.

Cheers

Mike

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: A Simple USB Application using HID and VB

Post by Benj »

Hi Mike,

I think for bus powered applications you have to keep the max current down below 100mA before the enumeration has completed and the device driver has been installed (if required). Once this has happened you can then ramp up the current to the amount specified. If you go over this specified current then the PC may disable the device so be careful with this. Running an ECIO etc in bus powered mode is no problem at all so I'm guessing the problem is something to do with your hardware setup. There are no config settings associated with this, the same programs runs on the ECIO when powered from a PSU or via the USB.

knighthawk
Posts: 8
Joined: Sun Dec 11, 2011 10:19 pm
Has thanked: 3 times
Been thanked: 2 times
Contact:

Re: A Simple USB Application using HID and VB

Post by knighthawk »

Hi Benj,

Thank you, that's just what I did in the end, and all is now working.

Interesting though that you can ramp up the current after initialization.

Thanks again.

Mike

mohamednagib26w
Posts: 1
Joined: Fri Nov 08, 2013 8:02 am
Contact:

Re: A Simple USB Application using HID and VB

Post by mohamednagib26w »

greetings i have a problem with usb hid module i am using flowcode v5 and when simulating with proteus the pc defines the usb hid but i have an error with a yellow triangle on th drive and
(code 10) on the drive that says missing hardware id help pls

superman7
Posts: 1
Joined: Sat Jan 04, 2014 10:46 am
Contact:

Re: A Simple USB Application using HID and VB

Post by superman7 »

The only thing is now is when I SendDataDirect("0x61") it seems to never stop sending and also send the wrong character.

knighthawk
Posts: 8
Joined: Sun Dec 11, 2011 10:19 pm
Has thanked: 3 times
Been thanked: 2 times
Contact:

Re: A Simple USB Application using HID and VB

Post by knighthawk »

Hi,

You need to send 0x00 when you have finished or it will send constantly.

Are you using ASCII characters to send, if so these are wrong, USB HID characters have different ID's.

Excel File attached with codes.

Also Attached above on Benj post.

Mike
Attachments
HID Keyboard Scan Codes.xls
Hid Codes
(41 KiB) Downloaded 669 times

Hugo
Posts: 2
Joined: Sun May 25, 2014 11:39 am
Contact:

Re: A Simple USB Application using HID and VB

Post by Hugo »

HI ALL.

I would like to make a thermostate, and I would like it’s adjustments (actual temperature, hysteresis, etc.) to adjust and follow through USBHID contact. I tried to apply the enclosed programmes but I got stuck in several points on the pic site. There was no prograblem with the PC site. I would like to transmit 64 bits from both of sites, as it can be seen in the example. :cry:
My hardware is a 18f4550 pic and a 20Mhz quarz.
My problems:
- If I turn the thermostate on, it stops at the command Initialise HID till I don’t attach it on the PC. Then it works forward and sends the data. If I intermit the contact and then I attach the thermostate again to the PC, it doesn’t send the data again. If I turn the Enumeration Wait on, it proceeds over the command Initialise HID without connection. But after connection it doesn’t send and receive data however Windows shows it is connected.
So, how can be the USB connection intermitable and connectable at any time while it works?
How can I receive data through USB?
How has to be adjusted Descriptors, to work the pic with the enclosed PC programme?
Perhaps could someone make a programme in FC5 for me, that works with an application running on PC?

Thanks in advance
Attachments
usbhid.zip
(325.08 KiB) Downloaded 999 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: A Simple USB Application using HID and VB

Post by Benj »

Hello,

A few pointers when working with the HID component,

Add a fairly large delay after the initialise function 1 or 2 seconds to ensure enumeration has happened before any data is sent. Delays between transmission are also recommended or the HID data endpoints can potentially become saturated and fall over.

I would switch off the enumeration wait setting so that the board waits for the USB connection to be established.

When unplugging the board from USB I take it the board is still powered, maybe instead you could power from the USB or reset the firmware via a software command so that the device will reliably start up again when plugged back in.

Post Reply