Timer 1 Less accurate the Longer it runs

oracacle

Senior Member
I have been playing with the timer on a 20X2. Ideally I would need it to activate and deactivate from picaxe input.
I tried this
Code:
[color=Navy]#no_data
#no_table[/color]
[color=Blue]setfreq m8[/color]
[color=Navy]#terminal 9600[/color]
[color=Black]main:
      
      [/color][color=Blue]do while [/color][color=Purple]pinb.3 [/color][color=DarkCyan]= [/color][color=Navy]0
      [/color][color=Blue]loop
      settimer [/color][color=Navy]65521
      [/color][color=Blue]do while [/color][color=Purple]pinb.4 [/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=DarkCyan]and [/color][color=Purple]timer [/color][color=DarkCyan]< [/color][color=Navy]60000
      [/color][color=Blue]loop
      settimer off
      sertxd ([/color][color=Black]#[/color][color=Purple]timer[/color][color=Black],[/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Blue])
      let [/color][color=Purple]timer [/color][color=DarkCyan]= [/color][color=Navy]0
      [/color][color=Blue]do while [/color][color=Purple]pinb.3 [/color][color=DarkCyan]= [/color][color=Navy]1
      [/color][color=Blue]loop
      goto [/color][color=Black]main[/color]
I provided different time between each input using a 28X1 which has quite good accuracy according to my scope
I expected it to be around 2ms over with 1ms input from the 28X1 which is there or there about. I upped the time to 100ms and consistently got 86ms reading from the 20x2 timer, and when I tried 1000ms I got a reading of ~866ms. This can get to more than a second at 10 second time intervals.

I don't really want to use Hippy's high accuracy timer due to both hardware and software increases, and later a plan to try and use the same timer to look for a specific time interval.
I was hoping for a 0.5ms resolution but if accuracy is going to be better by dropping the resolution that will have to be done, but I cant go any lower than 1ms resolution
 

hippy

Technical Support
Staff member
Using -

Code:
Do
Loop While pinB.3 = 0
Will be slightly more responsive than -

Code:
Do While pinB.3 = 0
Loop
The first compiles as if -

Code:
WaitLoop:
  If pinB.3 = 0 Then WaitLoop
The second as -

Code:
WaitLoop:
  If pinB.3 <> 0 Then ExitLoop
  Goto WaitLoop
ExitLoop:
Also, the closer the number in 'SETTIMER number' gets to 65535 the less accurate the timer may get. After every overflow there is an internal increment, the 'timer' variable gets incremented and the timer get set to the preload number. The higher the number, the more often that happens, the more time is spent in internal interrupts rather then in letting the time increment -

Code:
Ticks     : ||||||||||||____||||||
                        ____
Overflow  : ___________|    |_____
                         _____
Interupt  : ____________|     |___

Restart   : ----------------|-----

Lost time :            |<-->|
 

oracacle

Senior Member
Well that makes sense I suppose.

I set the settimer to 65506 which should gives just 1ms per major tick. at 10 seconds it a little over 0.5seconds off with your suggested changes to the loops.
Think I may try I higher clock speed, hopefully the error will be reduced due to both smaller intervals on the timer and each command taking less time to complete

Edit: Just tried @64mhz settimer 65286 and I get a little over 0.5s off with a 56second signal from the 28x1. I did set it to 59000 but I guess there is some variance in its clock.
Looks like thing can get back on track for now.
 
Last edited:
Top