/*
///////////////////////////////////////////////////////////////////////////////
ATMEGA 128, 2561 KEY Library V.1.0
1st v.1.0 : 2011년 9월 23일
제작자 : (주)이지플로우
///////////////////////////////////////////////////////////////////////////////
*/
#ifdef EF_KEY4BY4
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// 메트릭스 4BY4 포트를 정의합니다.
// Matrix KEY 4BY4 : 16 Keys
// Key data Input:Bit 0,1,2,3
// Row Output:Bit 4,5,6,7
#define KEY4BY4_DATAIN (PINB)
#define KEY4BY4_DATA (PORTB)
#define KEY4BY4_DATA_DIR (DDRB)
#define KEY4BY4_ROW_BIT0 CLEARBIT (KEY4BY4_DATA, 4)
#define KEY4BY4_ROW_BIT1 CLEARBIT (KEY4BY4_DATA, 5)
#define KEY4BY4_ROW_BIT2 CLEARBIT (KEY4BY4_DATA, 6)
#define KEY4BY4_ROW_BIT3 CLEARBIT (KEY4BY4_DATA, 7)
#define KEY4BY4_DIR_BIT0 SETBIT (KEY4BY4_DATA_DIR, 4)
#define KEY4BY4_DIR_BIT1 SETBIT (KEY4BY4_DATA_DIR, 5)
#define KEY4BY4_DIR_BIT2 SETBIT (KEY4BY4_DATA_DIR, 6)
#define KEY4BY4_DIR_BIT3 SETBIT (KEY4BY4_DATA_DIR, 7)
#define KEY4BY4_ROW_INIT ( (1<<4)|(1<<5)|(1<<6)|(1<<7) )
#define KEY4BY4_KEY_DEBOUNCE ( 20/4) // 20ms
#define KEY4BY4_KEY_DEBOUNCE_LONG (1000/4) //2000ms
// newkeyData16 에 내용이 있으면 처리합니다.
// newLongKeyData16 에 내용이 있으면 처리합니다.
///////////////////////////////////////////////////////////////////////////////
//Interrupt KeyScan key-matrix 4 by 4
//인터럽트에서 4*4 키이 메트릭스 스캔 입력을 받습니다.
//스캔 주기는 1초에 100번 (10ms)로 호출합니다.
unsigned char IntKeyData[4], oldIntKeyData[4];
unsigned char newIntKeyData[4], newLongIntKeyData[4];
unsigned char cntIntKeyDebounce[4];
unsigned int cntIntKeyDebounceLong[4];
unsigned char cntIntKeyRow,flagIntKeyRun;
void IntCheckKey (void )
{
//4비트 입력을 부논리로 받습니다.
IntKeyData[cntIntKeyRow] = (KEY4BY4_DATAIN ^ 0x0F) & 0x0F;
//새로 입력 받은 키이가 같은지 비교합니다.
if ( IntKeyData[cntIntKeyRow] == 0 ) { oldIntKeyData[cntIntKeyRow]=0; return; }
if ( IntKeyData[cntIntKeyRow] != oldIntKeyData[cntIntKeyRow] )
{
oldIntKeyData[cntIntKeyRow] = IntKeyData[cntIntKeyRow];
cntIntKeyDebounceLong[cntIntKeyRow] = KEY4BY4_KEY_DEBOUNCE_LONG;
cntIntKeyDebounce[cntIntKeyRow] = KEY4BY4_KEY_DEBOUNCE;
}
else
{
if (cntIntKeyDebounce[cntIntKeyRow] != 0)
{
cntIntKeyDebounce[cntIntKeyRow]--;
if (cntIntKeyDebounce[cntIntKeyRow] == 0)
{
newIntKeyData[cntIntKeyRow] = IntKeyData[cntIntKeyRow];
flagIntKeyRun = 1;
}
}
if (cntIntKeyDebounceLong[cntIntKeyRow] != 0)
{
cntIntKeyDebounceLong[cntIntKeyRow]--;
if (cntIntKeyDebounceLong[cntIntKeyRow] == 0)
{
newLongIntKeyData[cntIntKeyRow] = IntKeyData[cntIntKeyRow];
flagIntKeyRun = 2;
}
}
}
}
void IntKeyScan4by4 (void )
{
cntIntKeyRow &= 0x03;
IntCheckKey();
KEY4BY4_DATA = KEY4BY4_ROW_INIT;
KEY4BY4_DATA_DIR = 0x0; //all input
cntIntKeyRow ++;
cntIntKeyRow &= 0x03;
if ( cntIntKeyRow == 0 ) { KEY4BY4_ROW_BIT0; KEY4BY4_DIR_BIT0; }
else if ( cntIntKeyRow == 1 ) { KEY4BY4_ROW_BIT1; KEY4BY4_DIR_BIT1; }
else if ( cntIntKeyRow == 2 ) { KEY4BY4_ROW_BIT2; KEY4BY4_DIR_BIT2; }
else { KEY4BY4_ROW_BIT3; KEY4BY4_DIR_BIT3; }
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//Key NO: k15 k14 k13 k12 k11 k10 k9 k8 k7 k6 k5 k4 k3 k2 k1 k0
//BIT : 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
unsigned char KeyNumber;
unsigned char LongKeyNumber;
unsigned char CheckKEY4By4( unsigned char i, unsigned char j)
{
i = newIntKeyData[i];
if ( (i&0x01) != 0 ) { return j+1; }
if ( (i&0x02) != 0 ) { return j+2; }
if ( (i&0x04) != 0 ) { return j+3; }
if ( (i&0x08) != 0 ) { return j+4; }
return 0;
}
unsigned char CheckLongKEY4By4( unsigned char i, unsigned char j)
{
i = newLongIntKeyData[i];
if ( (i&0x01) != 0 ) { return j+1; }
if ( (i&0x02) != 0 ) { return j+2; }
if ( (i&0x04) != 0 ) { return j+3; }
if ( (i&0x08) != 0 ) { return j+4; }
return 0;
}
void RunKey4by4 (void )
{
unsigned char j;
KeyNumber = 0;
LongKeyNumber = 0;
if (flagIntKeyRun == 1)
{
j = CheckKEY4By4(0,0); if(j!=0) { KeyNumber = j; goto RunKey4by4lp; }
j = CheckKEY4By4(1,4); if(j!=0) { KeyNumber = j; goto RunKey4by4lp; }
j = CheckKEY4By4(2,8); if(j!=0) { KeyNumber = j; goto RunKey4by4lp; }
j = CheckKEY4By4(3,12); if(j!=0) { KeyNumber = j; goto RunKey4by4lp; }
RunKey4by4lp:
newIntKeyData[0] = 0;
newIntKeyData[1] = 0;
newIntKeyData[2] = 0;
newIntKeyData[3] = 0;
}
if (flagIntKeyRun == 2)
{
j = CheckLongKEY4By4(0,0); if(j!=0) { LongKeyNumber = j; goto RunKey4by4lp2; }
j = CheckLongKEY4By4(1,4); if(j!=0) { LongKeyNumber = j; goto RunKey4by4lp2; }
j = CheckLongKEY4By4(2,8); if(j!=0) { LongKeyNumber = j; goto RunKey4by4lp2; }
j = CheckLongKEY4By4(3,12); if(j!=0) { LongKeyNumber = j; goto RunKey4by4lp2; }
RunKey4by4lp2:
newLongIntKeyData[0] = 0;
newLongIntKeyData[1] = 0;
newLongIntKeyData[2] = 0;
newLongIntKeyData[3] = 0;
}
flagIntKeyRun = 0;
}
///////////////////////////////////////////////////////////////////////////////
#endif
|