Can you help me time a millisecond event?

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

Moderators: Benj, Mods

Post Reply
trailie
Posts: 3
Joined: Sun Apr 04, 2010 7:40 pm
Contact:

Can you help me time a millisecond event?

Post by trailie »

Hello all, this is my first time posting here and also my very first go at a PIC project of my own.

What I'm trying to do is build a chronometer to measure the velocity of a BB bullet, it should be somewhere in the region of 330 ft/s. give or take a bit, and display the result on an LCD.
The plan is to shot a BB down a short tube, looking through the tube are two pairs of photodiodes to detect the pellet, the first transmitter/reciever will start a timer and the second will stop the timer. So measuring the time taken, and knowing the distance between the sensors, it should be an easy calculation to arrive at the velocity. By rough calculations it should take about 0.1mS for the BB to pass between sensors that are 3 inches apart.

So far I have used flowcode to sucessfully get the LCD to display some menu instructions and respond to some switch inputs. The detector pairs are working too. The project is built around a 16F627 simply because I had one!

The point where I'm stuck at is how to do the timing. I don't fully understand how the different timers work but I'm guessing I could use TMR0 to do this. I also don't know how to initiate or program these timers in the flowcode program. Do I do it with an interupt? and what does the flowchart look like to do this. I did try to follow a tutorial but just could'nt figure things out on my own.

If someone could walk me through this I would appreciate it greatly.

Ralph

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

Re: Can you help me time a millisecond event?

Post by Steve »

I'm afraid you'll probably need to resort to a little C code here. The chip you are using has a 16-bit wide TMR1 resource which should do what you need.

You will need to use C code to set up the TMR1 resource with the appropriate pre/post scaling and other options. The details are in the datasheet for this. You will end up with a line of C code that needs to go into a C code icon in your flowchart with something like the following:

Code: Select all

t1con = 55;
When you want to start the timer, use the following C code:

Code: Select all

t1con.TMR1ON = 1;
And you can stop the timer by setting the TMR1ON bit of the register to zero. You can then read the value of the timer by reading the tmr1h and tmr1l values into Flowcode variables and doing th enecessary calculations. For example, if you have 2 variables "time_lo" and "time_hi" set in Flowcode, you can stop and read the timer value with this C code:

Code: Select all

//stop the timer
t1con.TMR1ON = 0;

//read the timer registers into Flowcode variables
FCV_TIME_LO = tmr1l;
FCV_TIME_HI = tmr1h;

trailie
Posts: 3
Joined: Sun Apr 04, 2010 7:40 pm
Contact:

Re: Can you help me time a millisecond event?

Post by trailie »

Mmmmmm........ ok Steve, give me a couple of days to try that out and I'll get back to you. But I suspect I'll need further instructions ! Thanks.

trailie
Posts: 3
Joined: Sun Apr 04, 2010 7:40 pm
Contact:

Re: Can you help me time a millisecond event?

Post by trailie »

Well, more than a year has passed and I'm only just returning to this project now. I have to try and pick up from what I remember last. :?

So.. the suggestion was to create two variables time_lo and time_hi, presumably for the start and stop events ?? ... but should these variables be a byte or interger when I'm defining them?

Also you say I will need C code to set up the TMR1 resource. I may need help with that , but one step at a time for now.
Thanks, Ralph

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Can you help me time a millisecond event?

Post by Spanish_dude »

The time_lo and time_hi is the timer value of the interrupt. Check Steve's post for that.

To set up TMR1 you'll need to take a look at the datasheet of your microcontroller.

Post Reply