• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 667
  • Last Modified:

PIC16f877

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;                              
      do
      {
      TMR1IF = 0;      
      trig = 1;                                    // start trigger pulse
      while(!TMR1IF);                              // wait
      trig = 0;                                          // end trigger pulse
      TMR1ON = 0;                                          // stop timer
      i--;
      }
      while(i<10);                                          
      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
      
}
0
vpool
Asked:
vpool
1 Solution
 
WelkinMazeCommented:
Hi,
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;                        
     do
     {
     TMR1IF = 0;    
     trig = 1;                              // start trigger pulse
     while(!TMR1IF);                         // wait
     trig = 0;                                   // end trigger pulse
     TMR1ON = 0;                                   // stop timer
     i--;
     }
     while(i<10);
0
 
steveiamCommented:
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?
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now