Writing a USB Joystick using PIC and MikroC

355 Views Asked by At

I'm using PIC18F2458 as USB Joystick HID. The program works, but I have few problems.

Right now, The USB is not detected If I can't write more than 5 values ( Throttle, X,Y, 4 buttons). The current problem is If I'm using an analog sensor for the throttle, other axis x,y getting read too, because they are going to be read from analog input and of course there is noise, so whenever I use a rotary resistor on the throttle usb input, I get other axis written too.

How can I fix that issue ?

second issue how would I add two throttle for the usb descriptor ?

Here is the current code:

unsigned int average(int channel)
{
 int samples = 16;
 unsigned int sum = 0;
 int i;
 for (i = 0; i < samples; i++)
 {
 
     sum+=(Adc_Read(channel)>>2)-128;
 }
 sum = sum/16;
 return sum;
}

void analogo() {

    usb[0] = average(1);   // Get 10-bit results of AD conversion
    usb[1] = average(9);
    usb[2] = average(8);
}

void main(void){
                              // Disable comparators

 ADCON1  = 0x05;                         // Configure all ports with analog function as digital
 TRISA.RA1 =1;
 TRISB.RB2=1;
 TRISB.RB3 =1;
 
 HID_Enable(&readbuff,&writebuff);       // Enable HID communication

 //vl6180x_configure();

  while(1){
  analogo();
  USB_Polling_Proc();

    writebuff[0]=usb[0];         // throttle
    writebuff[1]=usb[1];        // X axis
    writebuff[2]=usb[2];        // Y  axis
    writebuff[3]=0xFF;        //button  0xFF
    writebuff[4]=0xFF;        //button POV 0xFF



    HID_Write(&writebuff,5);


   }

}

and the Descriptor

const struct {
  char report[USB_HID_RPT_SIZE];
}
hid_rpt_desc =

     {0x05, 0x01, // USAGE_PAGE (Generic Desktop)
     0x15, 0x00, // LOGICAL_MINIMUM (0)
     0x09, 0x04, // USAGE (Joystick)
     0xA1, 0x01, // COLLECTION (Application)
     0x05, 0x02, // USAGE_PAGE (Simulation Controls)
     0x09, 0xBB, // USAGE (Throttle)
     0x15, 0x00, // LOGICAL_MINIMUM (0)
     0x26, 0xFF, 0x00, // LOGICAL_MAXIMUM (255)
     0x75, 0x08, // REPORT_SIZE (8)
     0x95, 0x01, // REPORT_COUNT (1)
     0x81, 0x02, // INPUT (Data Var Abs)
     0x05, 0x01, // USAGE_PAGE (Generic Desktop)
     0x09, 0x01, // USAGE (Pointer)
     0xA1, 0x00, // COLLECTION (Physical)
     0x09, 0x30, // USAGE (X)
     0x09, 0x31, // USAGE (Y)
     0x95, 0x02, // REPORT_COUNT (2)
     0x81, 0x02, // INPUT (Data Var Abs)}
     0xC0, // END_COLLECTION
     0x09, 0x39, // USAGE (Hat switch)
     0x15, 0x00, // LOGICAL_MINIMUM (0)
     0x25, 0x03, // LOGICAL_MAXIMUM (3)
     0x35, 0x00, // PHYSICAL_MINIMUM (0)
     0x46, 0x0E, 0x01, // PHYSICAL_MAXIMUM (270)
     0x65, 0x14, // UNIT (Eng Rot:Angular Pos)
     0x75, 0x04, // REPORT_SIZE (4)
     0x95, 0x01, // REPORT_COUNT (1)
     0x81, 0x02, // INPUT (Data Var Abs)
     0x05, 0x09, // USAGE_PAGE (Button)
     0x19, 0x01, // USAGE_MINIMUM (Button 1)
     0x29, 0x0A, // USAGE_MAXIMUM (Button 10)
      0x15, 0x00, // LOGICAL_MINIMUM (0)
      0x25, 0x01, // LOGICAL_MAXIMUM (1)
      0x75, 0x01, // REPORT_SIZE (1)
      0x95, 0x0C, // REPORT_COUNT (12) 2 bits added to switch report count
                  // so bytes are even. Bytes must be even.
      0x55, 0x00, // UNIT_EXPONENT (0)
      0x65, 0x00, // UNIT (None)
      0x81, 0x02, // INPUT (Data Var Abs)
0xC0 // END_COLLECTION
  };
0

There are 0 best solutions below