






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Steven Theriault
TA: Uriel Rodriguez Jason Plew Instructor: A. A. Arroyo
December 10, 2002
The sonar works by sending out a sound pulse at a frequency above the human hearing
range. When the sound hits an object, the pulse is sent back to the sonar where is can be
heard. The sonar measures the time it took for the sound to return to the sonar and can be
used to calculate the distance to the object.
The SRF04 has four connections to use the sonar. Two of them are power and ground.
There is an input line for initiating a sonar ping, and an output line to receive a pulse.
The width of the pulse is determined length of time it takes for the sound to return to the
sonar.
To initiate a sonar ping, bring the input line high for 10μs and then back low. This will
send a sonic burst out (see figure 2). After 100μs the output will go high and will remain
high for 100μs to 18ms. If an echo is not detected, the sonar will time out after 18ms and
the output line will go low. To initiate another pulse, the controller must wait 10ms.
Figure 2: SRF04 Timing Diagram
Software was written in assembly language (Appendix A) for the Atmel ATmega163 to
test the SRF04. The software prints to the screen the number of cycles it takes to pulse
on the output line of the sonar. These cycles have been calculated into seconds and are
shown in Figure 3. The graph has imposed over the sample points a linear regression
line.
Distance vs. Delay
0
200
400
600
800
1000
1200
1400
1600
1800
0 20 40 60 80 100 Distance (inches)
Delay (μs)
Delay (μs) Linear (Delay (μs))
Figure 3: Distance vs. Delay
As can be seen by the graph above, the sonar is very linear. The sonar has a resolution of
about 2.5 inches and a useful range of 3 inches to 7 feet. After 7 feet the output is not
steady, and an accurate reading cannot be obtained.
sbi UCSRB,TXEN ;enable UART transmit
;init T ldi temp,0b10000010 ;prescaler clk/ out TCCR1B,temp ;enable input noise filter ;falling edge input capture
mainloop: sbi PORTB,0 ;initiate sonar ping
ldi temp,20 ;wait 10us wait10us: dec temp brpl wait10us
ldi temp,0b00100100 ;clear overflow flag 1 out TIFR,temp ;clear IC flag
in capturel,TCNT1L ;store timer1 value in captureh,TCNT1H
cbi PORTB,0 ;start sonar ping
chk_ic_flg: in temp,TIFR sbrs temp,ICF1 ;wait for IC flag rjmp chk_ic_flg
in temp,ICR1l ;load capture registers in temp1,ICR1h
cp temp,capturel ;test original time with new capture time cpc temp1,captureh
brsh captSub ;if new>original branch
;$FFFF - original time + new time ldi temp2,$ff ;use temp3:temp2 for accumulator ldi temp3,$ff
sub temp2,capturel ;$FFFF - original time sbc temp3,captureh
add temp2,temp ; + new time adc temp3,temp
mov temp,temp2 ;move acculmulator back to temp,temp1 to print via UART mov temp1,temp
jmp printResults
captSub: sub temp,capturel ;subtract 16bit time sbc temp1,captureh
printResults: push temp1 ;push capture time for UART call sendHEX
push temp call sendHEX call sendEOL
ldi temp,$4F ;wait 10ms wait10ms: ldi temp1,$FF wait10msa: dec temp brne wait10msa dec temp brne wait10ms
jmp mainloop
;Method to send hex number to UART ;gets number from stack ;stack is emptied after routine call sendHEX: ;UART TX most significant nibble pop temp2 ;save return addr from stack pop temp pop temp ; pop off stack to temp reg push temp ;push temp back on stack
swap temp ;make most sig nibble the least sig nibble
;Method to send EOL to UART ; ; sendEOL: ldi temp,$a ;send Line Feed
waitTX2: sbis UCSRA,UDRE ;wait until tx reg is empty jmp waitTX
out UDR,temp ;send data
ldi temp,$d ;send Carriage Return
waitTX3: sbis UCSRA,UDRE ;wait until tx reg is empty jmp waitTX
out UDR,temp ;send data
ret ;return from subroutine