Espanol
AddThis Social Bookmark Button

06a Keyboard Manager

Keyboard Manager

//Title:	gest_tastiera_100.c
//Author:	najro.prestato
//Date:		12/03/07
//Current Rev:	100
//Mod.Descr.:	Keys capture module
//
//	ptastihi and ptastilo are freezing the keyboard state at the first acquisition.
//	mtastihi and mtastilo are freezing the keyboard state at the next acquisition.
//	tastihi and tastilo are used so that every program's module can have real 
//	time access to the keboard state.
//	NB: 	key pressed		bit=0
//		key not pressed     	bit=1
//
//	Example 1: 
//		no key pressed
//			tastilo=11111111b 	tastihi=11111111b
//
//	Example 2: 
//		key4pressed for a NOT acceptable time
//			ptastilo=11111110b (*)	ptastihi=11111111b
//		after x mS key not active	
//			mtastilo=11111111b 	mtastihi=11111111b
//		as mtastilo is different of ptastilo, not updating tastilo and tastihi that mantaince
//		the previous value, that is the configuration for no key pressed (Example 1)
//
//	Example 3: 
//		key 4 pressed for an acceptable time
//			ptastilo=11111110b(*) 	ptastihi=11111111b
//		after xmS key active	
//			mtastilo=11111110b(*) 	mtastihi=11111111b
//		as mtastilo is equal to ptastilo, updating tastilo and tastihi wit the new
//		configuration, that is:
//			tastilo=11111110b(*) 	tastihi=11111111b
//
//	(*) for the program, the bit 0 of tastilo represents the key 4
//                but it can be configured as you wish


//1: Directives
#include "sfr62.h"
#include "global.h"
#include "gest_wdt.h"
#include "gest_tastiera.h"

//2: Local Variables
union 	register 	             tastilo,tastihi;
unsigned char 		ptastilo,ptastihi;
unsigned char	 	mtastilo,mtastihi;
unsigned char		statotastiera,tmrari;

//3: Local Function Prototypes
void gest_tastiera(void);

//4: Keyboard Management Module
void gest_tastiera(void)
{
	//NB: Key not pressed = 1 logic
	//
	//Define temporary register
	uchar wkreg;
	
	switch (statotastiera)
	{
		//Keyboard definition aquisition for the first time and antibouncing activation
		case 0:
			wkreg=0xFF;
			//Acquisition keys line 1 (1-2-3-4)
			TR1=0;
			wkreg=p3;
			ptastilo=wkreg>>4;
			TR1=1;
			//Acquisition keys line 2 (5-6-7-8)
			TR2=0;
			wkreg=p3 & 0xF0;
			ptastilo=ptastilo+wkreg;
			TR2=1;
			//Acquisition keys line 3 (9-0-U-D)
			TR3=0;
			wkreg=p3;
			ptastihi=wkreg>>4;
			TR3=1;
			//Acquisition keys line 4 (S-W/R- - )
			TR4=0;
			wkreg=p3 & 0xF0;
			ptastihi=ptastihi+wkreg;
			TR4=1;
			//Preload timer antibouncing
			tmrari=cTMRARI;
			//Goto antibouncing management
			statotastiera=01;
			break;
	//Waiting antibouncing end and verify keyboard configuration with eventual assignment
		case 1:
			//Attendi conclusione antirimbalzo
			if (tmrari==00)
			{
				wkreg=0xFF;
				//Reacquisition keys line 1 (1-2-3-4)
				TR1=0;
				wkreg=p3;
				mtastilo=wkreg>>4;
				TR1=1;
				//Reacquisition keys line 2 (5-6-7-8)
				TR2=0;
				wkreg=p3 & 0xF0;
				mtastilo=mtastilo+wkreg;
				TR2=1;
				//Reacquisition keys line 3 (9-0-U-D)
				TR3=0;
				wkreg=p3;
				mtastihi=wkreg>>4;
				TR3=1;
				//Reacquisition keys line 4 (S-W/R- - )
				TR4=0;
				wkreg=p3 & 0xF0;
				mtastihi=mtastihi+wkreg;
				TR4=1;
			//Verify if configuration valid and eventual transfer on tastilo 
			//and tastihi the new configuration
				if ((mtastilo==ptastilo) || (mtastihi==ptastihi))
				{
					tastilo.byte=mtastilo;
					tastihi.byte=mtastihi;
				}
				//Reinit acquisition
				statotastiera=00;
			}
			break;
		//
		default:
			//In case of unexpected values on statotastiera, restart cycle.
			statotastiera=00;
			break;
	}
	//Update the WDT management register
	checkwdt+=cWDTRO;
}

Practically it's acquisting (using support registers) at regular intervals the keyboard configuration and then are being compared; in the case that are equals the registers tastilo and tastihi; in this manner it's getting free by eventual spike problems, spurious, rapid acctivations, etc.