Exp 5
Exp 5
Experiment No: 5
Aim:
To study the programming and interfacing of an 8-bit PIC18F4520 microcontroller.
Problem Statement:
To write a program where the PIC microcontroller is interfaced with ADC and to
transfer ADC values in serial port or LCD screen.
Software Used:
1. MPLAB X IDE v5.50
2. Proteus 8 professional
Code:
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "newfile.h"
void main(void){
TRISAbits.TRISA0=1;
ADCON0=0x01;
ADCON1=0x00;
ADCON2=0x87;
unsigned char x;
unsigned int v1,v2,v3;
TXSTA=0x02;
RCSTA=0x90;
SPBRG=15;
TXSTAbits.TXEN=1;
v3=v2;
while(1)
{
ADCON0bits.GO=1;
while(ADCON0bits.DONE==1);
v1 = ADRESH;
v1 = v1<<8;
v1 = v1+ADRESL;
v2=v1;
if(v3 !=v2){
Sardar Patel Institute of Technology
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai-400058-India Electronics
Engineering Department
Subject: MA Lab Academic Year: 2020-21
v3=v2;
TXREG=((v2/10000)%10)|0x30;
while(PIR1bits.TXIF==0);
TXREG=((v2/1000)%10)|0x30;
while(PIR1bits.TXIF==0);
TXREG=((v2/100)%10)|0x30;
while(PIR1bits.TXIF==0);
TXREG=((v2/10)%10)|0x30;
while(PIR1bits.TXIF==0);
TXREG=(v2%10)|0x30;
while(PIR1bits.TXIF==0);
TXREG='_';
while(PIR1bits.TXIF==0);
}
}
return;
}
Circuit Diagram:
Sardar Patel Institute of Technology
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai-400058-India Electronics
Engineering Department
Subject: MA Lab Academic Year: 2020-21
Output:
ADC value: (for 30%op is 00307)
Conclusion:
➢ In this experiment, I learnt how to program a PIC18F4520 microcontroller and
print numbers on virtual terminal
➢ I used POT-HG which is a variable resistor that allows to change the resistance
during simulation run-time
➢ The AN0(channel 0) pin is used from a PIC microcontroller. The resolution of
the 10-bit ADC is [5/ (1024)] that is 4.8828 mV as Vrefs are not used.
➢ Thus we successfully interfaced a PIC microcontroller