hello im trying to test an hc-sr04 ultrasonic sensor using this code, what supposed to happen is that i send a pulse on trigger pin of the module wait for rising edge of echo pin which interrupts and sets a flag and timer1 starts counting ,i poll the pin till it becomes low then i save the timer value and stop the timer, turn value of timer to a bunch of characters and send over to serial monitor.
the serial communication works because i've tried to send characters and it worked. but when i run this code i keep getting garbage values and then my laptop crashes MULTIPLE_IRQ_REQUESTS ERROR.
code:
#define F_CPU 8000000UL #define TRIGGER 6 #define ECHO 2 #include <util/delay.h> #include <avr/interrupt.h> #include <avr/io.h> #include <stdlib.h> volatile uint16_t travel_time; volatile unsigned char count =0xff; volatile unsigned char rising_edge=0x00; volatile unsigned char wave_sent=0x00; void start_timer1(void); void send_wave(void); ISR(INT0_vect){ rising_edge=0xff; } int main(void) { MCUCSR|=(1<<JTD); MCUCSR|=(1<<JTD); DDRD|=(1<<4); PORTD&=~(1<<TRIGGER); DDRD|=(1<<TRIGGER); DDRD&=~(1<<ECHO); PORTD&=~(1<<ECHO); GICR|=(1<<INT0); MCUCR=(1<<ISC00)|(1<<ISC01); UBRRL=51; UCSRB|=(1<<TXEN); UCSRC|=(1<<UCSZ0)|(1<<UCSZ1)|(1<<URSEL); char to_monitor[8]; sei(); while(1){ if(!wave_sent){ send_wave(); wave_sent=0xff; } if(rising_edge){ start_timer1(); while(1){ if(~(PIND&(1<<ECHO))){ travel_time=TCNT1; itoa(travel_time,to_monitor,10); TCCR1B=0X00; for(count=0;count<=7;++count){ UDR=to_monitor[count]; _delay_ms(15); } break; } } rising_edge=0x00; wave_sent=0x00; } } } void start_timer1(void){ TCNT1H=0X00; TCNT1L=0X00; TCCR1A=0X00; TCCR1B=(1<<CS11); } void send_wave(void){ PORTD|=(1<<TRIGGER); _delay_us(15); PORTD&=~(1<<TRIGGER); }