#include <mega128.h>
#include <delay.h>
//----------------------------------------------------
#define lcd_dt PORTB
#define lcd_en PORTA.6
#define lcd_rw PORTA.5
#define lcd_rs PORTA.4
#define SERVO_1 PORTA.0
//----------------------------------------------------
unsigned int i;
unsigned char PW[6] = {0,0,0,0,0,0};     //비밀번호 변수
unsigned int INPUTVALUE[6];
unsigned char setcnt = 0;
unsigned char keycnt = 0;
unsigned char value;
unsigned char keyvalue;
//----------------------------------------------------
void SERVO_CLS()
{
    for(i=0;i<30;i++)
    {
        SERVO_1 = 1;
        delay_ms(1.45);
        SERVO_1 = 0;
        delay_ms(18.55);
    }
}
void SERVO_OPEN()
{
    for(i=0;i<30;i++)
    {
        SERVO_1 = 1;
        delay_ms(2.3);
        SERVO_1 = 0;
        delay_ms(17.7);
    }
}
//---------------Key_read-----------------------------
#define pKEYPAD_OUT_A_HIGH      PORTC |= 0x01
#define pKEYPAD_OUT_A_LOW       PORTC &= 0xFE
#define pKEYPAD_OUT_B_HIGH      PORTC |= 0x02
#define pKEYPAD_OUT_B_LOW       PORTC &= 0xFD
#define pKEYPAD_OUT_C_HIGH      PORTC |= 0x04
#define pKEYPAD_OUT_C_LOW       PORTC &= 0xFB
#define pKEYPAD_IN            (PINC>>4)

char KeypadIn(void)                 //키패드 입력을 반환하는 함수

    char d;
     
    // 1열 읽기
 pKEYPAD_OUT_A_LOW;
 pKEYPAD_OUT_B_HIGH;
 pKEYPAD_OUT_C_HIGH;
 
 d = pKEYPAD_IN;
 d = pKEYPAD_IN;
 d = pKEYPAD_IN;
 d = pKEYPAD_IN;
 d = pKEYPAD_IN;
  
 if(d==0x0E){
  return '1';
 }
 if(d==0x0D){
  return '4';
 }
 if(d==0x0B){
  return '7';
 }
 if(d==0x07){
  return '*';
 }
  
 // 2열 읽기
 pKEYPAD_OUT_A_HIGH;
 pKEYPAD_OUT_B_LOW;
 pKEYPAD_OUT_C_HIGH;
 
 d = pKEYPAD_IN;
 d = pKEYPAD_IN;
 d = pKEYPAD_IN;
 d = pKEYPAD_IN;
 d = pKEYPAD_IN;
  
 if(d==0x0E){
  return '2';
 }
 if(d==0x0D){
  return '5';
 }
 if(d==0x0B){
  return '8';
 }
 if(d==0x07){
  return '0';
 }
  
 // 3열 읽기
 pKEYPAD_OUT_A_HIGH;
 pKEYPAD_OUT_B_HIGH;
 pKEYPAD_OUT_C_LOW;
 
 d = pKEYPAD_IN;
 d = pKEYPAD_IN;
 d = pKEYPAD_IN;
 d = pKEYPAD_IN;
 d = pKEYPAD_IN;
  
 if(d==0x0E){
  return '3';
 }
 if(d==0x0D){
  return '6';
 }
 if(d==0x0B){
  return '9';
 }
 if(d==0x07){
  return '#';
 }
  
 return ' '; //입력이 없을 경우 ' '

//----------------------------------------------------
void dsp_str(char n)           //CLCD문자열 출력함수
{
    lcd_rs=1;
    lcd_dt=n;
    lcd_en=1;
    delay_us(1);
   
    lcd_en=0;
    delay_us(50);
}
void dsp_cmd(char n)            //CLCD 명령어 함수
{
    lcd_rs=0;
    lcd_dt=n;
   
    lcd_en=1;
    delay_us(1);
   
    lcd_en=0;
    delay_us(50);
}
/*void dsp_num(char n)         //CLCD 숫자 출력 함수
{
    dsp_str(n + 0x30);
}*/
void lcd_init() //16x2line
{
    lcd_dt=0;
    lcd_rw=0;
    lcd_rs=0;
    lcd_en=0;
    delay_ms(10);
   
    dsp_cmd(0x30); //8bit mode
    delay_ms(5);
   
    dsp_cmd(0x30);
    delay_ms(1);
   
    dsp_cmd(0x30);
    delay_ms(5);
   
    dsp_cmd(0x38); //function set
   
    dsp_cmd(0x0c); //display on/off
    dsp_cmd(0x14); //cursor/display
    dsp_cmd(0x06); //entry mode
    dsp_cmd(0x01);
    delay_ms(2); //display clear
}
char str01[]={ "Door lock system" };
char str02[]={ " PW : " };
void dsp_lcd()          //처음 문구 출력
{
    dsp_cmd(0x80); //line1
    for(i=0; i<16; ++i) dsp_str(str01[i]);
    dsp_cmd(0xc0); //line2
    for(i=0; i<6; ++i) dsp_str(str02[i]);
}
void pwcheck() //비밀번호 확인 함수
{
    if((PW[0] ==INPUTVALUE[0])&&(PW[1]==INPUTVALUE[1])&&(PW[2]==INPUTVALUE[2])&&(PW[3]==INPUTVALUE[3])&&(PW[4]==INPUTVALUE[4])&&(PW[5]==INPUTVALUE[5]))   //입력값과 모든 비밀번호가 같을 경우
    {
        SERVO_OPEN();                                         //문열림
        keycnt=0;
        dsp_cmd(0xc6);
        for(i=0;i<6;i++)
        {
            INPUTVALUE[i]=' ';
            dsp_str(INPUTVALUE[i]);
         
        }
       
    }
    else
    {
        SERVO_CLS();                                             //문 닫힘
        keycnt=0;
        dsp_cmd(0xc6);
        for(i=0;i<6;i++)
        {
            INPUTVALUE[i]=' ';
            dsp_str(INPUTVALUE[i]);
        }
    }
}
void key_check()            //A, B, C 를 전부 LOW, 포트입력을 받아서 0x0f 가 아니라면, 키가 하나 이상이 눌렸을 때
{  
    if(KeypadIn()== '*')                                  // '*' 버튼이 눌렸을 경우 비밀번호 입력 시작
    {
        setcnt++;
        keycnt=0;
        dsp_cmd(0xc6);
        for(i=0;i<6;i++)
        {
            INPUTVALUE[i]= ' ';
            dsp_str(INPUTVALUE[i]);
        }
        if(setcnt>1) setcnt=0;   
    }
    if(KeypadIn()== '#')                       // '#' 버튼이 눌렸을 경우 언제든지 패스워드 체크
    {
        setcnt=0;
        pwcheck();   
    }
    pKEYPAD_OUT_A_LOW;
    pKEYPAD_OUT_B_LOW;
    pKEYPAD_OUT_C_LOW;
    value = 0;
    value = pKEYPAD_IN & 0x0f;

    if(setcnt==1)
    {
        if(value != 0x0f)       // 어떤버튼이든 눌렸을 때
        {
            PORTD=0x00;         // 버튼 상태 확인용 LED ON
       
            keyvalue = KeypadIn();
       
            if(( keyvalue != '*' ) && ( keyvalue != '#'))
            {
                keycnt++;
                if(keycnt == 1)      //두번째 부터 버튼 값을 각 value에 저장
                {
                    INPUTVALUE[0] = KeypadIn();
                    dsp_cmd(0xc6);
                    dsp_str(INPUTVALUE[0]);
                }
                if(keycnt == 2)
                {
                    INPUTVALUE[1] = KeypadIn();
                    dsp_cmd(0xc6);                       //각 값을 value에 저장 후 이전 값 출력을 '*' 으로 바꿈
                    dsp_str('*');
               
                    dsp_cmd(0xc7);
                    dsp_str(INPUTVALUE[1]);
                }      
                if(keycnt == 3)
                {
                    INPUTVALUE[2] = KeypadIn();
                    dsp_cmd(0xc7);                       //각 값을 value에 저장 후 이전 값 출력을 '*' 으로 바꿈
                    dsp_str('*');
               
                    dsp_cmd(0xc8);
                    dsp_str(INPUTVALUE[2]);
                }
                if(keycnt == 4)
                {
                    INPUTVALUE[3] = KeypadIn();
                    dsp_cmd(0xc8);                       //각 값을 value에 저장 후 이전 값 출력을 '*' 으로 바꿈
                    dsp_str('*');
               
                    dsp_cmd(0xc9);
                    dsp_str(INPUTVALUE[3]);
                }
                if(keycnt == 5)
                {
                    INPUTVALUE[4] = KeypadIn();
                    dsp_cmd(0xc9);                       //각 값을 value에 저장 후 이전 값 출력을 '*' 으로 바꿈
                    dsp_str('*');
               
                    dsp_cmd(0xca);
                    dsp_str(INPUTVALUE[4]);
                }
                if(keycnt == 6)
                {
                    INPUTVALUE[5] = KeypadIn();
                    dsp_cmd(0xca);                       //각 값을 value에 저장 후 이전 값 출력을 '*' 으로 바꿈
                    dsp_str('*');
                   
                    dsp_cmd(0xcb);
                    dsp_str(INPUTVALUE[5]);
               
                    keycnt = 0;               
                }
            }
            value=0;
        }   
        else PORTD=0xff;        // 버튼 상태 확인용 LED OFF
    }
    delay_ms(150); 
}
void main()
{
    DDRA=0xff;              //OUT
    DDRB=0xff;              //OUT
    DDRC=0x0f;           
    DDRD=0xff;              //OUT
    lcd_init();
    dsp_lcd();
    PORTD=0xff;
   
    while(1)
     {  //endless loop
        key_check();
      
     }
}  

 

 

'AVR 8BIT > 디지털도어락' 카테고리의 다른 글

소스  (1) 2011.10.20
디지털 도어락  (65) 2011.10.14
Posted by 숭덕이