Why does this code divide the time over 29? This code is for connecting ultrasound with PIC. I'm working with a 4 MHz clock and the pic type is P16F877A .
program HCSR04
dim duration_us as word
dim distance1 as integer
dim length_cm as string[5]
main:
TRISC.0= 0
TRISC.3=1
TRISD.7=0 ' B8 to B2 for LCD, B1=trig, B0=echo
TRISB=0x00
T1CON = 0x10
run:
duration_us = 0
distance1 = 0
TMR1L = 0 ' Clear the TMR1L value
TMR1H = 0
PORTC.0 = 1 'initialize the trigger
delay_us(100)
PORTC.0=0
T1CON.0 =1 ' Enable TMR1
while PORTC.3= 0 ' Waiting for the echo
wend
T1CON.0 =1 ' Enable TMR1
while PORTC.3= 1 ' While echo received, let the TMR1 count
wend
T1CON.0 =0 ' disnable TMR1
duration_us = TMR1L+ (TMR1H<<8 )' Retain TMR1 value = ON pulse
distance1 = (duration_us)/29
PORTD.7=1
PORTB=distance1
delay_ms(1000)
goto run
end.