/*
///////////////////////////////////////////////////////////////////////////////
ATMEGA 128, 2561 KEY Library V.1.0
1st v.1.0 : 2013년 5월 18일
제작자 : (주)이지플로우
///////////////////////////////////////////////////////////////////////////////
*/
#ifdef EF_KEY8BY4
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// 메트릭스 8BY4 포트를 정의합니다.
// Matrix KEY 8BY4 : 32 Keys
// Key data Input:Bit 0,1,2,3,4,5,6,7
// Row Output:Bit 0,1,2,3 또는 4,5,6,7
//#define ROW0123 //Use Row bit 0,1,2,3 else 4,5,6,7
#define KEY8BY4_DATAIN (PINB)
#define KEY8BY4_DATA (PORTB)
#define KEY8BY4_DATA_DIR (DDRB)
#define KEY8BY4_ROW (PORTE)
#define KEY8BY4_ROW_DIR (DDRE)
#ifdef ROW0123
#define KEY8BY4_ROW_BIT0 CLEARBIT (KEY8BY4_ROW, 0)
#define KEY8BY4_ROW_BIT1 CLEARBIT (KEY8BY4_ROW, 1)
#define KEY8BY4_ROW_BIT2 CLEARBIT (KEY8BY4_ROW, 2)
#define KEY8BY4_ROW_BIT3 CLEARBIT (KEY8BY4_ROW, 3)
#else
#define KEY8BY4_ROW_BIT0 CLEARBIT (KEY8BY4_ROW, 4)
#define KEY8BY4_ROW_BIT1 CLEARBIT (KEY8BY4_ROW, 5)
#define KEY8BY4_ROW_BIT2 CLEARBIT (KEY8BY4_ROW, 6)
#define KEY8BY4_ROW_BIT3 CLEARBIT (KEY8BY4_ROW, 7)
#endif
#ifdef ROW0123
#define KEY8BY4_DIR_BIT0 SETBIT (KEY8BY4_ROW_DIR, 0)
#define KEY8BY4_DIR_BIT1 SETBIT (KEY8BY4_ROW_DIR, 1)
#define KEY8BY4_DIR_BIT2 SETBIT (KEY8BY4_ROW_DIR, 2)
#define KEY8BY4_DIR_BIT3 SETBIT (KEY8BY4_ROW_DIR, 3)
#else
#define KEY8BY4_DIR_BIT0 SETBIT (KEY8BY4_ROW_DIR, 4)
#define KEY8BY4_DIR_BIT1 SETBIT (KEY8BY4_ROW_DIR, 5)
#define KEY8BY4_DIR_BIT2 SETBIT (KEY8BY4_ROW_DIR, 6)
#define KEY8BY4_DIR_BIT3 SETBIT (KEY8BY4_ROW_DIR, 7)
#endif
#ifdef ROW0123
#define KEY8BY4_ROW_INIT ( (1<<0)|(1<<1)|(1<<2)|(1<<3) )
#else
#define KEY8BY4_ROW_INIT ( (1<<4)|(1<<5)|(1<<6)|(1<<7) )
#endif
#define KEY8BY4_KEY_DEBOUNCE ( 20/4) // 20ms
#define KEY8BY4_KEY_DEBOUNCE_LONG (1000/4) //2000ms
// newkeyData32 에 내용이 있으면 처리합니다.
// newLongKeyData32 에 내용이 있으면 처리합니다.
///////////////////////////////////////////////////////////////////////////////
//Interrupt KeyScan key-matrix 8 by 4
//인터럽트에서 8*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 )
{
//8비트 입력을 부논리로 받습니다.
IntKeyData[cntIntKeyRow] = (KEY8BY4_DATAIN ^ 0xFF);
//새로 입력 받은 키이가 같은지 비교합니다.
if ( IntKeyData[cntIntKeyRow] == 0 ) { oldIntKeyData[cntIntKeyRow]=0; return; }
if ( IntKeyData[cntIntKeyRow] != oldIntKeyData[cntIntKeyRow] )
{
oldIntKeyData[cntIntKeyRow] = IntKeyData[cntIntKeyRow];
cntIntKeyDebounceLong[cntIntKeyRow] = KEY8BY4_KEY_DEBOUNCE_LONG;
cntIntKeyDebounce[cntIntKeyRow] = KEY8BY4_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 IntKeyScan8by4 (void )
{
cntIntKeyRow &= 0x03;
IntCheckKey();
KEY8BY4_ROW |= KEY8BY4_ROW_INIT; //0123 or 4567bit high
//KEY8BY4_DATA_DIR = 0x0; //all input
cntIntKeyRow ++;
cntIntKeyRow &= 0x03;
if ( cntIntKeyRow == 0 ) { KEY8BY4_ROW_BIT0; KEY8BY4_DIR_BIT0; }
else if ( cntIntKeyRow == 1 ) { KEY8BY4_ROW_BIT1; KEY8BY4_DIR_BIT1; }
else if ( cntIntKeyRow == 2 ) { KEY8BY4_ROW_BIT2; KEY8BY4_DIR_BIT2; }
else { KEY8BY4_ROW_BIT3; KEY8BY4_DIR_BIT3; }
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//Key NO: k16 k15 k14 k13 k12 k11 k10 k09 k08 k07 k06 k05 k04 k03 k02 k01
//[0]BIT: 7 6 5 4 3 2 1 0
//[1]BIT: 7 6 5 4 3 2 1 0
//Key NO: k32 k31 k30 k29 k28 k27 k26 k25 k24 k23 k22 k21 k20 k19 k18 k17
//[2]BIT: 7 6 5 4 3 2 1 0
//[3]BIT: 7 6 5 4 3 2 1 0
//newIntKeyData[4],newLongIntKeyData[4]
unsigned char KeyNumber;
unsigned char LongKeyNumber;
unsigned char CheckKEY8BY4( 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; }
if ( (i&0x10) != 0 ) { return j+5; }
if ( (i&0x20) != 0 ) { return j+6; }
if ( (i&0x40) != 0 ) { return j+7; }
if ( (i&0x80) != 0 ) { return j+8; }
return 0;
}
unsigned char CheckLongKEY8BY4( 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; }
if ( (i&0x10) != 0 ) { return j+5; }
if ( (i&0x20) != 0 ) { return j+6; }
if ( (i&0x40) != 0 ) { return j+7; }
if ( (i&0x80) != 0 ) { return j+8; }
return 0;
}
void RunKey8by4 (void )
{
unsigned char j;
KeyNumber = 0;
LongKeyNumber = 0;
if (flagIntKeyRun == 1)
{
j = CheckKEY8BY4(0,0); if(j!=0) { KeyNumber = j; goto RunKEY8BY4lp; }
j = CheckKEY8BY4(1,8); if(j!=0) { KeyNumber = j; goto RunKEY8BY4lp; }
j = CheckKEY8BY4(2,16); if(j!=0) { KeyNumber = j; goto RunKEY8BY4lp; }
j = CheckKEY8BY4(3,24); if(j!=0) { KeyNumber = j; goto RunKEY8BY4lp; }
RunKEY8BY4lp:
newIntKeyData[0] = 0;
newIntKeyData[1] = 0;
newIntKeyData[2] = 0;
newIntKeyData[3] = 0;
}
if (flagIntKeyRun == 2)
{
j = CheckLongKEY8BY4(0,0); if(j!=0) { LongKeyNumber = j; goto RunKEY8BY4lp2; }
j = CheckLongKEY8BY4(1,8); if(j!=0) { LongKeyNumber = j; goto RunKEY8BY4lp2; }
j = CheckLongKEY8BY4(2,16); if(j!=0) { LongKeyNumber = j; goto RunKEY8BY4lp2; }
j = CheckLongKEY8BY4(3,24); if(j!=0) { LongKeyNumber = j; goto RunKEY8BY4lp2; }
RunKEY8BY4lp2:
newLongIntKeyData[0] = 0;
newLongIntKeyData[1] = 0;
newLongIntKeyData[2] = 0;
newLongIntKeyData[3] = 0;
}
flagIntKeyRun = 0;
}
///////////////////////////////////////////////////////////////////////////////
#endif
|