|
~:temperature controller program in keil :~
#include<reg51.h> //include file for 8051 registers
#include <string.h>
sbit en = P2^7; // define en, rs & rw pins for LCD
sbit rs = P2^5;
sbit rw = P2^6;
sbit b = P0^7; // define b as busy flag
sbit cs = P3^0; // define cs, wr, rd & intr as pins for
sbit wr = P3^1; // ADC
sbit rd = P3^2;
sbit intr = P3^3;
sbit heater = P2^0; // define P2.0 & P2.1 as pins to on/off
sbit cooler = P2^1; // heater and cooler
unsigned char data1,data2;
int c=0; // initialize counter
void writecmd(unsigned char a); //function that writes command on LCD
void writedat(unsigned char b); // function that sends data to LCD
void busy(void); // function to check LCD busy
void writestr(unsigned char *s); // function that writes string on LCD
void delay(void); // to generate delay
void display(unsigned char z); //converts hex in to ASCII and display it
void timr1(void) interrupt 3 //timer 1 overflow interrupt subroutine
{
c++; // increase count when interrupt occur
ET1=0;
TH1 = 0x3C; // load same value again in T1
TL1 = 0xAF;
if(c==50) //if count is 50 means 50×50ms=2.5 sec
{
c=0; // set counter again to 0
EA = 0; // disable all interrupts
writecmd(0xC0); // go to next line in LCD
writestr("curr temp="); // write string
intr = 1;
cs = 0;
rd = 1;
wr = 0; // write enable to ADC
P1 = 0x01; // select channel 1
while(!intr); // wait for conversion over
rd = 0; // read enable
wr = 1;
data2=P1; // get the data from P1
display(data2); // display it
writestr("'C ");
if(data2>=data1) // if current temp is higher
{
writecmd(0xC0);
writestr("over temperature"); //display message
heater = 0; // turn off heater
cooler = 1; // turn on cooler
}
else
{
heater = 1; // or turn on heater
cooler = 0; // turn off cooler
}
EA = 1; //enable interrupt before exiting
} // loop
}
void display(unsigned char z)
{
unsigned char tmp; // define temporary variable
unsigned char ASCII[2]; // define 2 digit variable
tmp = z>>4; // get upper nibble
if(tmp <= 0x09) // if its less then 9 convert it
ASCII[1] = tmp+0x30; // in ASCII by adding 30
else
{
ASCII[1] = tmp-0x09; //otherwise first deduct 9
ASCII[1] += 0x40; // then convert it in ascii
}
tmp = (z & 0x0f); // get lower nibble and perform
if(tmp <= 0x09) // same operation
ASCII[0] = tmp+0x30;
else
{
ASCII[0] = tmp-0x09;
ASCII[0] += 0x40;
}
writedat(ASCII[1]);
writedat(ASCII[0]);
}
void delay(void)
{
int x;
for(x=0;x<10000;x++);
}
void writecmd(unsigned char a)
{
busy(); //check if LCD is busy or not
rs = 0; // select command register
rw = 0; // write to LCD
P0 = a; // send byte to LCD
en = 1; // apply strobe
en = 0;
}
void writedat(unsigned char b)
{
busy(); //check if LCD is busy or not
rs = 1; // select data register
rw = 0;
P0 = b; // write to LCD
P0 = a; // send byte to LCD
en = 1; // apply strobe
en = 0;
}
void busy()
{
en = 0; //disable display
P0 = 0xFF; // P0 as input port
rs = 0; // select command register
rw = 1; // read enable
while(b==1) // wait until busy flag is 1
{
en=0;
en=1;
}
en=0;
}
void writestr(unsigned char *s)
{
unsigned char l,i;
l = strlen(s); //get the length of string
for(i=1;i<l;i++) // send all characters one by one
{
writedat(*s); // till the end of string
s++;
}
}
void main()
{
TMOD = 0xA0; // initialize timer 1 with 16 bit timer
TH1 = 0x3C; // load initial count 15,535 = 3CAFh
TL1 = 0xAF;
heater = 1; // initially heater is on
cooler = 0;
writecmd(0x3C); // initialize LCD
writecmd(0x0E);
writecmd(0x01);
writestr("set temp="); // send string to LCD
delay();
intr = 1;
cs = 0; // enable ADC
rd = 1;
wr = 0; // write enable
P1 = 0x02; // select 2nd channel
while(!intr); // wait until conversion is over
rd = 0; // read enable
wr = 1;
data1=P1; // get digital data from ADC
display(data1); // display it
writestr("'C ");
TR1=1; // start timer 1
IE = 0x88; // enable timer 1 overflow interrupt
while(1); // continuous loop
} |