Lab 06-Application and Understanding of Analog To Digital Conversion (A/D)

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Real Time Embedded Systems Lab Department of Electrical & Computer Engineering

Lab 06- Application and understanding of Analog to Digital Conversion


(A/D).
Task 01:
Make a Voltmeter that measures from 0 volts to +100 volts and shows the Output on LCD.
C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xc.h>
#include "config.h"
void cmd(unsigned char);
void dat(unsigned char);
void main(void)
{
int i=0;
TRISD=0x00;
TRISC=0x00;
TRISAbits.RA0=1;
ADCON0 = 0x81;
ADCON2 = 0xCE;
cmd(0x01);
cmd(0x80);
cmd(0x0C);
cmd(0x06);
cmd(0x38);
while(1)
{
ADCON0bits.GO_DONE = 1;
while( ADCON0bits.GO_DONE==1 );
unsigned int z =((ADRESH<<8)+ADRESL);
float volt=z*(0.0895*(100/2));
unsigned char s[25];
sprintf(s,"%2.2fVolts",volt);
while(s[i]!='\0')
{
dat(s[i]);
i++;
}
__delay_ms(150);
cmd(0x01);
i=0;
}
}
void cmd(unsigned char k)
{
LCD=k;
RS=0;
RW=0;
EN=1;
__delay_ms(150);
EN=0;
}

Registration No: FA18-BSEE-019


Real Time Embedded Systems Lab Department of Electrical & Computer Engineering

void dat(unsigned char k)


{
LCD=k;
RS=1;
RW=0;
EN=1;
__delay_ms(150);
EN=0;
}

Config File:
// PIC18F4520 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1H
#pragma config OSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3 // Brown Out Reset Voltage bits (Minimum setting)

// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN
bit))
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = OFF // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on
Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power
operation)
#pragma config MCLRE = OFF // MCLR Pin Enable bit (RE3 input pin enabled; MCLR disabled)

// CONFIG4L
#pragma config STVREN = OFF // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed
Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
Registration No: FA18-BSEE-019
Real Time Embedded Systems Lab Department of Electrical & Computer Engineering

#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-
3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table
reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table
reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table
reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table
reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected
from table reads executed in other blocks)

// #pragma config statements should precede project file includes.


// Use project enums instead of #define for ON and OFF.

#define LCD PORTD


#define RS PORTCbits.RC0
#define RW PORTCbits.RC1
#define EN PORTCbits.RC2

#define _XTAL_FREQ 8000000


Proteus Circuit Diagram

FIGURE 1:CIRCUIT AND WORKING CIRCUIT

Registration No: FA18-BSEE-019


Real Time Embedded Systems Lab Department of Electrical & Computer Engineering

Task 02:
Describe A/D Registers in detail with each bit and its configuration.
Answer:-

PIC18 ADC peripheral has 10 bit ADC.It can have 5 to 15 channels of analog input channel, depending
upon the pic family. In PIC4520/458, pins RA0-RA7 of the PORTA are used for the 8 analog channels. The
converted output binary data is held by two special function register called ADRESL(A/D Result low) and
ADRESH(A/D Result high). Because the ADRESL: ADRESH register gives us 16 bits and ADC data out is
only 10 bit, 6 bit of 16 are unused. We have option of either making upper or lower bit unused. We have
option of using Vdd the voltage source of the PIC18 chip itself as the Vref or connecting it to an external
voltage source of Vref.

ADCON0 Register.
The ADCON0 Register is used to set the conversion time and select the analog input channel among other
things. In order to reduce the power consumption of PIC ADC feature is turned off when the
microcontroller is powered up. We turn on ADC with the ADON bit of ADCON0 Register. The other
important bit is the GO/DONE bit. We use this bit to start conversion and monitor it to see if conversion has
ended

Registration No: FA18-BSEE-019


Real Time Embedded Systems Lab Department of Electrical & Computer Engineering

FIGURE 2:ADCON0 REGISTER

ADCON1 Register.
This is another major register of PIC18 ADC. The ADCON1 Register is used to select the Vref Voltage
among other things. After A/D conversion is complete the results are in ADRESL(A/D Result low) and
ADRESH(A/D Result high) the ADFM bit of the ADCON1 Register is used for making it right -justified or
left-justified because we only need 10 out of 16 bits.

Registration No: FA18-BSEE-019


Real Time Embedded Systems Lab Department of Electrical & Computer Engineering

Analysis:
In this lab I’ve learned about ADC module how it performs analog to digital conversion in the PIC18 family.
I’ve Also learned about different register that are used by ADC module and how different bits of the register perform
differentoperation.
In the task 1 I’ve made a voltmeter by using the ADC which display the voltage from 0 to 100 on the LCD.

Registration No: FA18-BSEE-019

You might also like