Open changheepark-dev opened 9 months ago
0, 1로 측정 한 것
#define F_CPU 16000000UL // 16MHz의 오실레이터를 사용
#include <avr/io.h> //입출력 포트 사용을 위한 헤더 include
#include <avr/delay.h> //딜레이 사용을 위한 헤더 inlcude
#include <avr/interrupt.h> //인터럽트 사용을 위한 헤더 include
#include "lcd.h" //LCD 사용을 위한 헤더 include -> " "외부의 파일을 include
// setting ///////////////////////////////////////////////////////////////////////////////
#define TRIG 7 // PE7 (변경 불가)
#define TRIG_delay 80 // 기본 80, 연산 속도에 맞춰 변경 가능, 너무 줄일 시 측정 오류
#define interval 700 // 측정 간격 5 ~ 2700 [mm]
int tNum(unsigned int NUM) {
unsigned char Buff[4] = "0";
Buff[0] = '0'+((NUM %1000)/100);
Buff[1] = '0'+((NUM %100)/10);
Buff[2] = '0'+(NUM %10);
LCDPuts(Buff);
}
int main(void){
DDRE=0x80;
PORTE = 0x7f;
MCU_Init(); //LCD 사용을 위한 MCU 설정
LCDInit(); //LCD 사용을 위한 초기화 설정
while(1){
unsigned char check = 0;
TCCR0 = 7;
PORTE |= (1<<TRIG); //Trig=HIGH -> 거리 측정 명령 시작
_delay_us(10); //10us동안 유지
PORTE &= ~(1<<TRIG);
while (!(PINE & (1<<0))) {}
TCNT0 = 0;
while (TCNT0 < ((interval + 50) / 10.88)){
for (unsigned char a = 0 ; a < 7; a++) {
if (PINE & (1<<a)) {
if (TCNT0 > ((interval) / 10.88)) {
check |= (1 << a);
}
}
}
}
TCCR0 = 0;
for (int a = 0; a < 4; a++) {
LCDMove(0,a*4);
tNum((check >> a) & 0x01);
}
LCDMove(1,0);
tNum((check >> 4) & 0x01);
LCDMove(1,4);
tNum((check >> 5) & 0x01);
LCDMove(1,8);
tNum((check >> 6) & 0x01);
LCDMove(1,12);
tNum(check);
_delay_ms(TRIG_delay);
}
}