/* Instruction Press any button except * and #
button 'C' is to Clear Screen,
others will be displayed as it is.*/
void outport_d(char );
void outport_b(char );
char inport_c();
void wait_until_press();
char check_row_1(char );
char check_row_2(char );
char check_row_3(char );
char check_row_4(char );
void setup()
{
outport_d(0x00);
char z,key=0;
while(1)
{
if(key==0||key==7||key==1||key==12){outport_b(0x3C);}
else{outport_b(0x3D);}
wait_until_press();
outport_b(0x00);
dly(); // row 1
outport_b(0x20);
z=inport_c();
key=check_row_1(z);
if(key==1)
{
outport_d(0x18);outport_b(0x3C);delay(500);
}
else if(key==2)
{
outport_d(0x6C);outport_b(0x3D);
for(int j=0;j<15;j++){delay();}
}
else if(key==3)
{
outport_d(0x3C);outport_b(0x3D);
for(int j=0;j<15;j++){delay();}
}
else if(key==10)
{
outport_d(0xDC);outport_b(0x3D);
for(int j=0;j<15;j++){delay();}
}
else if(key==100)
{
outport_b(0x10);
z=inport_c();
key=check_row_2(z);
if(key==4)
{
outport_d(0x98);outport_b(0x3D);
for(int j=0;j<15;j++){delay();}
}
else if(key==5)
{
outport_d(0xB4);outport_b(0x3D);
for(int j=0;j<15;j++){delay();}
}
else if(key==6)
{
outport_d(0xF4);outport_b(0x3D);
for(int j=0;j<15;j++){delay();}
}
else if(key==11)
{
outport_d(0xF0);outport_b(0x3D);
for(int j=0;j<15;j++){delay();}
}
else if(key==100)
{
outport_b(0x08);
z=inport_c();
key=check_row_3(z);
if(key==7)
{
outport_d(0x1C);outport_b(0x3C);
for(int j=0;j<15;j++){delay();}
}
else if(key==8)
{
outport_d(0xFC);outport_b(0x3D);
for(int j=0;j<15;j++){delay();}
}
else if(key==9)
{
outport_d(0xBC);outport_b(0x3D);
for(int j=0;j<15;j++){delay();}
}
else if(key==12)
{
outport_d(0x00);outport_b(0x3C);
}
else if(key==100)
{
outport_b(0x04);
z=inport_c();
key=check_row_4(z);
if(key==0)
{
outport_d(0xFC);outport_b(0x3C);
}
else if(key==13)
{
outport_d(0x78);outport_b(0x3D);
for(int j=0;j<15;j++){delay();}
}
}
}
}
delay();
}
}
char inport_c()
{
char q;
volatile char *dir_c,*inn_c;
dir_c=0x27;inn_c=0x26;
*dir_c=0x00;
q=*inn_c;
return q;
}
void wait_until_press()
{
volatile char *dir_c,*inn_c;
dir_c=0x27;inn_c=0x26;
*dir_c=0x00;
while(*inn_c==0);
}
void outport_d(char q)
{
volatile char *dir_d,*out_d;
dir_d=0x2A;out_d=0x2B;*dir_d=0xFC;
*out_d=q;
}
void outport_b(char q)
{
volatile char *dir_b,*out_b;
dir_b=0x24;out_b=0x25;*dir_b=0x3D;
*out_b=q;
}
char check_row_1(char z)
{
if(z==0x01)
{
return 1;
}
else if(z==0x02)
{
return 2;
}
else if(z==0x04)
{
return 3;
}
else if(z==0x08)
{
return 10;
}
else
{
return 100;
}
}
char check_row_2(char z)
{
if(z==0x01)
{
return 4;
}
else if(z==0x02)
{
return 5;
}
else if(z==0x04)
{
return 6;
}
else if(z==0x08)
{
return 11;
}
else
{
return 100;
}
}
char check_row_3(char z)
{
if(z==0x01)
{
return 7;
}
else if(z==0x02)
{
return 8;
}
else if(z==0x04)
{
return 9;
}
else if(z==0x08)
{
return 12;
}
else
{
return 100;
}
}
char check_row_4(char z)
{
if(z==0x02)
{
return 0;
}
else if(z==0x08)
{
return 13;
}
else
{
return 100;
}
}
void delay()
{
volatile long i;
for(i=0;i<20000;i++);
}
void dly()
{
volatile long i;
for(i=0;i<300;i++);
}
void loop() {
// put your main code here, to run repeatedly:
}