-3
\$\begingroup\$

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.
\$\endgroup\$
13
  • 2
    \$\begingroup\$ 4GHz clock on a PIC16? \$\endgroup\$
    – Matt Young
    Commented Mar 31, 2015 at 15:16
  • \$\begingroup\$ @MattYoung That is as impressive as the question is impossible to understand. \$\endgroup\$
    – Majenko
    Commented Mar 31, 2015 at 15:18
  • 1
    \$\begingroup\$ The language appears to be some kind of Basic... \$\endgroup\$
    – Eugene Sh.
    Commented Mar 31, 2015 at 15:22
  • 3
    \$\begingroup\$ Look up the speed of sound in meters per second. Convert it to centimeters per microsecond. Does that look like approximately 1/29? \$\endgroup\$
    – Tut
    Commented Mar 31, 2015 at 15:54
  • 2
    \$\begingroup\$ You've just discovered a "magic number" You might enjoy reading about these on stackoverflow.com . Then you can go slap the person who wrote your code. \$\endgroup\$
    – gbarry
    Commented Mar 31, 2015 at 16:29

1 Answer 1

3
\$\begingroup\$

You divide the duration by 29 because that is evidently how you convert the unspecified time period into a distance of unspecified units.

Without knowing what the time period is measured in, nor the units of the distance you expect, why 29 was chosen is anyone's guess.

While I am not familiar with the language you are using, I can kind of follow the flow of it, however that is a completely pointless exercise since without knowing what pins are connected to what and in what way we can't know what the program is supposed to be doing.

My understanding:

  • Timer is set to 1:2 prescale, so ticks at 0.5MHz (\$F_{OSC}/4/2\$).
  • Set port C3 (receiver, which is an input) HIGH - what is this supposed to achieve?!
  • Set port D7 to an output
  • Clear timer.
  • Wait 1ms (why?!)
  • Wait while C3 is low
  • Start the timer
  • Wait while C3 is high
  • Turn off the timer
  • Divide the time (in 2µs counts) by 29
  • Set port D7 high
  • Output the distance to PORTB

Now, is it me, or does that program make no sense whatsoever? At no point is the trigger being activated. You're randomly setting other port pins to input or output or high or low for no apparent reason.

\$\endgroup\$
1
  • \$\begingroup\$ the program used is " microbasic pro for pic" ,the trigger connected to portc bin 0 ,while the echo signal connected to portc bin 3 \$\endgroup\$
    – sepeee
    Commented Mar 31, 2015 at 15:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.