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.

Muchado about BSU

Status
Not open for further replies.

SwingeyP

Member
Ok so it's USB but that didn't rhyme. :)

I have a little side project that I want to do which is going to use a USB connection to PC.
It will be a keyboard HID.

There will be 14 buttons. Each button can be mapped to a keyboard key stroke. This will be done via differing program versions
For example button 1 = A, button 2 = B, button 3 = X and so on.

I have been looking at Eric's examples for USB which I have working on a PICDEM FSUSB board (ebay purchase).

Here's the thing. I don't really know (understand) how the USB works. Can anyone point me in the direction of some bedtime reading.
I need to know the sequence something like ....

if button press
identify button number
associate button with key
send down the usb ----> This is what I need to know I am a bit confused by Reports & Features
endif

Just a simplified psuedo version. I appreciate I have to switch on USB and some sort of polling?

Any help please?

Regards - Paul
 
I found that "Reports and Features" are just "Inbuffers" and "Outbuffers"... If you have the USB set up correctly the transfer is almost automatic... The PIC will sent a buffer array to the PC and the PC will send the buffer array back..

The idea is that you ask a feature request and receive a report back.... The one Oshonsoft implemented, I could use all of the 8 report bytes and all of the 8 feature bytes making 16 byte buffer and got quite fast results...
 
Hi again.

After some bedtime reading on USB I am now more confused than ever.

Some questions.

How do I define the USB device as a keyboard?
Do I need to or will the generic HID do?

Do I put the keystroke hex in the report or feature buffers to send to the PC?

I'm still unsure as to what is what with feature and report.
Eric's example shows both as input and output.

To send a keystroke which do I use Feature or Report?

It appears that the USB buffer never clears. How do I clear it? - If I set it to all zero's will these be sent to the keyboard routines of the PC?
The button press needs to be momentary. The PC will handle debounce (I think) as repeat key presses.

Lots of things to think about.

I have attached some code built around Eric's example and this compiles and runs on my demo board.

Does anyone know of a 'HID keyboard sniffer' that would show key presses depending on the button press?

Cheers - Paul
 

Attachments

  • HID_08_07_14.bas
    4.3 KB · Views: 294
Hi Paul,
Download free from the net Snoopy Pro
https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0CCIQFjAA&url=https://sourceforge.net/projects/usbsnoop/&ei=4va7U86qOMmr7Aa0v4GACg&usg=AFQjCNGMgM3UfTybfiznPclvAecpt9WImQ&bvm=bv.70138588,d.ZGU
You can send the Keycode either by Report or Feature, I would use Report,
Use the method I have shown in my Demo for pre loading a buffer array before calling USBService, this avoids corrupting your data.

E

EDIT:
Which PIC are using the program header says 16F877A, which is not for USB.???
 
Last edited:
hi Paul,
Look at this summary I did for the USB on Oshonsoft

The Report and Feature work both for Send and Receive data from the PIC to PC.
When you call USBService the Data is sent from PIC to PC to PIC

Code:
'preload Feature array, before calling service
f1(0) = "P"
f1(1) = "O"
f1(2) = "R"
f1(3) = "T"
f1(4) = "A"
f1(5) = "="
f1(6) = 0x20  '''''Note the IDE will not allow a space of " "
f1(7) = PORTA And 0xfc  'read PORTA digital inputs
f1(7) = ShiftRight(f1(7), 2)  'move to lower nibble
f1(7) = f1(7) Or 0x30  'make ascii

'preload Report array with ascii value, before calling service
r1(0) = "V"
r1(1) = "0"
r1(2) = "="
r1(3) = asc1(3)
r1(4) = "."
r1(5) = asc1(2)
r1(6) = asc1(1)
r1(7) = asc1(0)


UsbService

Code:
'Report Data from PC to PIC, controlled by PC
'Report data received is stored in UsbIoBuffer array.
usbonioout:

'transfer to local Report buffer
'else I/O values get corrupted

For x = 0 To 7
r1(x) = UsbIoBuffer(x)
Next x
 

Attachments

  • USB supportV1.doc
    29 KB · Views: 435
I have a little side project that I want to do which is going to use a USB connection to PC.
It will be a keyboard HID.

There will be 14 buttons. Each button can be mapped to a keyboard key stroke. This will be done via differing program versions
For example button 1 = A, button 2 = B, button 3 = X and so on.

hi Paul,
Reading your initial post I would suggest the following method.

Load the Report Output buffer in the PIC as.
r1(0)= PORTA
r1(1) =PORTB
call USBservice ' this will send all 8 Bytes of the Report to the PC program.r1(0)....thru r1(7)

The PC program will Read r1(0) and r1(1) Bytes and Decode to your Action/Function of choice

Eric
 
Hi Eric.

I based my initial tests on your code in the forum 'stickies'.

It's extremely useful. It took me a while to get my head around a few things but i'm getting there slowly.
I'm still not sure however if the device should be reccognised as a keyboard or if the HID is sufficient.
I have been reading the USB HID USAGE TABLES DOC and have the keyboard codes.

The r1(0)=PortA is a great idea i'll give it a go.
 
hi Paul,
It will not be recognised as a keyboard, it doesn't work that way, its recognised as a Generic device, which means it could be any device that wants to exchange data with the PC, using HID USB.
I would use a delay between USBservice calls, say about 50mS or 100mS, this will keep the connection active, so loop around PORTA and PORTB every 100mS and send a Report to the PC every 100mS
You could use say, r1(7) with Bit 0, set to a 1, when a new key is pressed and set to 0 when not.

In that way you could use 'handshaking' between the PIC and PC when new Key sent and then PC to PIC, data read OK, so clear r1(7)....etc

Eric
 
The problem is I have no control over the PC program as such. The device MUST behave as a keyboard does. I will be using an off the shelf program that just looks for key strokes.
If you haven't guessed already this will be the keyboard encoder with mappings to a games (arcade) machine. There are plenty of these around but I just wanted to know how the encoder worked really and see if I could build one myself.

I can't use the R1(o) = portA etc as each button sends a specific keyboard code.

I have attached some code which sends the correct scan code each time a button is pressed.
I think this is very simplified and i'm sure my problems lie in defining the USB device as a Keyboard (HID Keyboard) or whatever is required.

Does anyone know how to do it?

Regards - Paul
 

Attachments

  • HID_08_07_14.bas
    7.8 KB · Views: 366
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top