You are on page 1of 2

cONSTRUCTION

plete program is divided in twelve


different functions. Out of these twelve
functions, four functions are for LCD
handling, four for timer operation, two
delay functions, one interrupt function
and the last one is the main function.
LCD handling functions. The various functions are as follows:
writecmd function. It sends command byte to LCD by taking one argument byte that is sent to port P0
writedata function. It sends data
byte to be displayed on LCD. It also
takes one argument byte and sends it
to P0
writestr function. It writes the
whole string (message) on LCD taking pointer as an argument that points
address of first character of string.
Through the pointer it sends all the
characters one by one to port P0
busy function. It checks the status
of busy flag of LCD. If the flag is set
that means LCD is not ready and program remains within loop. When flag
is reset that means LCD is ready and
program comes out of loop
Delay functions.
keydly function. It generates key
debounce delay of around 0.1 second

using for loop statement twice


dly1sec function. It generates approximately one second delay using
for loop
Interrupt function.
takeinput function. This interrupt
function handles external interrupt1. It
just saves the status of port P1 whenever external interrupt1 occurs
Timer operation functions.
Start function. It starts the timer
operation by sending high logic to port
pin P3.7 and energising the relay. The
countdown time is displayed on LCD
as Device ON. Then it sends low logic
to port pin P3.7 and de-energises the
relay. Again countdown time is displayed as Device OFF. It then checks
the status of port pin P3.6. If it is high
the cycle repeats continuously and if it
is low the operation stops
Inctime function. It increments
ontime or offtime variables till maximum limit (60 second) is reached. To
display the variable on LCD the value
must be in ASCII format. So first two
digits are separated and then converted into equivalent ASCII values using
array named ascii. This array includes
ASCII values of all 0 to 9 digits

Dectime function. It is similar to


Inctime function. The difference is
that it will decrement ontime or offtime variable till minimum limit (1
second) is reached
Enter function. It changes the messages on LCD as Enter OFF time:,
Press Start, etc. It is also used to enter
ontime and offtime variables, alternatively.
Main function. It first initialises
LCD and then displays message Enter
ON time:. It then checks the status
of port pin P3.5 of microcontroller. If
port pin P3.5 is high, it means timer
operation is controlled through keypad
and if it is low the timer operation is
controlled through remote. It waits in
continuous loop till any key is pressed
from keypad or remote. As the key
is pressed, the function is detected
and it calls the particular function
corresponding to that key like Start,
Inctime, Dectime or Enter.
EFY note. The source code of this
article is available on www.efymag.
com website.
The author is lecturer in electronics and communication in EC engineering department, Government
Polytechnic, Amreli

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

You might also like