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.

Writing USB Joystick for PIC18F using MikroC

Status
Not open for further replies.
Code:
void select_channel(int channel)
{
     if ( channel == 1 )
    {
      ADCON0.CHS0 = 0x1;
      ADCON0.CHS2 = 0x0;
      ADCON0.CHS1 = 0x0;
      ADCON0.CHS3 = 0x0;
    }
    else if (channel == 8)
    {
      ADCON0.CHS0 = 0x0;
      ADCON0.CHS2 = 0x0;
      ADCON0.CHS1 = 0x0;
      ADCON0.CHS3 = 0x1;
    }
    else
    {
      ADCON0.CHS0 = 0x1;
      ADCON0.CHS2 = 0x0;
      ADCON0.CHS1 = 0x0;
      ADCON0.CHS3 = 0x1;
    }

}
unsigned int ADC_read_implementation(int channel){

    unsigned int adval = 0;
    ADCON2.B7 = 1; //results right justified
    ADCON2.B0 = 0;
    ADCON2.B1 = 1;
    ADCON2.B2 = 1;
  
    ADCON2.B3 = 1;
    ADCON2.B4 = 0;
    ADCON2.B5 = 1;

    ADCON1 = 0x05;

    ADCON0.B0 = 1; //turn on ADC

    ADCON0.GO_DONE = 1;
    while(ADCON0.GO_DONE == 1);
    adval = (ADRESH << 8) + ADRESL;

    return adval;
}


     select_channel(1)
     Delay_ms(5);
     usb[0] = (char)ADC_read_implementation();
     select_channel(8)
     Delay_ms(5);

     usb[1] = (char)ADC_read_implementation();
     select_channel(9)
     Delay_ms(5);
     usb[2] = (char)ADC_read_implementation();
 
That's the right sort of thing - but as I've as I've said, the delay required is dependent on the source impedance.

I'd be inclinded to start high (100mS?), and make sure that the controls don't affect each other - then reduce the delay until they start affecting each other, then increase it again.
 
Very strange problem, I disabled all ADC things, and I still use the POT and I still get values on the USB device Joystick, any idea about that problem ?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top