unsigned int tick_ms;
struct Key{
unsigned char pre_key:1; //먼저 눌려진 키값
unsigned char post_key:1; //현재 눌려진 키값
#define KEY_PRESSED 1
#define NONE_PRESSED 0 // 키처리하고 나서 클리어
unsigned char flag_mode; //동작상태값
unsigned int cnt; //채터링 방지를 위한 비교 카운트
};
struct Key Key_mode,Key_debug;
void key(unsigned char Port,unsigned int Key, struct Key *Key_state,unsigned int chatterin_ms)
{
Key_state -> post_key = (Port & Key) ? 1 : 0;
if(Key_state->post_key)
{
Key_state -> pre_key = Key_state->post_key;
}
if((Key_state->pre_key)!= (Key_state->post_key))//key Pressed
{
if((tick_ms-(Key_state->cnt)) > chatterin_ms)
{
Key_state->pre_key=Key_state->post_key;
if((Key_state->pre_key) == 0)
{
Key_state->flag_mode = KEY_PRESSED;
}
}
else;
}
else //KEY 에 변화가 없으면, 카운트 백업해둔다.
{
Key_state->cnt = tick_ms;
}
}
//tick_ms 는 인터럽트에서 1ms씩 증가하는 변수.
int main
{
key(PINA,(1<<5),&Key_mode,1000);
key(PINB,(1<<9),&Key_debug,100);
if(Key_mode.flag_mode == KEY_PRESSED)
{
Key_mode.flag_mode =NONE_PRESSED;
//실행구문
}
}
'Language > C Programming' 카테고리의 다른 글
C Programming :: splint 함수 (0) | 2012.04.17 |
---|---|
C Programming:: #pragma pack(1) (0) | 2012.03.14 |
C programming :: 전처리기 매크로 치환 (0) | 2011.12.19 |
C programming :: Quick Sorting (0) | 2011.12.19 |
C programming :: Ring Buffer (0) | 2011.12.19 |