I am trying to read analogic signal for a sort of mouse with a pic18f14k50 controller. Here the simple circuit: http://dl.dropbox.com/u/14663091/schematiconew.pdf . I have to read analogic signal from AN9 circuit port. Main function reads from the port, and blinks 30 time if threshold is reached:

void main(void) 

    #if defined(USB_INTERRUPT)

    while(1) {

        if ((USBDeviceState < CONFIGURED_STATE) || (USBSuspendControl == 1)) continue;

        if (!HIDTxHandleBusy(lastTransmission)) {
            int readed = myReadADC2(); //Here i tried both myReadADC2() or myReadADC1()
            if (readed > 40) { //If read threshold > 40, blink led 30 times
                int i;
                for (i = 0; i < 30; i++) {
        lastTransmission = HIDTxPacket(HID_EP, (BYTE*)hid_report_in, 0x03);

        } /* [Ed: added] */
    }//end while
}//end main

I used two method to read from the AN9 port, myReadADC() that uses OpenADC() API method:

int myReadADC(void) 
    #define ADC_REF_VDD_VDD_X 0b11110011                      
            ADC_CH9 & ADC_INT_OFF,
            0b00000010); // channel 9
    ConvertADC();                 // Start conversion
    while (BusyADC());             // Wait for completion
    return ReadADC();           // Read result

and myReadADC2(), that implements manual read from the port.

int myReadADC2() 
    int iRet;
    OSCCON = 0x70;         // Select 16 MHz internal clock
    ANSEL = 0b00000010;  // Set PORT AN9 to analog input
    ANSELH = 0;          // Set other PORTS as Digital I/O
    /* Init ADC */
    ADCON0 = 0b00100101;   // ADC port channel 9 (AN9), Enable ADC
    ADCON1 = 0b00000000;   // Use Internal Voltage Reference (Vdd and Vss)
    ADCON2 = 0b10101011;   // Right justify result, 12 TAD, Select the FRC for 16 MHz
    iRet = 100;

    ADCON0bits.GO = 1;
    while (ADCON0bits.GO);   // Wait conversion done
    iRet = ADRESL;           // Get the 8 bit LSB result
    iRet += (ADRESH << 8); // Get the 2 bit MSB result
    return iRet; 

Both cases doesn't works, i touch (sending analogic signal) port AN9 but when I set high threshold (~50) led don't blinks, with low threshold (~0) it blinks immidiatly when i provide power to the PIC. Maybe i'm using wrong port? I'm actually passing AN9 as reading port? Or maybe threshold is wrong? How can i found the right value? Thank you

Here the MPLAB C18 Apis http://dl.dropbox.com/u/14663091/API%20microchip%20C18.pdf .

  • \$\begingroup\$ I can't access dropbox where I'm at right now, but I know one thing that stinking messed with me was making sure I was using a pullup or pulldown...have you checked that? (It might not be your actual code at all that is causing the problem, but this is just speculation) \$\endgroup\$ – onaclov2000 Dec 13 '10 at 20:33
  • \$\begingroup\$ Did you ever get this squared away? \$\endgroup\$ – onaclov2000 May 9 '11 at 13:15
  • 1
    \$\begingroup\$ And now the link is dead. This is why we don't use dropbox. \$\endgroup\$ – Kevin Vermeer May 30 '12 at 21:16

I think there is a bug in your myReadADC2() sub...

try return iRet; instead of return iDelay;

  • \$\begingroup\$ Sorry, the real code is correct, it was only a typo. \$\endgroup\$ – Emilio Dec 14 '10 at 15:45

The PIC ADC has a surprisingly low input impedance. I'm not sure touching the input is going to work well. Most of the time, you will need an op-amp to buffer the signal into the PIC, unless your signal is naturally low-impedance.

Read the datasheet carefully. It would be nice if datasheets put known limitations and gotchas is large bold print, but usually they are hidden in footnotes and electrical characteristic tables.

  • \$\begingroup\$ This has been my experience t0o. Especially true of many of the analog mux built into the PICs. \$\endgroup\$ – kenny Dec 17 '10 at 23:51

Add a connector for ICSP/debugging, and use a PICkit or ICD 3 for debugging your code. You can then concentrate on getting the ADC working, and then sort out the USB code.

You will find things much easier if you use the Microchip ADC code in their libraries.


Your schematic seems to indicate that nothing is connected to pin 9, which happens to be AN9.

Could you, at least temporarily, put a button with a pullup resistor -- identical to the one you already have on pin 5 -- so we know that the pin actually is being pulled high and low?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.