º» ³»¿ëÀº ¸¶ÀÌÄÄÀ¸·Î ¿ÜºÎ Push Swich¸¦ ÀÔ·Â ¹ÞÀ½¿¡ ÀÖ¾î xor ·ÎÁ÷(µÎ ÀÔ·ÂÀÌ °°À¸¸é 0, ´Ù¸£¸é 1À» Ãâ·ÂÇÔ)À» ÀÌ¿ëÇÑ °ÍÀÔ´Ï´Ù.
#define KEY_CHA 0x01
#define KEY_CHB 0x02
unsigned char KeyInCount,KeyInValue,KeyInTempBuffer,KeyInOldValue,KeyTemp;
//ŸÀ̸Ӹ¦ ÀÌ¿ëÇÏ¿© ÀÏÁ¤ÇÑ tickÀ¸·Î Æ÷Æ®·Î ºÎÅÍ ÀÔ·ÂÀ» ½ºÄµÇÑ´Ù.
// Timer 0 overflow interrupt service routine
#pragma savereg+
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here
TCNT0=0xB5;
b4msec=1;
#asm("wdr"); //complement
KeyInDetect();
}
#pragma savereg-
//½ÇÁ¦ ½ºÄµÇÏ°í ÀÌÀüÀÇ °ª°ú ºñ±³ÇÏ°í KeyÀÇ Ã¤Å͸µÀ» Á¦°ÅÇÑ´Ù.
void KeyInDetect(void)
{
KeyTemp=~(PINB & 0x03);
if(KeyInTempBuffer == KeyTemp){
if(KeyInCount==5){
KeyInCount=6;
KeyInValue=KeyTemp;
}
else{
if(KeyInCount<5){
++KeyInCount;
}
}
}
else{
KeyInTempBuffer = KeyTemp;
KeyInCount=0;
}
}
//detectµÈ Å° °ªÀ» ó¸®ÇÑ´Ù.
//º» ÇÔ¼ö´Â while¹® ¼Ó¿¡ µÐ´Ù-Áï Æú¸µÃ³¸®ÇÑ´Ù.
void KeyProc(void)
{
unsigned char val;
val= KeyInValue ^ KeyInOldValue;
if(val){
if(val & KEY_CHA){
if(KeyInValue & KEY_CHA){
//Å°ÀÇ »óÅ°¡ Low¿¡¼ High·Î º¯Çß½¿
}
else{
//Å°ÀÇ »óÅ°¡ High¿¡¼ Low·Î º¯Çß½¿
}
}
if(val & KEY_CHB){
if(KeyInValue & KEY_CHB){
//Å°ÀÇ »óÅ°¡ Low¿¡¼ High·Î º¯Çß½¿
}
else{
//Å°ÀÇ »óÅ°¡ High¿¡¼ Low·Î º¯Çß½¿
}
}
KeyInOldValue=KeyInValue;
}
}