Title:Adc Using PIC18F4520
Program:
//Author : Palak Patel
//Contact No:9173211683
//Title:ADC 20 V
//Platform: PIC18F4520
//Software MPLAB
#include<p18f4520.h>
#pragma config OSC =INTIO67
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config PBADEN = OFF
#pragma config LPT1OSC = OFF
#pragma config MCLRE = OFF
#pragma config STVREN = OFF
#pragma config LVP = OFF
#pragma config XINST = OFF
void cmd(unsigned char);
void dat(unsigned char);
void Delay_ms(unsigned char);
void main()
{
unsigned char temp[] = "TEMP", tdata[4],i,k;
unsigned int x=0,y,c=0x100,z,x1,DisplayVolt;
unsigned long int x2;
float p,tmpr,f;
TRISA=0x01;
TRISC=0x00;
cmd(0x01); //Clear Lcd
cmd(0x02); //4 Bit Specification
cmd(0x28); //4 Bit specification
cmd(0x0c);
cmd(0x80); //Cursor starting point
cmd(0X06);
i=0;
while(temp[i]!='\0')
{
dat(temp[i]);
i++;
}
cmd(0xC1); //Position second line 2nd character
ADCON1=0x0E;
ADCON0=0x00;
ADCON2=0x9C;
ADCON0bits.ADON = 1;
while(1)
{
ADCON0bits.GO=1;
while(ADCON0bits.GO==1);
x=ADRESH;
y=ADRESL;
z=(x*c)+y;
f=(z*(float)0.2456);
x1=(int)f;
DisplayVolt = x1 * 8;
tdata[0] = DisplayVolt/1000 + 48;
tdata[1] = (DisplayVolt/100)%10 + 48;
tdata[3] = (DisplayVolt/10)%10 + 48;
cmd(0xC2);
dat(tdata[0]);
dat(tdata[1]);
dat(".");
dat(tdata[3]);
}
}
void cmd(unsigned char k)
{
PORTC &=0x0F;
PORTC |=(k & (0xF0));
PORTC |=(2<<0);
Delay_ms(2);
PORTC=PORTC & 0xFC;
Delay_ms(20);
PORTC &=0x0F;
PORTC |=((k<<4) & (0xF0));
PORTC |=(2<<0);
Delay_ms(20);
PORTC=PORTC & 0xFC;
Delay_ms(20);
}
void dat(unsigned char k)
{
PORTC &=0x0F;
PORTC |=(k & 0xF0);
PORTC |=(3<<0);
Delay_ms(20);
PORTC=PORTC & 0xFD;
Delay_ms(20);
PORTC &=0x0F;
PORTC |=((k<<4) & (0xF0));
PORTC |=(3<<0);
Delay_ms(20);
PORTC=PORTC & 0xFD;
Delay_ms(20);
}
void Delay_ms(unsigned char p)
{
unsigned int i,j;
for(i=0;i<p;i++)
{
for(j=0;j<20;j++);
}
}
Simulation:
Program:
//Author : Palak Patel
//Contact No:9173211683
//Title:ADC 20 V
//Platform: PIC18F4520
//Software MPLAB
#include<p18f4520.h>
#pragma config OSC =INTIO67
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config PBADEN = OFF
#pragma config LPT1OSC = OFF
#pragma config MCLRE = OFF
#pragma config STVREN = OFF
#pragma config LVP = OFF
#pragma config XINST = OFF
void cmd(unsigned char);
void dat(unsigned char);
void Delay_ms(unsigned char);
void main()
{
unsigned char temp[] = "TEMP", tdata[4],i,k;
unsigned int x=0,y,c=0x100,z,x1,DisplayVolt;
unsigned long int x2;
float p,tmpr,f;
TRISA=0x01;
TRISC=0x00;
cmd(0x01); //Clear Lcd
cmd(0x02); //4 Bit Specification
cmd(0x28); //4 Bit specification
cmd(0x0c);
cmd(0x80); //Cursor starting point
cmd(0X06);
i=0;
while(temp[i]!='\0')
{
dat(temp[i]);
i++;
}
cmd(0xC1); //Position second line 2nd character
ADCON1=0x0E;
ADCON0=0x00;
ADCON2=0x9C;
ADCON0bits.ADON = 1;
while(1)
{
ADCON0bits.GO=1;
while(ADCON0bits.GO==1);
x=ADRESH;
y=ADRESL;
z=(x*c)+y;
f=(z*(float)0.2456);
x1=(int)f;
DisplayVolt = x1 * 8;
tdata[0] = DisplayVolt/1000 + 48;
tdata[1] = (DisplayVolt/100)%10 + 48;
tdata[3] = (DisplayVolt/10)%10 + 48;
cmd(0xC2);
dat(tdata[0]);
dat(tdata[1]);
dat(".");
dat(tdata[3]);
}
}
void cmd(unsigned char k)
{
PORTC &=0x0F;
PORTC |=(k & (0xF0));
PORTC |=(2<<0);
Delay_ms(2);
PORTC=PORTC & 0xFC;
Delay_ms(20);
PORTC &=0x0F;
PORTC |=((k<<4) & (0xF0));
PORTC |=(2<<0);
Delay_ms(20);
PORTC=PORTC & 0xFC;
Delay_ms(20);
}
void dat(unsigned char k)
{
PORTC &=0x0F;
PORTC |=(k & 0xF0);
PORTC |=(3<<0);
Delay_ms(20);
PORTC=PORTC & 0xFD;
Delay_ms(20);
PORTC &=0x0F;
PORTC |=((k<<4) & (0xF0));
PORTC |=(3<<0);
Delay_ms(20);
PORTC=PORTC & 0xFD;
Delay_ms(20);
}
void Delay_ms(unsigned char p)
{
unsigned int i,j;
for(i=0;i<p;i++)
{
for(j=0;j<20;j++);
}
}
Simulation:
For Basic Electronics Kindaly follow this Link:
No comments:
Post a Comment