Questions on ultrasonic distance finder


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    TMR1H increments when TMR1L overflows at 256. So its 4x256+23.

    Keep in mind that the timer will run at 1/4 the frequency of your main oscillator.

    edit: also, pauseus 1 will not result in a 1us pause because pauseus has a minimum pause time. have a look at pauseus in the PBP manual.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    Did you find this post helpful? Yes | No

    Default

    Do you really have to built it yourself?

    Look at the http://www.robot-electronics.co.uk/a...c_Rangers.html

    Once I had to built that ranger in 1990 but then I could not find ready made modules.

    After a few year I tested the modules of the above link and was surprised.

    First of the idea to drive the Tx US transducer by the well known MAX232 chip (it can produce fast +/- 10 volts of pulses!) and second by its simplicity and performance.

    Ioannis

  3. #3
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118


    Did you find this post helpful? Yes | No

    Default

    Well the main reason why I want to build it myself is to learn everything about it, I really like the challenge and when I go to bed just about every other day or so I sleep well because I like the feeling of having learned something new.

    Now trying to calculate and figure how I got this result on my display is probably going to keep me up for a few days because even after Kamikaze's response I am still confused. Maybe it's all about the timing differences between the speed my brain works and the speed of a PIC at 4MHz.

    Or maybe I am trying to calculate something that is uncalculatable!

  4. #4
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    OK, if you are running your PIC at 4MHz, then the timer will run at 1MHz, which means that TMR1L will increment every 1uS (when the prescale is set to 1:1).

    timing the command PAUSEUS 1 will not give you a result of 1 becuase of 2 reasons. First, PAUSEUS has a minimum pause time and 1uS is so fast that the time it takes to check the timer value comes into play.

    Try this:

    temp VAR word

    TMR1H=0 ' Reset timer value
    TMR1L=0
    T1CON.0=1 ' Start the timer
    PAUSEUS 1000 ' Pause 1000uS
    T1CON.0=0 ' Stop the timer
    temp.HighByte=TMR1H
    temp.LowByte=TMR1L
    LCDOUT $FE,$80,"TMR1 = ", dec temp

    The LCD should display somewhere around 1000 - Probably a little more because of the time it takes to start and stop the timer.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  5. #5
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118


    Did you find this post helpful? Yes | No

    Default

    you are absolutely right, 1001 is what I get.

    I am sorry if I ask again about the timing but are you able to explain this?

    if I keep the timer running, I removed the PAUSEUS and I do something like this:
    Code:
    LCDOUT $FE,$80,"H",dec TMR1H,"L",dec TMR1L,"H",dec TMR1H,"L",dec TMR1L
    The first set of H/L I get 401
    The second set I get 4604

    Why it isn't the second set taking arround 800?
    Is it because the actual calculation is based on the assembly and when it is compiling it is doing funny things?

  6. #6
    Join Date
    Nov 2005
    Location
    Perth, Australia
    Posts
    429


    Did you find this post helpful? Yes | No

    Default

    Its possible that the LCDOUT routine really is taking that long.

    But ive found that its generally best to stop the timer to take a reading, and usually best to store that reading in a variable before displaying it, or doing anything else with it.

    See what happens if you do it like this:


    temp VAR word

    TMR1H=0 ' Reset timer value
    TMR1L=0
    T1CON.0=1 ' Start the timer
    pauseus 1000
    T1CON.0=0 ' Stop the timer
    temp.HighByte=TMR1H
    temp.LowByte=TMR1L
    T1CON.0=1 ' Restart the timer
    LCDOUT $FE,$80,"TMR1 = ", dec temp
    T1CON.0=0 ' Stop the timer
    temp.HighByte=TMR1H
    temp.LowByte=TMR1L
    LCDOUT $FE,$C0,"TMR1 = ", dec temp

    The difference between the 2 results will tell you how long the first LCDOUT routine is taking.
    "I think fish is nice, but then I think that rain is wet, so who am I to judge?" - Douglas Adams

  7. #7
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118


    Did you find this post helpful? Yes | No

    Default

    ...well I am getting closer to the numbers I am expecting but...

    My values were 1001 and 4206 so it took 3205 to perform LCDOUT.

    Then I restarted the timer just before the last LCDOUT and added this to your code and got a value of 7405

    T1CON.0=0 ' Stop the timer
    temp.HighByte=TMR1H
    temp.LowByte=TMR1L
    LCDOUT $FE,$80,"TMR1 = ", dec temp

    So the second time LCDOUT executed it took 3199
    I did it a third time and found a different number again.

    So LCDOUT executed 3 times and it took less time everytime it executed.

    1st time took 3205
    2nd time took 3199
    3rd time took 3187

    I figured writting to the LCD on a blank line took 3205
    overwriting on the first line of the LCD took 3199
    and overwritting on the second line took 3187

    But I did it a fourth time (overwrote on the first line) and it took 3312
    This is the code I ended up with:
    Code:
    temp VAR word
    TMR1H=0 ' Reset timer value
    TMR1L=0
    T1CON.0=1 ' Start the timer
    pauseus 1000
    T1CON.0=0 ' Stop the timer
    temp.HighByte=TMR1H
    temp.LowByte=TMR1L
    T1CON.0=1 ' Restart the timer
    LCDOUT $FE,$80,"TMR1 = ", dec temp
    T1CON.0=0 ' Stop the timer
    temp.HighByte=TMR1H
    temp.LowByte=TMR1L
    T1CON.0=1 ' Restart the timer
    LCDOUT $FE,$C0,"TMR1 = ", dec temp
    T1CON.0=0 ' Stop the timer
    temp.HighByte=TMR1H
    temp.LowByte=TMR1L
    T1CON.0=1 ' Restart the timer
    LCDOUT $FE,$80,"TMR1 = ", dec temp
    T1CON.0=0 ' Stop the timer
    temp.HighByte=TMR1H
    temp.LowByte=TMR1L
    T1CON.0=1 ' Restart the timer
    LCDOUT $FE,$C0,"TMR1 = ", dec temp
    T1CON.0=0 ' Stop the timer
    temp.HighByte=TMR1H
    temp.LowByte=TMR1L
    LCDOUT $FE,$80,"TMR1 = ", dec temp
    This beeing said, if I start over from the begining, no matter how many times I am always getting the same results so this rules out a bad resonator or voltage noise or any othe hardware issue.

    So there is something in the process that is hapenning which is irregular and I wish I could understand what it is!

    I am really sorry to be such a pain and I appreciate your patience but I am still a little confused.

    Mike

Similar Threads

  1. Ultrasonic distance sensor with PIC16F84A
    By MrRoboto in forum mel PIC BASIC
    Replies: 3
    Last Post: - 29th June 2009, 09:01
  2. Sharp GP2D12 Range Finder???
    By Gixxer in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 13th December 2007, 09:21
  3. sensor ultrasonic 8051&PIC
    By hd_uni_pro in forum Schematics
    Replies: 1
    Last Post: - 13th September 2006, 12:58
  4. ultrasonic sensor
    By PoTeToJB in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 9th May 2006, 20:26
  5. SRF04 Range Finder 16F628 R18iXL Board
    By Spindle in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 20th June 2005, 02:08

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts