key pad scaning logic

keypad()
{

	while(1)							// 1st while loop
	{
		while(1)						  // 2nd while loop
		{	
			do
			{
				IOCLR0=0x0ff;			// row and column = 0000 i.e clear row and column data
				IOSET0=0xf;			// column = 1111
				a=0x00;
				a=IOPIN0;			// check for key press
				a=a & 0xf;			// mask other pins to check the column pins only.
			}while(a==0xf);				// check for any change in column value (0111 or 1011 or 1101 or 1110). if all are one non of the key is pressed

			IOCLR0=0x00ff;				// row and column = 0000 clear row and column data
			IOSET0=0xe0;				// row = 1110 i.e. pin P0.19 =0
			IOSET0=0xf;				// column = 1111
			read_port=0x00;
			read_port=IOPIN0;			// check for the key press

			if((read_port=read_port & 0xf)!=0xf)	  // mask other pins to check the column pins only  
			{					// check for any change in column value (0111 or 1011 or 1101 or 1110). if all are one non of the key is pressed
				row=0;				// if there is any change then it belongs to row 0
				break;				 // come out of 2nd while loop
			}

			IOCLR0=0x0ff;				// row and column = 0000 clear row and column data
			IOSET0=0xd0;				 // row = 1101 i.e. pin P0.20 =0
			IOSET0=0xf;				 // column = 1111
			read_port=0x00;
			read_port=IOPIN0;			 // check for the key press

			if((read_port=read_port & 0xf)!=0xf)	   // mask other pins to check the column pins only 
			{					 // check for any change in column value (0111 or 1011 or 1101 or 1110). if all are one non of the key is pressed
				row=1;				  // if there is any change then it belongs to row 1
				break;				  // come out of 2nd while loop
			}

			IOCLR0=0x0ff;				// row and column = 0000 clear row and column data
			IOSET0=0xb0;				 // row = 1011 i.e. pin P0.21 =0
			IOSET0=0xf;				 // column = 1111
			read_port=0x00;
			read_port=IOPIN0;			 // check for the key press

			if((read_port=read_port & 0xf)!=0xf)	   // mask other pins to check the column pins only 
			{					   // check for any change in column value (0111 or 1011 or 1101 or 1110). if all are one non of the key is pressed
				row=2;				   // if there is any change then it belongs to row 2
				break;				  // come out of 2nd while loop
			}

			IOCLR0=0x0ff;				// row and column = 0000 clear row and column data
			IOSET0=0x70;				// row = 0111 i.e. pin P0.22 =0
			IOSET0=0xf;				   // column = 1111
			read_port=0x00;
			read_port=IOPIN0;			// check for the key press

			if((read_port=read_port & 0xf)!=0xf)	   // mask other pins to check the column pins only
			{					   // check for any change in column value (0111 or 1011 or 1101 or 1110). if all are one non of the key is pressed
				row=3;				   // if there is any change then it belongs to row 3
				break;				   // come out of 2nd while loop
			}	

		}
																  // end of 2nd while loop

//------------------
		if(read_port==0xe)
			lcddata(key[row][0]);

		else if(read_port==0xd)
			lcddata(key[row][1]);

		else if(read_port==0xb)
			lcddata(key[row][2]);		

		else if(read_port==0x7)
			lcddata(key[row][3]);
	}			 									// end of 1st while loop

}

void lcddata(unsigned char key)
{
	//Just printing the values of keys on GLCD screen by using set_data function...

 	if(key=='0')		set_data(10,10,"0",6,0);
	else if(key=='1')	set_data(20,10,"1",6,0);
	else if(key=='2')	set_data(30,10,"2",6,0);
	else if(key=='3')	set_data(40,10,"3",6,0);
	else if(key=='4')	set_data(50,10,"4",6,0);
	else if(key=='5')	set_data(60,10,"5",6,0);
	else if(key=='6')	set_data(70,10,"6",6,0);
	else if(key=='7')	set_data(80,10,"7",6,0);
	else if(key=='8')	set_data(90,10,"8",6,0);
	else if(key=='9')	set_data(100,10,"9",6,0);
	else if(key=='A')	set_data(110,10,"A",6,0);
	else if(key=='B')	set_data(120,10,"B",6,0);
	else if(key=='C')	set_data(130,10,"C",6,0);
	else if(key=='D')	set_data(140,10,"D",6,0);
	else if(key=='E')	set_data(150,10,"E",6,0);
	else if(key=='F')	set_data(160,10,"F",6,0);
	else if(key=='G')	set_data(170,10,"G",6,0);
	else if(key=='H')	set_data(180,10,"H",6,0);
	else if(key=='I')	set_data(190,10,"I",6,0);
	else if(key=='J')	set_data(200,10,"J",6,0);
	else if(key=='K')	set_data(210,10,"K",6,0);
	else if(key=='L')	set_data(220,10,"L",6,0);
	else if(key=='M')	set_data(230,10,"M",6,0);
	else if(key=='N')	set_data(240,10,"N",6,0);
	else if(key=='O')	set_data(250,10,"O",6,0);
	else if(key=='P')	set_data(10,20,"P",6,0);
	else if(key=='Q')	set_data(20,20,"Q",6,0);
	else if(key=='R')	set_data(30,20,"R",6,0);
	else if(key=='S')	set_data(40,20,"S",6,0);
	else if(key=='T')	set_data(50,20,"T",6,0);
	else if(key=='U')	set_data(60,20,"U",6,0);
	else if(key=='V')	set_data(70,20,"V",6,0);
	else;
}