TUTORIAL ON A/D CONVERTER PIC

on Saturday, 16 November 2013


In our next post we will look at the temperature sensor project which is based on A/D converter, so its necessary for us to understand concepts of A/D converter of PIC microcontroller.
Before we try to understand Analog to Digital Converter, we need to look few important terminology while converting Analog quantities to the Digital values.

Physical data like temperature, pressure, humidity,etc is converted to the electrical signal(voltage/current/charge/capacitance) using TRANSDUCER. If we want to convert temperature to electrical signal we use temperature sensor, similarly we use corresponding sensors for this purpose.

As Transducer's output will be in the form of voltage, current, charge,etc it has to be converted to voltage using SIGNAL CONDITIONING in order to send to A/D converters. (Now a days sensors performs this action).

Now A/D CONVERTER takes the voltage signal and converts to digital data and fed it to Microcontroller. As our microcontroller have A/D converter we need not to worry much.

 ANALOG WORLD(temp, pressure, etc)
l
l
TRANSDUCER
l
l
SIGNAL CONDITIONING
l
l
Analog to Digital Converter
l
l
MICROCONTROLLER

In our case 2nd step and 3rd will be performed by Sensor and last two steps are performed by Microcontroller. 

A/D CONVERTER: 

PIC18f452 have inbuilt A/D converter of 10bits wide having 8  analog pins AN0 TO AN7(remember from the pin diagram). So it is  not necessary to interface a separate A/D converter module to our microcontroller. 


Step size =  Vref / max bit value

Vref = User should choose, we are taking 5v
Max bit value1024. As i said A/D in Pic18f452 is 10 bits wide so max binary value for 10bits 1111111111 , when we convert it in decimal it will be 1023(starts from 0) so total 1024.

Step size =5v/1024 = 5000mv/1024 = 4.88mv

Whatever value we get from the A/D converter we need to multiply it by 5000/1024 (i.e 4.88) to get actual value.
In mikroC we have library functions for reading analog values from Analog pins,but as i said in my previous post that  it is not a good practice to use  library functions. So we dont use library function for reading analog values from the sensor. 

STEPS FOR READING  ANALOG INPUTS:
Read PIC18f datasheet to understand it better but i'll cover all important steps here. 
A/D module have 4 registers. They are ADRESH, ADRESL, ADCON0, ADCON1 and they all are 8 bit registers.

ADRESH= A/D Result High Register 
ADRESL= A/D Result Low Register
ADCON0= A/D Control 0
ADCON1= A/D Control 1

After Analog values have been converted to Digital, the result will be saved in ADRESH & ADRESL registers they are of 8 bits wide each. But do you remember i said that A/D of PIC is of 10bits wide then how can result of 10 bits will be saved in 8bit registers, that is the reason why it have two registers ADRESH & ADRESL total of 8+8 i.e 16 bits in which 10bit result will be saved.  

We have provision of selecting result in the right side or left side of registers using control register(ADCON1).
ADCON0 & ADCON1 are control registers, they are used to select the Vref , how many number of analog inputs enable, starting the A/D conversion , selecting Right justified or Left justified, etc
ADCON1:  This register configures the function of the port pins. Lets see functionality of each pin 
Bit7= if 1 then Right justified
            0  then Left justified
Bit6= clock bit just take it as 0 
Bit5, Bit 4= not implemented, so take as 0
Bit3, Bit2, Bit1, Bit 0 =  These 4 bits are used to configuring port pins i.e whether port pins are used as analog or digital, which pin is used as Vref source, etc  look at the below table
If we want all 8 pins (AN7 to AN0) as analog pins then we use 0000,  Vdd acts as +Vref and Vss acts as -Vref.
If we want AN7 to AN4 and AN2 to AN0 as analog pins then we use 0001 then AN3 pin acts as +Vref and Vss acts as -Vref. 
So on... 
In our project we make all Analog pins to active i.e AN0 to AN7. We make lower 4 bits(bit0 to bit3) of ADCON1 register to 0000. 

So what will be the value to be stored in ADCON1 register for right justified and enable all analog pins.

ADCON1=0b10000000//Right justified, 8 bits analog purpose,etc

ADCON0:This register controls the A/D converter module i.e to start the module, to stop the module, selecting the particular analog pin , etc. 
Bit7, Bit6 = Clock select, just make it 0
Bit5-3 = Select the analog pins
              0 0 0 = AN0 
              0 0 1 = AN1
              0 1 0 = AN2
              0 1 1 = AN3
              1 0 0 = AN4
              1 0 1 = AN5
              1 1 0 = AN6
              1 1 1 = AN7
For example sensor is connected to AN3 pin then we make bits5-3 as 011, if sensor is connected to pin AN7 then we make bits 5-3 as 111. 
In our project we use AN1 pin of microcontroller so we make bits5-3 as 010.

Bit2 = Go/Done bit = To start A/D conversion
           1= Start the A/D conversion
           0= conversion completed/ stops

Bit1= unused i.e 0
Bit0 = A/D ON bit, if 1 then A/D will be ON but not started untill bit2 is high. 
So to start the conversion Bit0 should be 1 and Bit1 should be 1. When the conversion completes automatically bit1 will becomes 0. 

ADCON0=0b00000001 // ON A/D converter
ADCON0=0b00000011 // ON A/D converter & Start A/D conversion

We are almost done, lets see each step one after the other in steps. 

STEPS FOR PROGRAMMING:

STEP1: Configure the A/D module
  • Configure analog pins, voltage reference (Vref), and digital I/O (i.e ADCON1 register)
  • Select A/D input channel( i.e ADCON0)
  • Turn ON A/D converter(using ADCON0)
 ADCON1=0b10000000
ADCON0=0b00000001


STEP2: Wait for little time, lets say 200ms 
Delay_ms(200)


STEP3: Start conversion (using ADCON0)
ADCON0.F2=1

STEP4: Wait until ADCON0.F2 bit is 0 i.e whenever it is 0 it means conversion have been completed
while(ADCON0.F2= = 1)
                                                              {
                                                                  }

i.e stay in loop until conversion is not over, when conversion is over come out of loop. Above statement can also be written as
while(ADCON0.F2 = = 1) ;
NOTE: You can use interrupts by replacing while loop.

STEP5: If control comes out of the loop, then save the  digital output from registers ADRESH, ADRESL . 
variable_x = ADRESH;
variable_y=ADRESL;

STEP6: Convert both the 8 bit data to a single 16 bit data. 
                                                int variable_x,variable_y, AD_result;
                                                variable_x=ADRESH;
                                                variable_y=ADRESL;
                                                AD_result= (variable_y<<8) | (variable_x); 

STEP7: Multiply result with the step size (remember i have discussed at start) i.e 5000/1024
Result= AD_result * 5000/1024;
 
That's it , now variable 'Result' have the output of A/D converter. We will do a project using A/D converter in our next post.


AUTHOR: YOUSTRON SIC




























 

























0 comments:

Post a Comment