디스플레이 > 4x3 키 매트릭스 & FND(4자리) 프로테우스 ( 코드비전 & AVRstudio)

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

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

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

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

BASIC4MCU > 디스플레이

FND | 4x3 키 매트릭스 & FND(4자리) 프로테우스 ( 코드비전 & AVRstudio)

페이지 정보

작성자 키트 작성일17-08-31 16:41 조회2,990회 댓글0건

첨부파일

본문

 

3660040649_QSdpVv1Y_25C125A625B825F1_25BE25F825C025BD.PNG3Ftype3Dw740

//=====================================================================//
#include <mega128.h>
#include <delay.h>
#include <stdio.h>
//=====================================================================//
char fnd_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //캐소드
//char fnd_table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E}; //애노드
char fnd_cnt=0,fnd_buf[4]={0};
//
interrupt [TIM0_COMP] void timer0_comp_isr(void){ // 매치 인터럽트
    PORTB=0xFF//캐소드
    //PORTB=0; //애노드
    switch(fnd_cnt){
        //캐소드
        case 0: PORTA=fnd_table[fnd_buf[0]];      PORTB=~1break;
        case 1: PORTA=fnd_table[fnd_buf[1]];      PORTB=~2break;
        case 2: PORTA=fnd_table[fnd_buf[2]]|0x80; PORTB=~4break;
        case 3: PORTA=fnd_table[fnd_buf[3]];      PORTB=~8break;
        //애노드
        //case 0: PORTA=fnd_table[fnd_buf[0]];      PORTB=1; break;
        //case 1: PORTA=fnd_table[fnd_buf[1]];      PORTB=2; break;
        //case 2: PORTA=fnd_table[fnd_buf[2]]&0x7F; PORTB=4; break;
        //case 3: PORTA=fnd_table[fnd_buf[3]];      PORTB=8; break;
    }
    if(++fnd_cnt>3)fnd_cnt=0;
}
//
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(1); // 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(1); // 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(1); // 4번째 줄 선택
  if(!(PINC&1))KeyBuf='*';
  if(!(PINC&2))KeyBuf='0';
  if(!(PINC&4))KeyBuf='#';
  DDRC=0; PORTC=0xFF// 4번째 줄 해제
  //
  return KeyBuf; // Key 없으면 0xFF 리턴
}
//=====================================================================//
void main(void){
    char key,old;
    int i;
    DDRA=0xFF; DDRB=0x0F;
    TCCR0=0x0C; OCR0=249;  TIMSK=2;  //16000000/  64/(1+249)= 1000Hz=1ms
    SREG=0x80;
    old=KeyScan(); //무한루프 들어가기 전에 키값을 저장해 두고
    while(1){
        key=KeyScan();                                    // 키를 입력받아서
        if(old!=key){                     // 저장된 값과 다르면 (새로운 키가 눌렸으면)
            if((key>='0')&&(key<='9')){                   // 키값의 범위를 체크하고
                for(i=2;i>=0;i--)fnd_buf[i+1]=fnd_buf[i]; // 버퍼를 시프트 시킨 후
                fnd_buf[0]=key&0x0F;                      // 버퍼0에 키값을 저장
            }
        old=key; // 현재 키값을 저장
        }
    }
}
//=================================================================
 
 
 
 
 
 
 
//=================================================================
#define F_CPU 16000000UL
#include <avr/io.h> 
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdio.h>
//=====================================================================//
char fnd_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //캐소드
//char fnd_table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E}; //애노드
unsigned char fnd_cnt=0,fnd_buf[4]={0};
//
ISR(TIMER0_COMP_vect){ // 매치 인터럽트
    PORTB=0xFF//캐소드
    //PORTB=0; //애노드
    switch(fnd_cnt){
        //캐소드
        case 0: PORTA=fnd_table[fnd_buf[0]];      PORTB=~1break;
        case 1: PORTA=fnd_table[fnd_buf[1]];      PORTB=~2break;
        case 2: PORTA=fnd_table[fnd_buf[2]]|0x80; PORTB=~4break;
        case 3: PORTA=fnd_table[fnd_buf[3]];      PORTB=~8break;
        //애노드
        //case 0: PORTA=fnd_table[fnd_buf[0]];      PORTB=1; break;
        //case 1: PORTA=fnd_table[fnd_buf[1]];      PORTB=2; break;
        //case 2: PORTA=fnd_table[fnd_buf[2]]&0x7F; PORTB=4; break;
        //case 3: PORTA=fnd_table[fnd_buf[3]];      PORTB=8; break;
    }
    if(++fnd_cnt>3)fnd_cnt=0;
}
//
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(1); // 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(1); // 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(1); // 4번째 줄 선택
  if(!(PINC&1))KeyBuf='*';
  if(!(PINC&2))KeyBuf='0';
  if(!(PINC&4))KeyBuf='#';
  DDRC=0; PORTC=0xFF// 4번째 줄 해제
  //
  return KeyBuf; // Key 없으면 0xFF 리턴
}
//=====================================================================//
int main(void){
    char key,old;
    int i;
    DDRA=0xFF; DDRB=0x0F;
    TCCR0=0x0C; OCR0=249;  TIMSK=2;  //16000000/  64/(1+249)= 1000Hz=1ms
    SREG=0x80;
    old=KeyScan();
    while(1){
        key=KeyScan();
        if(old!=key){
            if((key>='0')&&(key<='9')){ 
                for(i=2;i>=0;i--)fnd_buf[i+1]=fnd_buf[i];
                fnd_buf[0]=key&0x0F;
            }
        old=key;
        }
    }
}
//=================================================================

댓글 : 0

조회수 : 2,990

등록된 댓글이 없습니다.

게시물 검색

디스플레이 목록

게시물 검색


Privacy Policy
MCU BASIC ⓒ 2017
PC버전