PhilHornby
Senior Member
In another thread, there was the following conversation :-
Initially, I used a simple 'Toggle B.1' in a loop, measuring the achieved frequency on a 'scope vs different values of TIMER Preload. The effect seemed highly non-linear - ranging from some 950Hz to about 86Hz. This was rather hard to log, so I changed to incrementing a variable instead. I then used the TIMER itself, to calculate how many incrementing loops were executed per second.
Of course, using the TIMER to highlight short-comings with the TIMER could be problematic! I added the Interrupt Routine to double-check that TIMER overflow wasn't occurring and also compared some of the results with my (un-log-able) frequency measurements. I'm fairly confident that the measured results are correct.
At the end of each loop, the program outputs the SETTIMER Preload used, along with the 'TIME taken' to perform a 1000 loops. This data was eventually loaded into Excel to produce charts.
Two formulas were used (presented here for review).
The length of the TIMER tick is given by: = ((65536-PRELOAD) * 32 / 1000000) [for an 8MHz 20X2]
The number of COUNTER incrementing loops per second is given by =(1000/(TIMER value * TIMER Tick Time)) [where TIMER value is the value of TIMER at loop end, and TIMER Tick Time is calculated above]
This is the code I used :-
I discovered that you cannot use "0" as a SETTIMER Preload; it is interpreted as "OFF". I also discovered that values of 65532 or greater cause the Picaxe to lock-up. (Requiring a hardware RESET to load a new program).
I performed several runs, using different loop parameters to show the detail of what I found. At some values of PRELOAD, there is strange oscillation present. Initially I thought this was just integer arithmetic at play, but it's also there, to some degree, when using the Frequency-measuring method. I don't know what its significance is.
I present two graphs - the second being effectively, a zoomed-in version of the first. They are plots of "Counts per second" vs TIMER Preload. :-
From these results, it would seem unwise to use a counter Preload of <5mS on an 8MHz Picaxe 20X2
Me said:Is there a recommendation, as to whether to go for one long tick, or lots of shorter ticks? (i.e. 1 x 0.1Sec vs 10 x 0.01Sec or even 100 x 0.001Sec)
I was idly curious, as to the size of this overhead - so set about measuring it.hippy said:I would say one long tick would be better. T1S_8 would interrupt your PICAXE program once for a one second interval. Why have 10, 100 or more interrupts to achieve the same thing ? There's overhead for every tick but more so for more frequent ticks, and the accuracy probably decreases with shorter ticks. If there is 1us lost per tick that's almost nothing over a one second period with one tick, but a full millisecond if you had a 1,000 ticks in the same period.
Initially, I used a simple 'Toggle B.1' in a loop, measuring the achieved frequency on a 'scope vs different values of TIMER Preload. The effect seemed highly non-linear - ranging from some 950Hz to about 86Hz. This was rather hard to log, so I changed to incrementing a variable instead. I then used the TIMER itself, to calculate how many incrementing loops were executed per second.
Of course, using the TIMER to highlight short-comings with the TIMER could be problematic! I added the Interrupt Routine to double-check that TIMER overflow wasn't occurring and also compared some of the results with my (un-log-able) frequency measurements. I'm fairly confident that the measured results are correct.
At the end of each loop, the program outputs the SETTIMER Preload used, along with the 'TIME taken' to perform a 1000 loops. This data was eventually loaded into Excel to produce charts.
Two formulas were used (presented here for review).
The length of the TIMER tick is given by: = ((65536-PRELOAD) * 32 / 1000000) [for an 8MHz 20X2]
The number of COUNTER incrementing loops per second is given by =(1000/(TIMER value * TIMER Tick Time)) [where TIMER value is the value of TIMER at loop end, and TIMER Tick Time is calculated above]
This is the code I used :-
Rich (BB code):
#Picaxe 20X2
symbol Counter = W27
symbol N = W26
;Gosub Interrupt_Enable - only used to verify TIMER OVERFLOW wasn't occurring
for N = 65001 to 65531 step 2 ;VARIOUS VERSIONS WERE TRIED.
Counter = 0
TIMER = 0
settimer N
do while Counter < 1000
inc Counter
loop
settimer OFF
sertxd (cr,lf,#N,tab,#TIMER) ; Data for excel to process
next N
end
Interrupt:
SerTxd( "Timer Overflow " )
toggle B.1
Interrupt_Enable:
timer = 0
SetIntFlags $80, $80
toFlag = 0
Return
I performed several runs, using different loop parameters to show the detail of what I found. At some values of PRELOAD, there is strange oscillation present. Initially I thought this was just integer arithmetic at play, but it's also there, to some degree, when using the Frequency-measuring method. I don't know what its significance is.
I present two graphs - the second being effectively, a zoomed-in version of the first. They are plots of "Counts per second" vs TIMER Preload. :-
From these results, it would seem unwise to use a counter Preload of <5mS on an 8MHz Picaxe 20X2
Last edited: