Whenever you are using an port as an analog input, you have to make sure that the port is properly configured using both the TRIS register and the ADCON1 register (or the CONFIG3H register for PortB<4:0>). By default, when the PIC18s reset, all port pins are automatically configured to be analog input (pending on the setting of the CONFIG3H register), but your firmware might have changed this setting prior to using the ADC module.
For instance, to set AN12 to be an analog input, you would need to do the following:
- Set the TRISB register, bit0, to 0x1 (AN12 is on port B0).
- Set PCFG of ADCON1 to 0x0.
For the lower nibble of PortB (PORTB<4:0>), and PBADEN bit of the CONFIG3H register can be set instead of playing with the TRIS settings:
PBADEN: PORTB A/D Enable bit
(Affects ADCON1 Reset state. ADCON1 controls PORTB<4:0> pin configuration.)
1 = PORTB<4:0> pins are configured as analog input channels on Reset
0 = PORTB<4:0> pins are configured as digital I/O on Reset
By default, PBADEN is set to 1, but I do not know what SDCC sets this value if left unspecified.