Society of Robots - Robot Forum

Software => Software => Topic started by: hoheiho on February 27, 2011, 11:07:57 AM

Title: SRF04 sensor input using PORT
Post by: hoheiho on February 27, 2011, 11:07:57 AM
i am using PIC18F4550 with SRF04 sensor. I connect the Trigger pulse (output from pin) and Echo pulse (input to pin) with PIC pin 28(RD5) and 29(RD6). I need to supply a short 10uS pulse to the trigger input to start the ranging. The SRF04 will send out an 8 cycle burst of ultrasound at 40khz and raise its echo line high. It then listens for an echo, and as soon as it detects one it lowers the echo line again. The echo line is therefore a pulse whose width is proportional to the distance to the object. Now i am using
TRISD=0b01000000;
/** Trigger pulse***/
PORTDbits.RD5 = 1;
Delay1KTCYx(30);   //delay for 20us
PORTDbits.RD5 = 0;
/**Echo pulse***/
PORTDbits.RD6 = 1;

What should i do next? How can i do the coding to measure the Echo pulse and put it back to PIC? I am using 1MHz internal clock for hardware PWM. Do i need to define another clock for the sensor?If yes, how many do i need? 1 for Trigger and 1 for Echo? Can i do it like If Echo pulse has 200us, send it back to PIC, PIC do somethings. How to write the code to let PIC know what pulse did Echo measure?

Thank you very much...Hope someone can give me a hand coz i stuck in this part for a long time.
Title: Re: SRF04 sensor input using PORT
Post by: hopslink on February 27, 2011, 05:33:58 PM
It happens that someone asked the same question here just the other day. See this post (http://www.societyofrobots.com/robotforum/index.php?topic=13304.0). The code shown is for an AVR micro but the code logic should be easy to follow.

A hardware solution would be better. The most complete hardware solution for the PIC is probably to use the Capture mode of the Capture/Compare/PWM module and the associated pin to read the Echo pulse.
 
Title: Re: SRF04 sensor input using PORT
Post by: hoheiho on March 01, 2011, 11:46:37 AM
Thats very useful link :), Thanks! But here is a part i am not quite understand. Do you comparing 1 and 0 with the Echo pulse? using AND OR gate to work that out? Because i am working with C18 so not quite sure about the code here. And how it count up?Every 10us will add 1 in the counter? What  i have done so far in C18 :
/** Trigger pulse***/
PORTDbits.RD5 = 1;
Delay1KTCYx(30);   //delay for 20us
PORTDbits.RD5 = 0;
/**Echo pulse***/
while(!PORTDbits.RD6); // wait for echo goes high
/**measure echo pulse**/
Open Timer0(Timer_INT_OFF &
                  T0_8BIT &
                  T0_SOURCE_INT &
                  T0_PS_1_1);
WriteTimer0(0);
PORTDbits.RD6 = 1
ReadTimer???

After look through the data sheet and C18 compiler i still dun get how to do write timer and read timer. Should i use them to find out the Echo pulse input?

P.S i am using 1MHz Fosc
Thanks!!!!
Title: Re: SRF04 sensor input using PORT
Post by: hopslink on March 01, 2011, 04:34:05 PM
Quote
Do you comparing 1 and 0 with the Echo pulse? using AND OR gate to work that out? Because i am working with C18 so not quite sure about the code here. And how it count up?Every 10us will add 1 in the counter?

Yes to all your questions. The code posted was based round the GCC AVR compiler and those are commonly used conventions for pin manipulation (not a huge fan myself as for me it does not read easily...)

The code posted there had some errors, which I had highlighted with comments to help the original poster out. So the code was not correct and the comments were not particularly useful to you - for that I apologise. For your use I have corrected the errors and ported to C18 and updated comments below.

Code: [Select]
// wait for start of Echo pulse on PORTD,2
while (PORTDbits.RD2 == 0)

// measure the length of Echo pulse on PORTD,2
while (PORTDbits.RD2 != 0)

    Delay10TCYx(1);
    count++;          // time the Echo pulse by counting 10 instruction cycle 'ticks'
   
    // C18 doesn't appear to offer microsecond delays in the libraries....
        // tune the delay according to your Oscillator frequency and level of accuracy you want
    // count should be an unsigned int or unsigned long type - depending on your required accuracy
}

Apologies if there are any errors - I've just been on a C18 crash course... ;)
Title: Re: SRF04 sensor input using PORT
Post by: daisordan on March 02, 2011, 06:13:31 AM
i don't think there is "count ++" function in C18
but i am still new to it :)
Title: Re: SRF04 sensor input using PORT
Post by: hoheiho on March 02, 2011, 06:16:46 AM
i look through the C18 compiler and there is no "count ++" function. Will it be any other places?
Title: Re: SRF04 sensor input using PORT
Post by: hopslink on March 02, 2011, 07:27:44 AM
count++;

is C shorthand for

count = count + 1;

++ should be available in all c compilers. If not use the alternative above.

count is not a function it is a variable which you must declare. Hence the comment describing which variable types are suitable.

Title: Re: SRF04 sensor input using PORT
Post by: waltr on March 02, 2011, 08:09:23 AM
hoheilo,
 Here are two links, one is a good PIC tutorial, the second is an on-line C Reference:

http://www.gooligum.com.au/tutorials.html (http://www.gooligum.com.au/tutorials.html)
http://publications.gbdirect.co.uk/c_book/ (http://publications.gbdirect.co.uk/c_book/)

Do start at the beginning of the tutorial so that you learn how PICs work. The code in the lessons can be run in the MPLAB Simulator so you do not need to buy the PIC chips in the lessons.

In addition to the on-line C book this following book is highly recommended and is written by to creators of the C language. It is really the only C reference you'll ever need:
http://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628 (http://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628)
Title: Re: SRF04 sensor input using PORT
Post by: daisordan on March 04, 2011, 04:08:27 PM
thats helpful link :D