Keil Code for Reading 4x4 Matrix Keypad and sending to System buffer

#include <reg51.h>
#include <string.h>

char READ_SWITCHES(void);
char get_key(void);


unsigned char i;
sbit RowA = P3^0;     //RowA
sbit RowB = P3^1;     //RowB
sbit RowC = P3^2;     //RowC
sbit RowD = P3^3;     //RowD

sbit C1   = P3^4;     //Column1
sbit C2   = P3^5;     //Column2
sbit C3   = P3^6;     //Column3
sbit C4   = P3^7;
void delay(unsigned int time){
   unsigned int i;
   for(i=0;i<time;i++);
 }
void send(unsigned char d)
{
while(!TI);
TI=0;
SBUF=d;
}
char get_key(void)           //get key from user
{
char key = 'n';              //assume no key pressed

while(key=='n')              //wait untill a key is pressed
key = READ_SWITCHES();   //scan the keys again and again

return key;                  //when key pressed then return its value
}
char READ_SWITCHES(void)
{
RowA = 0; RowB = 1; RowC = 1; RowD = 1;  //Test Row A

if (C1 == 0) { delay(10000); while (C1==0); return '7'; }
if (C2 == 0) { delay(10000); while (C2==0); return '8'; }
if (C3 == 0) { delay(10000); while (C3==0); return '9'; }
if (C4 == 0) { delay(10000); while (C4==0); return '/'; }

RowA = 1; RowB = 0; RowC = 1; RowD = 1;  //Test Row B

if (C1 == 0) { delay(10000); while (C1==0); return '4'; }
if (C2 == 0) { delay(10000); while (C2==0); return '5'; }
if (C3 == 0) { delay(10000); while (C3==0); return '6'; }
if (C4 == 0) { delay(10000); while (C4==0); return 'x'; }

RowA = 1; RowB = 1; RowC = 0; RowD = 1;  //Test Row C

if (C1 == 0) { delay(10000); while (C1==0); return '1'; }
if (C2 == 0) { delay(10000); while (C2==0); return '2'; }
if (C3 == 0) { delay(10000); while (C3==0); return '3'; }
if (C4 == 0) { delay(10000); while (C4==0); return '-'; }

RowA = 1; RowB = 1; RowC = 1; RowD = 0;  //Test Row D

if (C1 == 0) { delay(10000); while (C1==0); return 'C'; }
if (C2 == 0) { delay(10000); while (C2==0); return '0'; }
if (C3 == 0) { delay(10000); while (C3==0); return '='; }
if (C4 == 0) { delay(10000); while (C4==0); return '+'; }

return 'n';            // Means no key has been pressed
}
main()
{
    char key;
    P0=0x00;                      // P0 and P2 as output ports
    P2=0x00;
    TMOD=0x20;            //Enable Timer 1
    TH1=0XFD;
    SCON=0x50;
    TR1=1;
    TI=1;
    while(1)
    {
    key = get_key(); 
    send(key);
    }       // clear memory and home cursor

}


THANK YOU FOR VISITING..!!
PLEASE COMMENT

No comments:

Post a Comment