Problems using timer/count input in count mode

Dr_John

Member
I'm trying to use the "counter/timer" of the picaxe 28x2
to count pulses on pin C.0 while my program is busy doing other things.
I was getting more counts than expected, and assumed that there might
be "contact bounce" as the input is derived from an analogue source.

I therefore decided to test the mechanism using an output pin on the same chip to drive the counter input.

Circuit:
a completely bare AXE200 surface mount 28x2 module with -
pins 27,28 connected to a 9v PP3 power source. with 1000uF capacitor.
pin 5 (C.0) connected to pin6 (C.1)

That's it!




Program:

let dirsc = %00000010
SETTIMER COUNT 65535

do
pause 500
inc b2
toggle c.1 ' provide pulse to counter input
b0=pinsc ' report value for debug
b1=timer ' report value for debug
debug
loop

Results: using the above program, b1 (timer) is reported to increase by two every time round the loop.
B2 is alternately one or two more than b1. B0 reads alternately 3, and 0

I conclude that the timer circuit is counting 2 for every positive transition.


Further tests:
I changed the preload value from 65535 to 65534, and the program behaved as expected.
B1 increases by 1 every other loop. B1 is approximately half the value of b2.

With a preload of 65533, B1 counted every 3 times round the loop.

I want it to count up by one every time round the loop.
How do I make it do this?

John Fisher
 

hippy

Technical Support
Staff member
We would need to investigate the issue. As a workround, for a count that is always incrementing by 2, it should be possible to simply divide the 'timer' value by two to get the actual count.
 

Dr_John

Member
We would need to investigate the issue. As a workround, for a count that is always incrementing by 2, it should be possible to simply divide the 'timer' value by two to get the actual count.
Thanks Hippy.
I would really like to know if you can reproduce this. - the circuit is simple enough!
Firmware version B.3
(PICAXE-28X2 (40X2)b Firmware version 3)

If that helps

I'll use the workround in the meantime.
 

Dr_John

Member
**I have mistakenly misled you - sorry.**

B1 increases by 2 every other time round the loop.
(it's still not what is expected - it should increase by 1 every other loop)

it still needs investigating however.
I will try other sources for the clean square waveform.

John F
 

StigOfTheDump

Senior Member
I have reproduced it on an AXE200 (vB.2). It would be interesting if somebody could try it on an AXE201. I split your toggle up with high and low with sertxd before, between and after. It does appear to count 2 after the high (but misses the first one?) I've never tried this before but would have expected the count to overflow at the first high or even after it went low again, but it seems to wait for the 2nd high.

What spec was your original program chip?

EDIT: I also added the second ground on pin 4. I know the chips like to have both, but I don't know if this applies to the modules. It made no difference.
 
Last edited:

StigOfTheDump

Senior Member
Sorry, I misunderstood. I had thought you were using a 28X2 chip in your circuit then used a separate 28X2 module for the test.
 

Buzby

Senior Member
I've just noticed you are using 'debug' to view the results of your tests.

See http://www.picaxe.com/BASIC-Commands/Advanced-PICAXE-Configuration/settimer/

It says :

Note that the debug command temporarily disables the timer (during the actual variables transmission). Therefore use of the debug command at the same time as the timer will cause false readings.
I don't know what effect debug has when the timer is running as a counter, so I'd use sertxd to get the results instead. ( Or even better, hserout. )
 

Dr_John

Member
I've just noticed you are using 'debug' to view the results of your tests.

See http://www.picaxe.com/BASIC-Commands/Advanced-PICAXE-Configuration/settimer/

It says :



I don't know what effect debug has when the timer is running as a counter, so I'd use sertxd to get the results instead. ( Or even better, hserout. )
New Program:
Code:
let dirsc = %00000010
SETTIMER COUNT 65535

do
pause 500
inc b2
toggle c.1
b0=pinsc
b1=timer
pause 100

sertxd("b1=",#b1," b2=",#b2,13,10)

loop
results:
b1=0 b2=1
b1=0 b2=2
b1=2 b2=3
b1=2 b2=4
b1=4 b2=5
b1=4 b2=6

No improvement
Thanks for trying!
I'll need to look at the hardware to use hserout - it comes from a different pin.

Further test exonerates debug:
Code:
#no_data
#no_table
let dirsc = %00000010
SETTIMER COUNT 65535


pause 500

b0=timer
high c.1
pause 50
low c.1
pause 100

b1=timer
high c.1
pause 50
low c.1
pause 100

b2=timer
high c.1
pause 50
low c.1
pause 100

b3=timer
high c.1
pause 50
low c.1
pause 100

b4=timer
high c.1
pause 50
low c.1
pause 100

b5=timer
high c.1
pause 50
low c.1
pause 100


debug
stop
This gives values: 0,0,2,4,6,8 for the timer
 
Last edited:

Buzby

Senior Member
I still can't get to my hardware, but looking at your latest code I would say it's doing exactly what I would expect !.

Variable b2 increments every pass through the loop, and the toggled pin will alternate Hi/Low on alternate loops.

This means the counter only sees a rising edge every other loop, so its value will be half of b2.

Replace toggle with seperate High Low commands, and see what happens then.
 

StigOfTheDump

Senior Member
This is where I got to last night with an AXE200 vB.2. Seems to be just the same as yours.

Code:
[color=Navy]#terminal 9600[/color]
[color=Blue]pause [/color][color=Navy]5000[/color]
[color=Blue]let [/color][color=Purple]dirsc [/color][color=DarkCyan]= [/color][color=Navy]%00000010[/color]
[color=Blue]SETTIMER COUNT [/color][color=Navy]65535[/color]

[color=Blue]do
pause [/color][color=Navy]500[/color]

[color=Blue]inc [/color][color=Purple]w2
w1[/color][color=DarkCyan]=[/color][color=Purple]timer
w0[/color][color=DarkCyan]=[/color][color=Purple]pinsc[/color]
[color=Blue]sertxd ([/color][color=Red]"After inc  w2="[/color][color=Black],#[/color][color=Purple]w2[/color][color=Black],[/color][color=Red]" w1="[/color][color=Black],#[/color][color=Purple]w1[/color][color=Black],[/color][color=Red]" w0="[/color][color=Black],#[/color][color=Purple]w0[/color][color=Black],[/color][color=Blue]cr[/color][color=Black],[/color][color=Blue]lf)

high c.1[/color]
[color=Purple]w1[/color][color=DarkCyan]=[/color][color=Purple]timer
w0[/color][color=DarkCyan]=[/color][color=Purple]pinsc[/color]
[color=Blue]sertxd ([/color][color=Red]"After high w2="[/color][color=Black],#[/color][color=Purple]w2[/color][color=Black],[/color][color=Red]" w1="[/color][color=Black],#[/color][color=Purple]w1[/color][color=Black],[/color][color=Red]" w0="[/color][color=Black],#[/color][color=Purple]w0[/color][color=Black],[/color][color=Blue]cr[/color][color=Black],[/color][color=Blue]lf)

low c.1 [/color][color=Green]' provide pulse to counter input[/color]
[color=Purple]w0[/color][color=DarkCyan]=[/color][color=Purple]pinsc [/color][color=Green]' report value for debug[/color]
[color=Purple]w1[/color][color=DarkCyan]=[/color][color=Purple]timer [/color][color=Green]' report value for debug[/color]
[color=Blue]sertxd ([/color][color=Red]"After low  w2="[/color][color=Black],#[/color][color=Purple]w2[/color][color=Black],[/color][color=Red]" w1="[/color][color=Black],#[/color][color=Purple]w1[/color][color=Black],[/color][color=Red]" w0="[/color][color=Black],#[/color][color=Purple]w0[/color][color=Black],[/color][color=Blue]cr[/color][color=Black],[/color][color=Blue]lf[/color][color=Black],[/color][color=Blue]cr[/color][color=Black],[/color][color=Blue]lf)

loop[/color]
Serial Terminal.jpg
 

hippy

Technical Support
Staff member
We have observed anomalous behaviour using "SetTimer Count 65535" but behaviour was as expected when using a preload value of less than 65535. We cannot immediately explain this and it will need to be investigated further.
 

Goeytex

Senior Member
@hippy

While you are at it, try using settimer on a 20X2 with a preload value >= 65532. This will hang the program and further programming will require a hard reset.
 

hippy

Technical Support
Staff member
While you are at it, try using settimer on a 20X2 with a preload value >= 65532. This will hang the program and further programming will require a hard reset.
My guess would be that it is generating internal clock overflow interrupts at a faster rate than the firmware can service them. Increasing the operating speed and reducing the preload value would probably be the solution for any practical application.
 

Buzby

Senior Member
My guess would be that it is generating internal clock overflow interrupts at a faster rate than the firmware can service them. Increasing the operating speed and reducing the preload value would probably be the solution for any practical application.
Hi hippy,

Will that idea work ?.

If the clock is running 4 times faster, the firmware will be running 4 times faster, but the counter will be 4 times faster as well.

Won't the net result be the same ?

Cheers,

Buzby
 

Goeytex

Senior Member
My guess was the same as hippy's as I discovered this well over 2 years ago and have casually mentioned this in several posts ( but no official response). So after a lot of testing I gave up on using settimer (internal counter) with a preload value > 65531.

Increasing the processor speed alone is little help. However, hippy's idea also included reducing the preload value. So it works and is the only practical work around.

I might suggest at least a footnote in the manual that warns of this limitation. I suspect that the 28X2 and 40X2 will behave in a similar manner but I have not tested these.
 

Technical

Technical Support
Staff member
The PICAXE firmware does not change the way in which it processes with whatever the preload value is set to (e.g. 65535 or 65530 or anything else).

Therefore in theory the value 65535 should work just as well as the external count preload value on a 28X2/40X2 as any other value.

However we strongly suspect this reported issue with the value 65535 is a by-product of the recently disclosed Microchip silicon issue, which you can see listed as point 11 in this datasheet if you are interested:
http://ww1.microchip.com/downloads/en/DeviceDoc/80000498G.pdf

Kindly note that this errata 11 update was only published less than a month ago (30th July 2014), so is an extremely recent addition, hence we could not have known about this issue in the past. If an incorrect false timer interrupt fires you will indeed get addtional extra false 'counts'.

Unfortunately silicon issues on existing parts are simply beyond our control, so there is little we can do apart from developing a workaround for any future firmware update. We will also look at updating the documentation to mention this limitation.

Thanks for your understanding on this.
 

Goeytex

Senior Member
This errata sheet linked above applies to PIC18F25/45K22, (Picaxe 28X2 & 40X2). #11 only applies to Asynchronous External Input mode.

So just to clarify, this does not apply to the issue I brought up with 20X2 and settimer with preload > 65531.
 

Technical

Technical Support
Staff member
This errata sheet linked above applies to PIC18F25/45K22, (Picaxe 28X2 & 40X2). #11 only applies to Asynchronous External Input mode.
So just to clarify, this does not apply to the issue I brought up with 20X2 and settimer with preload > 65531.
No, hippy has already answered that one - it is a completely different situation to the external pulse count. Quite simply if you fire so many internal timer interrupts so quickly together there won't be any time left to do anything else. This is not a bug, just a system processing limitation.
 
Top