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
Counter not working in 16 bit
Moderator: Benj
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: Counter not working in 16 bit
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: Counter not working in 16 bit
If you do then I would recommend looking here.Benj wrote:Might be worth trying the latest XC8 to see if it's a compiler bug.
Can you also try
Code: Select all
IntValue = ByteLow+(ByteHigh << 8)
Code: Select all
IntValue = ByteLow+(ByteHigh * 256)
Martin
-
- Posts: 75
- Joined: Thu May 24, 2012 1:15 pm
- Location: Cape Town
- Has thanked: 30 times
- Been thanked: 28 times
- Contact:
Re: Counter not working in 16 bit
Thank you very much Ben and Martin
I will give this a go and let you know.
cheers Howard
I will give this a go and let you know.
cheers Howard
-
- Posts: 75
- Joined: Thu May 24, 2012 1:15 pm
- Location: Cape Town
- Has thanked: 30 times
- Been thanked: 28 times
- Contact:
Re: Counter not working in 16 bit
Hmmm yes X<<8 worked and X*256 does not work.
Thanks a lot Martin I would have never got this on my own.
Thanks a lot Martin I would have never got this on my own.
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: Counter not working in 16 bit
Hi Howard,
Glad the X<<8 worked for you.
Thank you for keeping us updated.
Glad the X<<8 worked for you.
Thank you for keeping us updated.
Martin
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: Counter not working in 16 bit
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
since a left shift (<<8) is the 'same' as multiplying by 256.
Type error possibly - using a byte value might cause problems somewhere?
Martin