Timer Circuit

BCJKiwi

Senior Member
@ OTM
Would you please advise;
1. Maximum speed you need to cope with. (30MPH, 60MPH?)
2. Do you want the answers in MPH or M/S or KPH - mixing measurement systems adds confusion.
3. Can you change the distance over which you measure the time?
4. Confirm the formula that you want to improve resolution with
5. Confirm the resolution required (2 dec places?)
 
Last edited:

otm

Member
@ OTM would you please advise;
1. Maximum speed you need to cope with. (30MPH, 60MPH?)
2. Do you want the answers in MPH or M/S or KPH - mixing measurement systems adds confusion.
3. Can you change the distance over which you measure the time?
4. Confirm the formula that you want to improve resolution with
5. Confirm the resolution required (2 dec places?)
Hello BCJKiwi,

1) The max speed of a car I have seen is 30MPH, Therefore the max speed of any of them will be 35 MPH.

2) M/S is the best (and it makes it look faster)

3) NO, I will be using a Cat5e cable to connect the light gates together, So it will be fixed to 4 meters

4) I want to improve the resolution of the formula I worked out earlier:

4 x 10000 / 266 (Using this gives an answer of 150, which translates to 15.0 m/s (15.0m/s is 30MPH))
................^ this number will change depending on the time

what that is in real terms is the distance in meters x a multiplier divided by the the time taken (time taken is divided by 100 beforehand).

It is derived from the speed = distance/time equation where 4 is the distance and 266 is the time taken for the rocket to cross the gates.

5) 2 dec. places would be great any higher is a bonus.

Owen.
 
Last edited:

BCJKiwi

Senior Member
Ummm 1234 won't fit in a b variable (8bit), a word variable (16bit) is needed.

Try this in the simulator
Code:
let w1 = 1234
let w2 = 1234
w3 = w1 * w2 ' low word
serout 7, 4,("w3 = w1 * w2 =", #w3, " ",cr,lf)
pause 1000
w4 = w1 ** w2 'high word
serout 7, 4,("w4 = w1 ** w2 =", #w4," ",cr,lf)
pause 1000
'full number
serout 7, 4,(#w3,#w4, " ",cr,lf)
pause 1000
 
Last edited:

BCJKiwi

Senior Member
with the division
Code:
let w9 = 10000
let w10 = 266
w11 = w9 / w10
w12 = w9 // w10
'Full number
serout 7, 4,(#w11,#w12," ",cr,lf)
pause 1000
For some reason the results in the simulator do not quite match the calculator - have not tried it in a chip.
 

otm

Member
with the division
Code:
let w9 = 10000
let w10 = 266
w11 = w9 / w10
w12 = w9 // w10
'Full number
serout 7, 4,(#w11,#w12," ",cr,lf)
pause 1000
For some reason the results in the simulator do not quite match the calculator - have not tried it in a chip.
BCJKiwi,

Wow, thats eloquently simple.

And I can see how the modulus divide works.

I can see what I was doing wrong with the code I was trying to replicate from the manual.

All I then need to do is break the words into bytes with my new knowledge of Bintoacsii like this (I hope!):

Code:
let w9 = 10000
let w10 = 266
w11 = w9 / w10
w12 = w9 // w10
'Full number
serout 7, 4,(#w11,#w12," ",cr,lf)
pause 1000

bintoacsii w11, b0, b1, b2
bintoacsii w12, b3, b4, b5
Then its just a case of displaying the data which is a doddle!

I've used several calculators whith the mod function (both online ones and one that I have), they all say that 40000 mod 266 = 100. Does the PIC do it differently?

Cheers,

Owen.
 
Last edited:

BCJKiwi

Senior Member
PICAXE gives the same result!

40000 / 266 = 150 (150 x 266 = 39,900 which leaves a 'remainder' of 100)
40000 // 266 = 100 (the remainder). This is NOT the calculator decimal portion - 40000 / 266 = 150.3759)

The manual describes the // function as "modulus divide (returns the remainder)". My simple mind finds the term remainder easier to handle.
 
Last edited:

otm

Member
PICAXE gives the same result!

40000 / 266 = 150 (150 x 266 = 39,900 which leaves a 'remainder' of 100)
40000 // 266 = 100 (the remainder). This is NOT the calculator decimal portion - 40000 / 266 = 150.3759)

The manual describes the // function as "modulus divide (returns the remainder)". My simple mind finds the term remainder easier to handle.
OK that makes sense, thats why the result is different..

However, I would have throught that ther remainder wounldn't be too far off of the decimal value (surely decimal numbers are effectivly remainders?).

I'm lost again!! I thought I had it all in my head (Your help has been invaluable).

I'm well above my head now (still willing to learn though).

Cheers,



Owen.
 

otm

Member
EUREKA (I think)!


BCJKiwi, I think you may have been leading me onto this!!!

Somehow! I worked something out.

Lets take the 266 example which should = 15.03m/s

40000/266 = 150
40000//266 = 100

If we times the 100 (remainder of the second equation with the modulus divide equation) by 10 we get 1000. If you then divide the 10000by 226, you get the answer of 3.

combine the first division (150), and the 3rd division (37) you end up with 1503. Add the decimal and you have the answer to a resolution of 2 decimal places.

If I try it with 449 (should = 8.90m/s)

40000/449 = 89
40000//449 = 39

39 * 10 = 390
390/449 = 0, add the two = 890, add the decimal 8.90m/s (which is equal to 19.908mph)

This is an example of the code:

Code:
let w9 = 40000
let w10 = 250
w11 = w9 / w10
w12 = w9 // w10
w13 = w12 * 10 / w10 

'Full number
serout 7, 4,(#w11, #w13,"      ",cr,lf)
pause 1000

bintoascii w11, b0, b1, b2
bintoascii w13, b3, b4, b5

pause 1000
If w11 < 100 then
	serout 7, 4,("SPEED = ", b1, ".",b2, b5, " m/s", cr, lf)
 else
 	serout 7, 4,("SPEED = ", b0, b1, ".", b2, b5, " m/s", cr, lf)
endif
After testing, this method is only good to add one more decimal place (however, thats all thats needed!). When I use the google calc to convert MPH in m/s, I use the PIC to convert the time equvilant for the m/s answer by google back into m/s (Just testing known values). The last decimal can sometimes be a little different, However, when you convert the PIC answer back into MPH the answer is very colse to the original one entered into google (Very close!!). I was never expecting it to be accuratly precise, becuase i'm not using lads of decimal places, I'm chuffed......

Thank You everyone.

Had a horrible thought, Is my maths flawed????? IF the PIC counts on 100 usecond steps, and if the rocket takes 0.266666 seconds to go 4 meters, would the output from the PIC be 26600? If not, then we are back to the beginning!!!!!!!!!!!!!!!!!!!!!!!!!!


Cheers,

Owen.
 
Last edited:

otm

Member
Hello Everyone,

I need a bit more help, on something...

Now I know people may feel they have already helped me too much, and, now may be unwilling to help. To be fair, I can probably see where your coming from, BUT, I try and am willing to learn.

I was thinking about an error output, for instance, if the rocket goes too slow, the timer will overrun, and it will start from the beginning again. Soo if the output was 26600 when it goes really slow the output should be 26600 + 65532 correct?!

Now I was thinking, The pic needs to moniter the value of the whole counter. If it equals 65532, AND the counter is still going, it set the value of a marker variable ie. b9 to 1. Then when it comes to calculating the speed if b9 = 1 instead of outputing the speed like it normally would (which would be wrong) it just output an error.

I did a little thought experiment and found that the minimum speed before the PIC overruns is 14 MPH. so the error could say "Too Slow Speed < 6.2 m/s"

As far as I can tell the only way to do this is liek the counter, using PokeSfr, and peekSfr which I daren't touch!



It's not important,

But all help is greatly, greatly appreciated,

Owen.
 

hippy

Ex-Staff (retired)
Here's a version of the TrainTmr.txt file from earlier which includes checking for overflow - note the code related to PIR1. In this program I test by shorting C.0 to C.1 it reports how long the short lasted. If over 6553500us ( 6.5s) it reports "Overflowed" ...
 

Attachments

otm

Member
Here's a version of the TrainTmr.txt file from earlier which includes checking for overflow - note the code related to PIR1. In this program I test by shorting C.0 to C.1 it reports how long the short lasted. If over 6553500us ( 6.5s) it reports "Overflowed" ...
Thank You Very much Hippy,

Can you briefly explain how it works?

I see all the things you've added in relation to PIR1.

Thank You,

Owen.
 

hippy

Ex-Staff (retired)
The lsb ( bit 0 ) of the PIR1 register is a flag which is automatically set whenever Timer 1 overflows, so we simply clear it when starting ( PeekSfr ...,b0 : bit0=0 : PokeSfr ...,b0 ) then when we've finished check if the bit has been set ( PeekSfr ...,b0 : If bit0=1 ... ).
 

otm

Member
The lsb ( bit 0 ) of the PIR1 register is a flag which is automatically set whenever Timer 1 overflows, so we simply clear it when starting ( PeekSfr ...,b0 : bit0=0 : PokeSfr ...,b0 ) then when we've finished check if the bit has been set ( PeekSfr ...,b0 : If bit0=1 ... ).
Thats cute!!!

Small and perfectly formed!

Thank you very much, thats one more piece of new knowledge to add to the ever growing chalkboard.

Thank You again,

Owen.
 

otm

Member
Hello Everyone me again...

Something has happened...

I have updated the software to 5.2.7 I was checking everything I need (College starts again on monday so i can get back to the circuit, to work on it) before i program the PIC.

I tested this code:

Code:
'set test variables w9 = distance, w10 = time
Let w9 = 40000
Let w10 = 26600

'SPEED (m/s) calculation
w10 = w10 / 100
w11 = w9 / w10  
w12 = w9 // w10       'using modulus divide to find remainder
w13 = w12 * 10 / w10  'using remainder to find final decimal place 

serout 7, 4,(#w11, #w13,"      ",cr,lf)
pause 1000

'm/s into mph
w14 = w11 + w13
w14 = w14 * 22

'convert Words into individual bytes
bintoascii w11, b0, b1, b2
bintoascii w13, b3, b4, b5
bintoascii w14, b6, b7, b8,b9, b10
pause 1000

serout 7, 4,(254,1)
serout 7, 4,( b0, b1 , b2, b3, b4, b5)
pause 4000


'decide where decimal goes
If w11 < 100 then
      serout 7, 4, (254,1)
      serout 7, 4,("SPEED =  ", b1, ".",b2, b5, "m/s")
      serout 7, 4, (254, 192)
      serout 7, 4,("OR c. ", b7,b8,".",b9, " MPH")
 else
 	serout 7, 4,(254, 1)
 	serout 7, 4,("SPEED = ", b0, b1, ".", b2, b5, "m/s")
 	serout 7, 4,(254, 192)
 	serout 7, 4,("OR c. ", b7,b8,".",b9, "MPH")
endif

When it gets to this line:
Code:
serout 7, 4,(#w11, #w13,"      ",cr,lf)
it displays 1503 (Which is correct for the value of w10

BUT, when it gets to this line:
Code:
bintoascii w11, b0, b1, b2
b0 no equals 2, it should equal 1, its changed from the last time I tested the code.. I've changed nothing but it doesn't work as it should anymore, is it something to do with the new software update?

it displays 25.03 m/s (meaning eveything else is ok) and 34.7 MPH (Which is correct.. it is an approxomation so it should be 33.6 or something like that but 34.7 is more than ok)

this means that it is just b0 playing up. when i change b0 to b1 (changeing all other variables accordingly) its still b1 displays as 2 not 1. Is this a software glitch? I see one of the updates is:

- Some word values in calculations not rounding down correctly

Have I found a bug!?

Cheers,

Owen.
 

hippy

Ex-Staff (retired)
Odd, when I modified the code to use SERTXD rather than SEROUT 7, it all seemed to be okay, giving ...

1503
150003
SPEED = 15.03m/s
OR c. 33.6MPH

Code:
#Picaxe 28x2
#Terminal 9600
#No_Table
#No_Data

Pause 2000

Let w9 = 40000
Let w10 = 26600

w10 = w10 / 100
w11 = w9 / w10  
w12 = w9 // w10       'using modulus divide to find remainder
w13 = w12 * 10 / w10  'using remainder to find final decimal place 

sertxd(#w11, #w13 ,CR,LF)

w14 = w11 + w13
w14 = w14 * 22

bintoascii w11, b0, b1, b2
bintoascii w13, b3, b4, b5
bintoascii w14, b6, b7, b8,b9, b10

sertxd( b0, b1 , b2, b3, b4, b5, cr,lf)

If w11 < 100 then
  sertxd("SPEED =  ", b1, ".",b2, b5, "m/s", CR,LF)
  sertxd("OR c. ", b7,b8,".",b9, " MPH")
else
  sertxd("SPEED = ", b0, b1, ".", b2, b5, "m/s", CR,LF)
  sertxd("OR c. ", b7,b8,".",b9, "MPH")
endif
 

otm

Member
This is weird..

I've just put your code, striaght into my PC, with sertxd, and B0 is still 2 not 1........

it displays 1503, 250003 then 25.03m/s or c. 34.7MPH

How strange..

Cheers,

Owen.
 
Last edited:

hippy

Ex-Staff (retired)
Very strange. I'm using Programming Editor 5.2.7, 28X2 (5V) firmware B.1

I have found a problem using BinToAscii from word to three bytes

- w11 = 12345
- BinToAscii w11, b0, b1, b2
- SerTxd( b0, b1 , b2, CR, LF )

Shows "<<45", where as ...

- w11 = 12345
- BinToAscii w11, b0, b0, b0, b1, b2
- SerTxd( b0, b1 , b2, CR, LF )

Shows "345" as expected.

I suspect BinToAscii for three bytes converts as "b0=w11/100+$30" rather than "b0=w11/100//10+$30" but why the very same code doesn't work the same way ( assuming you have 2.5.7 / B.1 ) on yours I don't know.

It's something we will have to investigate.
 

MartinM57

Moderator
I have found a problem using BinToAscii from word to three bytes

- w11 = 12345
- BinToAscii w11, b0, b1, b2
- SerTxd( b0, b1 , b2, CR, LF )

Shows "<<45", where as ...
..which according to Manual 2 is not an allowed combination

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

i.e. you have to provide 5 bytes for the answers?

EDIT: - and how do you get 4 characters out of sertxd when you only send 3 bytes?
 

hippy

Ex-Staff (retired)
There's a definite issue with BinToAscii words to three bytes as described above and it seems to affect all PICAXE. That's a compiler issue and firmware is not faulty. Arguably the manuals do say to use three bytes with byte variables, five bytes for word variables. Three bytes works for when word variables hold 0 to 999, not above.

I suspect it's something to to do with character sets why I saw '<<' ( single char ) and you see '2'.

A workround may be to only use BinToAscii on words to five bytes, repeat the first byte to pad out ...

Code:
#Picaxe 28x2
#Terminal 9600
#No_Table
#No_Data

Pause 2000

Let w9 = 40000
Let w10 = 26600

w10 = w10 / 100
w11 = w9 / w10  
w12 = w9 // w10       'using modulus divide to find remainder
w13 = w12 * 10 / w10  'using remainder to find final decimal place 

sertxd(#w11, #w13 ,CR,LF)

w14 = w11 + w13
w14 = w14 * 22

bintoascii w11, [b]b0, b0,[/b] b0, b1, b2
bintoascii w13, [b]b3, b3,[/b] b3, b4, b5
bintoascii w14, b6, b7, b8, b9, b10

sertxd( b0, b1 , b2, b3, b4, b5, cr,lf)

If w11 < 100 then
  sertxd("SPEED =  ", b1, ".",b2, b5, "m/s", CR,LF)
  sertxd("OR c. ", b7,b8,".",b9, " MPH")
else
  sertxd("SPEED = ", b0, b1, ".", b2, b5, "m/s", CR,LF)
  sertxd("OR c. ", b7,b8,".",b9, "MPH")
endif
 

hippy

Ex-Staff (retired)
@ MartinM57 : Posts crossed; the '<<' is a single character, Spanish opening quotation mark.
 

otm

Member
Hippy,

Your not going to believe this..

I tried your new workaround, and guess what, b0 = 2. I simply dont get it.

I've re downloaded the programmer off of the PICAXE website, reinstalled it, and still same problem. What i dont get is how it worked fine in 5.2.6, I've changed nothing, and now its all wrong. Like I say since working out the maths I havent been able to download any of it onto the PIC so I dont know if it acctually works, I'm only going by the simulator.

I dont know.....

Owen.
 

hippy

Ex-Staff (retired)
Aha ... Simulator. I guess I should have paid more attention to "before i program the PIC".

I get the same erroneous result as you do, so we will look into that.

On the real chip it should work providing word values <= 999 or you used five bytes to BinToAscii into.
 

otm

Member
Aha ... Simulator. I guess I should have paid more attention to "before i program the PIC".

I get the same erroneous result as you do, so we will look into that.

On the real chip it should work providing word values <= 999 or you used five bytes to BinToAscii into.
OOOHHHH, so this is just the SIM being funny, I should probably have made it clearer, all the work I've done on the maths, has been during the summer holidays, I've been unable to test it actually, so, I've relied on the SIM reacting like a real PIC would, so you can understand my somewhat bafflement when it all went p-tong. When I download to the PIC it should be all ok then?

Thank You for clearing that up, But at least I might have highlighted a problem :)

Again Thank You SO much,

Owen.
 

hippy

Ex-Staff (retired)
Yes, fingers crossed it should be okay downloaded on a real PICAXE. Sorry for the problem but thanks for noticing and reporting it.
 

richtell

New Member
how to read output of Hippy's TrainTmr

I have a need to meauser the initial velocity of a potato fired from a spud gun- science fair material of course, not to have any fun...
I have built Hippy's circuit and downloaded the file. I am using two IR LED/IR phototransistor pairs (Common collector with a 47K resistor between the transistor and ground) for the start and stop signal. The signal is fed thru a 4011 quand NAND gate. With a scope I have confirmed that CounterIn, C.0, only pulses between the start and stop signals, passing my hands thru the sensors. So, the input and output work, but where do I read the output?

The trouble is, where does the output go? I should see "ElaspedTime= XXX us. where?
This is my first time using a Picaxe, and I'm perplexed. When I run Simulate the program just cycles in the WaitForTimerStarted loop.
After I download the program, which I have done many times, a curious screen shows up with Serial Terminal N 9600 etc in the Title bar, and spaces for input and output buffers below. I don't know what to do here, and have been just closing the dialogue box.
 

manuka

Senior Member
"Our industries are starved of engineers. There are real problems on the education front; and, of course, what we've got now is the environmental challenge coming up.

The consequences if we don't inspire the next generation are that we will wither as a country

Lord Drayson-UK science minister
If this is indeed the Bloodhound intention (rather than a "toys for boys" ego trip),then it may well be that designing a car to get "1000 mpg" will be more cost effective,& far better received. It's tempting to ponder hence the acclaimed 3000 km Australian Solar Challenge race (Darwin - Adelaide), which has had immense educational & technical spinoffs since it first ran in 1987. See => http://globalgreenchallenge.com.au/the-event

Great timing in fact! The 10th event begins in a few days, this year also with an eco theme, & numerous budding engineers are again involved. No doubt the alcohol fueled "Top Gear" entry will appeal to all red blooded Englishmen, although I doubt it'll sustain the >100 km/hr speeds of the solar vehicles if Jeremy Clarkson is behind the wheel...
 
Last edited:
Top