Motion senosr3 example using Timer1/Pic16f877a

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

Moderators: Benj, Mods

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

Motion senosr3 example using Timer1/Pic16f877a

Post by khaled yasin »

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 don't 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 that 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 traveled like this Distance = ( Capture_TMR1H << 8 ) + Capture_TMR1L

then i will have a distance value measured in mm's

i will use an interrupt timer1 overflow any way>>is that needed?? >> and i will have to combine what to get distance ????

i don't 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 bits

of timer1????????also i don't know how to set the prescaler to 1/8 and i assume that a system division by 4 is done??
Attachments
my motion sensor program.fcf
(6 KiB) Downloaded 405 times
Last edited by khaled yasin on Mon Nov 10, 2008 9:30 pm, edited 3 times in total.

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:

Re: Motion senosr3 example using Timer1/Pic16f877a

Post by Benj »

Hello
and system division by four>>>how do we do that???i don't know!!can someone tell me??>>
This is built into the PIC architecture and cannot be disabled.
still missing how to get the low and high bits of timer1?
The low and high bits of the timer can be collected by reading the registers using a piece of C code. My programming in C article in the articles section should help here.
also i don't know how to set the prescaler to 1/8 and i assume that a system division by 4 is done?
This should be in the properties of the interrupt enable icon.

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

Re: Motion senosr3 example using Timer1/Pic16f877a

Post by khaled yasin »

Hello...
and what about using interrupt timer1 over flow????does i need it based on my calculations i don't need it

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

Re: Motion senosr3 example using Timer1/Pic16f877a

Post by khaled yasin »

cant you see my code to see what i am missing in C-codes
i used
tmr1=0;
FCV_TMR1H=tmr1h;load a high start up value into the timer

FCV_TMR1L=tmr1l;load a low start up value into the timer

FCV_TMR1=tmr1;


is these C-codes are right??

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:

Re: Motion senosr3 example using Timer1/Pic16f877a

Post by Benj »

Hello

I dont think this is correct.

Code: Select all

tmr1=0;
Instead you would probably need to use

Code: Select all

tmr1h=0;
tmr1l=0;
Then to load the flowcode variables into the timer you would have to do the following.

Code: Select all

tmr1h = FCV_TMR1H;
tmr1l = FCV_TMR1L;
To read the timer variables into flowcode use the following.

Code: Select all

FCV_TMR1H = tmr1h;
FCV_TMR1L = tmr1l;

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

Re: Motion senosr3 example using Timer1/Pic16f877a

Post by khaled yasin »

What C instruction will cause timer1 to start timing ?
Is it
tmr0h = 1;
tmr0l = 1;
or something else

and what C code will cause the timer to stop or be disabled ?

and what C instruction will turn on the timer1 with a predefined prescaler 1 or 2 or 4 or 8?
is it
t1con = 0x##; ##=some numbers

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:

Re: Motion senosr3 example using Timer1/Pic16f877a

Post by Benj »

Hello Khaled

All the answers to your questions are available in the device datasheet available from here.

http://ww1.microchip.com/downloads/en/D ... 39582b.pdf

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

Re: Motion senosr3 example using Timer1/Pic16f877a

Post by khaled yasin »

lets say i don't know how to use timers at all :roll: :roll: :roll: and i need to find another way to calculate the time for the ultrasonic sensor echo pulse transmit and receive period so what if i create a counter inside a while loop and this counter starts counting from zero to an unspecified value and this counter will start incrementing as soon as the echo pulse is gone high and will stop as soon as this echo pulse goes low again and then i will see how many times the counter has incremented and the number of these increments will equal the time but my problem is that if for example the counter counted 100 counts how can i convert this to time,,,,what do i need to know to equal the 100 increments to an equal amount of time in sec or us...,,,,,
am not sure but i think i will need the time for each instruction cycle??? is that correct???also what should i assign the variable will they be byte or integers?

User avatar
Steve
Matrix Staff
Posts: 3426
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Motion senosr3 example using Timer1/Pic16f877a

Post by Steve »

Overall, I think it would be easier to implement this with a timer. If you intend to do it in the way you suggest, you should be using your own ASM code for the timing routine because you will need to know exactly how many instructions are being executed to work out the time.

The alternative approach is to implement some crude timing routing and then calibrate the sensor yourself. I.e. take various measuments of the timer count AND measure the distance manually - and then create some kind of table so you can work out the formula.

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

Re: Motion senosr3 example using Timer1/Pic16f877a

Post by khaled yasin »

hello...
from a pin in the pic i send a trigger pulse for at least 10 microseconds to the sensor then the sensor echo pin will keep sending high signal to another pin in the pic until it receives an echo back and immediately lowers its echo that was going to the pic pin,for that i use a timer to start counting at same time when the sensor starts sending high to the pin and stops this signal when it stops sending the high signal to the pic,myy question is that should i use an if statement that says if pinX is high start timing and after it another if statement that says if pinx is low stop timer or should i use two while loops the first one says while pin x = keep looping and after it i start the timer the another while loop that says while pin x is high keep looping and after it i stop the timer,
so its a square wave, but how to tell pic when this square wave starts and when it finishes :?: :?:

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:

Re: Motion senosr3 example using Timer1/Pic16f877a

Post by Benj »

Hello Khaled

Looks like what you should do is this.

1) Switch your output high to enable the sending of the ping.
2) Start the timer interrupt running.
3) Wait in a while loop for the receiving pin to go high.
4) Collect the timer value.
5) Switch your output low to disable the sending of the ping.
6) Reset the timer values
7) Wait in a while loop for the receiving pin to go low.
8 ) Return to start...

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

Re: Motion senosr3 example using Timer1/Pic16f877a

Post by khaled yasin »

Hello...
am still struggling to get the ultrasonic sensor to get distance readings:cry: :cry: :cry:
anyway i used the motion sensor example using timer 0 you send to me but the problem is that the output "distance" shown on the LCD is always = 255 mm so what's wrong , also i might have told you this before but if you take a quick look at the program you gave me you will see that it sends a high pulse to the trigger pin the starts the timer and then an if statement to see if the there is an echo line going high and then it starts capturing the time but the problem as i can see is that how to tell the pic that the sensor echo line pic C4 has gone low"which indicates that the sound wave have returned and that now we need to collect the timer 0value and use it to calculate the distance,as i can see it just collect's the time and immediately goes out and calculate the distance..could that be my problem,
Attachments
MotionSensor2.fcf
(6 KiB) Downloaded 396 times

Post Reply