This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

gloable variable change by interrupt

I have a global int variable, and it will be change in ISR

because 8051 is a 8-bit MCU

I have a code
if(a == 0) it needs to check lowwer byte= 0 and higher byte =0

but interrupt a--;
so if a=0x100
lowwer byte =0
interrupt happen a = 0x0ff
higher byte = 0

It will have a wrong result

How ca I do?

Parents
  • The keyword here is "Atomicity" - it is well covered in The Literature

    An Atomic operation is one that cannot be interrupted.

    As you correctly observe, 16-bit operations are not Atomic on an 8051 - therefore you will need to disable the interrupt while you do the check.

    Or maybe you could have your ISR provide a separate a_is_zero flag (then, maybe, the value of 'a' need not be public?)

Reply
  • The keyword here is "Atomicity" - it is well covered in The Literature

    An Atomic operation is one that cannot be interrupted.

    As you correctly observe, 16-bit operations are not Atomic on an 8051 - therefore you will need to disable the interrupt while you do the check.

    Or maybe you could have your ISR provide a separate a_is_zero flag (then, maybe, the value of 'a' need not be public?)

Children