Counter not working in 16 bit

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

Post Reply
howard123
Posts: 75
Joined: Thu May 24, 2012 1:15 pm
Location: Cape Town
Has thanked: 30 times
Been thanked: 28 times
Contact:

Counter not working in 16 bit

Post 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
Attachments
counter test.fcfx
(61.43 KiB) Downloaded 180 times

User avatar
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

Post 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.

medelec35
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

Post 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.
Martin

howard123
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

Post by howard123 »

Thank you very much Ben and Martin

I will give this a go and let you know.

cheers Howard

howard123
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

Post 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.

medelec35
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

Post by medelec35 »

Hi Howard,
Glad the X<<8 worked for you.
Thank you for keeping us updated.
Martin

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: Counter not working in 16 bit

Post 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

Post Reply