Sie sind auf Seite 1von 13

//==========================================================================

//==================inckude=================================
#include<pic.h>
//===============configuration==============================
__CONFIG (0x3F32);
//===============define IO port=============================
#define
gas_sensor
RC3
//18..
#define

tx_enable

RC5

//24..

#define
#define
#define
#define

tank1_lvl1
tank1_lvl2
tank1_lvl3
tank1_lvl4

RD0
RA5
RE0
RE1

//19..
//7..
//8..
//9..

#define
#define
#define
#define

tank2_lvl1
tank2_lvl2
tank2_lvl3
tank2_lvl4

RE2
RC0
RC1
RC2

//10..
//15..
//16..
//17..

#define

rpm_pulses

RB0

#define
#define
#define

valve_rly
prs_gen_rly
burner_rly

RD3

#define

pump_rly

#define

buzz

#define
#define
#define
#define
#define
#define
#define
#define

rs
en
lcd4
lcd5
lcd6
lcd7

flow sensor..

RD4
//27
//22..
RC4
//23..
RD5

//28
RD6

RB2
RB3
RB4
RB5
RB6
RB7
CHANNEL0
CHANNEL1

//33

//29..

//35
//36
//37
//38
//39
//40
0b10000001 // AN0
0b10001001 // AN1

//==============FUNCTION PTOTOTYPE=========================
void lcdinit (void);
void lcdreset (void);
void lcdcmd (unsigned char value);
void lcddata (unsigned char value);
void line1 (void);
void line2 (void);
void clear_lcd_screen (void);
void write_nibble (unsigned char data);
void lcd_pulse_command();
void lcd_pulse_data();
void project_message1();
void project_message2();
void read_adc(void);
void bcd (unsigned int value);
void split_rpm(unsigned int value);
void get_temp_post_point_value (unsigned int value);
void display_temperature(void);

temp..
pres..

void
void
void
void

display_pressure(void);
display_rpm(void);
send_data();
msdelay (unsigned int itime);

unsigned int result, rpm_var, pulse_counter, timer_counter ;


unsigned char set_temp, cur_temp, temperory, msb,tenth, lsb, temp_msb, temp_tent
h, temp_lsb, temp_post_point, tank1_msb, tank1_lsb;
unsigned char rpm_lsb, rpm_tenth, rpm_hundreth, rpm_msb, gas_status, set_prsr, c
ur_prsr, prsr_msb, prsr_tenth, prsr_lsb, tank2_msb, tank2_lsb;
bit
valve_flage;
//signed char difference;
void main ()
{
ADRESH=0;
ADRESL=0;

//clear A/D result


//clear A/D result

///////////////////////////////////////////////////////
TRISA0=1;
//configure PORTA I/O direction
TRISA1=1;
//configure PORTA I/O direction
TRISA5=1;
//configure PORTA I/O direction
TRISB = 0b00000011;

//Configure Port B

TRISC0=1;
TRISC1=1;
TRISC2=1;
TRISC3=1;
TRISC4=0;
TRISC5=0;

//configure
//configure
//configure
//configure
//configure
//configure

PORT
PORT
PORT
PORT
PORT
PORT

as
as
as
as
as
as

INput
INput
INput
INput
OUTput
OUTput

TRISD0=1;
TRISD3=0;
TRISD4=0;
TRISD5=0;
TRISD6=0;

//configure
//configure
//configure
//configure
//configure

PORT
PORT
PORT
PORT
PORT

as
as
as
as
as

INput
OUTput
OUTput
OUTput
OUTput

TRISE0=1;
TRISE1=1;
TRISE2=1;

//configure PORT as INput


//configure PORT as INput
//configure PORT as OUTput

/////////////////setting ADCON1 Register///////////////


ADCON1=0b11000100;
// A/D result right justified, c
onversion clk=fosc/64, just A0,A1,A3 as analog input

//

//////////////////Configure UART////////////////////////
TRISC6=0;
//set RC6 as output TX
TRISC7=1;
//set RC7 as input RX
SPBRG=31;
//set baud rate as 9600 baud @XTAL=20MHZ
BRGH=1;
//baud rate high speed option
TXEN=1;
//enable transmission
TX9 =0;
//8-bit transmission
RX9 =0;
//8-bit reception
CREN=1;
//enable reception
SPEN=1;
//enable serial port
//////////////////Configure TIMER////////////////////////

T1CON=0b00110000;
//timer1(16bit), prescaler with
factor of 8, 1 timer step=(4/20)8=1.6micros
///////////////////////////////////////////////////////
INTF =0;
INTEDG=0;
//RB0 negative edge trigered,,,, by default it is positi
ve edge triggered
INTE=0;
//initially disabled
TMR1IF=0;
TMR1IE=1;
//enabled
PEIE=1;
//peripheral interrupt FOR timer1
GIE =1;
//global interrupt
tx_enable=1;
buzz=0;
prs_gen_rly=0;
burner_rly=0;
pump_rly=0;
valve_rly=1;
valve_flage=1;
set_temp=50;
set_prsr=50;
lcdinit();
line1();
project_message1();
line2();
project_message2();
msdelay(532);
clear_lcd_screen();

// 4sec switching tyme

rpm_var = 0;
pulse_counter = 0;
timer_counter = 0;
TMR1H =0x85;
TMR1L =0xEE;
TMR1ON =1;
INTE=1;

// FOR 50m SEC


// FOR 50m SEC
//interupt enabled

///////////////////////////////////////////////////////
while(1)
{
if ( timer_counter==80 )
{
rpm_var = pulse_counter * 15;
split_rpm ( rpm_var );
pulse_counter = 0;
timer_counter = 0;
TMR1H =0x85;
// FOR 50m SEC
TMR1L =0xEE;
// FOR 50m SEC
TMR1ON =1;
INTE=1;
//interupt enabled
}
if ( RCIF==1 )

{
temperory=RCREG;
if ( temperory=='A' )
{
while ( RCIF==0 );
set_temp=RCREG;
msdelay(50);
TXREG='A';
while(TXIF==0);
//only send the new data after the previous data finish sent
send_data();
}
if ( temperory=='B' )
{
while ( RCIF==0 );
set_prsr=RCREG;
msdelay(50);
TXREG='B';
while(TXIF==0);
//only send the new data after the previous data finish sent
send_data();
}
if ( temperory=='C' )
{
while ( RCIF==0 );
temperory=RCREG;
msdelay(50);
TXREG='C';
while(TXIF==0);
//only send the new data after the previous data finish sent
send_data();
}
if ( temperory=='D' )
{
while ( RCIF==0 );
temperory=RCREG;
msdelay(50);
pump_rly=1;
TXREG='D';
while(TXIF==0);
//only send the new data after the previous data finish sent
send_data();
}
if ( temperory=='E' )
{
while ( RCIF==0 );
temperory=RCREG;
msdelay(50);
pump_rly=0;
TXREG='E';
while(TXIF==0);
//only send the new data after the previous data finish sent
send_data();

}
if ( temperory=='F' )
{
while ( RCIF==0 );
temperory=RCREG;
msdelay(50);
valve_rly=1;
valve_flage=1;
TXREG='F';
while(TXIF==0);
//only send the new data after the previous data finish sent
send_data();
}
if ( temperory=='G' )
{
while ( RCIF==0 );
temperory=RCREG;
msdelay(50);
valve_rly=0;
valve_flage=0;
TXREG='G';
while(TXIF==0);
//only send the new data after the previous data finish sent
send_data();
}
}
gas_status='0';
if (gas_sensor == 0)
gas_status='1';
if (tank1_lvl1 == 0)
{
tank1_msb = '2';
tank1_lsb = '0';
}
else
{
if(tank1_lvl2 == 0)
{
tank1_msb = '1';
tank1_lsb = '5';
}
else
{
if(tank1_lvl3 == 0)
{
tank1_msb = '1';
tank1_lsb = '0';
}
else
{
if(tank1_lvl4 == 0)
{
tank1_msb = '0';
tank1_lsb = '5';

}
else
{
tank1_msb = '0';
tank1_lsb = '0';
}
}
}
}
if (tank2_lvl1 == 0)
{
tank2_msb = '2';
tank2_lsb = '0';
}
else
{
if(tank2_lvl2 == 0)
{
tank2_msb = '1';
tank2_lsb = '5';
}
else
{
if(tank2_lvl3 == 0)
{
tank2_msb = '1';
tank2_lsb = '0';
}
else
{
if(tank2_lvl4 == 0)
{
tank2_msb =
tank2_lsb =
}
else
{
tank2_msb =
tank2_lsb =
}
}
}
}

'0';
'5';

'0';
'0';

ADCON0=CHANNEL0;
nversion clk=fosc/64, CHANNEL0, ADC ON
read_adc();
cur_temp = result/2;
bcd (cur_temp );
temp_msb = msb + 0x30;
temp_tenth = tenth + 0x30;
temp_lsb = lsb + 0x30;
get_temp_post_point_value (result);

//ADCON0=0b10000001 =>co

ADCON0=CHANNEL1;
nversion clk=fosc/64, CHANNEL1, ADC ON
read_adc();
cur_prsr = (result*12)/100 ;
bcd (cur_prsr);
prsr_msb = msb + 0x30;

//ADCON0=0b10001001 =>co

prsr_tenth = tenth + 0x30;


prsr_lsb = lsb + 0x30;
line1();
lcddata('T');
lcddata('=');
display_temperature();
lcddata(',');
lcddata('P');
lcddata('=');
display_pressure();
lcddata('p');
lcddata('s');
lcddata('i');
lcddata(',');
lcddata(' ');
lcddata('G');
lcddata('A');
lcddata('S');
lcddata('=');
lcddata(gas_status);

//psi 3digit

line2();
lcddata('R');
lcddata('P');
lcddata('M');
lcddata('=');
display_rpm();
lcddata(',');
lcddata('T');
lcddata('N');
lcddata('K');
lcddata('1');
lcddata('=');
lcddata(tank1_msb);
lcddata(tank1_lsb);
lcddata(',');
lcddata('T');
lcddata('N');
lcddata('K');
lcddata('2');
lcddata('=');
lcddata(tank2_msb);
lcddata(tank2_lsb);
if (cur_temp >= set_temp)
burner_rly=0;
else
burner_rly=1;
if (valve_flage==0)
{
if (cur_prsr >= set_prsr)
prs_gen_rly=0;
else
prs_gen_rly=1;
}
}
}

///////////////////////////////////////////////////////
void interrupt isr()
{
if (TMR1IF==1)
{
TMR1ON=0;
TMR1IF=0;
timer_counter++;
if (timer_counter==80)
INTE=0;
else
{
TMR1H =0x85;
TMR1L =0xEE;
TMR1ON =1;
}

// FOR 50m SEC


// FOR 50m SEC

}
if (INTF==1)
{
INTF=0;
pulse_counter++;
}
}
///////////////////////////////////////////////////////
void lcdinit()
{
lcdreset();
lcdcmd(0x28);
lcdcmd(0x0c);
lcdcmd(0x01);
lcdcmd(0x02);
}
///////////////////////////////////////////////////////
void lcdreset()
{
msdelay(3); //22msec
write_nibble(0x03);
lcd_pulse_command();
msdelay(2);
//15msec
write_nibble(0x03);
lcd_pulse_command();
msdelay(1);
//7.5msec
write_nibble(0x03);
lcd_pulse_command();
msdelay(1);
//7.5msec
write_nibble(0x02);
lcd_pulse_command();
msdelay(1);
//7.5msec

}
///////////////////////////////////////////////////////
void lcdcmd (unsigned char value)
{
unsigned char tempr;
tempr = value >>4;
write_nibble(tempr);
lcd_pulse_command();
write_nibble(value);
lcd_pulse_command();
msdelay(1);

//7.5msec

}
///////////////////////////////////////////////////////
void lcddata (unsigned char value)
{
unsigned char tempr;
tempr = value >>4;
write_nibble(tempr);
lcd_pulse_data();
write_nibble(value);
lcd_pulse_data();
msdelay(1);

//7.5msec

}
///////////////////////////////////////////////////////
void line1()
{
lcdcmd(0x80);
}
///////////////////////////////////////////////////////
void line2()
{
lcdcmd(0xc0);
}
///////////////////////////////////////////////////////
void clear_lcd_screen ()
{
lcdcmd(0x01);
}
///////////////////////////////////////////////////////
void write_nibble (unsigned char data)
{
lcd4 = data & 0x01;
data = data >>1;
lcd5 = data & 0x01;
data = data >>1;
lcd6 = data & 0x01;
data = data >>1;
lcd7 = data & 0x01;

}
///////////////////////////////////////////////////////
void lcd_pulse_command()
{
rs = 0;
en = 1;
en = 0;
}
///////////////////////////////////////////////////////
void lcd_pulse_data()
{
rs = 1;
en = 1;
en = 0;
}
///////////////////////////////////////////////////////
void project_message1()
{
unsigned char x;
unsigned char project_name1[]= "Physical Parameters Sys ";
for (x=0;x<24;x++)
{
lcddata(project_name1[x]);
}
}
///////////////////////////////////////////////////////
void project_message2()
{
unsigned char x;
unsigned char project_name2[]= "
Behria Universty
for (x=0;x<24;x++)
{
lcddata(project_name2[x]);

";

}
}
///////////////////////////////////////////////////////
void read_adc(void)
{
unsigned char i;
unsigned long result_temperary=0; // 4 byte long
for(i=200;i>0;i-=1)
//looping 200 times for gett
ing average value
{
//
msdelay(4);
//30msec
ADGO = 1;
//ADGO is the bi
t 2 of the ADCON0 register,,start conversion
while(ADGO==1);
//ADC start, ADGO=0 afte
r finish ADC progress
result=ADRESH;
result=result<<8;
//shift to left for 8 bi
t
result=result|ADRESL;
//10 bit result from ADC ,, or f
unction
result_temperary+=result;
sult_temperary + result
}

// result_temperary = re

result = result_temperary/200;

//getting average value

}
///////////////////////////////////////////////////////
void bcd (unsigned int value)
{
unsigned char i;
i=value/10;
lsb=value%10;
msb=i/10;
tenth=i%10;
}
///////////////////////////////////////////////////////
void split_rpm(unsigned int value)
{
unsigned char i,j;
i = value/10;
rpm_lsb = value%10;
j = i/10;
rpm_tenth = i%10;
rpm_msb = j/10;
rpm_hundreth = j%10;
rpm_lsb = rpm_lsb + 0x30;
rpm_tenth = rpm_tenth + 0x30;
rpm_hundreth = rpm_hundreth + 0x30;
rpm_msb = rpm_msb + 0x30;
}
///////////////////////////////////////////////////////
void get_temp_post_point_value (unsigned int value)
{
unsigned char i;
i=value%2;
temp_post_point=(i*10)/2;
temp_post_point=temp_post_point + 0x30;
}
///////////////////////////////////////////////////////
void display_temperature ()
{
lcddata(temp_msb);
lcddata(temp_tenth);
lcddata(temp_lsb);
lcddata('.');
lcddata(temp_post_point);
lcddata('C');
}
///////////////////////////////////////////////////////
void display_pressure ()
{
lcddata(prsr_msb);
lcddata(prsr_tenth);
lcddata(prsr_lsb);

}
///////////////////////////////////////////////////////
void display_rpm ()
{
lcddata(rpm_msb);
lcddata(rpm_hundreth);
lcddata(rpm_tenth);
lcddata(rpm_lsb);
}
///////////////////////////////////////////////////////
void send_data ()
{
TXREG=temp_msb;
while(TXIF==0);
end the new data after the previous data finish sent
//
msdelay(4);
// 30 msec

//only s

TXREG=temp_tenth;
while(TXIF==0);
end the new data after the previous data finish sent
//
msdelay(4);
// 30 msec

//only s

TXREG=temp_lsb;
while(TXIF==0);
end the new data after the previous data finish sent
//
msdelay(4);
// 30 msec

//only s

TXREG='.';
while(TXIF==0);
end the new data after the previous data finish sent
//
msdelay(4);
// 30 msec

//only s

TXREG=temp_post_point;
while(TXIF==0);
end the new data after the previous data finish sent
//
msdelay(4);
// 30 msec

//only s

TXREG=prsr_msb;
while(TXIF==0);
end the new data after the previous data finish sent
//
msdelay(4);
// 30 msec

//only s

TXREG=prsr_tenth;
while(TXIF==0);
end the new data after the previous data finish sent
//
msdelay(4);
// 30 msec

//only s

TXREG=prsr_lsb;
while(TXIF==0);
end the new data after the previous data finish sent
//
msdelay(4);
// 30 msec

//only s

TXREG=rpm_msb;
while(TXIF==0);
end the new data after the previous data finish sent
//
msdelay(4);
// 30 msec

//only s

TXREG=rpm_hundreth;
while(TXIF==0);
end the new data after the previous data finish sent
//
msdelay(4);
// 30 msec

//only s

TXREG=rpm_tenth;
while(TXIF==0);
end the new data after the previous data finish sent
//
msdelay(4);
// 30 msec

//only s

TXREG=rpm_lsb;
while(TXIF==0);
end the new data after the previous data finish sent
//
msdelay(4);
// 30 msec

//only s

TXREG=tank1_msb;
while(TXIF==0);
end the new data after the previous data finish sent
//
msdelay(4);
// 30 msec

//only s

TXREG=tank1_lsb;
while(TXIF==0);
end the new data after the previous data finish sent
//
msdelay(4);
// 30 msec

//only s

TXREG=tank2_msb;
while(TXIF==0);
end the new data after the previous data finish sent
//
msdelay(4);
// 30 msec

//only s

TXREG=tank2_lsb;
while(TXIF==0);
end the new data after the previous data finish sent
//
msdelay(4);
// 30 msec

//only s

TXREG=gas_status;
while(TXIF==0);
end the new data after the previous data finish sent
//
msdelay(4);
// 30 msec

//only s

msdelay(50);
}
///////////////////////////////////////////////////////
void msdelay (unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}