Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC USB implementation

Status
Not open for further replies.

ARandomOWl

New Member
I am making a PIC-based voltmeter/thermometer and I would like to use a USB interface as a way of logging data. My PIC will need to read a few ADC channels, do some simple calculations then display the results on an LCD and relay them through USB to the PC.

Now I would like to send the data to the PC in the main program loop on demand. Is there a way I can do this without using interrupts for USB? Or is USB such that the bus must be kept alive using interrupts?

Maybe I can place data in a buffer during my main loop and send it to the PC during the USB interrupt. As long as the interrupt occurs more often than I take readings, no data should be lost.

The data I am sending will be about 12 bytes long and will need to be sent about 10 times per second.

As above I am thinking I could implement interrupt transfer method through HID. If anyone could point me in the right direction that would be great, thanks.
 
There is extensive documentation / examples for C24. I bought the pic24fj128ga010 dev board it comes complete with a pic18f4550 and the add on TFT 3.2" touch screen. The C24 c compiler is free for the light version. The dev kit only came in at @ £120.00 direct from microchip direct ( you can get it at farnell ).... Another Thought would be to go for .net. I have a cobra from GHI... Perfect for what you need ( if you can code in C# )
 
I realise there is a lot of stuff for C30. My question was specifically directed at the USB protocol, not at the development/language tools. Thanks.
 
Ok, maybe I didn't ask my question very well. After some thought it seems there will be no problem using the interrupt method. I will just need to send a "NACK" style byte if there is no new data available (ie. if the interrupt if being called faster than I can read the ADC etc.)

Does the USB HID driver poll my PIC itself or do I need to poll my PIC from my software? Or does it even need to be polled at all? I haven't come across this information in the USB spec or any of the sites I have looked at.
 
The HID driver that oshonsoft provided, gives you the option to call (via a timer) the USB packets...If you just view his code (I know its in basic, but you'll get the idea)

Do you want to see the VB6 code he provided as a sample?

Code:
Private Sub Command1_Click()
If HIDTerminal1.HID_Detected = True Then Exit Sub
HIDTerminal1.HID_VendorID = HIDTerminal1.dec4(Text1)
HIDTerminal1.HID_ProductID = HIDTerminal1.dec4(Text2)
If HIDTerminal1.HID_VendorID < 0 Or HIDTerminal1.HID_VendorID > 65535 Or HIDTerminal1.HID_ProductID < 0 Or HIDTerminal1.HID_VendorID > 65535 Then Exit Sub
Call HIDTerminal1.HIDConnect
If HIDTerminal1.HID_Detected = False Then
    printline "HID (VendorID:" + HIDTerminal1.hex4(HIDTerminal1.HID_VendorID) + ";ProductID:" + HIDTerminal1.hex4(HIDTerminal1.HID_ProductID) + ") not detected or could not be accessed..."
    printline ""
Else
    printline "HID detected!"
    printline "VendorID: " + HIDTerminal1.hex4(HIDTerminal1.HID_VendorID)
    printline "ProductID: " + HIDTerminal1.hex4(HIDTerminal1.HID_ProductID)
    printline "ManufacturerString: " + HIDTerminal1.HID_ManufacturerString
    printline "ProductString: " + HIDTerminal1.HID_ProductString
    printline "SerialNumberString: " + HIDTerminal1.HID_SerialNumberString
    printline "VersionNumber: " + HIDTerminal1.hex4(HIDTerminal1.HID_VersionNumber)
    printline "InputReportLength: " + Trim(Str(HIDTerminal1.HID_InputReportLength))
    printline "OutputReportLength: " + Trim(Str(HIDTerminal1.HID_OutputReportLength))
    printline "FeatureReportLength: " + Trim(Str(HIDTerminal1.HID_FeatureReportLength))
    printline ""
End If
End Sub

Private Sub Command2_Click()
Dim data(7) As Byte
Dim result As Boolean
If HIDTerminal1.HID_Detected = False Then Exit Sub
Randomize
data(0) = Int(255 * Rnd)
data(1) = Int(255 * Rnd)
data(2) = Int(255 * Rnd)
data(3) = Int(255 * Rnd)
data(4) = Int(255 * Rnd)
data(5) = Int(255 * Rnd)
data(6) = Int(255 * Rnd)
data(7) = Int(255 * Rnd)
result = HIDTerminal1.HIDSendReport(data(0), data(1), data(2), data(3), data(4), data(5), data(6), data(7))
If result = False Then
    printline "Output Report send error!"
    printline ""
Else
    printline "Output Report sent..."
    printline HIDTerminal1.hex2(data(0)) + " " + HIDTerminal1.hex2(data(1)) + " " + HIDTerminal1.hex2(data(2)) + " " + HIDTerminal1.hex2(data(3)) + " " + HIDTerminal1.hex2(data(4)) + " " + HIDTerminal1.hex2(data(5)) + " " + HIDTerminal1.hex2(data(6)) + " " + HIDTerminal1.hex2(data(7))
End If
End Sub

Private Sub Command3_Click()
Dim data(7) As Byte
Dim result As Boolean
If HIDTerminal1.HID_Detected = False Then Exit Sub
result = HIDTerminal1.HIDReadReport(data(0), data(1), data(2), data(3), data(4), data(5), data(6), data(7))
If result = False Then
    printline "Input Report read error!"
    printline ""
Else
    printline "Input Report received..."
    printline HIDTerminal1.hex2(data(0)) + " " + HIDTerminal1.hex2(data(1)) + " " + HIDTerminal1.hex2(data(2)) + " " + HIDTerminal1.hex2(data(3)) + " " + HIDTerminal1.hex2(data(4)) + " " + HIDTerminal1.hex2(data(5)) + " " + HIDTerminal1.hex2(data(6)) + " " + HIDTerminal1.hex2(data(7))
End If
End Sub

Private Sub Command4_Click()
Dim data(7) As Byte
Dim result As Boolean
If HIDTerminal1.HID_Detected = False Then Exit Sub
Randomize
data(0) = Int(255 * Rnd)
data(1) = Int(255 * Rnd)
data(2) = Int(255 * Rnd)
data(3) = Int(255 * Rnd)
data(4) = Int(255 * Rnd)
data(5) = Int(255 * Rnd)
data(6) = Int(255 * Rnd)
data(7) = Int(255 * Rnd)
result = HIDTerminal1.HIDSendFeature(data(0), data(1), data(2), data(3), data(4), data(5), data(6), data(7))
If result = False Then
    printline "Feature Report send error!"
    printline ""
Else
    printline "Feature Report sent..."
    printline HIDTerminal1.hex2(data(0)) + " " + HIDTerminal1.hex2(data(1)) + " " + HIDTerminal1.hex2(data(2)) + " " + HIDTerminal1.hex2(data(3)) + " " + HIDTerminal1.hex2(data(4)) + " " + HIDTerminal1.hex2(data(5)) + " " + HIDTerminal1.hex2(data(6)) + " " + HIDTerminal1.hex2(data(7))
End If
End Sub

Private Sub Command5_Click()
Dim data(7) As Byte
Dim result As Boolean
If HIDTerminal1.HID_Detected = False Then Exit Sub
result = HIDTerminal1.HIDReadFeature(data(0), data(1), data(2), data(3), data(4), data(5), data(6), data(7))
If result = False Then
    printline "Feature Report read error!"
    printline ""
Else
    printline "Feature Report received..."
    printline HIDTerminal1.hex2(data(0)) + " " + HIDTerminal1.hex2(data(1)) + " " + HIDTerminal1.hex2(data(2)) + " " + HIDTerminal1.hex2(data(3)) + " " + HIDTerminal1.hex2(data(4)) + " " + HIDTerminal1.hex2(data(5)) + " " + HIDTerminal1.hex2(data(6)) + " " + HIDTerminal1.hex2(data(7))
End If
End Sub

Private Sub Form_Load()
'
End Sub

Private Sub printline(ByVal inputline As String)
board.Text = board.Text + inputline + vbCrLf
board.SelStart = 1000000
End Sub

His driver is an ocx so you'll be able to use it in c# or c++ Its just so you can get the idea of HID usage
 
hi Ian,
Ref the Oshonsoft idea.
This is a VB5 HID USB demonstrator I did for Oshonsoft HID, the zip has the source code plus other support files.

The USB_Demo3.bas works with the USB_Terminal3.exe
 

Attachments

  • usb_demo1.zip
    101.3 KB · Views: 240
  • USB_Demo3.bas
    5.5 KB · Views: 344
  • AAesp01.gif
    AAesp01.gif
    17.1 KB · Views: 269
hi owl no idea if this is of any help https://www.waitingforfriday.com/in...c_HID_devices_based_on_the_PIC18F_and_Windows
also if you look in the project section of the site there is a couple more usb projects, it is in hi tech c i believe. the guy that runs the site (simon) is realy realy helpful and has a forum. sorry if it isnt relevant to want you want to do. if nothing elese it may answer some of your questions and has a basic inface for the pc to comunicate with the pic, its written in c# has the source code and is open source
cheers jason
 
Thanks Jason, I will have a look at that as well.

The problem I have with the Microchip USB stack is that in all of the examples, there are so many pre-processor directives for different dev boards that I can't understand what is going on. I have yet to come across a "barebones" example.
 
THAT is one of my issues... I know microchip software engineers are trying to cover all aspects, but trimming away the fat makes for an easier read...I quite agree

Hey Eric... Fancy making one for VB2010!!! Please!!!!
 
Last edited:
dont quote me but i think the hi tech C one on simons site is a Hi tech C port of microchips stack and has been cut down and trimmed! so it may well be what your after! if not look at the other usb projects on there i tried one and i am pretty sure its very well trimmed to the basics. hope its of use mate
 
Hey Owl,
I've jsut started reading up on the USB Protocol and The Host always Polls the USB device. There are 4 modes of data transfer: bulk, isochronous, interrupt and control. I believe that the interrupt transfer type will best suit your need as it's only small bits of data on irregular intervals

With the interrupt mode, the Host will poll your device for any data, if your PIC responses with no data, then no action is taken, only when your PIC actually has data will the Host read it and process it PC side.

That's about as much as I know about USB at the moment. I hope this was of some help to you.

Also,
Bulk transfer if for large amounts of data, which is ideal for USB Flash Sticks. Isochronous is used for streaming data that is time critical e.g. USB Speakers/Mic, Webcam. Interrupt is ideal for devices that send little amounts of data on irregular intervals such as Mice, Keyboards, various HID devices and so on. Control is a requirement for all devices as it is used for getting the device descriptors and setting up pipelines and addresses.

Cheers
Roman
 
Last edited:
Roman, thanks for the info, I have already read up on the different types of transfers. Do you know, is it the USB driver or the application that polls the device?

Thanks for all the bits of info guys. I will look through them all in due course. A bit busy with other things at the moment :)
 
from what i read on simon's site its the application software that does the polling, the driver is basicaly just that
 
Roman, thanks for the info, I have already read up on the different types of transfers. Do you know, is it the USB driver or the application that polls the device?

Thanks for all the bits of info guys. I will look through them all in due course. A bit busy with other things at the moment :)

I don't know the answer to that question yet. I'm slowly reading a book on USB so sorry I can't be of real help here
 
Status
Not open for further replies.

Latest threads

Back
Top