i have written a flowcode3 program to control an ultrasonic sensor SRF05 but i have some problems with using timers and some other calculations
Am starting AT LAST!!! to understand what is going on with the motion sensor2 example
so if am using an 8MHz xtal crystal and a prescaler of 1/8
and system division by four>>>how do we do that???i dont know!!can someone tell me??>>
then each instruct cycle time will equal 2 micro seconds
then assuming that speed of sound 338m/sec=.338 mm/micro seconds
.338usx2us=.676mm>>>/2>>>.338mm range
i will use tmr1 and the sensor am using after triggering it for at least 10 micro seconds
sends a high echo pulse for 35 milli seconds period then it will lower its
triggering the pulse without returning an
echo pulse so for that i will need an interrupt to see how many times
Timer1 has overflowed"counted">> because the sensor
will have lowered its echo line after more than one timer1 overflow>>>is that right??because
my timer1 period=1048560 micro seconds=1048,560 milli seconds>>but if i use a lower prescaler lets say like
1/1 then the timer1 period will equal 131070 micros seconds=131.070 milli seconds which is more than enough
"almost three times" for the sensor to send a high pulse for an echo and to lower it because it will have lowered its
echo long time before that,then i will have to stop timer1 and write a C-code to
get timer1"16-bit"value which is made of two parts TMR1H and TMR1L>>with whatever prescaleri use>>> so
i have to get these two parts the high and the low part of Timer1 and combine them together
to find the distance travelled like this Distance = ( Capture_TMR1H << 8 ) + Capture_TMR1L
then i will have a distance value measured in mm's
i will not use an interrupt timer1 overflow any way>>is that needed?? >> and i will have to combine what to get distance ????
i dont know???does this method needs an interrupt Timer1 overflow???i think if i use a timer overflow interrupt it will never be > 1???is that right???
I have included a program that i just wrote in flow code using timer1 but it still missing how to get the low and high parts
I really don’t know if this is going to help you at all (and I’m away this weekend, so don’t get upset, if I don’t answer until sunday evening CET).
I don’t know Flowcode and to be honest, I haven’t heard about it before, but I know PIC C and have used the SRF05, so maybe my I can help you.
I used the SRF05 together with a PIC33FJ128MC802 chip.For measuring distance, I use timer3. I have set up timer 3 with an interrupt and a gate. The gate feature is enabling the timer to measure, how long time the gate pin is high and fire an interrupt when it goes low. The echo pin of the SRF05 is wired to the gate pin and the time the echo pin is high is proportional with the distance (see SRF05 spec sheet). So, it is all just a matter of setting the PING pin high, wait 10usec, setting the PING pin low and in the interrupt routine, read the timer value, which is the distance.
I don’t know if your PIC has the gate pin feature. What PIC chip are you using?
My test code is here. It doesn’t do much, since it is a cut’n’paste from a larger program:
I have never used the 16f877, but a quick look at the datasheet reveals that it does not have the gated timer mode that I use with the dsPIC. Your code looks like it ought to work. I have some questions, though:
1. Won’t “if (0xff)” always be true? Also, you have an if(0) laterthat always evaluates to false.
2. You set t1con = 0x3B. 0x3B = 111011, which, according to datasheet is: TMR1ON=1 TMR1CS = 1 T1SYNC=0 T1OSCEN=1 T1CKPS=11
I notice that you have TMR1CS = 1 => External clock. Do you have an external clock in your setup? (Not the main crystal on OSC1/2, but a separate timer oscillator on T1OSI/T1OSO). If you don’t, you need to set TMR1CS=0 or else the timer doesn’t run. Also, I’m not sure that you need T1OSCEN=1 when using the interal timer, but I’m not sure on this.
thank you very much for your help JKA you wont believe how long i have been struggling with SRF05 i knew that my problem is in uisng timer1 as it should be…i am using 8mhz crystal on pins OSC1/2 so i think i have to change TMR1CS=0,i
but i didnt get what you mentioned about the if statments " Won’t “if (0xff)” always be true? Also, you have an if(0) laterthat always evaluates to false" may be i didnt understand how ultrasonic sensor works???
this is my procedure…
1. Turn Sensor Pwr Hi Delay for 100ms
2.C-code Containing
tmr1h=FCV_CAPTUREH; load pic tmr1h,tmr1l in flowcode tmr1l=FCV_CAPTUREL;
3. Output 1 to the INIT pin delay_us(10);or 20 micro seconds
In C, 0 is the same as false and everything else is true, if you use it in if and while statements. So, if(0xff) or if(255) in decimal is always true. Likewise, if(0) is always false.
The question is, what are you trying to do here:
trisc = trisc & 0xef; if (0xff) portc = (portc & 0xef) | 0x10; <-- This is always executed, becuase 0xff is always true else portc = portc & 0xef;
and here:
trisc = trisc & 0xef; if (0) portc = (portc & 0xef) | 0x10; else portc = portc & 0xef; <-- This is always executed, becuase 0 is always false
I don’t know, if it has any effect on your program, though
my inquiry is when i trigger the sensor lets say pin C2 to the SRF05 the sensor will send an echo pulse to pic pin lets say C3 the sensor will send(raise) its echo line so that an input 5v will always be sent to the pin C3 ,at thisinstance i will start a timer to measure time so i need a statement that will instruct the pic to start a timer as soon as the echo pin goes high (C5 is 5v) and to keep measuring time untill the echo pin goes low by either detecting something or after 30 milli seconds if nothing is detected so then i will have to stop the timer and get that value and do the calculations,so my question is what is the suitable instruction to start the timer and stop it is it if ECHO start timer and how to stop the timer do i use while ECHO keep looping is that right or is there another way to do it
Connect the ECHO pin of SRF05 to CCP1 (or CCP2 if you prefer that). In CCP1CON set CCP1Mode to "Capture every rising edge".
Create an interrupt routine for the CCP1 capture, a global variable called "measured" and another one called "done"
When you wish to measure the distance, do:
Initialize measured and done to 0 Reset and enable timer1 Pull the trigger pin of SRF05 high and low to send a ping. while(!done); //Loops until done is true (=not 0) //Now measured holds the time of the ping
In the interrupt routine you have: if (measured == 0) { measured = timer1 value Configure CCP1CON to "Capture every falling edge" } else { measured = timer1 value - measured. done = 1 }
Does this make sense?
What I try to say is, you setup the capture module to trigger an interrupt when the CCP1 pin goes high. This is when the SRF05 starts to measure the distance. In the interrupt routine we note the time this happens (because time has passed from the reset and enable of timer 1 and until the echo pin goes high). Then we reconfigure the capture module to trigger the interrupt when CCP1 goes low. This is when the SRF05 receives the echo. The time that the CCP1 pin was high (= the time of the echo to arrive) is the difference between these two timer1 values.
THANK you very much for your help jka ,actually pic 16f877 has two CCP modes tha can be used but i need these two pins for "DC pwm motor controlling"
thanks for your help and thank you again.i willl tell you waht happens with me later on …actually its 2 am in the morning here in jordan and am trying to get the sensor to function as it should…