/*
///////////////////////////////////////////////////////////////////////////////
ATMEGA 128, 2561 KEY Library V.1.0
1st v.1.0 : 2011년 9월 23일
제작자 : (주)이지플로우
///////////////////////////////////////////////////////////////////////////////
*/
#ifdef EF_KEY4
//#define KEY4_DATA_0123
#define KEY4_DATA_4567
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// 4Key 포트를 정의합니다.
// Key data Input:Bit 0,1,2,3 or
// Key data Input:Bit 4,5,6,7
#define KEY4_DATAIN (PINB)
#define KEY4_DATA (PORTB)
#define KEY4_DATA_DIR (DDRB)
#define KEY4_BIT0 CLEARBIT (KEY4_DATA_DIR, 0)
#define KEY4_BIT1 CLEARBIT (KEY4_DATA_DIR, 1)
#define KEY4_BIT2 CLEARBIT (KEY4_DATA_DIR, 2)
#define KEY4_BIT3 CLEARBIT (KEY4_DATA_DIR, 3)
#define KEY4_BIT4 CLEARBIT (KEY4_DATA_DIR, 4)
#define KEY4_BIT5 CLEARBIT (KEY4_DATA_DIR, 5)
#define KEY4_BIT6 CLEARBIT (KEY4_DATA_DIR, 6)
#define KEY4_BIT7 CLEARBIT (KEY4_DATA_DIR, 7)
#define KEY4_KEY_DEBOUNCE ( 20/4) // 20ms
#define KEY4_KEY_DEBOUNCE_LONG (1000/4) //2000ms
// newkeyData16 에 내용이 있으면 처리합니다.
// newLongKeyData16 에 내용이 있으면 처리합니다.
///////////////////////////////////////////////////////////////////////////////
//Interrupt KeyScan key-matrix 4 by 4
//인터럽트에서 4*4 키이 메트릭스 스캔 입력을 받습니다.
//스캔 주기는 1초에 100번 (10ms)로 호출합니다.
unsigned char IntKeyData, oldIntKeyData;
unsigned char newIntKeyData, newLongIntKeyData;
unsigned char cntIntKeyDebounce;
unsigned int cntIntKeyDebounceLong;
unsigned char cntIntKeyRow,flagIntKeyRun;
void IntCheckKey4 (void )
{
//4비트 입력을 부논리로 받습니다.
#if defined(KEY4_DATA_0123)
IntKeyData = (KEY4_DATAIN ^ 0x0F) & 0x0F;
#else
IntKeyData = (KEY4_DATAIN ^ 0xF0) & 0xF0;
#endif
//새로 입력 받은 키이가 같은지 비교합니다.
if ( IntKeyData == 0 ) { oldIntKeyData=0; return; }
if ( IntKeyData != oldIntKeyData )
{
oldIntKeyData = IntKeyData;
cntIntKeyDebounceLong = KEY4_KEY_DEBOUNCE_LONG;
cntIntKeyDebounce = KEY4_KEY_DEBOUNCE;
}
else
{
if (cntIntKeyDebounce != 0)
{
cntIntKeyDebounce--;
if (cntIntKeyDebounce == 0)
{
newIntKeyData = IntKeyData;
flagIntKeyRun = 1;
}
}
if (cntIntKeyDebounceLong != 0)
{
cntIntKeyDebounceLong--;
if (cntIntKeyDebounceLong == 0)
{
newLongIntKeyData = IntKeyData;
flagIntKeyRun = 2;
}
}
}
}
void IntKeyScan4 (void )
{
cntIntKeyRow ++;
cntIntKeyRow &= 0x03;
if ( cntIntKeyRow == 0 ) { IntCheckKey4(); }
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//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 CheckKEY4( unsigned char i)
{
#if defined(KEY4_DATA_0123)
if ( (i&0x01) != 0 ) { return 1; }
if ( (i&0x02) != 0 ) { return 2; }
if ( (i&0x04) != 0 ) { return 3; }
if ( (i&0x08) != 0 ) { return 4; }
#else
if ( (i&0x10) != 0 ) { return 1; }
if ( (i&0x20) != 0 ) { return 2; }
if ( (i&0x40) != 0 ) { return 3; }
if ( (i&0x80) != 0 ) { return 4; }
#endif
return 0;
}
void RunKey4 (void )
{
unsigned char j;
KeyNumber = 0;
LongKeyNumber = 0;
if (flagIntKeyRun == 1)
{
j = CheckKEY4(newIntKeyData);
if( j!=0 )
{
KeyNumber = j;
newIntKeyData = 0;
}
}
if (flagIntKeyRun == 2)
{
j = CheckKEY4(newLongIntKeyData);
if( j!=0 )
{
LongKeyNumber = j;
newLongIntKeyData = 0;
}
}
flagIntKeyRun = 0;
}
///////////////////////////////////////////////////////////////////////////////
#endif
|