Posted on 2006-05-01
Last Modified: 2013-11-12
Hi Steveiam

This is my function for reading distance...

I want to send 10 pulse burst of 40 KHZ

could you please tell me corrections please

//pIc running at 4MHZ

#define trig RB0

unsigned int read_distance(int)
      TMR1H = 0xff;                                    // prepare timer for 25uS pulse
      TMR1L = -14;
      T1CON = 0x21;                                          // 1:* prescale and running
      int i=10;                              
      TMR1IF = 0;      
      trig = 1;                                    // start trigger pulse
      while(!TMR1IF);                              // wait
      trig = 0;                                          // end trigger pulse
      TMR1ON = 0;                                          // stop timer
      TMR1H = 0;                                          // prepare timer to measure echo pulse
      TMR1L = 0;
      T1CON = 0x20;                                    // 1:* prescale but not running yet
      TMR1IF = 0;
      while(!echo && !TMR1IF);            // wait for echo pulse to start (go high)
      TMR1ON = 1;                                          // start timer to measure pulse
      while(echo && !TMR1IF);                  // wait for echo pulse to stop (go low)
      TMR1ON = 0;                                          // stop timer
      return (TMR1H<<8)+TMR1L;            // TMR1H:TMR1L contains flight time of the pulse in *** uS units
Question by:vpool
    LVL 11

    Expert Comment

    What is your problem? Do you receive some compilation error or something else is wrong?
    At my first glance I see the following:
    In the following code you initialize i with 10 then decrease it with i-- and finally check in the while statement if it is <10. I think you should check in the while statement if i>0 not if i<10

         int i=10;                        
         TMR1IF = 0;    
         trig = 1;                              // start trigger pulse
         while(!TMR1IF);                         // wait
         trig = 0;                                   // end trigger pulse
         TMR1ON = 0;                                   // stop timer
    LVL 3

    Accepted Solution

    It'll accidentally work with i<10, as it'll overflow to 255 (8 bit integer), then exit the loop on the next round.

    Also, the return type of your function is an "unsigned int".  This is an 8 bit datatype (check the CCS PIC-C compiler manual for the sizes and limits of the basic data types).  You then try to make up a 16 bit value and return that, with the line:

    long read_distance(int)
        [ ... code goes here ... ]
        return (TMR1H<<8)+TMR1L;

    You need to use a "long" for the return type, and having a type cast wouldn't hurt either:

       return ((long)TMR1H << 8) + TMR1L;

    Also.. you have read_distance(int)... specifying that the function takes an integer argument as input.  This isn't wrong and doesn't cause any errors, but that variable isn't available to the function, as you're not giving it a name in the function declaration, e.g.:

      read_distance(int myArgument)

    If you're not using the value that's passed in, then you might as well remove it.  This'll save you a few bytes of program memory, and make the code a bit faster.  Both good things, when coding for a embedded micros, like these PICs!

    If you download Microchip's free compiler (MPLAB), you can integrate the CCS PIC-C compiler with it, and then take advantage of the simulator within MPLAB.  This is really handy for stepping through your code, and watching the inputs and outputs of a simulated PIC.  Very useful if you don't have an oscilloscope, or when its difficult to monitor the real chip in action.  Stepping through with the simulator can make it much easier to see what's happening, and track down problems in code.

    Anyway... as WelkinMaze asks.. what specifically is the problem with this?  What is it doing, or not doing, as expected?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Introduction This question got me thinking... ( Why shouldn't we use Globals? This is a simple question without a simple answer.  How do you explain these concepts to a programmer w…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now