Analog to Digital Converter (ADC) in PIC Microcontroller
ADC module of PIC microcontroller have usually 5 input for 28 pin devices and 8 inputs for 40 pin devices. The conversion of analog signal to PIC ADC module results in corresponding 10 bit digital number. PIC ADC module has software selectable high and low voltage reference input to some combination of VDD, VSS, RA2 and RA3.
In the following example project we will convert analog input to channel 1 to 10 bit digital number with low voltage reference (Vref-) 0v and high voltage reference (Vref+) 5V. The output will be displayed using 10 LEDs. You can change the Vref- and Vref+ by configuring the ADCON1 register.
ie,
0v = 0000000000
5v = 1111111111
Resolution = (Vref+ – Vref-)/(1024-1) (as it is 10 bit ADC)
= 5/1023
= 4.887 mV
Thus it means that for a change in 4.887mV, the binary output changes by 1.
Circuit Diagram
Note: VDD and VSS of the pic microcontroller is not shown in the circuit diagram. VDD should be connected to +5V and VSS to GND.
MikroC Program
unsigned int adc; void main() { ADCON1 = 0x80; TRISA = 0xFF; // PORTA is input TRISC = 0x3F; // Pins RC7, RC6 are outputs TRISB = 0; // PORTB is output do { adc = ADC_Read(1); // Get 10-bit results of AD conversion //of channel 1 PORTB = adc; // Send lower 8 bits to PORTB PORTC = adc >> 2; // Send 2 most significant //bits to RC7, RC6 } while(1); }
You can vary the analog input value by varying the resistance of the potentiometer in the circuit.
This tutorial covers only the basic ADC operation. If you want to know more about it, please refer the datasheet of PIC 16F877A. If you have any doubt regarding this please do comment.