After some of you got a little sober after the smooth operator 60s' twists, here's an an example of ADC usage in MikroC.
The codes are commented so you should roughly knows what the codes is all about and what each line is doing. I'll do a write up on ADC on the next post. In most cases, some form of scaling is required so some understanding of ADC reference voltage is crucial. How the board is wired up for the lesson... noticed the purposed-built LCD ribbon cable? Update 080425: A short brief on ADC. PIC18F4550 (and most ADC capable PIC) has a 10-bit ADC core. The ADC is based on analog reading between VRef- to VRef+. When input is at VRef-, the value returned will be 0 (zero) and when input is at VRef+, the value returned will be 1023 (2^10 - 1, i.e., 2 to the power of 10 minus 1). In the current example, via setting done with ADCON1, VRef- is set to VSS (GND) and VRef+ is set to VDD (+5V). Under this environment, the resolution of the ADC is +5V/1024 = about 5mV. When the potentiometer is at minimum (0V) the result is 0 and when at maximum (+5V) the result is 1023. To get the actual voltage reading, you need to do scaling, the math as follows: Actual Voltage = (ADC Reading * Value at VRef+) / 1024 i.e., if result is 512, then the actual voltage is 512 * 5 / 1024 = 2.5V
If you need higher resolution, you can provide the PIC with your own VRef- & VRef+ directly to the AN2 & AN3 pins. Change ADCON1 settings to 0b00111011. If you set VRef+ to about 1V, the resolution becomes 1mV (1V/1024).
For quick reference, ADCON1 setting information is below: On PIC18F4550, the analog pins are on 3 separate ports... do your own searching below... Remember to set TRISA, TRISB, TRISE and ADCON1 configuration accordingly...