BASIC4MCU | 질문게시판 | atmega128 4x4 키패드 도어락, 초음파 센서 결합질문!
페이지 정보
작성자 운치킨 작성일2022-11-14 18:58 조회1,142회 댓글2건본문
초음파 센서가 읽어들이는 신호 딜레이 때문에 FND가 엄청 빠르게 깜빡깜빡 거리는 거 같습니다. FND를 이용한 키패드 도어락과 초음파 센서로 LED를 켜지게 하는 두 개의 동작을 별개로 동작시키고 싶은데 FND 데이터를 읽는 부분을 인터럽트 내부에 넣으려고 하는데 손을 못대겠습니다 ㅜㅜ 소스좀 도와주세요
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdio.h>
#define Trigger_ON PORTF=0x01 //초음파 트리거 PF0
#define Trigger_OFF PORTF=~0x01
#define Echo (PINF&0x04) //초음파 에코 PF2
void hc_sr04_init(void);
unsigned int getEcho(void);
volatile char fnd_num[10] = { 0xc0 , 0xf9 , 0xa4 , 0xb0 , 0x99 , 0x92 , 0x82 , 0xd8 , 0x80 , 0x90 };
char fnd_digit[4] = { 0x10 , 0x20 , 0x40 , 0x80};
char KeySet(void);
void ledBuf1 (void);
void ledBuf2 (void);
//void pwReset (char *pWBuf);
void ledBuf_open(void);
int main(void)
{
DDRA = 0xff;
DDRE = 0xff;
DDRB = 0x01;
DDRD = 0xff;
TCCR3A = 0x82;
TCCR3B = 0x1b;
ICR3 = 4999; //TOP
OCR3A = 375; //0도
int range=0;
hc_sr04_init();
PORTA= 0xff;
char key;
char pWBuf[4] = { 'n' , 'n' , 'n' , 'n' }; // 초기화는 'n'로 됨
char pW[4] = { fnd_num[0], fnd_num[0], fnd_num[0], fnd_num[0]}; // 초기 비밀번호 0000
int pWC = 0;
int pWCD = 0;
while (1)
{
if ( pWC == 1) // 비밀번호 변경
{
do
{
key = KeySet();
ledBuf1();
}while( key == 'n');
if( key == '#' )
{
for( unsigned int i = 0 ; i < 4 ; i++)
{
pW[i] = 'n';
_delay_ms(100);
}
while( pW[3] == 'n')
{
do
{
key = KeySet();
PORTA = 0xfb;
PORTD = fnd_digit[pWCD];
_delay_ms(5);
}while ( key == 'n');
if(pW[0] == 'n')
{
pWCD = 1;
pW[0] = key;
_delay_ms(300);
key = 'n';
}
else if(pW[1] == 'n')
{
pWCD = 2;
pW[1] = key;
_delay_ms(300);
key = 'n';
}
else if(pW[2] == 'n')
{
pWCD = 3;
pW[2] = key;
_delay_ms(300);
key = 'n';
}
else
{
pWCD = 0;
pW[3] = key;
_delay_ms(300);
key = 'n';
}
}
ledBuf2();
pWC = 0;
}
else
{
ledBuf2();
pWC = 0;
}
}
else
{
key = KeySet();
if(key == '*') // 입력한 비밀번호 초기화
{
for( unsigned int i = 0 ; i < 4 ; i++)
{
pWBuf[i] = 0xff;
}
}
else if ( key == 'A') // 비밀번호 일치 여부 확인
{
if( pWBuf[0] != pW[0])
{
for( unsigned int i = 0 ; i < 4 ; i++)
{
pWBuf[i] = 0xff;
}
key = 'n';
}
else if( pWBuf[1] != pW[1])
{
for( unsigned int i = 0 ; i < 4 ; i++)
{
pWBuf[i] = 0xff;
}
key = 'n';
}
else if( pWBuf[2] != pW[2])
{
for( unsigned int i = 0 ; i < 4 ; i++)
{
pWBuf[i] = 0xff;
}
key = 'n';
}
else if( pWBuf[3] != pW[3])
{
for( unsigned int i = 0 ; i < 4 ; i++)
{
pWBuf[i] = 0xff;
}
key = 'n';
}
else
{
OCR3A=125;
_delay_ms(1000);
pWC = 1;
ledBuf_open();
for( unsigned int i = 0 ; i < 4 ; i++)
{
pWBuf[i] = 0xff;
}
continue;
}
}
else if ( key != 'n') // 비밀번호 할당
{
if(pWBuf[0] == 'n')
{
pWBuf[0] = key;
_delay_ms(200);
continue;
}
if(pWBuf[1] == 'n')
{
pWBuf[1] = key;
_delay_ms(200);
continue;
}
if(pWBuf[2] == 'n')
{
pWBuf[2] = key;
_delay_ms(200);
continue;
}
if(pWBuf[3] == 'n')
{
pWBuf[3] = key;
_delay_ms(200);
continue;
}
for( unsigned int i = 0 ; i < 4 ; i++)
{
pWBuf[i] = 'n';
}
key = 'n';
}
else // 키를 뗀 상태에서 처리
{
if( pWBuf[3] != 'n')
{
PORTD = 0x80;
PORTA = pWBuf[3];
_delay_ms(3);
}
if( pWBuf[2] != 'n')
{
PORTD = 0x40;
PORTA = pWBuf[2];
_delay_ms(3);
}
if( pWBuf[1] != 'n')
{
PORTD = 0x20;
PORTA = pWBuf[1];
_delay_ms(3);
}
if( pWBuf[0] != 'n')
{
PORTD = 0x10;
PORTA = pWBuf[0];
_delay_ms(3);
}
}
}
range=getEcho();
if (range < 20) PORTB = 0x01;
else PORTB = 0x00;
}
}
char KeySet(void)
{
char KeyBuff = 'n'; // 함수 내에서 사용될 버퍼 선언, 초기화 ( else KeyBuff = 'n' )
DDRC = 0x00; PORTC=0xff; // PC 초기화
DDRC = 0x01; PORTC &= ~0x01;
_delay_us(5);
if(!(PINC&0x10)) KeyBuff = fnd_num[1];
if(!(PINC&0x20)) KeyBuff = fnd_num[2];
if(!(PINC&0x40)) KeyBuff = fnd_num[3];
if(!(PINC&0x80)) KeyBuff = 'A';
DDRC=0x00; PORTC = 0xff; // 1 2 3 A 행 검사 끝
DDRC = 0x02; PORTC &= ~0x02;
_delay_us(5);
if(!(PINC&0x10)) KeyBuff = fnd_num[4];
if(!(PINC&0x20)) KeyBuff = fnd_num[5];
if(!(PINC&0x40)) KeyBuff = fnd_num[6];
if(!(PINC&0x80)) KeyBuff = 'B';
DDRC = 0x00; PORTC = 0xff; // 4 5 6 B 행 검사 끝
DDRC = 0x04; PORTC &= ~0x04;
_delay_us(5);
if(!(PINC&0x10)) KeyBuff = fnd_num[7];
if(!(PINC&0x20)) KeyBuff = fnd_num[8];
if(!(PINC&0x40)) KeyBuff = fnd_num[9];
if(!(PINC&0x80)) KeyBuff = 'C';
DDRC = 0x00; PORTC = 0xff; // 7 8 9 C 행 검사 끝
DDRC = 0x08; PORTC &= ~0x08;
_delay_us(5);
if(!(PINC&0x10)) KeyBuff = '*';
if(!(PINC&0x20)) KeyBuff = fnd_num[0];
if(!(PINC&0x40)) KeyBuff = '#';
if(!(PINC&0x80)) KeyBuff = 'D';
DDRC = 0x00; PORTC = 0xff; // * 0 # D 행 검사 끝
return KeyBuff;
}
void ledBuf1 (void)
{
DDRA = 0xff;
DDRE = 0xff;
PORTA = 0xfb;
PORTD = 0x10;
_delay_ms(5);
PORTA = 0xfb;
PORTD = 0x20;
_delay_ms(5);
PORTA = 0xfb;
PORTD = 0x40;
_delay_ms(5);
PORTA = 0xfb;
PORTD = 0x80;
_delay_ms(5);
}
void ledBuf2 (void)
{
DDRA = 0xff;
DDRE = 0xff;
OCR3A = 375;
_delay_ms(1000);
for( unsigned int i = 0 ; i < 2 ; i++)
{
PORTA = 0xfb;
PORTD = 0x10;
_delay_ms(500);
PORTA = 0xfb;
PORTD = 0x20;
_delay_ms(500);
PORTA = 0xfb;
PORTD = 0x40;
_delay_ms(500);
PORTA = 0xfb;
PORTD = 0x80;
_delay_ms(500);
}
PORTA = 0xff;
PORTD = 0xff;
}
void ledBuf_open(void)
{
DDRA = 0xff;
DDRE = 0xf0;
for( unsigned int i = 0 ; i < 2 ; i++)
{
for( unsigned int i = 0 ; i < 50 ; i++)
{
PORTA = 0xc0;
PORTD = 0x10;
_delay_ms(5);
PORTA = 0x8c;
PORTD = 0x20;
_delay_ms(5);
PORTA = 0x86;
PORTD = 0x40;
_delay_ms(5);
PORTA = 0xc8;
PORTD = 0x80;
_delay_ms(5);
}
PORTD = 0x00;
_delay_ms(1000);
}
}
/*void pwReset (char *pWBuf)
{
for( unsigned int i = 0 ; i < 4 ; i++)
{
pWBuf[i] = 'n';
}
}*/
unsigned int getEcho(void)
{
Trigger_ON;
_delay_us(1);
Trigger_OFF; // 10uS
while(Echo==0x00);
TCCR1B=(1<<CS11);
TCNT1=0x00;
while(Echo!=0x00);
TCCR1B=(1<<WGM12);
return (TCNT1/58); //cm로 변환 1cm은 58
}
void hc_sr04_init(void)
{
DDRF=0x03; // PF0 ouput Trigger, PF2 input Echo
TCCR1B=(1<<WGM12); // Set timer up in CTC mode
TCCR0 = 0x1F; //prscalar 128
OCR0 = 243;
}
댓글 2
조회수 1,142master님의 댓글
master 작성일운치킨님의 댓글
운치킨
넵 감사합니다! 디버깅 해보겠습니다 ㅜㅜ