You are on page 1of 3

MCH3014 MICROCONTROLLERS LABORATORY

LAB 5 A/D Converter Application

Date
:
Section :

Student Names/ID :

EXPERIMENT 5
A/D Converter Application
Objective
To learn how to program in MikroC to run ADC (Analog to Digital Converter) in Easy Pic 5 board
which is a testbed for PIC programming.

A/D Converter
The EasyPic5 development board has two potentiometers for working with analog-to-digital
converter (ADC). Both potentiometers outputs are in the range of 0-5V. Two analog signals can be
connected to two different analog input pins simultaneously. The jumper group J15 enables
connection between potentiometer P1 and one of the following pins : RA0, RA1, RA2, RA3, or RA4
The jumper group J16 enables connection between potentiometer P2 and one of the following pins:
RA1, RA2, RA3, RA4, or RA5.
In order to measure analog signal without interference, turn off the corresponding switch on the
SW1. This will disable connection between PORTA pins and pull-up/down resistors.

Figure 1: A/D converter

1. ADC Example
Following code reads the voltage input through analog input pin of PIC and prints it on LCD in Volts in
the third decimal place. We use PIC 16F877A.

MCH3014 MICROCONTROLLERS LABORATORY

LAB 5 A/D Converter Application

sbit LCD_RS at RB4_bit;


sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
void main() {
int i, j; // i is the analog reading in mV, and j is the digit to be printed extracted from i .
char text[14]; // The character array which holds the digits of the ADC voltage input.
TRISA.F0=1; // We set the direction of RA0 as input, since we read analog data through this pin
TRISB=0;
ADC_Init();
LCD_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
lcd_out(1,10,"V");
while(1){
i=ADC_READ(0)*(5000/1024); /* ADC_READ(0) is the reading through the analog pin RA0, and is a
number between 0 and 1023 since analog data is 10 bits. Here we convert it to mVolts, given that it
reads a voltage value between 0 and 5 Volts. */

// We print i on the LCD digit by digit, starting from the left.


j=i/1000; /* Division yields a floored result, e.g. 5 /
= . o, here we take the thousa ds place of I */
FloatToStr(j,text); // Convert the digit to text to be able to print on LCD.
lcd_out(1,4,text);
lcd_out(1,5,"."); // Insert the point separating the fractional and integer part
j=i%1000; // Modulus 1000 of i , that ea s, we drop the thousa ds place
j=j/
; // Now we take the hu dreds place
FloatToStr(j,text);
lcd_out(1,6,text);
j=i%100; // Modulus 100 of i , i.e., this ti e we drop the hu dreds place
j=j/ ; // Take the te s place
FloatToStr(j,text);
lcd_out(1,7,text);
2

MCH3014 MICROCONTROLLERS LABORATORY

j=i% ; // Take odulus


FloatToStr(j,text);
lcd_out(1,8,text);
}
}

LAB 5 A/D Converter Application

, that is, drop the te s place, so that fi ally o ly o es place re ai s.

2. ADC Example 2
Write such a code that reads the voltage input through analog input pin of PIC RA0 and displays it on
LCD as an integer in Volts.
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
void main() {

}
}

You might also like