View Single Post
  #5   Spotlight this post!  
Unread 20-01-2008, 00:11
dcbrown dcbrown is offline
Registered User
AKA: Bud
no team
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Hollis,NH
Posts: 236
dcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud of
Re: SRF04 Ultrasonic Rangefinder Unresponsive

how about something like this. It should fire a ~20us drive pulse and then listen on the echo line. The echo line should be asserted shortly after the sonar is fired and remain high until the first echo is detected -or- no echo and the line will drop in 36ms.

Code:
void ping()
{

unsigned int count;
char usec;
#define drive_pin rc_dig_in01
#define echo_pin  rc_dig_in02

    //~20uS pulse out
    drive_pin = 1;
    for (usec=20; --usec != 0; )
    {
	Nop();
	Nop();
	Nop();
	Nop();
	Nop();
    }
    drive_pin = 0;

    //echo pulse should start shortly... timeout in ~20ms
    count = 35500;
    while(echo_pin == 0)
    {
	count++;
	if (count == 0)
	{
	    printf( "whoa, where'd the pulse go?\n");
	    return;
	}
     }

     // ok, count the pulse width, count*2 is approx. uSec.
    count = 0;
    while (echo_pin != 1)
    {
	Nop(); Nop(); Nop(); Nop();
	count++;
            if (count == 0) break;
    }
    printf( "count = %d\n", count );
    return;
}