디스플레이 > 4x3 키 매트릭스 & CLCD(8비트) 프로테우스 (AVRstudio) - LOCK 추가

인기검색어 > 아두이노 센서 ATMEGA128

최신글 질문게시판 동영상강좌 가입하기

▼ BASIC4MCU 후원업체 신제품 정보 ▼

▲ BASIC4MCU 후원업체 신제품 정보 ▲

BASIC4MCU > 디스플레이

LCD | 4x3 키 매트릭스 & CLCD(8비트) 프로테우스 (AVRstudio) - LOCK 추가

페이지 정보

작성자 키트 작성일17-08-31 17:41 조회762회 댓글0건

첨부파일

본문

 

3660040649_0zjL9Pu3_25C125A625B825F1_25BE25F825C025BD.PNG3Ftype3Dw740
3660040649_Z6vCEgm7_25C125A625B825F1_25BE25F825C025BD.PNG3Ftype3Dw740

//=====================================================================//
#define F_CPU 16000000UL // 16 MHz
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <string.h>
//=====================================================================//
#define lcd_RS_ON  PORTB|=0x01
#define lcd_RS_OFF PORTB&=~0x01
#define lcd_E_ON   PORTB|=0x04
#define lcd_E_OFF  PORTB&=~0x04
#define lcd_Out    PORTA
//
char str1[]= " basic4mcu.com  ";
char str2[]= " Key Matrix  ";
char str3[]= " door lock test ";
//
void lcdData(char d){ lcd_RS_ON; lcd_Out=d; lcd_E_ON; _delay_us(1); lcd_E_OFF; _delay_us(50); }
void lcdCmd(char c){ lcd_RS_OFF; lcd_Out=c; lcd_E_ON; _delay_us(1); lcd_E_OFF; _delay_ms(2); }
//
void lcd_gotoxy(char x, char y){
    if     (y==0)lcdCmd(0x80+x);
    else if(y==1)lcdCmd(0xC0+x);
    else if(y==2)lcdCmd(0x94+x);
    else if(y==3)lcdCmd(0xD4+x);
}
//
void lcd_puts(char *str){ while(*str)lcdData(*str++); }
//
void lcd_init(void){
    DDRA=0xFF;    DDRB|=0x07;   _delay_ms(30);
    lcdCmd(0x38); lcdCmd(0x38); lcdCmd(0x38);
    lcdCmd(0x0E); lcdCmd(0x06); lcdCmd(0x01); _delay_ms(1); 
}
//
char KeyScan(void){
  char KeyBuf=0xFF;  // 키 값이 들어갈 버퍼, 초기값 0xFF
  DDRC=0; PORTC=0xFF;  // 포트 초기값, 입력핀 내부풀업저항 사용
  //
  DDRC=0x08; PORTC&=~0x08; _delay_us(1); // 1번째 줄 선택
  if(!(PINC&1))KeyBuf='1';
  if(!(PINC&2))KeyBuf='2';
  if(!(PINC&4))KeyBuf='3';
  DDRC=0; PORTC=0xFF// 1번째 줄 해제
  //
  DDRC=0x10; PORTC&=~0x10; _delay_us(5); // 2번째 줄 선택
  if(!(PINC&1))KeyBuf='4';
  if(!(PINC&2))KeyBuf='5';
  if(!(PINC&4))KeyBuf='6';
  DDRC=0; PORTC=0xFF// 2번째 줄 해제
  //
  DDRC=0x20; PORTC&=~0x20; _delay_us(5); // 3번째 줄 선택
  if(!(PINC&1))KeyBuf='7';
  if(!(PINC&2))KeyBuf='8';
  if(!(PINC&4))KeyBuf='9';
  DDRC=0; PORTC=0xFF// 3번째 줄 해제
  //
  DDRC=0x40; PORTC&=~0x40; _delay_us(5); // 4번째 줄 선택
  if(!(PINC&1))KeyBuf='*';
  if(!(PINC&2))KeyBuf='0';
  if(!(PINC&4))KeyBuf='#';
  DDRC=0; PORTC=0xFF// 4번째 줄 해제
  //
  return KeyBuf; // Key 없으면 0xFF 리턴
}
//=====================================================================//
void beep_S(){ TCCR0=0x9B; _delay_ms( 100); TCCR0=0; _delay_ms( 100); }
void beep_L(){ TCCR0=0x9B; _delay_ms(1000); TCCR0=0; }
//=====================================================================//
#define    Normal_mode 0
#define    RePass_mode 1
unsigned char mode=Normal_mode;
//
char passWD[]="1234";
char keyBuf[]="    ";
char dispBuf[]="    ";
char buf[30],key,old_key=0xFF;
//
int main(void){
    int i;
    DDRB=0x30;       // buzzer & servo
    lcd_init();
    lcd_gotoxy(0,0); lcd_puts(str1);
    lcd_gotoxy(0,1); lcd_puts(str2); _delay_ms(1000);
    lcdCmd(1); //clear
    lcd_gotoxy(0,0); lcd_puts(str3);
    OCR0=124;
    TCCR1A=0x82; TCCR1B=0x1A; OCR1A=3000; ICR1=39999;
    while(1){
        key=KeyScan();
        if(old_key!=key){ old_key=key;
            if((key>='0')&&(key<='9')){ beep_S(); 
                for(i=1;i<=3;i++)keyBuf[i-1]=keyBuf[i]; // 버퍼를 시프트 시킨 후
                keyBuf[3]=key;                          // 버퍼0에 키값을 저장
                //
                for(i=1;i<=3;i++)dispBuf[i-1]=dispBuf[i]; // 버퍼를 시프트 시킨 후
                dispBuf[3]='*';
            }
            if(key=='*'){ // enter
                if(mode==Normal_mode){ beep_S(); // 비밀번호 입력
                    if(!memcmp(passWD,keyBuf,4)){ // 비밀번호가 맞으면
                        lcd_gotoxy(0,1); lcd_puts("door open!!"); 
                        OCR1A=1200
                        beep_S(); beep_S(); _delay_ms(500); 
                        beep_S(); beep_S(); _delay_ms(500); 
                        OCR1A=3000
                    }
                    else// 비밀번호 틀리면
                        lcd_gotoxy(0,1); lcd_puts("error!      ");
                        beep_L(); _delay_ms(1000); 
                    }
                }
                else// pass word 변경 
                    for(i=0;i<4;i++){ passWD[i]=keyBuf[i]; }           // 버퍼 clear
                    mode=Normal_mode;
                }
                for(i=0;i<4;i++){ keyBuf[i]=dispBuf[i]=' '; }           // 버퍼 clear
            }
            if(key=='#'){ beep_S(); // mode 변경
                if(mode==Normal_mode)mode=RePass_mode; else mode=Normal_mode; // mode 변경
                for(i=0;i<4;i++){ keyBuf[i]=dispBuf[i]=' '; }                 // 버퍼 clear
            }
            //
            if(mode==Normal_mode)sprintf(buf,"%s        ",dispBuf);
            else                 sprintf(buf,"%s        ",keyBuf);
            lcd_gotoxy(0,1); lcd_puts(buf); 
        }
        //
        _delay_ms(100);
    }
}
//=================================================================

댓글 : 0

조회수 : 762

등록된 댓글이 없습니다.

게시물 검색

디스플레이 목록

게시물 검색


Privacy Policy
MCU BASIC ⓒ 2017
PC버전