관리 메뉴

안까먹을라고 쓰는 블로그

KEY 입력 기술 본문

Embedded/Atmega128

KEY 입력 기술

YawnsDuzin 2012. 9. 1. 20:26

 

반응형

디바운싱(debouncing) - 마이크로세서가 키를 여러번 누른 것으로 잘못 인식하는 것을 방지하기 위해서 키 접점에서의 진동을

                                      제거 하는 것.

 

 - 단순 입력방식

   키 스위치들의 ON/OFF 상태를 소프트웨어적으로 단순히 병렬 입력하여 읽어들이는 방법

 

 - 키 스캔 방식

   키 스위치들이 매트릭스(matrix)구조로 배열되어 있고 이것들의 ON/OFF 상태를 소프트웨어적으로

   스캔(scan)하여 읽어들이는 방법

 

 - 엔코더나 전용 LSI를 사용하는 방식 

    TTL 이나 CMOS 의 엔코더 소자를 사용하는 방법으로 각 키가 눌러지면 이에 해당하는 병력 출력신호를

    하드웨어적으로 발생한다.

 

#include "c:\AvrEdit\JJ128c\JJ128.h" 의 Key_input()함수

 

unsigned char key_flag = 0;

unsigned char Key_input(void)   /* input key SW1 - SW4 */
{ unsigned char key;

  key = PINF & 0xF0;    // any key pressed ?
  if(key == 0xF0)    // if no key, check key off
    { if(key_flag == 0)
        return key;
      else
        { Delay_ms(20);
          key_flag = 0;
          return key;
        }
    }
  else      // if key input, check continuous key
    { if(key_flag != 0)    // if continuous key, treat as no key input
        return 0xF0;
      else     // if new key, beep and delay for debounce
        { Beep();
          key_flag = 1;
          return key;
        }
    }
}

 

- 단순입력방식 프로그램

 

#include <avr/io.h>
#include "c:\AvrEdit\JJ128c\JJ128.h"

 

int main(void)
{
  MCU_initialize();                             // initialize MCU
  Delay_ms(50);                                 // wait for system stabilization
  LCD_initialize();                             // initialize text LCD module

  LCD_string(0x80,"   KEY INPUT    ");          // display title
  LCD_string(0xC0,"Press SW1-SW4 ! ");
  Beep();

 

  while(1)
    { switch(Key_input())                       // key input
        { case 0xE0 : PORTC = 0x10;
                      LCD_string(0xC0,"SW1 was pressed.");
                      break;
          case 0xD0 : PORTC = 0x20;
                      LCD_string(0xC0,"SW2 was pressed.");
                      break;
          case 0xB0 : PORTC = 0x40;
                      LCD_string(0xC0,"SW3 was pressed.");
                      break;
          case 0x70 : PORTC = 0x80;
                      LCD_string(0xC0,"SW4 was pressed.");
                      break;
          default:    break;
        }
    }
}

 

- 키 스캔방식 프로그램

                    +5V

                     |

  PD7 ______|■_|■_|■_|■
                    |   |   |   |  
  PD6 ______|■_|■_|■_|■
                    |   |   |   |   
  PD5 ______|■_|■_|■_|■
PF4/KEY1 ___|   |   |   |
PF5/KEY2 _______|   |   |
PF6/KEY3 ___________|   |
PF7/KEY4 _______________|   

          SW1 SW2 SW3  SW4  

 

 

#include <avr/io.h>
#include "c:\AvrEdit\JJ128c\JJ128.h"

 

unsigned char Keypad(void)   /* input keypad 0 - 9, *, # */
{ unsigned char i, key, row, column;
  unsigned char row_scan[3] = {0xC0, 0xA0, 0x60 };

  PORTD = 0x00;                                 // any key pressed ?
  Delay_us(1);
  column = PINF & 0xF0;
  if(column == 0xF0)                            // if not, key_flag = 0 ?
    { if(key_flag != 0)
        { Delay_ms(20);                         // if not, delay 20ms
          key_flag = 0;                         //         and key_flag = 0
        }
      return 0x00;                              // return without input key
    }

  else if(key_flag != 0)                        // if key_flag != 0, continuous key
    return 0x00;

  else
    { Beep();                                   // if key, beep and delay 50ms
      key_flag = 1;                             //         and key_flag = 1

      for(i=0; i<3; i++)                        // scan
        { row = row_scan[i];
          PORTD = row;
          Delay_us(1);
          column = PINF & 0xF0;
          if(column != 0xF0)
            break;
        }
      if(i == 3)
        return 0x00;

      key = row + ((column >> 4) & 0x0F);
      switch(key)                               // get key ASCII code
        { case 0xCE : key = '1';  break;
          case 0xCD : key = '4';  break;
          case 0xCB : key = '7';  break;
          case 0xC7 : key = '*';  break;
          case 0xAE : key = '2';  break;
          case 0xAD : key = '5';  break;
          case 0xAB : key = '8';  break;
          case 0xA7 : key = '0';  break;
          case 0x6E : key = '3';  break;
          case 0x6D : key = '6';  break;
          case 0x6B : key = '9';  break;
          case 0x67 : key = '#';  break;
          default:    key = 0x00; break;
        }
      return key;
    }
}

 

int main(void)
{ unsigned char cursor, key;

  MCU_initialize();                             // initialize MCU
  Delay_ms(50);                                 // wait for system stabilization
  LCD_initialize();                             // initialize text LCD module

  LCD_string(0x80,"  KEYPAD INPUT  ");          // display title on text LCD
  LCD_string(0xC0,"                ");
  Beep();

  LCD_command(0x0E);           // display cursor
  LCD_command(0xC0);                            // cursor home
  cursor = 0;

  while(1)
    { key = Keypad();    // input keypad
      if(key != 0x00)           // if keypad inpy, display
        { LCD_data(key);
          cursor++;                             // if cursor = 16, cursor = 0
          if(cursor == 16)
            { LCD_command(0xC0);
              cursor = 0;
              Beep();
            }
        }
    }
}

 

반응형
Comments