Docsity

Analog to Digital Converter - Microprocessor Interfacing - Lab Manual, Exercises of Microprocessor and Interfacing

Toggle sidebar
bg1
1
Experiment4
AnalogtoDigitalConverter
1. WriteaCprogramtoconvertananalogsignalatAN1todigitalvalue.Thedigitalvalue
willberepresentedby8‐LEDsconnectedatPORTB(refertoFigure1).
Figure1
ExampleProgram
5V
GND
to AN1
POTENTIOMETER
AN1
5V
GND
PIC18F4580
Resistors
LEDs
PORTB
8 8
#include<p18f4580.h>
#pragmaconfigOSC=HS
#pragmaconfigWDT=OFF
#pragmaconfigLVP=OFF
unsignedcharSample_AN0; //VariabletostoreA/Dresult
voidGetVoltage(void)
{
ADCON0bits.GO=1; //startaconversion(sampling)
while(ADCON0bits.GO==1);//waitforcompletion
Sample_AN0=ADRESH; //TransferA/Dresult
}
voidResult(void)
{
PORTB=Sample_AN0;
}
voidmain(void) //mainprogram
{
ADCON1=0x00; //AN10‐AN0areanalog,VDDandVSSarereferences
ADCON2=0x05; //Leftjustified,convertusing1.25MHz
TRISB=0; //PortBasoutput
while(1)
{
ADCON0=0b00000101;//selectinputAN1,enableADC
GetVoltage();
Result();
}
}
Docsity.com
pf2

Partial preview of the text

Download Analog to Digital Converter - Microprocessor Interfacing - Lab Manual and more Exercises Microprocessor and Interfacing in PDF only on Docsity!

Experiment 4

Analog to Digital Converter

1. Write a C program to convert an analog signal at AN1 to digital value. The digital value

will be represented by 8 ‐LEDs connected at PORTB (refer to Figure 1).

Figure 1

Example Program

5V

GND

to AN

POTENTIOMETER

AN

5V

GND

PIC18F

Resistors PORTB LEDs 8 8

#include <p18f4580.h> #pragma config OSC = HS #pragma config WDT = OFF #pragma config LVP = OFF

unsigned char Sample_AN0; // Variable to store A/D result

void GetVoltage(void) { ADCON0bits.GO = 1; // start a conversion (sampling) while ( ADCON0bits.GO == 1 ); // wait for completion Sample_AN0 = ADRESH; // Transfer A/D result }

void Result(void) { PORTB = Sample_AN0; }

void main (void) // main program { ADCON1 = 0x00; // AN10 ‐ AN0 are analog, VDD and VSS are references ADCON2 = 0x05; // Left justified, convert using 1.25 MHz TRISB = 0; // PortB as output

while (1) { ADCON0 = 0b00 0001 01; // select input AN1, enable ADC GetVoltage(); Result(); } }

Docsity.com

Design Problem:

1. Write a C program to perform the analog‐to‐digital conversion on AN0 and AN1 channels

(refer to Figure 2) and complete the following task according to the Table1 below:

Table 1

Case Output

AN0 > AN1 PORTB = 0xF

AN0 < AN1 PORTB = 0x0F

AN0 = AN1 LEDs blinking

Figure 2

2. With refer to the example program; modify the program so that it will send the A/D

result to the PC using serial communication interface. Use the HyperTerminal to observe

the output.

5V

GND

to AN

POTENTIOMETER

AN

AN

5V

GND

PIC18F

Resistors PORTB LEDs 8 8

5V

GND

to AN

POTENTIOMETER

Docsity.com