|
Water level controller program
#include<reg51.h> #include <string.h>
sbit rs = P2^0; // defining different pins of 8051 sbit en = P2^2; sbit rw = P2^1; sbit wr = P3^6; sbit rd = P3^7; sbit led1 = P2^3; sbit led2 = P2^4; sbit led3 = P2^5; sbit led4 = P2^6; sbit led5 = P2^7; sbit wtrin = P3^4; sbit wtrout = P3^5; sbit wtrstp = P3^0; sbit limit = P3^1; unsigned char data d; unsigned int c=0;
void dely(); //function definations void bigdly(int); void lcddly(); void display(unsigned char z); void writecmd(unsigned char a); void writedat(unsigned char b); void writestr(unsigned char *s);
void int1(void) interrupt 2 // external interrupt 1 subroutine { EA=0; // disable all interrupts led5=1; // read pulse indication rd = 0; // send read pulse to ADC d=P1; // read the data rd=1; dely(); // led5=0; writecmd(0xC0); // display it on LCD writestr("cur water level:- "); display(d); EA=1; // enable all interrupts } void timer(void) interrupt 1 // timer overflow interrupt subroutine { EA=0; // disable all interrupts c++; // increment count TH0=0x3C; // relaod timer TL0=0xAF; if(c==40) // if count is 40 { led4=1; dely(); wr = 0; // send write pulse to ADC wr = 1; led4=0; c=0; // start countin again } EA=1; } void int0(void) interrupt 0 // external interrupt 0 subroutine { EA=0; //disable interrupts led3=0; led1=1; wtrin=1; // apply fwd puls and give indication dely(); wtrin=0; EA=1; }
void display(unsigned char z) { unsigned int tmp1,tmp2,t,t1,a; unsigned char asci[3]; tmp1 = (z & 0x0F); //
separate upper and lower nibbles of hex value tmp2 = z >> 4; tmp2 = tmp2*16; // convert it into decimal t = tmp1+tmp2; if(t>=100) // if its 3 digit { a=2; // take a as 3 digit value while(t>10) { t1=t%10; asci[a]=t1+0x30; // convert each digit in to ASCII t=t/10; a--; } asci[0]=t+0x30; } else { t1=t%10; // if 2 digit asci[2]=t1+0x30; t=t/10; asci[1]=t+0x30; asci[0]=0x30; // take first digit as 0 } writedat(asci[0]); // send all 3 digits to LCD writedat(asci[1]); writedat(asci[2]); } void dely() // delay of 100 ms { int i,j; for(i=0;i<100;i++) for(j=0;j<250;j++); } void bigdly(int k) // variable delay in seconds { int i,j; for(i=0;i<k;i++) for(j=0;j<10000;j++); } void lcddly() // LCD delay { int x; for(x=0;x<1500;x++); } void writecmd(unsigned char a) { lcddly(); rs = 0; rw = 0; P0 = a; en = 1; en = 0; } void writedat(unsigned char b) { lcddly(); rs = 1; rw = 0; P0 = b; en = 1; en = 0; } void writestr(unsigned char *s) { unsigned char l,i; l = strlen(s); // first get the lenght of message for(i=1;i<l;i++) { writedat(*s); // send all characters one by one to LCD s++; } } main() { P1=0xFF; P2=0x00; // initialize ports P3=0xCE; P0=0x00; IE=0x87; // enable interrupts TMOD=0x01; // initialize timer TH0=0x3C; // load timer to count up to TL0=0xAF; // 50,000 writecmd(0x3C); // initialize LCD writecmd(0x0F); writecmd(0x01); writestr("set water level:- "); // display set water level as display(0xB4); // 180d wtrin=1; // send fwd pulse led1=1; // indicate on LED led4=1; dely(); wr = 0; // send WR pulse to ADC wr = 1; TR0=1; // start timer wtrin=0; led4=0; agin:while(limit==1); // wait until set value is reached led1=0; led2=1; wtrstp=1; // stop water flow dely(); wtrstp=0; bigdly(200); // wait for 10 sec led2=0; led3=1; wtrout=1; // send rev pulse dely(); wtrout=0; bigdly(50); // after 2-3 sec limit=1; goto agin; // again wait for set value reached }
|