#pragma SMALL DB OE
#include<stdio.h>
#include<reg52.h>
//#include"stdlib2.h"
#define port P3
#define dataport P2
#define key P1
sbit rs=port^7;
sbit rw=port^5;
sbit en=port^6;
sbit col1=key^4;
sbit col2=key^5;
sbit col3=key^6;
sbit row1=key^0;
sbit row2=key^1;
sbit row3=key^2;
sbit row4=key^3;
sbit out=P0^7;
sbit pir1=P0^0;
sbit pir2=P0^1;
static unsigned int time;
static unsigned int count;
static unsigned timer,timer1;
static unsigned int bcounter=1;
int digit[3]={0,0,0};
int i,k;
void delay(unsigned int msec)
{
int i,j;
for(i=0;i<msec;i++);
for(j=0;j<1275;j++);
}
void lcd_cmd(unsigned char item) // Function to send command to LCD
{
dataport = item;
rs= 0;
rw=0;
en=1;
delay(1);
en=0;
return;
}
void lcd_data(unsigned char item) // Function to send data to LCD
{
dataport = item;
rs= 1;
rw=0;
en=1;
delay(1);
en=0;
return;
}
void lcd_data_string(unsigned char *str) // Function to send data to string
{
while(*str)
{
lcd_data(*str++);
delay(1);
}
return;
}
void lcd(unsigned char str[10])
{
lcd_cmd(0x38);
lcd_cmd(0x0e);
lcd_data_string(str);
}
void lcd_data_int(int time_val) // Function to send three digit number
{
int int_amt;
int_amt = time_val/100;
lcd_data(int_amt+48);
time_val = time_val%100;
int_amt = time_val/10;
lcd_data(int_amt+48);
int_amt = time_val%10;
lcd_data(int_amt+48);
}
void digit_set(int num)
{
if(count==4)
{
lcd_cmd(0x01);
lcd_data_string("done");
}
else
{
if(count==1)
digit[0]=num;
else
{
if(count==2)
{
digit[1]=digit[0];
digit[0]=num;
}
else
if(count==3)
{
digit[2]=digit[1];
digit[1]=digit[0];
digit[0]=num;
}
}
lcd_data((num+48));
}
}
void check_col1() // Col1 check
{
row1=row2=row3=row4=1;
row1=0;
if(col1==0) // Key 1
{
delay(10);
count=count+1;
digit[i]=0;
}
row1=1;
row2=0;
if(col1==0) // Key 4
{
delay(10);
count=count+1;
digit[i]=0;
}
row2=1;
row3=0;
if(col1==0) // Key 7
{
delay(10);
count=count+1;
digit[i]=0;
}
row3=1;
row4=0;
if(col1==0) // Key *
{
delay(10);
count=count+1;
if(count==4)
{
digit_set(10);
}
else
{
count=4;
digit_set(10);
}
}
row4=1;
}
void check_col2() //Col2 check
{
row1=row2=row3=row4=1;
row1=0;
if(col2==0) // Key 2
{
delay(10);
count=count+1;
digit_set(2);
}
row1=1;
row2=0;
if(col2==0) // Key 5
{
delay(10);
count=count+1;
digit_set(5);
}
row2=1;
row3=0;
if(col2==0) // Key 8
{
delay(10);
count=count+1;
digit_set(8);
}
row3=1;
row4=0;
if(col2==0) // Key 0
{
delay(10);
count=count+1;
digit_set(0);
}
row4=1;
}
void check_col3() // Col3 check
{
row1=row2=row3=row4=1;
row1=0;
if(col3==0) // Key 3
{
delay(10);
count=count+1;
digit_set(3);
}
row1=1;
row2=0;
if(col3==0) // Key 6
{
delay(10);
count=count+1;
digit_set(6);
}
row2=1;
row3=0;
if(col3==0) // Key 9
{
delay(10);
count=count+1;
digit_set(9);
}
row3=1;
row4=0;
if(col3==0) // Key #
{
delay(10);
EA=1;
lcd_cmd(0x01);
lcd_data_string("counting down . . .");
}
row4=1;
}
void keypad()
{
row1=row2=row3=row4=0;
while(col1==1&&col2==1&&col3==1);
row1=row2=row3=row4=0;
if(col1==0)
check_col1();
else
if(col2==0)
check_col2();
else
if(col3==0)
check_col3();
delay(10);
}
void main()
{
unsigned int k;
TMOD=0x01;
EA=0;
TH0=50621/256;TL0=50621%256;
ET0=1;
EX0=1;
IT0=1;
PX0=1;
TF0=0;
//TR0=1;
count=0; //initial count value
k=time;
pir1=pir2=0;
out=0;
lcd_cmd(0x38);
delay(2);
lcd_cmd(0x01); // Clear Screen
lcd("WELCOME");
delay(50000);
count=0;
delay(1);
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd("Enter time interval(second):");
for(i=0;i<3;i++)
{
delay(50);
lcd_cmd(0xc4+i);
row1=row2=row3=row4=0;
while(col1==1&&col2==1&&col3==1);
keypad();
time=(digit[0]+(digit[1]*10)+(digit[2]*100));
delay(1);
}
while(1);
{
int out;
if(pir1==0 && pir2==0)
out=0;
if(pir1==1 && pir2==0)
out=1;
if(pir1==0 && pir2==1)
out=1;
if(pir1==1 && pir2==1)
out=1;
if(out==1) {
EA=1;
}
if(time==0)
out=0;
}
}
void isr_t0()interrupt 1 using 2
{
TR0=0;
TF0=0;
TH0=50621/256;TL0=50621%256;
if(++timer==20)
{--time;//bcounter^=1;
timer=0;
}
if(++timer1==5){(bcounter^=1);timer1=0;}
TR0=1;
}
void key_isr() interrupt 0 using 1
{
EX0=0;
if(col3==0&&row4==0)
{
TR0=~TR0;
if(TR0)
time=0;
}
EX0=1;
}