CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Ultrasonic Distance measuring

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
taher



Joined: 12 Mar 2012
Posts: 4

View user's profile Send private message

Ultrasonic Distance measuring
PostPosted: Mon Mar 12, 2012 7:07 pm     Reply with quote

Hi everyone,
I am using Ping Ultrasonic sensor and trying to measure the distance of obstacle and I searched for that in the forum before I post that.
So I hope someone can help me.
(sorry for my english not that well)
This is my code:
Code:

#include <16F877.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD

#use delay(clock=40000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#include <LCD.C>

#define trig pin_B1   

int16 front_mont,mesure;
int valid=0;

#int_CCP1
void  CCP1_isr(void)
{
   front_mont=CCP_1;
   valid=1;
}

void main()
{
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
   setup_timer_2(T2_DISABLED,0,1);
   setup_ccp1(CCP_CAPTURE_RE);
   lcd_init();
   enable_interrupts(INT_CCP1);
   enable_interrupts(GLOBAL);

   // TODO: USER CODE!!
   
   
   while(true)
      {
         output_high(trig);                         // ping the sonar
         delay_us(5);                            // sending 5us pulse
         output_low(trig);
         if(valid)
            {
               mesure=front_mont*0.000272;//0.000272=2*10^-7*8*170
               valid=0;
            }
         else
            {
               set_timer1(0);
            }
           
         printf(lcd_putc,"\fDistance=%Lu",mesure);
      }

}
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

Re: Ultrasonic Distance measuring
PostPosted: Mon Mar 12, 2012 7:21 pm     Reply with quote

taher wrote:
Hi everyone,
I am using Ping Ultrasonic sensor and trying to measure the distance of obstacle and I searched for that in the forum before I post that.


Try again.

Use "ultrasonic range"

and you're going to find LOTS of results.

-ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Mon Mar 12, 2012 8:42 pm     Reply with quote

One thing to keep in mind is that most ultrasonic ranging is going to return the closest object (even if that was not what you wanted) - it will see the first echo. That is why I hate digital depth sounders - fish, kelp, thermocline all give false readings of "too shallow". That may not be an issue in your case, but be aware of the problem if it applies.

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
drh



Joined: 12 Jul 2004
Posts: 192
Location: Hemet, California USA

View user's profile Send private message

PostPosted: Tue Mar 13, 2012 8:55 am     Reply with quote

Another "minor" error. You have specified 40Mhz in the #use delay statement.
This chip has a maximum speed of 20Mhz. Did you intend to use 4Mhz?
_________________
David
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Mar 13, 2012 9:30 am     Reply with quote

Quote:

that most ultrasonic ranging is going to return the closest object


is SO true!!

REAL range finder systems use log amps to process multiple return signals of decreasing amplitude pulses and then adds analog differentiation to discriminate within a specified time window to show MULTIPLE returns
not just the closest .

MOST importantly - such designs DO NOT use anything so utterly primitive as a PING sensor either Very Happy Very Happy Smile Smile

i am hearing echoes of a very VERY familiar school project leaking out in this post
taher



Joined: 12 Mar 2012
Posts: 4

View user's profile Send private message

PostPosted: Tue Mar 13, 2012 6:37 pm     Reply with quote

thanks for reply
but my ultrasonic sensor send pulse of 40khz that's why i used
"#use delay=40000000"
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue Mar 13, 2012 6:46 pm     Reply with quote

The PIC processor doesn't care what frequency the ultrasonics run at. That #use delay tells the compiler how fast the PIC is running, ie. what the PIC clock speed is.
_________________
The search for better is endless. Instead simply find very good and get the job done.
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Tue Mar 13, 2012 8:01 pm     Reply with quote

taher wrote:
thanks for reply
but my ultrasonic sensor send pulse of 40khz that's why i used
"#use delay=40000000"


Even if that was an issue, 40khz is NOT the same as 40mhz (which is what you specified for the clock). As SherpaDoug points out though, they are not related. It is possible over a limited range to only accept echos within a window along the lines of what asmboy indicated. That is actually a feature that they have started adding to some of the high end camera lenses - you can limit the autofocus range so it can lock faster instead of trying to hunt through the whole range (usually going in the wrong direction first Evil or Very Mad )
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group