Experiment 10 Analog-to-Digital Converter (ADC) in PIC18F452 Objective

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

EXPERIMENT 10

Analog-to-Digital Converter (ADC) in PIC18F452

Objective:

 Familiarizing students with ADC in PIC18F452 and writing codes in MikroC to convert analog
values to digital values

Equipment:

 Hardware:
1. PIC18F452 microcontroller
2. 16x2 LCD
3. RIMS Trainer
4. DMM
 Software:
1. mikroC Pro
2. SmartPRO 5000U
3. Proteus

Introduction:

The Analog-to-Digital (A/D) converter module has five inputs for the PIC18F2X2 devices and eight for
the PIC18F4X2 devices. This module has the ADCON0 and ADCON1 register definitions that are
compatible with the mid-range A/D module. The A/D allows conversion of an analog input signal to a
corresponding 10-bit digital number.

The A/D module has four registers. These registers are:

 A/D Result High Register (ADRESH)


 A/D Result Low Register (ADRESL)
 A/D Control Register 0 (ADCON0)
 A/D Control Register 1 (ADCON1)

The ADCON0 register controls the operation of the A/D module. The ADCON1 register configures the
functions of the port pins.

ADCON0 Register:
The ADCON0 register with detail explanation of function of each bit is given below:

ADCON1 Register:

The ADCON1 register with detail explanation of function of each bit is given below:
where:

A = Analog input D = Digital I/O

C/R = # of analog input channels / # of A/D voltage references

The analog reference voltage is software selectable to either the device’s positive or negative supply
voltage (VDD and VSS), or the voltage level on the RA3/AN3/VREF+ pin and RA2/AN2/VREF- pin.
The A/D converter has a unique feature of being able to operate while the device is in SLEEP mode. To
operate in SLEEP, the A/D conversion clock must be derived from the A/D’s internal RC oscillator. A
device RESET forces all registers to their RESET state. This forces the A/D module to be turned off and
any conversion is aborted.

Each port pin associated with the A/D converter can be configured as an analog input (RA3 can also be a
voltage reference) or as a digital I/O. The ADRESH and ADRESL registers contain the result of the A/D
conversion. When the A/D conversion is complete, the result is loaded into the ADRESH/ADRESL
registers, the GO/DONE bit (ADCON0<2>) is cleared, and A/D interrupt flag bit, ADIF is set.

Steps for AD Conversion:

After the A/D module has been configured as desired, the selected channel must be acquired before the
conversion is started. The analog input channels must have their corresponding TRIS bits selected as an
input. The following steps should be followed for doing an A/D conversion:

1. Configure the A/D module:


 Configure analog pins, voltage reference and digital I/O (ADCON1)
 Select A/D input channel (ADCON0)
 Select A/D conversion clock (ADCON0)
 Turn on A/D module (ADCON0)
2. Configure A/D interrupt (if desired):
 Clear ADIF bit
 Set ADIE bit
 Set GIE bit
 Set PEIE bit
3. Wait the required acquisition time.
4. Start conversion:
 Set GO/DONE bit (ADCON0)
5. Wait for A/D conversion to complete, by either:
 Polling for the GO/DONE bit to be cleared (interrupts disabled)
OR
 Waiting for the A/D interrupt
6. Read A/D Result registers (ADRESH/ADRESL); clear bit ADIF if required.
7. For next conversion, go to step 1 or step 2 as required.

Selecting A/D Conversion Clock:

The A/D conversion time per bit is defined as TAD. The A/D conversion requires 12 TAD per 10-bit
conversion. The source of the A/D conversion clock is software selectable.

Configuring Analog Port Pins:

The ADCON1, TRISA and TRISE registers control the operation of the A/D port pins. The port pins that
are desired as analog inputs, must have their corresponding TRIS bits set (input). If the TRIS bit is
cleared (output), the digital output level (VOH or VOL) will be converted. The A/D operation is
independent of the state of the CHS2:CHS0 bits and the TRIS bits.

Note that:
 When reading the port register, all pins configured as analog input channels will read as cleared
(a low level). Pins configured as digital inputs will convert an analog input. Analog levels on a
digitally configured input will not affect the conversion accuracy.
 Analog levels on any pin that is defined as a digital input (including the AN4:AN0 pins) may
cause the input buffer to consume current that is out of the device’s specification.

Clearing the GO/DONE bit during a conversion will abort the current conversion. The A/D result register
pair will NOT be updated with the partially completed A/D conversion sample. That is, the
ADRESH:ADRESL registers will continue to contain the value of the last completed conversion (or the
last value written to the ADRESH:ADRESL registers). After the A/D conversion is aborted, a 2 TAD
wait is required before the next acquisition is started. After this 2 TAD wait, acquisition on the selected
channel is automatically started. The GO/DONE bit can then be set to start the conversion.

A/D RESULT REGISTERS:

The ADRESH : ADRESL register pair is the location where the 10-bit A/D result is loaded at the
completion of the A/D conversion. This register pair is 16-bits wide. The A/D module gives the flexibility
to left or right justify the 10-bit result in the 16-bit result register. The A/D Format Select bit (ADFM)
controls this justification. Figure below shows the operation of the A/D result justification. The extra bits
are loaded with ’0’s. When an A/D result will not overwrite these locations (A/D disable), these registers
may be used as two general purpose 8-bit registers.

Clock Selection:

 TAD is conversion time per bit


 10 bit A/D conversion time = 12 TAD
 After a conversion has completed, a 2 TAD delay must be provided before the acquisition starts
again
 Clock must be selected such that
“TAD ≥ 1.6 μs”
Example:
For Crystal of 10 MHz, what should be the clock source?
Rule: Select clock such that clock cycle time ≥ 1.6 μs
FOR 10 MHz Xtal if Clock Source “Fosc/2” is used then TAD=2/10Mhz=0.2us<1.6us so this cannot be
used. If Clock Source “Fosc/4” is used then TAD=4/10Mhz=0.4us<1.6us so this cannot be used also. If
Clock Source “Fosc/8” is used then TAD=2/10Mhz=0.8us<1.6us so this cannot be used as well. If Clock
Source “Fosc/16” is used then TAD=16/10Mhz=1.6us≥1.6us so clock source must be less than or equal
to “Fosc/16”.
A/D Conversion Example:
The resolution of a 10-bit ADC and step size is:

This is a 10bit ADC


If
V ref += 5V then step size
So, Resolution is: is:
10
2 −1 =1023 5/1023=4.88mV
4.88mV*Analog Input =
Digital Output.

Lab Task 1:
Write a code which takes analog input and converts it into digital data then sends its digital
output on LEDs. Verify the results on Proteus and hardware.

PROCEDURE:
We will make port A as input port and make port C and port D as output port. Next we need to set
value in ADCON0 and ADCON1 register .Run an infinite loop using while .Move the output to port C
and port D using ADRESL and ADRESH register. Build the code. Burn the hex file of the code in proteus
and see the results.
CODE:
void main() {
TRISA=0xFF;
TRISC=0x00;
TRISD=0x00;
ADCON0=0b11000101;
ADCON1=0b11001111;
while(1){ delay_ms(1);
Adc_Read(0);
PORTC=ADRESL;
PORTD=ADRESH; } }
PROTEUS SIMULATION:

Lab Task 2:
Write a code which takes analog input and converts it into digital data then sends its digital
output on LCD. Verify the results on Proteus and hardware.

PROCEDURE:
Set the module connections of LCD . In main function we will make port A as input port and set the
values for ADC using ADCON0 and ADCON1 register. Then Initialize and clear the LCD using built in
functions of LCD. Send the input value of ADC on LCD .Build the hex file in proteus and see the results.
CODE:
sbit LCD_RS at RC2_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;
sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;
char text[10];
unsigned long temp;
void main() { TRISA=0xFF;
ADCON0=0b11000101;
ADCON1=0b11001111;
Lcd_Init
Lcd_Cmd(_LCD_CLEAR
Lcd_Cmd(_LCD_CURSOR_OFF);
do { temp = ADC_Read(0
if(temp>=1){ LCD_Cmd(_LCD_CLEAR);
intTostr(temp,text);
LCD_Out(1,1,text); }
if(temp==0){ LCD_Cmd(_LCD_CLEAR);
LCD_Out(1,1,'0');}
Delay_ms(500); }
while(1);}
PROTEUS SIMULATION:
Lab Task 3:
Perform Task#1 again but without using built in ADC Library.

PROCEDURE:
We will make port A as input port and make port C and port D as output port. Next we need to set
value in ADCON0 and ADCON1 register .Run an infinite loop using while .Move the output to port C
and port D using ADRESL and ADRESH register. Build the code. Burn the hex file of the code in proteus
and see the results
CODE:
void main() {
TRISA=0xFF;
TRISC=0x00;
TRISD=0x00;
ADCON0=0b11000101;
ADCON1=0b11001111;
while(1){
delay_ms(1);
while(ADCON0.GO_DONE==1);
PORTC=ADRESL;
PORTD=ADRESH;
delay_ms(250); } }
PROTEUS SIMULATION:

Conclusion:

This lab was all about ADC in PIC18F452 We were able write codes in MikroC to convert analog
values to digital values. As 8051 did not provide us the facility of configuring an analogue signal , pic has
many advvanced functionalities.

You might also like