# Problems with PIC A/D conversion

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)
{
InitializeSystem();

#if defined(USB_INTERRUPT)
USBDeviceAttach();
#endif

while(1) {

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

if (!HIDTxHandleBusy(lastTransmission)) {
int i;
for (i = 0; i < 30; i++) {
Delay1KTCYx(0);
mLED_1_On();
Delay1KTCYx(0);
mLED_1_Off();
}
}
lastTransmission = HIDTxPacket(HID_EP, (BYTE*)hid_report_in, 0x03);

}//end while
}//end main


int myReadADC(void)
{
0b00000010); // channel 9
while (BusyADC());             // Wait for completion
}


int myReadADC2()
{
int iRet;
OSCCON = 0x70;         // Select 16 MHz internal clock
ANSELH = 0;          // Set other PORTS as Digital I/O
ADCON1 = 0b00000000;   // Use Internal Voltage Reference (Vdd and Vss)
ADCON2 = 0b10101011;   // Right justify result, 12 TAD, Select the FRC for 16 MHz
iRet = 100;

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 .

• 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) – onaclov2000 Dec 13 '10 at 20:33
• Did you ever get this squared away? – onaclov2000 May 9 '11 at 13:15
• And now the link is dead. This is why we don't use dropbox. – Kevin Vermeer May 30 '12 at 21:16

try return iRet; instead of return iDelay;

• Sorry, the real code is correct, it was only a typo. – 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.

• This has been my experience t0o. Especially true of many of the analog mux built into the PICs. – 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?