Sign In

How To Increase Operating Range With Ur Provides Firmware

akshay akshayvish23@gmail.com India

Hardware: Pic Microcontroller 16F877A Software: Mplab Ide 8.91

in these program please tell me how to increase the operating range of the us sensor

Images

Program

#include <xc.h>
#include <stdint.h>



#define US_PORT PORTA
#define  US_TRIS  TRISA


#define US_TRIG_POS  0


#define US_ERROR     -1
#define  US_NO_OBSTACLE          -2

#define THRESHOLD_DISTANCE  60  //CM



void HCSR04Init()
{
        //Set trigger port as output
   US_TRIS&=~(1<<US_TRIG_POS);

        //Setup Timer1
        T1CKPS0=1;  //Prescaller = 1:2
}

void HCSR04Trigger()
{
   //Send a 10uS pulse on trigger line
   US_PORT|=(1<<US_TRIG_POS); //high

        __delay_us(15);       //wait 15uS

   US_PORT&=~(1<<US_TRIG_POS);   //low
}
/*
 Returns the width of pulse in uS (micro seconds)
 */
int16_t GetPulseWidth(uint8_t n)
{
   uint32_t i,result;

        n++;//since echo lines starts at PORTA1 so convert 0 to 1 and so on

   //Wait for the rising edge
   for(i=0;i<600000;i++)
   {
      if(!(US_PORT & (1<<n)))
                    continue; //Line is still low, so wait
      else
                    break; //High edge detected, so break.
   }

   if(i==600000)
            return US_ERROR;  //Indicates time out

   //High Edge Found


   TMR1=0x00;  //Init counter
        TMR1ON=1;   //Start timer

        //Now wait for the falling edge
        for(i=0;i<600000;i++)
        {
           if(US_PORT & (1<<n))
           {
              if(TMR1 > 60000) break; else continue;
           }
           else
                         break;
        }



   if(i==600000)
            return US_NO_OBSTACLE;  //Indicates time out

        //Falling edge found
   //Stop Timer
   TMR1ON=0;

   result=TMR1;

   if(result > 60000)
            return US_NO_OBSTACLE;  //No obstacle
   else
            return (result * 0.4);  // since period of timer is 0.4uS
}

void Alert(uint8_t n)
{
    for(uint8_t i=0;i<n;i++)
    {
        RB1=1;//Start Vibrator

        __delay_ms(50);

        RB1=0;//Stop Vibrator
        __delay_ms(30);
    }
}

void main (void)
{
    int16_t r;

    //All PORTA pins are digital input and NOT analog
    PCFG2=1;
    PCFG1=1;

    //Vibrator motor or buzzer i/o port pin as output
    TRISBbits.TRISB1=0;
    RB1=0;//No alert at startup

    HCSR04Init();

    int d[3];

    while(1)
    {


        //loop for 3 sensors
        for(uint8_t i=0;i<3;i++)
        {
                //wait for all ECHO line to come down
                while(RA1==1);
                while(RA2==1);
                while(RA3==1);

      //Send a trigger pulse
      HCSR04Trigger();

      //Measure the width of pulse
      r=GetPulseWidth(i);


                if(r!=US_ERROR && r!=US_NO_OBSTACLE)
      {
         d[i]=(r*0.01718); //Convert to cm
      }
            }//For loop of 3 sensor

            //Alert
            if(d[1]<THRESHOLD_DISTANCE)
            {
                Alert(2);
            }
            else
            {
                if(d[2]<(THRESHOLD_DISTANCE-30))
                    Alert(3);
                else if (d[0]<(THRESHOLD_DISTANCE-30))
                    Alert(1);
            }
        __delay_ms(250);
    }//main while loop

}

Post a reply to akshay

Think you can help akshay ? Then post your thoughts that might help akshay. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.