Professional Documents
Culture Documents
timer.c
#include <reg51.h>
busy();
// strobe LCD till P0.7 is 1
#include <string.h>
// check for LCD is busy or not
en=1;
sbit rs = P3^0;
//
rs = 0;
}
declare P3.0 as rs pin
// clear rs pin for command
en=0;
sbit en = P3^2;
// declare
rw = 0;
}
p3.2 as enable pin
// clear rw pin to write
void writestr(unsigned char *s)
sbit rw = P3^1;
// declare
P2 = a;
{
p2.6 as read/write pin
// send command character
unsigned char l,i;
sbit b = P2^7;
//
en = 1;
l = strlen(s);
busy flag
// strob LCD
// get the length of string
sbit op=P3^7; / /
en = 0;
for(i=0;i<l;i++)
output pin
}
{
sbit pin=P3^6; / /
void writedat(unsigned char b)
writedat(*s);
input pin for repeat no repeat mode
{
// write every char one by one
sbit rmt=P3^5;
busy();
s++;
// input pin for remote or keypad opera// check for LCD is busy or not
}
tion
rs = 1;
}
unsigned char ascii[10] = {0x30,0x31,0x32,
// set rs pin for data
void inctime()
0x33,0x34,0x35,0x36,0x37,0x38,0x39};
rw = 0;
{
unsigned char data byte;
// clear rw pin to write
t2=0;
unsigned int i=0;j=0,k=0,ontime=0,offtime
P2 = b;
// minimum limit flag cleared
=0,t1=0,t2=0,tmp1,tmp2,flag,sflag;
// send data character
if(k==1) / /
void writecmd(unsigned char a);
//
en = 1;
check to increase ontime or offtime
function to send command to LCD
// strob LCD
{
void writedat(unsigned char b);
//
en = 0;
if(offtime<60) offfunction to send data to LCD
}
time++; // increase offtime till 60
void busy();
void busy()
else
// function to check LCD is busy or not
{
{
void writestr(unsigned char *s);
//
en = 0;
function to write string on LCD
// disable display
writecmd(0xC0);
void keydly()
P2 = 0xFF;
// function for key debounce delay
// configure P0 as input
writestr(60-max limit ); // if it is
{
rs = 0;
60 display message
int y,z;
// clear rs pin for command
for(y=0;y<50;y++)
rw = 1;
t1=1; / /
for(z=0;z<1000;z++);
// set rw pin to read
and set max limit falg
}
while(b==1)
void writecmd(unsigned char a)
{
}
{
en=0;
i=offtime/10;
1 1 0 S e p t e m b e r 2 0 1 1 e l e c t ro n i c s f o r yo u
w w w. e f y m ag . co m
Construction
// separate upper and
writecmd(0xC0);
{
j=offtime%10;
//
// show value 00
byte=P1;
lower digits
writestr(press en// take input from P1 when interrupt ar }
ter:);
rives
}
else
writedat(0x30);
// if ontime-offtime flag is not 1
writedat(0x30);
main()
{
i=j=0;
{
if(ontime<60) on
}
TMOD=0x10;
time++; // increase ontime till 60
else if(k==2)
P3=0x68;
else
// second time
// set keypad operation and re
{
{
peat mode
writecmd(0x01);
P2=0x00;
writecmd(0xC0);
// same as above
// clear LCD screen and
// P2 as output
w r i t e s t r ( P r e s s
writecmd(0x3C);
writestr(60-max limit );
start);
// display message
// Initialize LCD
sflag=1;
t1=1;
// set start flag
writecmd(0x0E);
}
k=0;
bgin:
writecmd(0x01);
i=ontime/10;
// reset counter
// clear display
j=ontime%10;
}
writestr(Set ON time and);
}
}
// display message and
if((t1==0)&& (t2==0))
//
void delay1sec()
writecmd(0xC0);
if max limit is not reached
// generate delay of exact 1 sec
writestr(press Enter:);
{
{
writedat(0x30);
int x;
// value as 00
writecmd(0xC0);
// print ontime/off
TL1 = 0xBF;
writedat(0x30);
time sec
TH1 = 0x3C;
IT1=1;
TR1 = 1;
// enable interrupt on falling
writestr(press enter:);
for(x=0;x<=20;x++)
edge
{
IE=0x84;
writedat(ascii[i]); // as one by one digit
while(TF1==0);
// enable ext interrupt 1
TF1 = 0;
flag=0;
writedat(ascii[j]);
TL1 = 0xBF;
// clear flag for entering values
w r i t e s t r (
TH1 = 0x3C;
sflag=0;
);
}
// start flag cleard
}
TR1 = 0;
agin:P1=0xFF;
}
}
// P1 as input
void dectime()
void start()
byte=0xFF;
// similar to above function
{
// initialize byte variable
{
int a,b;
if(rmt==1) while(P1==0xFF); / /
t1=0;
tmp1=ontime;
for keypad mode check status of P1
if(k==1)
// save ontime and offtime values
else while(byte==0xFF);
{
tmp2=offtime;
// for remote mode check byte content
if(offtime>1) offback:writecmd(0x80);
if(rmt==1) byte=P1;
time--;
writestr(Device ON: );
// if keypad mode save input in byte
if(offtime<=1)
// display message
switch(byte)
{
op=1;
// compare the content of byte
// switch ON relay
{
writecmd(0xC0);
for(a=tmp1;a>0;a--)
case 0xFE:
// start countdown
// for 1st key
writestr(01-min limit );
{
i=a/10;
t2=1;
j=a%10;
if(sflag==1) start();// if start flag is set
}
start
i=offtime/10;
writecmd(0x8B);
j=offtime%10;
else
// operation else
}
writedat(ascii[i]);
// change the display
else
digits
{
{
if(ontime>1)
ontime--;
writedat(ascii[j]); // on LCD after
if(ontime<=1)
d e l a y writecmd(0xC0);
// display message
{
1sec();
// every sec
}
writestr(time is not set);
writecmd(0xC0);
op=0;
// switch OFF relay
flag=1;
writestr(01-min limit );
writecmd(0x80);
writestr(Device OFF:); }
t2=1;
break;
}
for(b=tmp2;b>0;b--)
case 0xFD:
i=ontime/10;
// start countdown again
j=ontime%10;
{
inctime();
// for 2nd key in
}
i=b/10;
crease time
if((t1==0)&& (t2==0))
j=b%10;
{
break;
writecmd(0x8B);
case 0xFB:
writecmd(0xC0);
writedat(ascii[i]);
// display digits afdectime();
// for 3rd key dewritestr(press enter:);
ter
crease time
writedat(ascii[i]);
writedat(ascii[j]);
break;
d e l a y case 0xF7:
writedat(ascii[j]);
1sec();
// 1 sec delay
w r i t e s t r (
}
enter();
// for 4th key enter
);
if(pin==1) goto back;
values
}
// repeat continuously for repeat mode
}
else
break;
void enter()
// otherwise
}
// enter ontime & offtime and
{
keydly();
{
flag=1;
// key debounce delay
// display message
// set flag
if(flag==0) goto agin;
k++;
ontime=0;
// till start button is not
// increase count to enter off time
// clear ontime and offtime
pressed change and
if(k==1) / /
offtime=0;
else goto bgin;
first time
// and stop operation
// enter values otherwise start operation
{
}
again
writecmd(0x80);
}
}
writestr(Set OFF time
void takeinput() interrupt 2 // for remote
and); // display message and
operation mode
w w w. e f y m ag . co m
e l e c t ro n i c s f o r yo u S e p t e m b e r 2 0 1 1 1 1 1