📄 keypad.c
字号:
#include <mega32.h>
#include "keypad.h"
#include "cryptophone.h"
/***************************************************************************************************
Location : PORTC
Config : 11110000
High nibble is used to select row and low column is to read the whole row.
***************************************************************************************************/
// Lookup Table
unsigned int keys = 0;
flash unsigned char key_table[17]={ 0,
'D','#','0','*',
'C','9','8','7',
'B','6','5','4',
'A','3','2','1' };
/***************************************************************************************************
Function Name : interrupt [TIM2_OVF] void timer2_int(void)
Description : Scheduled keypad scanning process based on timer2 overflow interrupt which
is occured every 2ms;
Number of Argument : none
Type of Argument : void
Type of Return Value : void
***************************************************************************************************/
interrupt [TIM2_OVF] void timer2_int(void){
static unsigned char key_pressed_counter=20;
static unsigned char key_released_counter,column=FIRST_COLUMN;
static unsigned int row_data,crt_key;
TCNT2 = KEYPAD_TIMER_SET; // initialize timer2
row_data <<= 4;
row_data |= (~KEYIN & 0x0f);
column >>= 1;
if (column == (LAST_COLUMN>>1)){
column = FIRST_COLUMN;
if (!row_data) goto new_key;
if (key_released_counter){
--key_released_counter;
}else{
if (--key_pressed_counter == 9){
crt_key=row_data;
}else{
if (row_data != crt_key){
new_key: key_pressed_counter = 10;
key_released_counter = 0;
goto end_key;
};
if (!key_pressed_counter){
keys = row_data;
key_released_counter = 20;
};
};
};
end_key:;
row_data = 0;
};
KEYOUT = ~column;
}
/***************************************************************************************************
Function Name : unsigned char inkey(void)
Description : Convert scanned data provided by scanning routine.
The scanning routine provides scanned key as a 16 bit string, while the
pressed key is simply represented by the location of non zero bit.
Assumption : the [keys] variable contains scanned key
Number of Argument : none
Type of Argument : void
Type of Return Value : ASCII character [0123456789*#ABCD]
***************************************************************************************************/
unsigned char keypad_read(void){
unsigned char i=0; // declare bit counter
while(keys){ // detect non zero bit location and save it to i
keys >>= 1;
i++;
}
return key_table[i];
}
/***************************************************************************************************
Function Name : void init_keypad(void)
Description : Initialize Keypad
Assumption :
Number of Argument :
Type of Argument :
Type of Return Value :
***************************************************************************************************/
void keypad_init(void){
TCNT2 = KEYPAD_TIMER_SET; // set to overflow every 2ms
TCCR2 = ( TIMER_COUNTER_2_NORMAL_PORT_OPERATION | // Use OC2 as normal
TIMER_COUNTER_2_CLOCK_SELECT_CLK_IO_PRESCALING_256 | // prescalar 256
TIMER_COUNTER_2_WAVEFORM_NORMAL ); // Use timer2 as norml mode
TIMSK = TIMER_COUNTER_2_OVERFLOW_INTERRUPT_ENABLE; // enable timer2 overflow
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -