Analog to Digital Converter (ADC) in PIC Microcontroller 20

Posted By Ligo George

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  =  0 0 0 0

5v  =  1  1  1  1

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 Source Code:

unsigned int adc;
void main()
{
 CMCON = 0x07;
 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.

 


  • Blinkcruz

    hi..do have a program of it in flowcode?..

  • http://www.electrosome.com/ Ligo George

    Sorry …………..
    This link may help you……
    http://www.matrixmultimedia.com/mmforums/viewtopic.php?f=5&t=2562

  • Mahmud Ibrahim

    Very good tutorial.keep posting.
     

  • Aimanbanifadel

    hello ligo ,  the thermometer project show on the lcd temprature : and the value was 414 dgree

  • Aimanbanifadel

    then what is the problem as you think

  • http://www.facebook.com/lijoppans Ligo George

    I don’t understand your question…..Please elaborate ……..

  • Aimanbanifadel

    hi, i built the project very good but the result of the lcd is not correct the thermometer does not show the room temperature but show the 414 degree then i need to know where is the problem is it in the hex file or in the sensor ………note (i connect vref+ on5v……..and vref- on 0v) thanks for your care

  • Ligo George

    You needn’t connect +Vref & -Vref………..

    Check the output voltage of the LM 35 temperature sensor..

  • noel george paqueo

    can someome share the code for this project? thanks a lot

  • http://www.electrosome.com/ Ligo George

    Code is shared in the article itself…..

  • lusk

    dont u need to set the ansel byte??

  • http://www.electrosome.com/ Ligo George

    Nop, There is no register called ANSEL in PIC 16F877A……… it is in PIC 16F877 …

  • jaycee

    hi can you teach me a code for a IR motor speed control. using a program which can read a increment or decrement of speed and will vary the speed of the motor. The increment and decrement is based on the transmitter part of the IR transmitter. thanks a lot :)

  • jaycee

    and also there is a LED that will indicate the degree or level how fast the speed of the motor. for example. when you turn on the device, there is only one LED that is on, then when the increment button is pressed, the second LED will turn on. When the decrement button is pressed, the second LED will turn off and will return to only one LED is on.

  • http://www.electrosome.com/ Ligo George

    This comment is not related to the above article…..

    Please ask these doubts in our new forum:
    http://www.electrosome.com/forum/

  • sandeep bhardwaj

    Hii…In every tutorial different frequency of crystal has been used. but my board contains only 20Mhz crystal….but how will it be posssible to use different frequency crystal at the same for the two applications which are going to be perfermed simultaneously.

  • http://www.electrosome.com/ Ligo George

    I have used 8MHZ crystal for most of the projects and tutorials…
    You can use 20MHZ, simply by changing the oscillator frequency in MikroC settings… before compiling…

  • romli

    how to get 8 bit adc?

  • http://www.electrosome.com/ Ligo George

    Neglect the least significant 2 bits…

  • Pingback: xbee voltmeter using pic16f887