Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Clarification on toggling Bit Flags in an ISR

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
snow

Bascom Member



Joined: 28 Jun 2005
Posts: 200
Location: Ashburton / Mid Canterbury / New Zealand

newzealand.gif
PostPosted: Sun Mar 27, 2022 11:26 am    Post subject: Clarification on toggling Bit Flags in an ISR Reply with quote

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....
Code:


Dim TestFlag as Byte

'--------------Main-----------------------

Do
   
      Print "Test Flag";TestFlag
      Wait 1
Loop


'=====[ Tick timer ISR ]======================
TickTimer_isr:
   Toggle TestFlag
Return


 


Thanks Snow

(BASCOM-AVR version : 2.0.8.2 , Latest : 2.0.8.5 )
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sun Mar 27, 2022 12:33 pm    Post subject: Re: Clarification on toggling Bit Flags in an ISR Reply with quote

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 As Byte
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.

Code:
Dim TestFlag as Bit

Disable Interrupts
      Print "Test Flag";TestFlag
Enable Interrupts

TickTimer_isr:
   Toggle TestFlag
Return
Back to top
View user's profile
snow

Bascom Member



Joined: 28 Jun 2005
Posts: 200
Location: Ashburton / Mid Canterbury / New Zealand

newzealand.gif
PostPosted: Sun Mar 27, 2022 8:12 pm    Post subject: Reply with quote

Quote:

Is this your only question?


Yes it is. Thanks MWS for the conformation
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5916
Location: Holland

blank.gif
PostPosted: Sun Mar 27, 2022 8:36 pm    Post subject: Reply with quote

and have a look at DIM and the SAFE option.
_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
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