adalucio
Joined: 16 Mar 2009 Posts: 29
|
usb gamepad using pic18f4550 |
Posted: Thu Jun 24, 2010 12:29 pm |
|
|
Hi all
I'm tring to make a simple usb gamepad using pic18f4550...
main.c
Code: |
#include "main.h"
#include <pic18_usb.h> // Microchip PIC18Fxx5x hardware layer for usb.c
#include "usb_desc_hid.h"
#include <usb.c> // handles usb setup tokens and get descriptor reports
#define BUTTON1 PIN_B0
#define BUTTON2 PIN_B1
#define BUTTON3 PIN_B2
#define BUTTON4 PIN_B3
#define LED_USB PIN_B4
void usb_debug_task(void)
{
static int8 last_connected;
static int8 last_enumerated;
int8 new_connected;
int8 new_enumerated;
new_connected = usb_attached();
new_enumerated = usb_enumerated();
if (new_connected && !last_connected)
printf("USB connected, waiting for enumaration...\r\n");
if (!new_connected && last_connected)
printf("USB disconnected, waiting for connection...\r\n");
if (new_enumerated && !last_enumerated)
printf("USB enumerated by PC/HOST\r\n");
if (!new_enumerated && last_enumerated)
printf("USB un-enumerated by PC/HOST, waiting for enumeration...\r\n");
last_connected = new_connected;
last_enumerated = new_enumerated;
}
void main()
{
int8 out_data[4];
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
delay_us(10);
usb_init_cs();
while(1)
{
usb_task();
usb_debug_task();
// Controllo se viene riconosciuto correttamente dal PC
if(usb_enumerated())
{
output_high(LED_USB);
if(input(BUTTON1))
{
out_data[0] = 1;
printf("button1 pressed \n\r");
}
else
out_data[0] = 0;
if(input(BUTTON2))
{
out_data[1] = 1;
printf("button2 pressed \n\r");
}
else
out_data[1] = 0;
if(input(BUTTON3))
{
out_data[2] = 1;
printf("button3 pressed \n\r");
}
else
out_data[2] = 0;
if(input(BUTTON4))
{
out_data[3] = 1;
printf("button4 pressed \n\r");
}
else
out_data[3] = 0;
if(usb_put_packet(1, out_data, sizeof(out_data), USB_DTS_TOGGLE))
printf("usb_put_packet OK \n\r", );
else
printf("usb_put_packet ERROR \n\r");
delay_ms(10);
}
else
{
output_low(PIN_B0);
delay_ms(10);
}
}
}
|
Code: |
const char USB_CLASS_SPECIFIC_DESC[] =
{
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x05, // USAGE (Game Pad)
0xa1, 0x01, // COLLECTION (Application)
0x05, 0x09, // USAGE_PAGE (Button)
0x19, 0x01, // USAGE_MINIMUM (Button 1)
0x29, 0x04, // USAGE_MAXIMUM (Button 4)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x04, // REPORT_COUNT (4)
0x81, 0x02, // INPUT (Data,Var,Abs)
0x95, 0x04, // REPORT_COUNT (4)
0x81, 0x03, // INPUT (Cnst,Var,Abs)
0xc0 // END_COLLECTION
};
|
I'm getting in HyperTerminal
Code: |
USB connected, waiting for enumaration...
USB enumerated by PC/HOST
usb_put_packet OK
USB un-enumerated by PC/HOST, waiting for enumeration...
USB enumerated by PC/HOST
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
USB un-enumerated by PC/HOST, waiting for enumeration...
USB enumerated by PC/HOST
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
USB un-enumerated by PC/HOST, waiting for enumeration...
USB enumerated by PC/HOST
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
usb_put_packet ERROR
|
Windows xp tell me there are no problems with the peripherial...
What I'm doing wrong?
Thanks |
|