Help Programming Ranging Sensor

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Help Programming Ranging Sensor

Post by Ondra »

Good day all. I am working with a ranging sensor unit. I would like to use it but don't know where to begin. Here is the instruction on using the unit. What I don't know is how to program for this "The time between INIT going high and the Echo (ECHO) output going high is proportional to the distance of the target from the transducer."

Here is the instructions. Can any one help. Thanks in advance.

There are two basic modes of operation for the 6500 Series Sonar Ranging Modules: Single-echo mode and multiple-echo mode. The application of power (Vcc), the application of the initiate (INIT) input, and the resulting transmit output, and the use of the Blanking Inhibit (BINH) input are basically the same for either mode of operation. After applying power (Vcc) a minimum of 5 milliseconds must elapse before the INIT signal can be taken high. During this time, all internal circuitry is reset and the internal oscillator stabilizes. When INIT is raised to a high level, drive to the transducer (XDCR) output occurs. Sixteen pulses at 49.4 kHz with an amplitude of 0 volts to 400 volts peak to peak will excite the transducer as transmission occurs. At the end of the 16 transmitted pulses, a 200 VDC bias remains on the transducer (as recommended) for optimum receiving operation.
In order to eliminate ringing of the transducer from being detected as a return signal, the Receive (REC) input of the ranging control IC is inhibited by internal blanking for 2.38 milliseconds after the initiate signal. If a reduced blanking time is desired, then the BINH input can be taken high to end the blanking of the Receive input any time prior to internal blanking. This may be desirable to detect objects closer than 1.33 feet (corresponding to 2.38 milliseconds) and may be done of transducer damping is sufficient so that ringing is not detected as a return signal.
In the single-echo mode of operation (Figure 1), all that must be done next is to wait for the return of the transmitted signal, traveling at approximately 0.9 milliseconds per foot out and back. The returning signal is amplified and appears as a high logic level echo output. The time between INIT going high and the Echo (ECHO) output going high is proportional to the distance of the target from the transducer. If desired, the cycle can now be repeated by returning INIT to a low logic level and then taking it high when the next transmission is desired.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Hello Ondra

Sounds fairly straightforward.

I would do something along these lines.

First follow the initialisation procedure by keeping the INIT input low for 5 - 6 ms.

Then simply output a 1 on the INIT pin and enable the timer0 interrupt. You will want a fairly fast interrupt interval as these intervals will be your steps of sensitivity. Increment an integer variable every time the interrupt occurs. If the ECHO pin goes high then stop counting, disable timer interrupts and do the calculation to work out the distance.

Then you can print out the distance on an LCD etc.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Post by Ondra »

Thanks ben. I am using Timer0 interrupt to run my time_clock. Is there a way to create another timer? Say timer1.

Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Hello Ondra

Yes you can use the timer1 but you will have to use C code to configure it and to retreive the count value. Which PICmicro are you using. I will try to dig you out the code you will need.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Post by Ondra »

Thanks ben. I am using the 18F258 PIC.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Hello Ondra

You will need the following piece of code to initialise the timer1 and start it counting.

Code: Select all

t1con = 0x3B; //Enable timer1 with 1:8 prescaler
tmr1 = 0x00;  //Initialise the timer value to 0
The 1:8 prescalar value is equivalent to 1/32 of the crystal frequency.

Then to retreive the timer1 count you can do the following.

Create a INT variable in Flowcode called t1_count

Then create a C code icon containing the following code.

Code: Select all

t1con = t1con & 0xFE; //Stop Timer1 Counting
FCV_T1_COUNT = tmr1l; //Store LSB
FCV_T1_COUNT = FCV_T1_COUNT + (tmr1h << 8); //Store MSB

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Post by Ondra »

Thanks Ben. I used a C icon and inserted the code:

t1con = 0x3B; //Enable timer1 with 1:8 prescaler
tmr1 = 0x00; //Initialise the timer value to 0

when I tried to compile the program I got back this response:-

C:\Flocode Projects\elink10_24_07.c(2085:2): error: unknown identifier 'tmr1'
C:\Flocode Projects\elink10_24_07.c(2085:2): error: invalid operand 'tmr1'
C:\Flocode Projects\elink10_24_07.c(2085:7): error: failed to generate expression

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Hello Ondra

Because the timer1 is a 16 bit timer you will have to set the two 8 bit registers individually.

eg

Code: Select all

tmr1h = 0x00;
tmr1l = 0x00;

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Post by Ondra »

Ok just to make sure I have this right.
I would create 2 macros the first with the following code:
1. turn sensor Pwr Hi
2. wait (delay) 10mills
3. INT on
4. C Icon containing the following:-
t1con = 0x3B; //Enable timer1 with 1:8 prescaler
tmr1h = 0x00;
tmr1l = 0x00;

The second macro would contain the following:-
1. C Icon containg the following:-
t1con = t1con & 0xFE; //Stop Timer1 Counting
FCV_T1_COUNT = tmr1l; //Store LSB
FCV_T1_COUNT = FCV_T1_COUNT + (tmr1h << 8); //Store MSB
2. INT low
3. Sensor Pwr off


I was thinking I would use an Interrupt connected to the echo pin to call the second macro? Am I even close.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Hello Ondra

Yes i think you just about have it there.

1. Turn Sensor Pwr Hi
2. Wait Delay (10ms)
3. Output a 1 to the INIT pin
4. C Icon containg

Code: Select all

tmr1h = 0x00;
tmr1l = 0x00;
t1con = 0x3B; Start timer counting with 1 : 8 prescalar
5. Wait for ECHO pin to go high
6. C Icon containg

Code: Select all

t1con = t1con & 0xFE; //Stop Timer1 Counting
FCV_T1_COUNT = tmr1l; //Store LSB
FCV_T1_COUNT = FCV_T1_COUNT + (tmr1h << 8); //Store MSB 
7. Output a 0 to the INIT pin
8. Sensor power off.

The result will be stored in Flowcode variable t1_count.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

If say you are using a 10MHz crystal and the value in t1_count is say 314.

The value stored in the t1_count variable will be equivalent to:

10,000,000 / 32 = 312,500

1 / 312,500 = 3.2us

314 * 3.2us = 1.0048ms

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Post by Ondra »

Hi Ben thanks for all you help, I'm still at it.
the flow of the program works fine. I have setup the system so that it prints out the data through the RS232 port. I keep getting a 0 value. I can hear the sensor clicking, I tested that all the circuits are all switching correctly but again no data. I don't know if or how this is effecting the program but when I compile it this is what I get: -
any Ideas.
Note. I am using a clock speed of 18432000MHz.

Warning unreferenced functions removed:
FCM_Inter_Counter in: C:\Flocode Projects\elink10_30_07c.c
FCD_RS2320_GetDefines in: C:\Flocode Projects\elink10_30_07c.c
FCD_RS2320_ReceiveRS232Char in: C:\Flocode Projects\elink10_30_07c.c
RS232_Dummy_Function in: C:\Flocode Projects\elink10_30_07c.c

Building CASM file
Serious Warning: Possible sw stack corruption, function 'delay_ms' called by more than one asynchronous thread (main/Task, interrupt, interrupt low)
Serious Warning: Possible sw stack corruption, function 'delay_s' called by more than one asynchronous thread (main/Task, interrupt, interrupt low)

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Hello Ondra

If you send me your program then I will have a look and see if there is an easy fix.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Post by Ondra »

Hi Benj. You were helping with this project. I was pulled off of it to work on something else. I am back on it and would like to know if you can help me complete it? Getting the C code part working is where I need help. I am using a PIC 18F258 with a 1843200Hz crystal. Could correct the C code so that it works with the setup that I have. Also what would I change in the C code when I change the crystal to 19660800Hz? I'll email you the code that I've done so far. Thanks for your help thus far.

Ondra

khaled yasin
Posts: 43
Joined: Sat Sep 13, 2008 10:36 pm
Contact:

Re: Help Programming Ranging Sensor

Post by khaled yasin »

hello again...
still cant get anything from my SRF05 sensor :cry: :cry: :cry: :cry:
i did as mentioned above step by step but still nothing is working :cry: :cry:
this is what i did using pic16f877 and 8 mhz crystal

1. Turn Sensor Pwr Hi
Delay for 100ms

2.C-code Containing

tmr1h=FCV_CAPTUREH;
tmr1l=FCV_CAPTUREL;

3. Output 1 to the INIT pin
delay_us(10);or 20 micro seconds

4. C Icon contain g,initialize timer1 to zero

tmr1h = 0x00;
tmr1l = 0x00;

5. Wait for ECHO pin to go high

6. t1con = 0x3B; Start timer counting with 1 : 8 prescalar

7.Keep counting while ECHO pin is high (LOOP)

8. ECHO pin becomes LOW>>>>>C Icon containg

t1con = 0x30; //Stop Timer1 Counting
FCV_CAPTUREH = tmr1h;get tmr1h value
FCV_CAPTUREL = tmr1l;get tmr1l value

9.Distance = ( ( CaptureH << 8 ) + CaptureL )/58, calculate distance in cm >>(distance is an integer)

10. Output a 0 to the INIT pin

11.delay 100 Milli seconds

12.Repeat again
Timer period= 1048560 micro seconds = almost 1 second so there is no need for tmr1 overflow
i tried many times but still don't know what is missing :idea: :idea: :idea: :idea:
Attachments
SRF05 sensor.fcf
(4 KiB) Downloaded 216 times

Post Reply