# PIC math help

Status
Not open for further replies.

#### electronicsfreak

##### Member
well, here's my problem.

I've been revising/reworking an old project (might recognize it as the PIC Chronograph from another post)

it is now built with a larger microcontroller, and has a more sensitive 2 gate system.

in the programming, after the first photogate has been broken, the microcontroller will begin increasing a counter/storing #'s in a set of 3 registers.

these three registers will store the # in a decimal format. the first register for the 1ns place, the second for the 10ns place and the third for the 100ns place. (meaning it can only store a number at or below 999)

when the second photogate is broken, the counter will stop increasing this #.

the # stored in these three registers will be the amount of time there is between the breaking of the two gates.

the distance between the two gates is 1ft. it has been set up like this to help make the programming a little bit easier.

I need to somehow divide 1 by that # stored in the three registers so that I can have the device display the speed of whatever broke the two gates, but I have no idea how to do that using the PIC's instruction set.

I am programming in PICassembly using MLAB IDE

any ideas?

attached is the circuit diagram

#### electronicsfreak

##### Member
(sorry, the image was invalid on the first message, so here it is again)

#### Attachments

• PICChrono2.jpg
512.8 KB · Views: 160

#### dougy83

##### Well-Known Member
I need to somehow divide 1 by that # stored in the three registers so that I can have the device display the speed of whatever broke the two gates, but I have no idea how to do that using the PIC's instruction set.

An idea: convert the 3 digit registers into a 16-bit integer then use a prewritten division routine. You'll have to scale '1' (1 foot I assume) to, say, 1000 to give you feet/us or else you'll keep getting 0 as a result.

A division routine is available here: dontronics.com (division.htm)

Last edited:

#### ericgibbs

##### Well-Known Member
hi,
Its a basic elasped time measurement.
Use PORTB.0 as the start/stop Interrupt control for a an internal reg counter.

You can use one of the internal counters to give the required 'time' integer interval for the reg summer.

To simplify even further use a 3.768Mhz xtal [ or multiple] in place of the 4Mhz xtal.

Last edited:

#### dougy83

##### Well-Known Member
Use PORTB.0 as the start/stop Interrupt control for a an internal reg counter.
Or use a 16-bit hardware capture for better accuracy & less overhead.

To simplify even further use a 3.768Mhz xtal [ or multiple] in place of the 4Mhz xtal.
He's dealing with metric time measures, so 4MHz is good (gives 1 us resolution)... I think there is a mistake in his above post; he must be measuring us, not ns.

Its a basic elasped time measurement.
Yes, followed by a basic conversion to speed through scaled reciprocation.

No, I don't.

#### ericgibbs

##### Well-Known Member
Im puzzled why you are unable to follow my simple explanation.

My reply was intended for the OP's benefit, not so that you could go off in a childish Rant!.

The method I outlined is a well used way of doing exactly what the OP wants.

#### dougy83

##### Well-Known Member
Im puzzled why you are unable to follow my simple explanation.
I'm also puzzled. The way I read his post, he's after a speed, not time measurement.

My reply was intended for the OP's benefit, not so that you could go off in a childish Rant!.
Maybe. Well, at least my rant was concise.

The method I outlined is a well used way of doing exactly what the OP wants.
Again, I thought he was after a speed measurement.

#### ericgibbs

##### Well-Known Member
I'm also puzzled. The way I read his post, he's after a speed, not time measurement.

Maybe. Well, at least my rant was concise.

Again, I thought he was after a speed measurement.

Hi dougy,
If he chooses the time 'int' correctly for his internal timers, he can finish up with a count proportional/equal to speed.

I am always happy to discuss with a fellow engineer the ways to solve a problem and I hope it will be that way with us.

But I dont need lessons in how to suck eggs.

Last edited:

#### electronicsfreak

##### Member
Last night, my partner for this project came up with an algorithm that will take care of what we need.

Say x represents the number stored in the register.
In the program we then have x added to itself a Q number of times until the result =y, or a number that is equal to/slightly greater than one. At the moment that x added Q # if times together is greater than one, your speed is given as Q.

It is only accurate in very high velocities and doesn't give decimal numbers (Q is a whole number) but we just make a second loop that goes into the tenth place or hundreth place. I'm not quite sure how to explain it here, but i've tried it through my partners explanationm and it works.

Thankyou guys for your help, but I can't modify the circuitry any more as I already have a PCB ready and waiting for the components to arrive in the mail.

If anyone else has any suggestions I'd be glad to take them, but unless there is something that will work much better than our algorithm posted here, the programming will use what my partner came up with.

For dougy83 and ericgibbs:

I appreciate the help, but niether of the ways posted here I will be able to use as the circuit above here is final.

Also, before this thread becomes an argument over which way is better, please reconsider the reasoning on why you are posting. Such as: is it really necessary to argue with someone I've never met before over the internet?
Please don't take offence to this, I'm just saying what needs to be said before stones are hurled at each other.

#### pcbheaven.com

##### New Member
Some links on PIC math functions should help? Did i get this correct?

PIC routines

#### dougy83

##### Well-Known Member
If he chooses the time 'int' correctly for his internal timers, he can finish up with a count proportional/equal to speed.
I have no idea what you mean. I'm sure it's obvious to you, nevertheless.

But I dont need lessons in how to suck eggs.
I would never propose to provide tutition on such things, it's certainly not my area of expertise.

I'm just saying what needs to be said before stones are hurled at each other.
Hardly. Sorry if I upset you two.

#### Mr RB

##### Well-Known Member
Last night, my partner for this project came up with an algorithm that will take care of what we need.

Say x represents the number stored in the register.
In the program we then have x added to itself a Q number of times until the result =y, or a number that is equal to/slightly greater than one. At the moment that x added Q # if times together is greater than one, your speed is given as Q.
...

Yep "inversion by addition" is how I would have done it. By simply choosing the correct value for Y (the destination) you scale the result, so you can use different values of Y to give a result in FPS or m/s etc.

Once you have the first decimal digit you get the remainder, multiply the remainder by 10 and just do it again to get the second decimal digit. Etc for more digits.

It's not that fast as a calculation but it is pretty "bulletproof" hehe get the pun?

#### dougy83

##### Well-Known Member
If he chooses the time 'int' correctly for his internal timers, he can finish up with a count proportional/equal to speed.
Could you please explain how this is done. Thanks.

#### Mr RB

##### Well-Known Member
Could you please explain how this is done. Thanks.

Ok, let's say the detectors are 1 foot apart, the bullet goes through and you get an elapsed time period of 253uS. To convert from a period to a speed (like feet per second) you add X to itself until you reach Y (which is 1 second or 1000000uS).

So;
Add X to Accumulator until it reaches 1000000;
(it is added 3952 times, reaches 999856)
the remainder = 144
so feet per second = 3952 and (144/253)

Now if you need the result in metres per second, you just use a different Y value, so instead of Y=1000000 you use a new Y=304878.
(feet/metres = 1/0.304878)

So use the same hardware, and same calc code but just with the new destination Y value;
Add X to Accumulator until it reaches 304878;
(it is added 1205 times, reaches 304865)
the remainder = 13
so metres per second = 1205 and (13/253)

Obviously the remainder can be processed with a similar "addition division" to the main calc.

I hope that answered your question? You see that the same hardware and excactly the same code can give the answer in FPS or M/S, and the code is very compact and brutally simple, just a loop and addition, although it is slow time-wise (which doesn't matter at all for this app).

#### ericgibbs

##### Well-Known Member
hi Roman,
Thanks for answering, you beat me too it.
As you have clearly explained, its a standard solution for this problem.

dougy.
Reading some of your other thread posts, I suspect the real reason you are asking me is so that you can play some kind of childish tit for tat game.
I am old enough and wise enough to know when someone is trying to jerk me.

So I will pass on answering your question at this time, I have better things to do.

I will however, put the request for a reply on my job list, right after the one where I have to tidy my sock drawer, then I'll get back to you.

#### Mr RB

##### Well-Known Member
hi Roman,
Thanks for answering, you beat me too it.
...

Yeah sorry about that Eric, I typed about half of it in thinking he was asking about my last post, then realised he was specifically asking you something not asking me. So I thought "Bugger! It's almost answered now" and just typed a bit more and hit send.

#### ericgibbs

##### Well-Known Member
Yeah sorry about that Eric, I typed about half of it in thinking he was asking about my last post, then realised he was specifically asking you something not asking me. So I thought "Bugger! It's almost answered now" and just typed a bit more and hit send.

Hi Roman,
Thats not a problem.

I was going to post a section of one of my old Z80 programs showing the technique.
I was in marine surveying equipment design/manufacture for years, which I believe you were.?
As you know surveying covers many aspects of measurement with respect to time.

#### Mr RB

##### Well-Known Member
No I've never worked in marine surveying, that sounds like fun! I do have some innovative underwater ROV designs but given my current busy schedule and my level of lazyness they will probably never get built.

Last edited:

#### atferrari

##### Well-Known Member
What equipment?

I was in marine surveying equipment design/manufacture for years, which I believe you were.?
As you know surveying covers many aspects of measurement with respect to time.

Out of curiosity, can you tell more about, Eric?

I went at sea for long time and in my early years I was involved in hydrographic surveys mostly in the Paraná river or Antarctica.

I still recall an echosounder used to profile the bottom. It was a quite expensieve piece of equipment!. But nothing replaces the fun of crossing the river alone, with the Zodiac full ahead after leaving the geologist team on the other margin Nobody disputed the use of the boat to me (because it had the bad habit to deinflate, increasing the tendency of the bow to enter the water abruptly at any moment)

After quiting vessels I had the chance to be in close contact with some of the "piercing waves" ferries crossing from Buenos Aires to Montevideo sailing at about 55 knots. LOT of electronics!

#### ericgibbs

##### Well-Known Member
Out of curiosity, can you tell more about, Eric?
I went at sea for long time and in my early years I was involved in hydrographic surveys mostly in the Paraná river or Antarctica.

hi,
I ran my own small company for the last 25 years of my working life. E.S.P.(Electronics)Ltd. based in Portsmouth.
We designed and manufactured Laser Range finders, Laser profiler and bearing units, Data loggers, Depth digitisers, Digital Echosounders, Tide gauges, Chart Annotators and a host of other interfaces as well as surveying and plotting software.
In the UK we sold our products directly to clients and overseas thru agents

Client base was mainly harbour and river authorities, dredging and surveying companies.

Is this the information that you are asking for.?

Status
Not open for further replies.

Replies
1
Views
1K
Replies
2
Views
2K
Replies
4
Views
4K
Replies
10
Views
2K
Replies
6
Views
2K