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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
08/29/06 00:17
Read: times


 
#123244 - Detect Key is held down
Dear all,
I'm writing a piece of C code to control Lcd and Kepad and I have hard time to figure out how to detect key is held down. The task is very simple: Everytime a key is pressed a corresponding key will be displayed on Lcd. It seems to work OK so far. However, there is a small problem. I think the problem lies in the way that I used P3.
I use lower 4 bit of P3 to output data to Lcd, at the same time I also use the same port pin to connect to rows of keypad. These lines are use as row drive (output). It means everytime I write to Lcd and if there is key held down then data will not display correctly. For this reason I'm trying to find the way how to implement the key held down in the keypad routine I just wrote, and I don'n know how to do it. I've done this in assembly, and it works nicely, but I have hard time to make it work in C. Here is the keypad routine. Any input, suggestions are greatly appreciated. Have a good one.

/*----------------------------------------------------------------------------------*/
/* File Name	: Keypad
/* Processor	: 87C752
/* ToolChain	: Keil C51 V8.0
/* Author	: T.L
/* Version	: 1.0
/* Date		: 08/22/06
/*----------------------------------------------------------------------------------*/

#pragma code
#pragma aregs
#pragma db

#include <Reg752.h>
#include <Delay.h>

/*----------------------------------------------------------------------------------*/
/* Global Constants
/*----------------------------------------------------------------------------------*/

#define	SysClk		12000000		// 12MHz
#define True		1
#define	False		0
#define Num_Col		4
#define Num_Row		4
#define Lcd_Kp		P3

const char code row_scan[4] = {0x0e, 0x0d, 0x0b, 0x07};

const char code col_val[16] = {0x00, 0x00, 0x01, 0x00,
			       0x02, 0x02, 0x00, 0x00,
			       0x03, 0x00, 0x00, 0x00,
			       0x00, 0x00, 0x00, 0x00};

const char code key_code[16] = {'1', '2', '3', 'A',
			        '4', '5', '6', 'B',
			        '7', '8', '9', 'C',
			        '*', '0', '#', 'D'};

extern bit flag;
extern unsigned char valid_key;

/*----------------------------------------------------------------------------------*/
/* Function prototypes
/*----------------------------------------------------------------------------------*/

void Keypad (void);

/*----------------------------------------------------------------------------------*/

void Keypad (void)
{
	unsigned char col, row, key;
	static unsigned char cnt, key_save = 0x00;

	Lcd_Kp = 0xf0;				  //Set P3 high nibble as input

	for (row = 0; row < 4; row++)
	{
		Lcd_Kp = Lcd_Kp | row_scan[row];  //Sequentially set row0 to row 4 low

		col = Lcd_Kp;			  //Read port

		col = ((col & 0xf0) ^ 0xf0) >> 4; //Mask high nibble then shift to low

	// In order to get correct keycode col values are modified by col_val table
	// col_val table needs only 9 values corresponding to col values 1, 2, 4, 8.
	// The first value of the table is null value = 0.
	// Keycodes are found based on the formula:
	// Key = col + (row * 4).
	// Where: 4 = number of columns.
	// 	  col = 0 -> 3
	// 	  row = 0 -> 3
 
		if (col != 0)
		{
			key = (col_val[col] + (row * Num_Col));

			key = key_code[key];

	// Key debounce is done by comparing 10 conseccutive values stored in key
	// and key_save variables. Every time the routine is called it will compare
	// these values. If they are equaled then cnt increases by 1. After 10 time
	// the key is register to valid_key and visible to the calling routine and
	// cnt is reset to 0. 

			if (key != key_save)
			{ 
				key_save = key;

				cnt = 1;
			}

			else if ((key == key_save) && (cnt < 10))
			{
				cnt++;

			}

			else
			{
				valid_key = key_save;

				cnt = 0;

				flag = 1;
			}
		}
		Lcd_Kp = 0xf0;
	}
}

/*----------------------------------------------------------------------------------*/


Best regards,
T.L

List of 19 messages in thread
TopicAuthorDate
Detect Key is held down            01/01/70 00:00      
   Like a Debounce...?            01/01/70 00:00      
      Key held down            01/01/70 00:00      
         Holly Bad Ideas Batman!!            01/01/70 00:00      
            Remember the old CMOS setup for PC's?            01/01/70 00:00      
         (a bit of) the schematic would be helpful            01/01/70 00:00      
            Schematic            01/01/70 00:00      
               No            01/01/70 00:00      
                  Schematic            01/01/70 00:00      
                     Ok, why not do this            01/01/70 00:00      
                        I'll try            01/01/70 00:00      
                     Schematic            01/01/70 00:00      
                        "Typo"            01/01/70 00:00      
   Aside: Block comments            01/01/70 00:00      
      is that true?            01/01/70 00:00      
         Yes            01/01/70 00:00      
      Thank you            01/01/70 00:00      
      an element of style            01/01/70 00:00      
         I'd not            01/01/70 00:00      

Back to Subject List