| ??? 06/29/10 19:19 Read: times |
#176980 - Code! Responding to: ???'s previous message |
Per Westermark said:
Compare with zero is better Very informative! Will keep this thing in mind for every project now on. Thanks Per and Konstantinos :) Hi Murray, as far as code is concerned, I haven't explicitly defined the states of the keyboard as were used by Per:
void main(void)
{
/*----------setup interrupts-----------*/
EA=1; // enable interrupts
ET1=1; // Enable the Timer 1 overflow interrupt
/*-------------------------------------*/
/*----------- setup timers -------------*/
TMOD=0x10|0x01; // timer 1 & 0 both in mode 1, 16 bit timer
TH1 = SOFT_RTC_TH1_RELOAD; //50ms with 11.0592 MHz
TL1 = SOFT_RTC_TL1_RELOAD; //will be 49.9664 ms for (76,31)
/*-------------------------------------*/
TR1 = 1; //run software RTC
LCD_init();
UART_init();
while(1)
{ .
. //other stuff
keyboard_handler();
.
.
} // while(1)
} // main()
and here is the branching from main():
void keyboard_handler(void)
{
unsigned char key_temp, prev_key_from_buff;
key_temp = get_key_status();
if(multitap_timeout==0)
lcdcmd(CRSR_BLINK);
else
lcdcmd(CRSR_ON);
if(key_temp)
{
/* if(key_temp == '*')
{ gotocursorXY(crsr_x_global-1, crsr_y_global);
lcdchar(' ');
gotocursorXY(crsr_x_global-1, crsr_y_global);
return;
} */ //trying backspace
if(keys_cnt == 1)
{ if(keypress_buff[sizeof(keypress_buff)-2])/**/ //only if buffer is not empty
prev_key_from_buff = keypress_buff[sizeof(keypress_buff)-2]; //2nd last byte
else prev_key_from_buff = '~'; //if u r pressing key just after boot, last key would be '~'
}
else
prev_key_from_buff = keypress_buff[keys_cnt-2];
if((key_temp == prev_key_from_buff)) //repetition of same key without timeout period. Rotate through accepted keys.
{ if(multitap_timeout>0)
{ if(++index == strlen(keys_abc[key_temp-'0'])) //index can be 0..3 for "abc2"
index = 0;
if(crsr_x_global==1) //ranges from 1..20
gotocursorXY(20, crsr_y_global-1);
else
gotocursorXY(crsr_x_global-1, crsr_y_global);
lcdchar(*(keys_abc[key_temp-'0'] + index));
}
else //multitap_timeout = 0 ... 1 sec has elapsed since last keypress
{ index = 0;
lcdchar(*(keys_abc[key_temp-'0'] + index));
}
}
else //that is a different key from last pressed
{ index = 0;
lcdchar(*(keys_abc[key_temp-'0'] + index)); //print base char for that key
}
multitap_timeout = 20; //timeout must start at EVERY keypress
} // if(key_temp)
} // keyboard_handler()
I am saving nth key pressed into a circular buffer keypress_buff, n being = keys_cnt. get_key_status() returns the ASCII '1','2','3'... etc so '0' is offset to convert ASCII to decimal.
I hope you can make out the rest of the code. |
| Topic | Author | Date |
| Ideas for Multi-tap keyboard routine | 01/01/70 00:00 | |
| just follow | 01/01/70 00:00 | |
| Multi-tap is not too difficult | 01/01/70 00:00 | |
| Two-step operation. Keyboard input + post-processing | 01/01/70 00:00 | |
| State Machine! | 01/01/70 00:00 | |
| Agree 100% | 01/01/70 00:00 | |
| Time to code | 01/01/70 00:00 | |
| Software Timers! | 01/01/70 00:00 | |
| Practical Limits | 01/01/70 00:00 | |
| Don't lock up in infinite loops everywhere | 01/01/70 00:00 | |
| In the pseudo code... | 01/01/70 00:00 | |
| State Machine | 01/01/70 00:00 | |
| Divide by 5 | 01/01/70 00:00 | |
| Timer resolution | 01/01/70 00:00 | |
| State Machine | 01/01/70 00:00 | |
| Looks not bad programming practice | 01/01/70 00:00 | |
| Using Timer May Still be Possible | 01/01/70 00:00 | |
| Done ! | 01/01/70 00:00 | |
| Very Cool!!! | 01/01/70 00:00 | |
| Compare with zero is better | 01/01/70 00:00 | |
| Avoid ISR jitter using timer T1 | 01/01/70 00:00 | |
| Code! | 01/01/70 00:00 | |
Thanks Munish... | 01/01/70 00:00 |



