Settimer v Timer3

Radarman2

Member
HI all,
I'm creating a little timing system that needs to be accurate to 1mS (or better).
So I started looking at Settimer & Timer3.

If my calculations are correct, a Timer3 on a 20X2 running at 64MHz with 1:1 prescale would create a minor tick every 4.096mS (giving a Major Tick count of 244.14 every second). Page 242/243 of manual 2. Obviously no good for my needs.

A SetTimer on a 20X2 running at 64MHz would create 250 minor ticks every 1mS
So
Settimer 65286 should create a Major tick after 1mS of this running.

Is that correct?

If I've done my maths right the follwing code should count in mS intervals and increment b0 every 1mS: (b1 is there to count hundredths, b2 will be added later to count tenths of a second)

Code:
#picaxe 20x2
#no_data
#no_table
b0=0
b1=0
setfreq m64
Main:
settimer 65286
do until timer=1
	inc b0
	if b0=100 then
		inc b1
		b0=0
	endif
timer=0
loop
goto main
Can someone cast their beady eye over this and see I'm right please?
I've ran the simulator at 20mS delay and a 100 count on b0 should be 20mS x 100 = 2secs, but I'm timing it at 20secs. A b0 count of 10 does come out at 2secs (give or take, allowing for these oldish fingers), so I'm out by a factor of 10. Is this due to the programming editor or my code?
I'm about to put a hefty order in and don't want the wrong equipment if I can't get the resolution I need.
Cheers in advance for any replies that don't call me a dumbass lol.
 

womai

Senior Member
I think you need to clarify your requirements a bit further. First, is the 1ms the required accuracy or the require resolution? That's two quite different things. If it is resolution, you'll only need to make sure the Picaxe is sufficiently fast so it can catch events within 1ms. But if it is accuracy, then it not only depends on you code but also on the accuracy and stability of the clock source; the internal oscillator can be off by up to ~2%, a ceramic resonator is typically within 0.5 - 1%, a quartz crystal can easily achieve 100ppm = 0.01%. In addition it would be necessary to know over which time frame you need to be accurate to 1ms - if that's over e.g. 1sec then it's easy, but ove e.g. 1month you'd probably need to sync up to a GPS signal or similar...
 

Radarman2

Member
Sorry, should have been clearer.
It's for a sprint timer. Essentially a lad at work does a lot of fitness training and an accredited all-singing-all-dancing sprint gate system costs £5000 (with PC & software).
For his and his mate's own training they would like a system to time them over a 30m sprint. As an example if it took them a bit over 3 seconds for the run then the time would need to show 3.245 secs with an accuracy as good as possible, but realistically +or- a few mS would do.
There will be an IR barrier at the start that will register on the 20X2 (situated at the finish), an IR barrier at the end that again registers with the 20X2.
You break the beam, timer starts, break second beam, timer stops, time is displayed, reset for next runner.
I've worked out the code, electronics and mechanics of everything else, just wondering whether people think that the example code above would actually create a Major Tick at 1mS intervals and whether they're getting the same time results with the programming editor that I did.
I've worked out tiing intervals for SetTimer & Timer3, so will chuck up a pdf later so people can see what the differences are (as well as check my maths).
 

womai

Senior Member
If it shall at least be reproducible from one day to the next then you definitely need to run the Picaxe from an external reference - best is a crystal (plus suitable load capacitors - see data sheet for details but it's typicsally about 20pF between either end of the crystal and ground). Otherwise differences in ambient temperatures will cause the internal oscillator to drift around. Just imagine hot sun one day and rain and a cold breeze the next...

So I'd say a new 28X2 (which can run 64 MHz based on an external resonator or crystal) is a better bet than a 20X2 which always runs on its internal oscillator.

30m dash means timing durations of a few seconds (let's say abut 5 sec), so 1ms absolute accuracy means 0.001/5 = 200ppm. You definitely need a quartz crystal for that.
 

Radarman2

Member
Thanks Womai!
They'll be running it indoors with a normal ambient temperature, but I'll look into quartz crystals as the gym they run in will obviously have a temp change through the seasons. I know the LOs we use in work sit in an oven to keep them at a constant temp and hence more accurate.
Think I read that 28X2 has a 4xPLL, so I'll go hunt a 16MHz xtal.
 

clockwork

Member
Interesting project Radarman 2.

As you mentioned a Xtal Oven will help improve accuracy. Professional units are no doubt difficult to obtain, so I have attached a JPEG of a homebrew Xtal Oven, although there is no oven as such.

I can take no credit whatsoever for this circuit or drawing as it is the work of Australian Radio Amateur Drew Diamond VK3XU. (Taken from his excellent book "Radio Projects for the Amateur Vol 3", available from the GQRP Club)

Drews approach is simple but effective.

To aid accuracy even further you could also include the use of a Xtal that is of much higher frequency than required and divide the output down. For example to get a very accurate 2Mhz output take a 16 Mhz Xtal adjusted as close as possible to its quoted frequency (in a Xtal Oven, homebrew or otherwise) then divide down to the wanted 2Mhz output. If the Xtal in its oven is run for at least 10 minutes or so to allow it to stabilise then you should have a pretty accurate timing signal. Also well stabilised bypassed power supplies will also help.
 

srnet

Senior Member
I know the native PICs timers can be operated in gated mode, such that the timer count is not dependant on latency or variability in code between starting and stopping the timer.

But is thus true of PICAXE ?

If one is going as far as using a crystal oven, how can you be sure the differance between starting and stopping the timer will be short enough or predicatable enough to warrant the complications ?
 

Radarman2

Member
To aid accuracy even further you could also include the use of a Xtal that is of much higher frequency than required and divide the output down..
That's the opposite of what I've been taught and what we use at work. You start with a stable low value local oscillator then use doubler and trebler ccts to achieve the freq you want. eg 50MHz xtal output goes through a x2 then a x3 to get 300MHz stable to +or- 100Hz.
The PLL built into the picaxe should keep the freq very stable.
 
Last edited:

womai

Senior Member
I think an oven stabilized XCO is a bit overkill. According to spec typical crystals achieve 100-200ppm stability right out of the box with no oven. For a bit more mone (but still much less then an oven stabilized one ) you can get 25-50 ppm. And according to my estimate in the previous post 200ppm is all that is needed to be accurate to 1msec.

Wolfgang
 

srnet

Senior Member
I cannot see how dividing or multiplying a crystal based oscillator can, in itself, improve accuracy.
 

Radarman2

Member
I cannot see how dividing or multiplying a crystal based oscillator can, in itself, improve accuracy.
xtals are more accurate at lower frequencies. By a strange twist of fate you don't really multiply the inaccuracy, just the frequency.
 

geoff07

Senior Member
Well, actually you do multiply the inaccuracy as you are dealing with pure harmonics. It is just that crystals with a lower fundamental are more stable than those with a higher natural frequency because the cut used is typically less stable than that used for low freq crystals. So the multiplied inaccuracy of a low freq crystal may still be better than a higher-drift high freq item.

For Picaxe use this is hardly relevant because the critical item will be the code timing, which I would think would be longer and more difficult to calibrate for than the few-ppm errors in the clock itself. Ovens are relevant for radio use, where the stability is needed for accurate demodulation, but for a stand-alone timer for modest intervals I would suggest that any decent AT-cut crystal properly driven would be more stable than you could need.

Perhaps it would help to define 'high' and 'low' frequencies for crystals. You can get AT-cut fundamental crystals into the VHF range and overtone crystals much higher than that. The highest crystal frequency that a Picaxe can use is 16MHz (due to the use of the pll for higher clock speeds). I would count all Picaxe crystals as low frequency from a stability perspective.
 
Last edited:

srnet

Senior Member
xtals are more accurate at lower frequencies. By a strange twist of fate you don't really multiply the inaccuracy, just the frequency
On a crude average they might be, but since crystals are often available in a range of tolerances, its a missleading guideline.

You can get a 16Mhz at 10ppm and a 4Mhz at 50ppm, so the higher frequency is more accurate.

All depends on what the datasheet says, and how much you want to pay.
 

Radarman2

Member
I think the xtals we use are higher tolerance then seeing as they only drift about 100Hz from 50MHz when used in the 300MHz range.
Haven't come across lower spec ones, so pardon the multi-quoted comment.
 

clockwork

Member
Dividing Xtal frequencies down will reduce error but depends on quality of Xtal and other factors. Also as Xtals dont work at very high frequencies then multipling frquencies up is probably the only economic way.

Fortunatly most projects dont require too high a level of timing accuracy. The only occasion that a frequency has to be extremely accurate is in something like transmitting and receiving some of the modern ultra narrow bandwidth transmission methods. Some of these have a signal band width of 6Hz. Even when transmitting or receiving at 30 Mhz this is very stringent specification. It shows you have good some of todays equipment really is.

Good luck with the project
 

hippy

Ex-Staff (retired)
To aid accuracy even further you could also include the use of a Xtal that is of much higher frequency than required and divide the output down.
I'm not convinced, so perhaps I'm missing something.

For example a 4MHz 4ppm crystal through division becomes 2MHz 2ppm, 1MHz 1ppm, but the tolerance / error in all cases is the same percentage +/-0.0001%
 
Last edited:

srnet

Senior Member
Dividing Xtal frequencies down will reduce error but depends on quality of Xtal and other factors
It wont.

Take a 16Mhz 20ppm crystal then divide by 4

and a

4Mhz 20ppm crystal direct drive.

The 'error' is 80Hz in both cases .......
 

hippy

Ex-Staff (retired)
Having thought further on it, having said these are equivalent, they are not ...

4MHz 4ppm = 4000000 +/-16
2MHz 2ppm = 2000000 +/-4
1MHz 1ppm = 1000000 +/-1

To keep the same percentage error it would seem to have to be ...

1000000 +/-1 = 1MHz 1ppm
2000000 +/-2 = 2MHz 1ppm
4000000 +/-4 = 4MHz 1ppm
 

Radarman2

Member
Attachment: Timer3 v SetTimer

As per my earlier post...I've created a pdf detailing the differences between Timer3 & SetTimer, comparing the two and showing what the settings give.

If there are any errors, can you point them out so I can correct and re-post.
Ta
 

Attachments

clockwork

Member
The theory of dividing frequencies down came from an article I read years ago. It appears to make sense but after reading the above posts it is obviously incorrect. Apologies to all for wasting their time.

So it appears the only ways to increase accuracy of a Xtal timing a Picaxe is to either leave the oscillator running for long periods in order to thermally stabilise or to use some form of Xtal Oven or thermal stabiliser circuit. This assumes the Xtal's tolerances are as small as possible. Letting the oscillator run for long periods plus a well stabilised power supply seems to be the most economical answer.
 

hippy

Ex-Staff (retired)
If one wants a timer accurate to 1ms, that's 1 part per 1000 or 1000 parts per 1000000, so any crystal of 1000 ppm or better is good enough for 1ms accuracy and even cheap crystals or resonators will be far better than that.

From that -

1000 ppm = 1ms accuracy
100 ppm = 100us accuracy
10 ppm = 10us accuracy
1 ppm = 1us accuracy

For timing applications, the main need for low ppm rating is for long term accuracy. A million ticks of a 1 ppm crystal will only be out by 1us every second - hardly noticeable - but will be a whole second out after a million times a million ticks.

There are 31536000 million ticks in a year so even 1ppm accuracy would be 31.5 seconds out at the end of the year.

It's probably not worth worrying about crystal accuracy for most PICAXE projects; there is more likelihood of greater inaccuracies from delays to starting and stopping any timer than the crystal would introduce.
 
Top