#5
Post
by scaffold » 07 Jan 2010 03:46
should it be like this??
#define DELAY_FOR_NEXT 1000 // Delay before orginal Key is returnd time depends on how foten the "Call_Me_Every_1ms()" rutine is called
// Key_list[number of keys][number of characters/key]
// if you whant to have more characters on some keys then use a null on empty spots like below
//const unsigned char Key_list[9][6]={{'A','B','C','1',!,¤},{'D','E',0,0,'F','2',%,0},{'G','H','I','3','<','>'},{'J','K','L','4',0,0},{'M','N','O','5',0,0},{'P','Q','R','6',0,0},{'S','T','U','7',0,0},{'V','W','X','8',0,0},{'Y','Z','9',' ',0,0}};
const unsigned char Key_list[9][4]={{'A','B','C','1'},{'D','E','F','2'},{'G','H','I','3'},{'J','K','L','4'},{'M','N','O','5'},{'P','Q','R','6'},{'S','T','U','7'},{'V','W','X','8'},{'Y','Z','9',' '}};
unsigned short Delay_Timer= 0; // Timer for delay
unsigned char NewKey= 0; // use this as a way to see if the value is stepped or a new Key were pressed
void Call_Me_Every_1ms(){ // call this rutine at a steddy pase, if not value will step every time the DoKey() rutine is called whit the same value
if( Delay_Timer < 0xffff ) Delay_Timer++;
}
unsigned char DoKey(unsigned char Key) { // call this rutine with the Key pressed "as a character 0-9" and it returnes the value to display if called whit any other it returns null
static unsigned char step, last; // local
if( Key < '0' || Key > '9' ) return 0; // if not a valid value return null
if( Key == last && (Delay_Timer < DELAY_FOR_NEXT) ) { step++; NewKey= 1; } else { NewKey= 0; last= Key; step= 0; Delay_Timer= 0; } // check if same Key and the amount of time is less then time before next Key should be displayd
if( (step >= sizeof(Key_list[0])) || (Key_list[Key-'0'][step] == 0 ) ) step= 0; // if step is bigger than amount of chars in list or if null go to beginning of list
return Key_list[Key-'0'][step]; // return the value for the Key as a character
}
unsigned char tstkey= 0; // only for test of this
unsigned char display_char; // only for test of this
void main( void )
{
Keypad_Init(&PORTB);
Lcd_Init(&PORTD);
Lcd_Cmd(LCD_CLEAR);
Lcd_Cmd(LCD_CURSOR_OFF);
do{
display_char= DoKey('1');
if( NewKey ) // this is a check if display should move to new position and desplay character, remove if not needed
display_char;
else
display_char;
Call_Me_Every_1ms(); // update timer
Delay_ms(1);
}while(1);
}