로그인 | 회원가입 | 주문 조회
[소스]초음파센서(SRF04)를 ATMega128 Kit에 연결하여 구동해보자
글쓴이 김진호 등록일 2006-12-21 조회 12,316
거리감지용 초음파 센서를 이용하여 거리를 확인하고, seven segment에 표시하기 [준비재료] - ATmega128pro kit 1EA - SRF04(초음파센서) 1EA - 박스커넥터(10핀)2*5 암/수 각1EA [재료 변형작업] - 박스 핀 배선 변경 센서쪽 ATMega128 연결선 --------- --------- ----------- * * (1,x) * * (1,2) -->(1 + 10) * * (3,x) * * (3,4) -->(3 + 3) * * (5,x) * * (5,6) -->(5 + 5) * * (7,x) * * (7,8) -->(7 + 7) * * (9,x) * * (9,10) -->(9 + 9) [ATMega128 작업포트] DDRF = 0xBF; // 센서 i/o PORTF DDRC = 0xFF; // seven segment 위치조정 PORTC DDRA = 0xFF; // seven segment data PORTA [프로그램 내역] //ICC-AVR application builder : 2006-10-16 오후 6:44:14 // Target : M128 // Crystal: 16.000Mhz #include #include #define SersorPort PORTF #define SersorPinInput PINF #define SersorOutput PORTE #define ON 1 #define OFF 0 unsigned int dist=0, // 거리 dist_buff=0, // 거리임시저장 buff_cnt=0, // 버퍼카운트 waveflag=0, // 신호플래그 clrcnt=0; // 클리어카운트 unsigned int count=0; const unsigned char segment_data[10] = {63, 6,91,79,102,109,125,39,127,103}; unsigned int display_num[4]={0,0,0,0}; // Seven segment 4자리 숫자 출력 버퍼 // 트리거 신호 보냄 void Sensor_TRIGGER_ON(void) { SersorPort = SersorPort | 0x10; } // 트리거 신호 보내지 않음 void Sensor_TRIGGER_OFF(void) { SersorPort = SersorPort & 0xEF; } //에코 신호 char Sensor_ECHO(void) { return ((SersorPinInput & 0x40) >> 6); } //트리거 신호 char Sensor_TRIGGER(void) { return ( (SersorPinInput & 0x10) >> 4 ); } void port_init(void) { PORTA = 0x00; DDRA = 0xFF; PORTB = 0x00; DDRB = 0xFF; PORTC = 0x00; //m103 output only DDRC = 0xFF; PORTD = 0x00; DDRD = 0xFF; PORTE = 0x00; DDRE = 0xFF; //초기화 확인후, 정의 할 것 PORTF = 0x00; DDRF = 0xBF; PORTG = 0x00; DDRG = 0x03; } //TIMER0 initialize - prescale:32 // WGM: Normal // desired value: 32uSec // actual value: 32.000uSec (0.0%) void timer0_init(void) { TCCR0 = 0x00; //stop ASSR = 0x00; //set async mode TCNT0 = 0xF0; //set count OCR0 = 0x10; TCCR0 = 0x03; //start timer /* TCNT0 = 0xF0; //set count OCR0 = 0x10; TCCR0 = 0x03; //start timer */} #pragma interrupt_handler timer0_ovf_isr:17 void timer0_ovf_isr(void) { TCNT0 = 0xf0; //reload counter value count++; //32usec마다 카운트 올림 if(count>1500) //카운트 1500마다 신호 한번씩 보내고 클리어 카운트 올림 { Sensor_TRIGGER_ON(); count=0; clrcnt++; } if(clrcnt>50) //클리어 카운트가 100이면 거리버퍼 클리어. 클리어카운트 초기화. { dist_buff=0; clrcnt=0; } if(waveflag==OFF) //전단계까지 신호 받은 기억 없는데 { if(Sensor_ECHO()==ON) //좀전에 받았으면 신호 받았다고 기억함. 버퍼 카운트에 지금까지 카운트 값을 넣어줌 { waveflag=ON; buff_cnt=count; } Sensor_TRIGGER_OFF(); //트리거 신호 끔 } else if(waveflag==ON) { //신호 받았으면 if(Sensor_ECHO()==OFF) { //에코신호 끝났으면 dist = ((count-buff_cnt)/2); //거리는 카운트 - 버퍼카운트 나누기 2 dist_buff = dist; //거리버퍼는 거리 waveflag = OFF; //플래그 내리고 count = 0; //카운트와 클리어카운트 초기화 clrcnt = 0; } } } //UART0 initialize // desired baud rate: 9600 // actual: baud rate:9615 (0.2%) // char size: 8 bit // parity: Disabled void uart0_init(void) { UCSR0B = 0x00; //disable while setting baud rate UCSR0A = 0x00; UCSR0C = 0x06; UBRR0L = 0x67; //set baud rate lo UBRR0H = 0x00; //set baud rate hi UCSR0B = 0xF8; } #pragma interrupt_handler uart0_rx_isr:19 void uart0_rx_isr(void) { //uart has received a character in UDR } #pragma interrupt_handler uart0_udre_isr:20 void uart0_udre_isr(void) { //character transferred to shift register so UDR is now empty } #pragma interrupt_handler uart0_tx_isr:21 void uart0_tx_isr(void) { //character has been transmitted } //call this routine to initialize all peripherals void init_devices(void) { //stop errant interrupts until set up CLI(); //disable all interrupts XDIV = 0x00; //xtal divider XMCRA = 0x00; //external memory port_init(); timer0_init(); uart0_init(); MCUCR = 0x00; EICRA = 0x00; //extended ext ints EICRB = 0x00; //extended ext ints EIMSK = 0x00; TIMSK = 0x01; //timer interrupt sources ETIMSK = 0x00; //extended timer interrupt sources SEI(); //re-enable interrupts //all peripherals are now initialized } void delay(int msec) { int i,j; for(i=0; i11)SersorOutput=0x00; } } [참고자료] 초음파센서 : http://info.hobbyengineering.com/specs/devantech-srf04-tech.pdf [이 게시물은 administrator님에 의해 2011-12-06 04:26:09 통합정보검색에서 복사 됨]
다음글 [소스] 스테핑 라인트레이서 (ATMega16+초음파센서+스테핑모터)
이전글 [소스] 지그비 zig-100 통신 메뉴얼 및 소스