Timer Circuit

otm

Member
Hi, Its been a long time since I last used a PICAXE chip, and some of the products have changed a little. Hence, this post.

To add a little background.....

I dont know how many people have heard of the Bloodhound SSC. Its a new project to create a car capable of going over 1000MPH. Now these people are keen to get year 9 students involved in engineering, and my old School has taken the challenge. As part of their CDT lessons they are given a block of foam that they can shape aerodynamically.

This is the fun bit.....

in the back is placed a rocket motor.............

Their cars will be guided by a peice of wire so as they dont go off course.

My challenge is to create a timer system using light gates to time the rockets through a 10m distance (at a resolution of 10 milliseconds or greater). This time can then be used to create a speed in m/s, and thus MPH and display it on a AXE033.

The light gates will simply be an LDR and a laser diode spaced around 30cm apart.

My main question is:

a few years ago I would have bought just a 28X and be done with it. but you cant buy them anymore.. and the new 28x1 and 28x2 dont come with external resonators... I know I can add external resonators to these chips, but is it worth it?

What are the benefits of the 28X1 over the 28X2?
and if an external resonator is useful, what size would be best suited 4Mhz, 8Mhz or 16Mhz?

I know this post is long, sorry, you have finally made it to the end...... congratulations!!!

Thank You,

Owen.
 

SilentScreamer

Senior Member
The only advantage (to my knowledge) of a 28X1 over a 28X2 is price, that and some of the syntax has changed but its easy to get used to.

95% of the time the internal is fine, it is however less accurate than an external resonator. If you need one depends on how accurately you need to time the rocket.
 

otm

Member
SilentScreamer,

Thank you for your reply.

These rockets are going to be moving around 60MPH (dependent on how aerodynamic the foam blocks are). I was thinkin about using a resolution of 10milliseconds so the output is in the format (for example) 02:01 so that equates to 2 seconds and 10 millisecond the more accurate the better..

I would like the resolution to be higher but I dont know how realistic it would be!! (my initial instinct is that the highest fesable external resonator is best)

another question is how is it best to archeive the timer function in BASIC?

I've got a while to work it out, I just need a starting point!!!


Cheers,

Owen.
 

hippy

Technical Support
Staff member
The new 28x1 and 28x2 dont come with external resonators... I know I can add external resonators to these chips, but is it worth it?

It depends on what accuracy you want in timing and speed of operation. 28X2's can go up to 32MHz, 28X2-3V up to 64MHz using a resonator. It's also a relatively small price to pay.


What are the benefits of the 28X1 over the 28X2?

Usually the question would be the other way round. The HINTSETUP of the X2's could prove very useful.


and if an external resonator is useful, what size would be best suited 4Mhz, 8Mhz or 16Mhz?

For the 28X1's a 16MHz resonator is the fastest it supports, for 28X2's 8MHz gives 32MHz operation, for 28X2-3V 16MHz gives 64MHz operation.
 

SilentScreamer

Senior Member
Increasing the frequency causes commands such as pause to increase proportionally, i.e. overclocking a 28X1 to 16Mhz (originally 4) causes a "pause 1000" command to actually pause for 250ms.

You can use pulsein with additional circuitry, have a search or eclectic might do it for you:p.

What is the longest time that you will need to measure?

At 26.8 meters per second (60mph) they will travel between them in less than a second, therefore you can overclock the PICAXE to 16Mhz (X1) or 32 Mhz (X2) and have a resolution of 0.25ms, but if the rocket takes longer than 1.6 seconds between the gates it will have problems.

@Hippy - Can't an 28X2 5V do 40Mhz? (I know you get invalid baud rates)
 

otm

Member
Hello SilentScreamer and Hippy,

The total time to do 10m is a difficult one to work out because some will inevitably be slower than others, by how much I dont know.. I suppose one easy way is to use not just two light gates in a 10m straight, but 5 say, each spaced 2m apart.... this then also allows the pic to average the time (i.e. the time taken to go through gates 1-2, + time taken for 2-3, + time taken for 3-4, + time taken through 4-5 all divided by 4 gives an average time for the 10m) there is only 1 problem i can see with this and that is the time taken so save a variable will affect the timing... is that a true assumption?

This means that there is no way its going to take over 1.6 seconds to go through each sets of gates even at 5MPH it does 2.2m/s (unless my calcs are wrong!)

so if I bought a 28x2 and added a 16Mhz external resonator, the actual operating speed of the pic is 32Mhz giving me a resolution of 0.25ms.

Thank You again,

Owen.
 

SilentScreamer

Senior Member
This means that there is no way its going to take over 1.6 seconds to go through each sets of gates even at 5MPH it does 2.2m/s (unless my calcs are wrong!)
You can, but it can't easily be done while keeping a 0.25ms resolution.

Pulsein counts the length that a input pin is high in milliseconds. The maximum value is 65535 (a 16 bit number). If you overclock the PICAXE the resolution improves but the maximum duration decreases (if the speed of the PICAXE/resonator is doubled the resolution doubles to 0.5 milliseconds but the maximum length halves to 32.7675 seconds).

A little table:

Code:
28X1

PICAXE SPEED		MAX DURATION		RESOLTION
4Mhz (default)		65.535 seconds		1 millisecond
8Mhz			23.7675 seconds		0.5 millisecond
16Mhz			16.38375 seconds	0.25 millisecond

28X2

PICAXE SPEED		MAX DURATION		RESOLTION
8Mhz (default)		65.535 seconds		1 millisecond
16Mhz			23.7675 seconds		0.5 millisecond
32Mhz			16.38375 seconds	0.25 millisecond
64Mhz	(3V only)	8.191875 seconds	0.125 millisecond
P.S. I realised a mistake in my previous post while writing this, the rocket would have just over 16 seconds at 32Mhz with a resolution of 0.25milliseconds.
 
Last edited:

otm

Member
Hello everyone,

Womai, I have read your post, and it makes very intresting reading... a Chronograph was the word I've been trying to get out of my head onto the page!!!

If I understand correctly. A 28x2 with a 16Mhz resonator, using it PWM output can archieve a resolution os 0.2microseconds - that is the counter_clk attached to the And gate.

To Arm the system bring DFF_CLK_N low and then high again, and CTR_CLR high and then low again. (These are attached to the output of another PIC which is different to one producing the PWM clock)

If I use a 40x2 I can read the 2 byte results on one chip....

this bit confuses me:

The counter consists of four daisy-chained 4-bit counters (2 per 74HC393 chip, so that's just 2 chips, CTR1 and CTR2) and thus can count up to 65535; after that it will wrap

around to zero, so e.g. with a 5 MHz PWM signal you can measure intervals up to 13.1 msecs with a resolution of 0.2 usecs.
this means at full tilt the counter can only count for 13.1 milliseconds, so after 13.1milliseconds its going to reset to 0 and starts again? what happens if i need a longer interval (or is it a case of place the second sensor into a position where i dont need a longer time interval eg 15-20cm away from the from gate 1)

the last bit gets me to:

As an improvement, you could connect the output of the second DFF to another input on the Picaxe. The moment this line goes high indicates that the measurement is complete and the Picaxe can read back the counter value.
can you ellaborate please?

Is it then possible to place 3 of these chronograph devices within a short distance of each other and connect the outputs of each one to another PIC which then can average out the 3 readings and display the output?

Does the setup above mean that for each gate pair, I would need a 28x2 pic, a 40x2 pic to read the counter data, and another pic to control the device arming?




SilentScreamer, what you are saying is that the rocket could only take a maximum of 16.3 seconds to run the whole 10m section of the run? This doesn't factor into Womai circuit as mentioned above, or does it?





Right now we are moving into my second issue, which is that I am not as good as the people on this board when it comes to BASIC or infact most coding!! I'm not bad, but not that good either....


How would you go about setting up a PWM output? what Duty cycle would your system be best running off of?

How is it best to read and interpret the incoming bytes?

The resst I should be capable of doing..... (but you never know!!)

Cheers,

Owen
 

womai

Senior Member
>To Arm the system bring DFF_CLK_N low and then high again, and CTR_CLR
>high and then low again. (These are attached to the output of another PIC
>which is different to one producing the PWM clock)

You should be able to do all that with just a single Picaxe 40X2. No need to use a second or third Picaxe. The X2 is ideal because all pins are I/O pins, so you can have more inputs than on a 40X1. You need 16 lines to read the counter, 2 lines to set up the capture (DFF reset and conter reset), plus probably 2-3 lines to drive the LCD display and to get user input (reset, etc.). A single 40X2 can do all that.

>this means at full tilt the counter can only count for 13.1 milliseconds, so >
after 13.1milliseconds its going to reset to 0 and starts again? what
>happens if i need a longer interval

Yes, it will reset (start from zero again) if the counter does not get stopped before it reaches its limit. You could also detect this transition (on the 40X2, set up the latch to trigger on a negative transition of the counter's MSB).

For longer intervals there are 2 possibilities:

(1) reduce the PWM frequency, i.e. reduce the count rate. Of course this also reduces the timing resolution.

(2) use a longer counter. You can simply cascade another 74HC393, which will increase the time span by a factor of 256, that should be more than enough. Of course you'll need more inputs on the 40X2 to read back the full result; or (better) use shift registers to read back the data serially (in this case you can use the Picaxe's SPI commands which makes it pretty easy).

Actually there is a better counter chip that I recently discovered, it's the 74HC4040. It is a 12-bit counter so you'd only need 2 of them to replace 3 74HC393's.

>As an improvement, you could connect the output of the second DFF to >another input on the Picaxe.

If you don't, the Picaxe won't know when the counter has stopped counting.

If you need some help with the circuit schematic let me know.

Wolfgang
 
Last edited:

hippy

Technical Support
Staff member
@ SilentScreamer : Yes, the 28X2 can operate at 40MHz using a 10MHz crystal and caps or with a 10MHz resonator if one can find such a thing. 8MHz resonator and 32MHz operation is the practical limit without having to do more complicated calculations to determine periods for PAUSE etc and having to switch frequencies to use serial. In most cases it's likely the extra complication of using 40MHz over 32MHz isn't worthwhile but does add a +25% execution speed advantage.

Both 28X1 and 28X2 have an internal 16-bit timer which can be clocked from an internal or external source. The 28X1 has a Gate Control so all that would need is an external flip-flop to enable counting between the sensors. The 28X2 don't have Gate Control but that could be made part of the flip-flop circuit ( an extra AND gate ) or software control could poll the sensors and do it all in software ( HINTSETUP means you won't miss either triggers ). It seems to me to be largely pointless and overly complicated to add an external counter when the PICAXE has that on-chip.

The internal counter is effectively 17-bit because it has a flag to indicate it overflowed and reset to zero.

The first thing to do is decide what speeds are likely to be measured, from that one can determine what frequency the timer needs to be clocked at and that will indicate the base resolution of timing. With an external flip-flop and Gate Control, with the clock driven by PWMOUT, it should be possible to configure for any speed with best resolution for that speed.

If Schmitt Trigger NAND gates are used to buffer the break sensors a single DIP has four such gates and that is enough to create a flip-flop and Gate Control without adding anything extra.
 

hippy

Technical Support
Staff member
Some maths. Assume max speed is 100mph ...

mph / 2.23694 = metres per second

100 / 2.23694 = 44.70

44.70 m = 1 s
4470 cm = 1 s
1 cm = 1 / 4470 s
30 cm = 30 / 4470 s
30 cm = 0.0067114 s
30 cm = 6.7114 ms
30 cm = 6711.4 us

A counter clocked at 10MHz would have only just overflowed, so that suggests one could read the elapsed time to better than 0.003% accuracy. In practice it seems that an 8MHz clock should be ideal.
 

otm

Member
Right Im now soooooooooooooooooo confused!!

Womai, I understand everything you say (However, the thought of SPI gives me shivers!! although entirly possible). What you are sujjesting is using 2 74HC4040's to effectivly have 6 74HC393's linked... Then would need to pass the ouputs into a big old shift registor (I would have to use 12 D-type flip flops!).
What your saying about the extra PIC is that the 40x2 needs to know when the timer has stoped timing, thus the conter has stopped counting...the 40x2 then knows when that pin goes high that it start the shift resiter clocking meaning that it can read all incoming data.....

You could have the shift resister connected to the PWM pulse of the pic via an AND gate. and the other pin of the AND gate is connected the the 40x2 as well, when that pin goes high and only when it does the shift register will start shifting to the PWM clock (Does that make sense?).

What external resonator would you need for the 40x2 to create a 5MHz PWM (Is it 16MHz or did i get that wrong?)?

@Hippy you are saying that it can all run from within the chip, However the only problem I can see with that is me, and my coding ability (or thereof lack of!).

Looking at your calculations to travel 30cm is 6711.4 μseconds so 60cm would be twice that time which is 13.4 milliseconds... by adding the bigger counter this is a feasable distance

Trust circuit wizard!!! I doen't know what a 74HC393 is or a 74HC4040 is!!! what a bad peice of software (But it got me through my Alevel though!).
 
Last edited:

hippy

Technical Support
Staff member
@Hippy you are saying that it can all run from within the chip, However the only problem I can see with that is me, and my coding ability (or thereof lack of!).
It does involve poking with SFR's but people can help with that and other coding. All the PICAXE code has to do is configure and zero the internal timer, note when a run has finished, read the timer, convert to mph, display, repeat.

Because the command execution time is only a small multiple of the time between sensors best accuracy is with external hardware gating the clock pulses for the timer rather than software polling. That isn't too hard to achieve. The same hardware can be used to indicate to a 28X1 or 28X2 when the run has completed.

Looking at your calculations to travel 30cm is 6711.4 μseconds so 60cm would be twice that time which is 13.4 milliseconds... by adding the bigger counter this is a feasable distance
You can also halve the clocking speed which will allow the use of the internal timer ( in fact, due to it being effectively 17-bit, at 10MHz you could read up to 12.5ms or so ).

Almost any distance and suitable clocking rate would be usable.

The bigggest issue I see may be speed of switching of your break detectors.
 

otm

Member
Hello,

I am now sooo far out of my depth, I have no clue.


The idead for the gate, was a laser diode and an LDR, I however think that an LDR will to too slow to respond, so i am looking at photodiodes. The photodiode will be placed in a tube to cut out all external light sources, when the laser is on it there will be no question that the output is 1 and vice versa...

When it comes to the timing system, I am strugling with all the ideas currently on the table, they all have pros and cons, but I am strugling to decide what the hell is going on!!

I will be the first to admit that my coding ability is naff (bordering God awful). I've only used a PIC twice before!! one was my GCSE project 2 years ago with 4 input switches and the AXE033 LCD, and the speech synth thing. which was at the time I found hard to code... the 2nd time was a year later helping my best friend code his GCSE project a 15 minute timer.

So Hippy your suggestion of running it all on one pic is tough for me to get my head around. Likewise with womai suggestion.. It uses individual chips which I can get my head round, I dunderstand how the gate works, and the counter etc.. however, getting that into the PIC puts my head in a spin.


I'm starting to think I'm making this far too complicated... I wanted it to time to 0.001 of a second, but maybe thats just too hard.... if changin the resolution to 0.01 of a second makes it easier....

Equally I'm trying to work out how it can be upscaled into having 3 or 4 gates to then create an average speed, because (putting my physics hat on) results become more reliable if you repeat the experiment, but because the teams only have 1 rocket motor, they can only run once... Therefore the repeats need to be included, thus adding more gates, each one doing the speed calculation for its distance, then another PIC seperate from the gates takes in all the results, and then does all the maths on it...

However, I like a challenge and am perservering...

Womai thinks his system is best, and Hippy you think yours is better..

If you were me in my position, what would you do?

Thank you,

Owen.
 

womai

Senior Member
>Womai thinks his system is best, and Hippy you think yours is better..

As an engineer myself I'd say the best system is the one that gets the job done with the least effort.

Hippy has got a very valid idea using the hardware interrupt to start/stop the timing. Resolution/accuracy of around 1/100th second could be achievable that way if you clock the Picaxe at 32 MHz. If you keep the code for the two trigger events very similar, both of them will have similar time lag to the actual signal, so their difference will be more accurate than their absolute timing.

My idea will go to higher speed and higher resolution if needed. As for the shift register, no need to use a pile of separate flip-flops. There are 74HCxx shift registers with up to 8 stages in a single chip; look for types with parallel inputs (to load from the counters) and serial output. Here is a list of available 74xx devices:

http://en.wikipedia.org/wiki/List_of_7400_series_integrated_circuits

At first glance (I did not check every one thoroughly) the following look usable: 74HC165. 74HC166, 74HC198, 74HC299, 74HC323, 74HC498, 74HC598.

To generate a 5 MHz PWM signal, you need to clock the Picaxe 28X2 (or 40X2) to 40 MHz, i.e. use an external 10 MHz resonator (gets internally multiplied by 4) and in your program use "setfreq em40".

Wolfgang
 

hippy

Technical Support
Staff member
If you were me in my position, what would you do?
Get someone else to do the work for you :)

The attached program generates a 10KHz PWM and from that counts time in 100us intervals so it can be tested at human speeds. It should measure the time between the 1st input going low and the 2nd input going low. The hardware isn't tested but should work.

You can test it without any hardware other than the 28X2 as it measures the time C.1 (PWMOUT) is connected to C.0 (T13CLKI), so simply connect C.0 to C.1 for up to 6.5 seconds, the program will tell you how long that was for.

I didn't calculate mph from 'elapsedTime', and I'll leave that to others who are inspired to do high accuracy maths.

The only clever parts of the program are the twiddling with SFR registers and automatically determining when the timer has been triggered and when the timing is over; those just check if the timer is incrementing and when it has stopped doing so.

It's currently configured for internal operation at 8MHz but can be easily adjusted to other resonator speeds. It would be possible to use a 28X1 but I'd opt for a 28X2 as it's only a little more and offers greater options for external frequencies. I'd probably go for both 4MHz and 8MHz resonators and use whichever proved to be most useful with whatever setup you finally choose. Again it's only a small extra cost.

As to averaging over multiple runs; I'd stick to pen and paper and log each run make them do some paperwork to determine averages etc. If you wanted to time over different stretches it's probably easiest to duplicate the whole lot; one PICAXE could feed the final one which combined / averaged / displayed speeds etc.

I'd be tempted to 'splash out' and buy two sets anyway because then you have a unit for any PICAXE-inspired students to tweak and better what there is if they can and still have a working system you can keep safe for when used in anger. But we're probably getting ahead of ourselves.

Once you have something in front of you, working, and can see how it performs you will most likely feel more comfortable. You have to start somewhere :)
 

Attachments

Last edited:

otm

Member
Hello hippy,

I have read through your post, and through the code you provided. Does that code do all the timing functions I need? (as in bare basics....). So All I need to do is create the bistable latch, add the other NAND gates and attach to C.0 (pin11) and C.1 (pin12) and then create a suitable interface between the photodiode/phototransistor to the bistable. then how I ouput the data is my own option.

That seems simple enough, but there was NO way I could do anything like that!!

I've been thinking about the whole average speed thing, Im not sure its any point in this because the rocket motors only burn for 6 seconds, and after that the car will be constantly decelerating, this means each result from the light gates would be different. If I place the light gate as close to the point at which the rocket motor stops burning, that will be the top speed of the car and as fast as it will ever get.

As for resonator and the resolution it causes 100 microsecond steps is fine the output is going to be displayed at a max to 2 decimal places (in seconds). I needs to be accurate and the more decimal places the better, but there is a limt, and if you want to go further you end up paying thousands for expert equipment.


I will need to order parts before I can test anything for example, a pic 28x2!!

Once you have something in front of you, working, and can see how it performs you will most likely feel more comfortable. You have to start somewhere :)
I agree! Once i've got it and am testing things I can see how it works, what doesn't work as planed and then we can move from there.

Cheers,

Owen.
 

hippy

Technical Support
Staff member
Yes, all you need do is provide the hardware and add code to convert the time between the two gates to a speed.

Looking at your setup ( 10m track, rocket burn for 6 seconds, possible 60 mph speeds ) something doesn't add up for me - If they hit 60 mph they will cover 10m in about a third of a second. Assuming they have fairly fast acceleration ( like firework rockets do ) they are likely to have left the track and be burning for most of the time in the catch-net at the end of the track. The 10m is probably not enough to have reached anywhere near maximum speed. Unless the acceleration is much more sluggish than I imagine you won't have a problem with de-acceleration.

Looked at another way, to cover 10m in 6 seconds the average speed would have to be around 3.5 mph, anything faster than that and it falls off the end before rocket burn expires. There's probably a reason why rocket-sled test tracks are miles long :)
 

otm

Member
Hello Hippy,

I see your point, Its my fault for not explaining this sorry.....

the track is going to be 100 - 150 meters long. I will only be timing over a 10 meter stretch (prefferably the first 10m after the rocket has burned out). The cars might end up hitting the buffer at the end, but that will be after the rocket has burned out so a relevant speed can still be found.

Sorry for not making that clear.

No doubt I will need help again later on, but you have helped me soo much and given me the start I need.

Thank You,

Owen.
 

hippy

Technical Support
Staff member
Aha, yes you did say over a distance of 10m, but I latched onto "the light gates ... spaced around 30cm apart", which I now see is likely width across track not measuring distance !

It doesn't really matter as the only adjustment needed is to how fast the PWM which ticks the counter runs. Ideally that should give a result of 65535 or less over whatever the distance is. 10m at 60mph is about 0.5 seconds so a PWM of 100kHz should probably cover that timing resolution to 10us.

You need to run the timer slower than that to time slower travel but that shouldn't be a problem nor lose a lot of accuracy ( ie, if it takes too long to travel the distance the timer runs out ).
 

otm

Member
WOW hippy,

That really works well, I mean really well.

My question to you know is the answer in in useconds. However, I need the answer in seconds, because then I can divide the distance travelled by the time taken in seconds to find the average speed. How can you do this?


So I thought ok, thats easy, divide the output by 1 million. An error poped up say that that number was too in a single calculation, so I thought ok. Divide by 1000, and then by 1000 again. The ouput of that calculation on the Serial terminal was 000.

I know that the PIC isn't a big fan of numbers with a decimal point, but this just confuses me.

I am so pleased with everything im happy it works, Im soo grateful to you for giving me the start I needed.


I want to print the answer to the AXE033 LCD in m/s because it looks faster then in MPH (we did a few calcs. and we believe that the cars we've tried only reached 30MPH, maybe they could go faster if the areodynamics are better!). I would like the output to have two decimal points, but is that possible with the PIC's dislike of decimal points?

I have also spent the interim time looking at radio modules...... I have decided to make the serial LCD wireless. The PIC 28x2 program that you gave me will be at the transmitter end, and there will be another 28x2 at the receiver end. After reading around (and asking on a radio forum) I came across someone who is using AM radio modules (http://www.maplin.co.uk/searchtemplate.asp?criteria=AM-RT4-433/AMHRR3-433) to allow two PICs to communicate and swap variables.

Here is the code he showed me:
Code:
SerOut 5, T600, ("UU")   'open TX
Pause 100
SerOut 5, T600, ("XY", b1,b2,b3 )   'Transmit data

The t600 is baud rate. You may need to drop this to 400
The (“UUU”) wakes up the RX
The “XY” is a qualifier. The RX will ignore any data not preceeded by this
And of course the b1,b2,b3 are the data variables.

For the Receive code I use;-

Serin 1,T600, ("XY"), b1,b2,b3.

The T rate must match the Transmit code. Note the qualifier “XY” the variables will then be received.

For the LCD display I presume you are using the FRM 102 serial chip. If not, you have an awful lot of coding to do.

I use the serial chip and write to the display :-

high 7
pause 100
serout 7,T2400,(254,1,254,1) ‘ clear display
serout 7,T2400,(254,128,"Speed ",b1 “Temp “b2,” Dist “,b3)
He said he wasn't sure if this would work with a 28x2, and looking at the Example projects on the rev-ed site, there is a wireless greenhouse thing, but that uses the PICtalk protocol, which I've never heard of!!

I will probs need to change the code for the LCD, the reason why I am telling you this is because I somehow need to transmitt a variable that contains decimal..

One thing I have learnt from this is that control systems engineering, is probably not for me!!

Cheers,

Owen.
 

otm

Member
Ohh ok,

I'll delete my last post then , cause if he deleted it, then its no point me showing it,

My last question still stands.....

Cheers,

Owen.
 

BeanieBots

Moderator
I haven't gone through the code to work out what the units are, but the general idea is like this.

The PICAXE can only cope with numbers between 0 and 65535 in a word variable.
ALL numbers are integers, so you cannot have any decimal points.
As you have found out, dividing by a large number returns zero.
The trick is keep the number the way it is, but display it differently.

Let's say you have number "1234" and it's mS and you want it in seconds.
That would be "1.234" seconds.
You can't divide by 1000 because all you would get would be "1".
You need to break down the number into each digit.
There is a command to do that for you.

BINTOASCII wordvariable, tenthousands, thousands, hundreds, tens, units

Then, send each number in sequence with a decimal point where you want it.
 

otm

Member
Heloo BeanieBots,

Thankyou for your reply.

I think im understanding what your talking about.

Its a case of taking the large number stored int he word. And using the BINTOASCII command to break it into individual digits in there own individual variables.

I need to do the maths in such a way that I can divide the distance traveled by the time taken, and it still be a very large number, which I can then break up into individual digits, and then place the decimal where I want. (but surely thats almost impossible. if I turn the 4m into micrometers to suit the seconds, it equals 4 million, which is too big a number, if you could store 4 million you end up with a decimal when dividing the time by the number of microseconds taken)


When i try to use this code to work out the best way of using BINTOASCII (as well as find out what is good and bad code etc..):

Code:
Let w0 = 12456

main:

Bintoascii w0, b2, b3, b4, b5, b6
serout 7, 4, (#b2,#b4)
pause 5000
end
I intend the output to be 1 and 4, but instead I get 4952 as the output?

what have I done wrong?

Cheers,

Owen.
 
Last edited:

BeanieBots

Moderator
The bintoascii does what it says on the tin, so there is no need to use # in your serout.

try serout 7, 4, (b2,b4) instead.
 

lbenson

Senior Member
And "4952" because 49 is the decimal value of the ascii charactor, "1" and 52 is the decimal value of "4"--see, if need be, an ascii table here: http://www.asciitable.com/

Using the "#" on bytes which are already ascii charactors results in those values being displayed.
 

otm

Member
Hello BeanieBots,


So it does... Thank you

Ibenson, That clears it up, Cheers.

All I got to do now is work out how to do maths without decimals, and I'll be there (it sounds soo simple when you put it like that!).
 

BeanieBots

Moderator
It can be that simple.
Genral rules:-
Do any multiplication first so that you don't lose resolution.
Be careful not to overflow any word variables during the calculation.
Intermediate results on bytes are done at 16-bit so no need to use words all the time.

So, if you want b0/b1 when b0=5 and b1=17 (5/17=0.29541)
In integer maths
b0/b1=0
b0*10/b1=2
b0*100/b1=29
b0*1000/b1=294 and so on.

but b0*N must be less than 65535 or you will get an overflow.
 

otm

Member
Hello BeanieBots,

I can see what your saying, and how I might have to manipulate things. BUT, I dont have the hardware on me at the moment (it's at college). And I wont get at it until 5 or 6th of september...

I can't remember a typical output, for the tests I did... When I can get to it (and have the equipment avaliable to test it and sort things out) I can (somehow) use a frequency generator to simulate the car breaking the beams so I know its constant then work out the maths then..

Soo as before, (as a famous actor said) I'll be back!

Cheers,

Owen.
 

BeanieBots

Moderator
You don't need the hardware.
You know how far apart the sensors are.
You know how fast the cars are.
You know how fast the PICAXE is.

From then on it's all maths.
No, I won't do it for you!

Don't leave it until the last minute. Do the DESIGN now!
 

otm

Member
You don't need the hardware.
You know how far apart the sensors are.
You know how fast the cars are.
You know how fast the PICAXE is.

From then on it's all maths.
No, I won't do it for you!

Don't leave it until the last minute. Do the DESIGN now!
Hello Beanie Bots,

I don't know how fast the cars are, thats what I'm trying to find out! see the problem is, I was told they go 60MPH, but the fastest car I've witnessed we calculate (roughly) was only going 30MPH, which is quite different!!!!!

I can't remember off the top of my head whether the answer out of the PIC was x thousand microseconds, or x hundred microseconds... In my mind I think it makes a differeance to the maths needed, but in reality it probably doesn't!!!

I wasn't expecting you to do it for me, You've given me enough of a start, which is more than helpful, and with Hippy's help, I am far along the way.

I already have PCB's designed and am well on my way to completing this project. It was a slow start, but now I'm motoring.

now correct me if I'm wrong.

If the car is travelling at 15 m/s, the time taken to move 1 meter at this speed is 1/15 second (0.066666666 seconds).

so to go 4 meters is 0.266666664 seconds.

Therfore, the output of the PIC would be 26600 microseconds (its internal timer counts in 100 usecond steps using Hippy's code, using an 8MHz resonator, with a 10KHz PWM).

Using your intermediate maths thing I have to turn that into seconds, so I have to multipy 266600 by a number (to make a bigger number) then divide 4 by that number. But then this is the thing I'm struggling in my head is that that still gives a useless answer, becuase its ouput is in the the form, meters per usecond. Which I need to turn into m/s, Its a shame that the PIC can't use standard form! it would be soo much easier then!


I hope you can understand how much my head hurts everytime I think about this, all I need to do is get a few basic principles shoved into my head, and then I'm off.

Cheers,

Owen.
 
Last edited:

otm

Member
I feel slightly dumb and stupid.

I've spent SOOOOOOOOOOOOOOOOOOOOOOO long thinking about this, and the solution was staring me in the face. Unless my calculations above are completly wrong ( I just want someone to check cause I dont trust my maths)....

car will move between the gates in a time of 0.266666664 seconds (if its travelling at 30MPH) the PICAXE will read 26600 useconds (I Hope).

If i divide that answer by 100, I get a more managable 266.

so if I times the distance between the gates by 10000 then divide by the 266 I get an answer of 150. Which when the decimal is placed between the 2nd and 3rd digit it gives 15.0 m/s (which is 30MPH)

So:
Code:
w0= 4 * 10000 /266
I tried this principle with a speed of 20MPH (8.9m/s) i do the same as above but divide by 449 (44900 / 100) I get an answer of 89 put the decimal and hey presto 8.9.

I then added an IF..Else..EndIF statement so if the output is less then 100 it places the decimal in a different plcae (I dont want the 0 before the ouput if that makes sense)

Code:
w0 = 4 * 10000 /266
Bintoascii w0, b2, b3, b4
if w0 < 100 then 
 serout 7, 4,(b3, ".", b4, " m/s")
  else
   serout 7, 4,(b2, b3, ".", b4, " m/s")
endif
its just a test code I would need to put serout 7, 4, (254, 1) etc...

My issue is at the moment, that I want 2 decimal places. I cant times 4 by 100000 because thats too big a number, I cant divide 26600 by 1000 becuase then you lose the accuracy.

Can anyone shed a little light on this issue?

Thank You,

Owen.
 
Last edited:

BCJKiwi

Senior Member
Well now you need to delve into the mystery of 32 bit math which is supported on the PICAXE.

Do a search and you'll find a few threads.
There is this one http://www.picaxeforum.co.uk/showthread.php?t=8624 which on review seems a bit hard to follow - sorry!

Basically the internal calculation in PICAXE is 32 bit but a few tricks are required to access the full 32 bit and process it in 16 bit chunks and then put it back together.
 
Last edited:

hippy

Technical Support
Staff member
Just to be clear - Internal maths on PICAXE is 16-bit, with the exception of ** and */ which are special cases and do use 32-bit intermediate values and return 16-bit results. Which is what BCJKiwi is saying in context.
 

otm

Member
Hello again,

BCJKiwi, does it supries you, that after spending the best part of 2 hours looking at your post, looking at the snippets of code, running them on the simulator to see if I can see whats going on, that I feel like I've moved nowhere.

Hippy, I have tried using those two functions in simulation (** and */) and, even with reading through the Basic manual, I cant work out how to use it! I use this code (Which is in the same format as the manual):

Code:
let b1 = 1234
let b2 = 1234

w2 = b1 * b2
serout 7, 4,("b1 * b2 =", #w2, "    ")
pause 1000
w2 = b1 ** b2
serout 7, 4,("b1 ** b2 =", #w2,"   ")
pause 1000
w2 = b1 */ b2
serout 7, 4,("b1 */ b2 =", #w2, "    ")
pause 1000

and these are the answers I get: b1 * b2 =44100 b1 ** b2 =0 b1 */ b2 =172

Using a calculator 1234 x 1234 = 1522756 (I know there is an overflow in the PIC, but surly the numbers above should have some relation to this?) so where do the values above come from?

Sorry for not understanding the help your providing.

Owen.
 
Last edited:
Top