|
|
View previous topic :: View next topic |
Author |
Message |
davefromnj
Joined: 03 May 2007 Posts: 42
|
first time USB question...PIC18F4450 |
Posted: Sat Oct 10, 2009 6:10 pm |
|
|
Hey all,
All I want to do is have the PIC18F4450 act as a USB keyboard. I looked at the examples, but they are less than intuitive for me.
What would an example program look like that sent the letter "f" to a PC if let's say port B2 went high.
I don't get this because I see the usb_putc() function in the manual, but not used in the examples, and the examples using usb_put_packet(2,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
where array tx_msg gets stuffed with values earlier.
Thanks, sorry for not understanding well.
OK, I now see that the usb_putc() function is actually usb_cdc_putc() for applications where you'd want the PIC to act as a seril-to-usb interface.
So, just for sending a keyboard character to windows I have to use:
usb_put_packet(2,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
??? |
|
|
davefromnj2 Guest
|
newbie city... |
Posted: Sat Oct 10, 2009 11:18 pm |
|
|
I am basically trying to implement a simple keyboard in windows using a pic18F4450: a switch closes on an input pin and the pic sends a keyboard letter to windows via USB.
So I have a few questions for usb_put_packet(A,B,C,D)
A = Endpoint ----- I want to send data to, ok which one is the PC host, address 1?
B = Data ----- Can this just be an ASCII code? For example, can I put 0x46h in here for "F" ? Will windows know this is a keyboard character?
C = Length ----- Since I'm only sending one keyboard key at a time, should this just be 1?
D = DTS_TOGGLE ----- How does this work? What should go here if I only want the PIC to be a keyboard?
Also, what is the basic frame work for the files to include, and what is the necessary order?
What else would be missing from the following test code?
Code: | #include <18F4550.h>
#DEFINE USB_HID_DEVICE TRUE
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#include <pic18_usb.h
#include <usb_desc_kbmouse.h>
#include <usb.c>
void usb_keyboard_task(void);
void main(void)
{
usb_init_cs();
while(1)
if (input(PIN_B3))
{
usb_keyboard_task();
}
}
void usb_keyboard_task(void)
{
static int8 tx_msg[7]={0,0,0,0,0,0,0};
static int8 leds;
/*
if (!input(BUTTON))
tx_msg[2]=4;
else
tx_msg[2]=0;
usb_put_packet(2,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
*/
usb_put_packet(2, 0x46, 1, USB_DTS_TOGGLE);
//receive NUM LOCK, CAPS LOCK, etc LED status from PC.
//we won't do anything with it.
if (usb_kbhit(2))
{usb_get_packet(2, &leds, 1);
}
} |
|
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sun Oct 11, 2009 1:41 am |
|
|
You apparently didn't consider the fact, that an USB keyboard usually is following the HID (human interface device) class specification and not sending ASCII codes. If you intend to build a device, that can be recognized by a computer as keyboard, you should use HID. You can find more informations about USB device classes at www.usb.org/developers. If you just want to build some kind of terminal, you can use CDC (communication device class) and usb_putc(). |
|
|
davefromnj
Joined: 03 May 2007 Posts: 42
|
Thanks |
Posted: Sun Oct 11, 2009 1:18 pm |
|
|
FvM wrote: | You apparently didn't consider the fact, that an USB keyboard usually is following the HID (human interface device) class specification and not sending ASCII codes. If you intend to build a device, that can be recognized by a computer as keyboard, you should use HID. You can find more informations about USB device classes at www.usb.org/developers. If you just want to build some kind of terminal, you can use CDC (communication device class) and usb_putc(). |
I don't know anything about doing this project or I wouldn't post here.
Thank you for pointing out that a USB keyboard doesn't just send ascii codes, that I did not know or that I will have to use the HID class.
Does the USB spec. actually say what data needs to be sent in order for windows to see a keyboard keypress since it's not ascii?
Does the USB spec. define what values, etc are supposed to go into CCS's usb_put_packet(A,B,C,D) function?
Is there another spec. I'm missing?
I don't think I want to use the CDC mode because I don't want to write / support ANY driver for windows, linux, etc -- I really do only want to make a plain old usb keyboard. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|