//********************************************************************************
//Author : Palak Patel(M.Tech)
//Contact No:9173211683
//Title:Current Measurement Using PIC 18 Microcontroller
//Platform: pic18f4520
//Software:MPLAB
//********************************************************************************
Program:
#include<p18f4520.h>
#pragma config OSC = HS
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config MCLRE = OFF
#pragma config PBADEN = OFF
#pragma config STVREN = OFF
#pragma config LVP = OFF
void delay();
void send_Value(unsigned char p[5]);
void space();
void main()
{
unsigned char temp[] = "TEMP";
unsigned char tdata[4]=" . ",i=0;
unsigned int x=0,y,c=0x100,z,x1,DisplayVolt,e;
unsigned char k;
unsigned long int x2;
float p,tmpr,f,a,b,d;
TRISA=0x03;
TRISC=0x00;
SPBRG=125;
TXSTAbits.CSRC=1; //Master Mode
TXSTAbits.TX9=0; //8 Bit
TXSTAbits.TXEN=1; //Transmit enabled
RCSTAbits.SPEN=1;
TXSTAbits.SYNC=0; //Asynchronous Mode
TXSTAbits.SENDB=0; //Sync Break transmission completed
TXSTAbits.BRGH=1; //High Speed
while(1)
{
ADCON1=0x0E;
ADCON0=0x00;
ADCON2=0x9C;
ADCON0bits.ADON = 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;
a=DisplayVolt;
tdata[0] = DisplayVolt/1000 + 48;
tdata[1] = (DisplayVolt/100)%10 + 48;
tdata[3] = (DisplayVolt/10)%10 + 48;
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG='V'; //Transmit buffer
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG='1'; //Transmit buffer
space();
send_Value(tdata);
space();
ADCON1=0x0D;
ADCON0=0x04;
ADCON2=0x9C;
ADCON0bits.ADON = 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;
b=DisplayVolt;
tdata[0] = DisplayVolt/1000 + 48;
tdata[1] = (DisplayVolt/100)%10 + 48;
tdata[3] = (DisplayVolt/10)%10 + 48;
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG='V'; //Transmit buffer
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG='2'; //Transmit buffer
space();
send_Value(tdata);
space();
d=((a-b)/(float)0.5);
e=(int)d;
tdata[0] = e/1000 + 48;
tdata[1] = (e/100)%10 + 48;
tdata[3] = (e/10)%10 + 48;
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG='C '; //Transmit buffer
space();
send_Value(tdata);
space();
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG=0x0D; //Transmit buffer
}
}
void delay()
{
unsigned int j;
for(j=0;j<10000;j++);
}
void space()
{
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG=' '; //Transmit buffer
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG=' '; //Transmit buffer
}
void send_Value(unsigned char p[5])
{
unsigned char i=0;
while(p[i]!='\0')
{
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG=p[i]; //Transmit buffer;
i++;
delay();
}
}
Simulation:
//Author : Palak Patel(M.Tech)
//Contact No:9173211683
//Title:Current Measurement Using PIC 18 Microcontroller
//Platform: pic18f4520
//Software:MPLAB
//********************************************************************************
Program:
#include<p18f4520.h>
#pragma config OSC = HS
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config MCLRE = OFF
#pragma config PBADEN = OFF
#pragma config STVREN = OFF
#pragma config LVP = OFF
void delay();
void send_Value(unsigned char p[5]);
void space();
void main()
{
unsigned char temp[] = "TEMP";
unsigned char tdata[4]=" . ",i=0;
unsigned int x=0,y,c=0x100,z,x1,DisplayVolt,e;
unsigned char k;
unsigned long int x2;
float p,tmpr,f,a,b,d;
TRISA=0x03;
TRISC=0x00;
SPBRG=125;
TXSTAbits.CSRC=1; //Master Mode
TXSTAbits.TX9=0; //8 Bit
TXSTAbits.TXEN=1; //Transmit enabled
RCSTAbits.SPEN=1;
TXSTAbits.SYNC=0; //Asynchronous Mode
TXSTAbits.SENDB=0; //Sync Break transmission completed
TXSTAbits.BRGH=1; //High Speed
while(1)
{
ADCON1=0x0E;
ADCON0=0x00;
ADCON2=0x9C;
ADCON0bits.ADON = 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;
a=DisplayVolt;
tdata[0] = DisplayVolt/1000 + 48;
tdata[1] = (DisplayVolt/100)%10 + 48;
tdata[3] = (DisplayVolt/10)%10 + 48;
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG='V'; //Transmit buffer
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG='1'; //Transmit buffer
space();
send_Value(tdata);
space();
ADCON1=0x0D;
ADCON0=0x04;
ADCON2=0x9C;
ADCON0bits.ADON = 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;
b=DisplayVolt;
tdata[0] = DisplayVolt/1000 + 48;
tdata[1] = (DisplayVolt/100)%10 + 48;
tdata[3] = (DisplayVolt/10)%10 + 48;
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG='V'; //Transmit buffer
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG='2'; //Transmit buffer
space();
send_Value(tdata);
space();
d=((a-b)/(float)0.5);
e=(int)d;
tdata[0] = e/1000 + 48;
tdata[1] = (e/100)%10 + 48;
tdata[3] = (e/10)%10 + 48;
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG='C '; //Transmit buffer
space();
send_Value(tdata);
space();
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG=0x0D; //Transmit buffer
}
}
void delay()
{
unsigned int j;
for(j=0;j<10000;j++);
}
void space()
{
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG=' '; //Transmit buffer
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG=' '; //Transmit buffer
}
void send_Value(unsigned char p[5])
{
unsigned char i=0;
while(p[i]!='\0')
{
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG=p[i]; //Transmit buffer;
i++;
delay();
}
}
Simulation:
No comments:
Post a Comment