Page 1 of 1
Counter not working in 16 bit
Posted: Sun Apr 05, 2020 10:16 am
by howard123
Hello all
I am not able to get my T0 counter to work in 16 bit mode, using an external pin. It does count 8 bit values without a problem but for anything higher it returns unexpected numbers.
At times it seems as if TMR0H=0; is being ignored in the PulseCounter macro. I have implemented the TMR0L and TMR0H read and write sequences recommended from the data sheet.
The T1 and T5 counters work as expected in 16 bit mode.
Any ideas?
Thanks Howard
18F46K22 tested on hardware
Re: Counter not working in 16 bit
Posted: Sun Apr 05, 2020 3:23 pm
by Benj
Hi Howard,
Hmm your code looks ok to me, that's a strange one. Might be worth trying the latest XC8 to see if it's a compiler bug.
There is this in the datasheet but your code looks correct to be accounting for this.
TMR0H is not the actual high byte of Timer0 in 16-bit
mode; it is actually a buffered version of the real high
byte of Timer0 which is neither directly readable nor
writable (refer to Figure 11-2). TMR0H is updated with
the contents of the high byte of Timer0 during a read of
TMR0L. This provides the ability to read all 16 bits of
Timer0 without the need to verify that the read of the
high and low byte were valid. Invalid reads could
otherwise occur due to a rollover between successive
reads of the high and low byte.
Re: Counter not working in 16 bit
Posted: Sun Apr 05, 2020 6:23 pm
by medelec35
Benj wrote:Might be worth trying the latest XC8 to see if it's a compiler bug.
If you do then I would recommend looking
here.
Can you also try
Code: Select all
IntValue = ByteLow+(ByteHigh << 8)
as If i remember correctly I have has issues with
Code: Select all
IntValue = ByteLow+(ByteHigh * 256)
even though they are the same in theory.
Re: Counter not working in 16 bit
Posted: Mon Apr 06, 2020 9:19 am
by howard123
Thank you very much Ben and Martin
I will give this a go and let you know.
cheers Howard
Re: Counter not working in 16 bit
Posted: Mon Apr 06, 2020 3:45 pm
by howard123
Hmmm yes X<<8 worked and X*256 does not work.
Thanks a lot Martin I would have never got this on my own.
Re: Counter not working in 16 bit
Posted: Mon Apr 06, 2020 4:02 pm
by medelec35
Hi Howard,
Glad the X<<8 worked for you.
Thank you for keeping us updated.
Re: Counter not working in 16 bit
Posted: Mon Apr 06, 2020 4:48 pm
by mnf
Something odd though -
since a left shift (<<8) is the 'same' as multiplying by 256.
Type error possibly - using a byte value might cause problems somewhere?
Martin