Joined: 28 Jun 2005 Posts: 200 Location: Ashburton / Mid Canterbury / New Zealand
Posted: Sun Mar 27, 2022 11:26 am Post subject: Clarification on toggling Bit Flags in an ISR
Hi
Just after a clarification on the Bascom Manual concerning flipping bits in an ISR...
Quote:
BITS and Interrupts
Bits are stored in bytes. A write to a bit/boolean variable is non-atomic. Which means that multiple operations are required to update the bit value in the byte. When interrupts are used that update bits in the same byte, you can have the effect that a change becomes lost.
To prevent this you can disable interrupts and enable them after you have updated the bit variable. Or you can use a byte instead which is recommended since it would use less code.
So don't use bit datatypes use bytes which will toggle between 0-255 with the toggle statement in the ISR... result expected 0 and 255 Correct??... basically 'Toggle' command bit flips the whole byte, as per the simulator
Code something like....
Posted: Sun Mar 27, 2022 12:33 pm Post subject: Re: Clarification on toggling Bit Flags in an ISR
snow wrote:
result expected 0 and 255 Correct??
Is this your only question?
Yes.
In case you do not access any bit residing in a byte-container outside an ISR, you can use bit-vars.
Within ISRs other interrupts are disabled by default (in case you do no change this).
To have full control where your bits are container'd:
Code:
Dim fc AsByte
f1 Alias fc.1
f2 Alias fc.2
f3 Alias fc.3
Making any access to the bits in question atomic, you can use bits in your main code and ISR.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum