Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
12/17/06 21:43
Read: times


 
#129648 - Key scanning in C51
Off lately, I have been involved in converting all my A51 codes into C51. One of them happens to be the HEXkeypad key scanning (original A51 version is here http://www.8052.com/forum/read.phtml?id=120801). Below is the C51 version of the A51 key scan routine (concept remains same) which had been discussed elaborately in this forum before. I would appreciate it if anybody out there would post some comments regarding the reliability of the “unsigned char key_scan(void)” subroutine. The code is ‘working fine’ ‘for me’. (Out of my previous experiences I am sure that I will get quite a few comments on this sentence too. )

#include<reg51.h>
#include "delay.h"

#define LCDport P1
sbit RS=LCDport^1;
sbit RW=LCDport^2;
sbit EN=LCDport^3;
#include "4bit_lcd.h"

#define KEYPADport P0

sbit row0=KEYPADport^0;
sbit row1=KEYPADport^1;
sbit row2=KEYPADport^2;
sbit row3=KEYPADport^3;

#define debounce_delay 20

sbit clmn0=KEYPADport^7;
sbit clmn1=KEYPADport^6;
sbit clmn2=KEYPADport^5;
sbit clmn3=KEYPADport^4;

unsigned char code mat[4][4]={'1','2','3','A',	 // lookup table
                              '4','5','6','B',
			      '7','8','9','C',
			      '*','0','#','D'};



unsigned char find_clmn(unsigned int row_num)   // row_num = row-number of press key
{
	unsigned int clmn_num=0;

	while(clmn0!=0)			 // corresponding column of the press key will be grounded,
	{				//so check last bit for '0'
		KEYPADport<<=1;	       // shift left until column is found
		clmn_num++;	 // keep track of column-number
	}
	return mat[row_num][clmn_num];	 // return the character equivalent(look-up table)of pressed key
}


unsigned char key_scan(void)
{
	unsigned char character;
	unsigned int temp;

	do				 // check if all keys are released initially
	{
	KEYPADport=0xf0;		 // all rows low, all columns high
	temp=KEYPADport;	
        temp&=0xf0;			 // consider only columns
	}
	while(temp!=0xf0);		 // key pressed=> one of the columns go low

	while (1)
	{
		do
		{
			do		 // check if any key is pressed
			{
			delay_ms(debounce_delay);
			temp=KEYPADport;						 
	        temp&=0xf0;		 // consider only columns
			}
			while(temp==0xf0); // key pressed=> one of the columns go low

			delay_ms(debounce_delay); // provide debounce
			temp=KEYPADport;
                        temp&=0xf0;		 // consider only columns
		}
		while(temp==0xf0);		 // if temp!=0xf0, key-press confirmed

		row0=0;				 // induvidually ground each row to find row-number of pressed key
		row1=1;
		row2=1;
		row3=1;

		if(KEYPADport!=0xfe)		 // check for row0
		{
			character=find_clmn(0);
			break;
		}

		row0=1;
		row1=0;
		row2=1;
		row3=1;

		if(KEYPADport!=0xfd)		 // check for row1
		{
			character=find_clmn(1);	 // find column
			break;			 // stop scanning once pressed key is found 
		}

		row0=1;
		row1=1;
		row2=0;
		row3=1;

		if(KEYPADport!=0xfb)		// check for row2
		{
			character=find_clmn(2);
			break;
		}

		row0=1;
		row1=1;
		row2=1;
		row3=0;

		if(KEYPADport!=0xf7)		// check for row3
		{
		character=find_clmn(3);
		break;
		}	
	}
	return character;
}   

void main()
{
	lcd4_initialize();
	KEYPADport=0xff;
	while(1)
	{
		unsigned char keyscan_digit;

		keyscan_digit=key_scan();
		lcd_datw(keyscan_digit);
	}
}


List of 4 messages in thread
TopicAuthorDate
Key scanning in C51            01/01/70 00:00      
   Post delay.h, 4bit:_lcd.h,            01/01/70 00:00      
      "delay.h" & "4bit_lcd.h"            01/01/70 00:00      
         Header files            01/01/70 00:00      

Back to Subject List