Timer not giving what was expected

oracacle

Senior Member
I am running this code on 28x2, the timer count comes out at either 230 or 232.

Code:
[color=Navy]#terminal 9600[/color]
[color=Blue]setfreq m8
pause [/color][color=Navy]1000[/color]
[color=Blue]let [/color][color=Purple]dirsb [/color][color=DarkCyan]= [/color][color=Navy]%11111111[/color]
[color=Blue]let [/color][color=Purple]pinsb [/color][color=DarkCyan]= [/color][color=Navy]%00000000[/color]
[color=Black]init:
      [/color][color=Blue]pwmout B.0[/color][color=Black], [/color][color=Navy]199[/color][color=Black], [/color][color=Navy]399 [/color][color=Green]; 10000Hz at 50% @ 8MHz
      [/color][color=Blue]settimer count [/color][color=Navy]65535[/color]
[color=Black]main:
      [/color][color=Blue]if [/color][color=Purple]pinc.1 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]then                        [/color][color=Green];check for user input
            [/color][color=Blue]pulsout b.7[/color][color=Black], [/color][color=Navy]1000                   [/color][color=Green];hold and gate input high for 0.005 second
            [/color][color=Blue]pause [/color][color=Navy]1000
            [/color][color=Blue]let [/color][color=Purple]w0 [/color][color=DarkCyan]= [/color][color=Purple]timer                      [/color][color=Green];put timer value in w0
            [/color][color=Blue]bintoascii [/color][color=Purple]w0[/color][color=Black], [/color][color=Purple]b2[/color][color=Black],[/color][color=Purple]b3[/color][color=Black],[/color][color=Purple]b4[/color][color=Black],[/color][color=Purple]b5[/color][color=Black],[/color][color=Purple]b6       [/color][color=Green];convert to ascii
            [/color][color=Blue]sertxd ([/color][color=Black]#[/color][color=Purple]w0[/color][color=Black], [/color][color=Navy]13[/color][color=Black], [/color][color=Navy]10[/color][color=Blue])                [/color][color=Green];send timer value as seen by picaxe
            [/color][color=Blue]sertxd ([/color][color=Purple]b2[/color][color=Black],[/color][color=Purple]b3[/color][color=Black],[/color][color=Purple]b4[/color][color=Black],[/color][color=Purple]b5[/color][color=Black],[/color][color=Purple]b6[/color][color=Black],[/color][color=Navy]13[/color][color=Black],[/color][color=Navy]10[/color][color=Blue])       [/color][color=Green];send timer value as ascii
            [/color][color=Blue]let [/color][color=Purple]timer [/color][color=DarkCyan]= [/color][color=Navy]0                       [/color][color=Green];reset timer
            [/color][color=Blue]pause [/color][color=Navy]500                           [/color][color=Green];large debouce
      [/color][color=Blue]end if
      goto [/color][color=Black]main[/color]
the pwm and the pulsout and b.7 run into a nand, the output of that runs into another nand set as an inverter. when c.1 goes high (PTM connected here, pin tied low) b.7 goes high meaning that the signal will run through to the inverting part of the circuit.

The pulsout should give 0.005 seconds (5us * 1000). the PWM is set to 10khz, so each clock cycle should give 0.0001 seconds. Unless I have something wrong (which is highly possible) I would have expected 50 in the timer variable with the preset on timer of 65535.

I have noticed that going over 65531 does seem to make things go a little strange, in some cases possibly due to rounding or alike, 65532 produces 13 - 0.005/0.0004 = 12.5. however 65533 produces 19 with a calculated 16.66.... 65532 produced 22, expected 25 and 65535 produced 230.
I will check the numbers in a more rigorous manor in the next couple of days, but this does seem like an odd quirk that other may need to know about.
Its not something that cant be designed around, and for most applications that level of resolution most likely wont be needed.
 

Buzby

Senior Member

hippy

Technical Support
Staff member
I haven't thought too deeply about this issue but hardware counter overflows are what increment the 'timer' variable. If the PICAXE is executing the PULSOUT then it shouldn't be handling those overflows, so the timer shouldn't increment, except maybe at the end when a previously set overflow flag is set as seen.

But perhaps PULSOUT handles some interrupts, which skews the count, and also extends the PULSOUT time which skews it further. There's a likelihood that PULSOUT won't give exactly the time period specified anyway. And at high clocking rates the counters run faster than the PICAXE itself can keep up which adds further skewing.

I would slap a scope on the gating pulse to see what that is showing.

And on the counter input just to check it isn't being clocked more than you think it is.

It may be worth using another PICAXE to generate the gating pulse, have the PICAXE set timer to zero, wait for it be non-zero, then wait for it to not change subsequently, then output the result. Start with longer gating pulses and slower PWM and then decrease pulses, increase PWM.
 

oracacle

Senior Member
Finally gotten around to looking at this, I set a 08m2 to generate the PWM and the gating pulse, the results seem to be just as erratic.
Used a 1khz PWM and 2000 pulsout from the 08m2. Some results showed variance from 2 up past 180.
This is a bit of a side project, that can be done in other ways so its not a major issue for what I need, wasn't sure if it had been come across before
 
Top