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

Volatile keyword

IU have a variable which can be chyanged outside the interrupt but inside the ISR it is only read. Its value don't change in it. Should it be volatile.

Parents
  • The ISR always needs to read the variable - there are no cache somewhere for the processor to keep the value since previous interrupt.

    So you don't need "volatile" to make sure the ISR will pick up the current value. Unless you have nested interrupts that both uses the variable and the higher-prio interrupt may change the value.

    You do need "volatile" if the ISR changes a variable, to make sure the main loop will pick up the changes.

Reply
  • The ISR always needs to read the variable - there are no cache somewhere for the processor to keep the value since previous interrupt.

    So you don't need "volatile" to make sure the ISR will pick up the current value. Unless you have nested interrupts that both uses the variable and the higher-prio interrupt may change the value.

    You do need "volatile" if the ISR changes a variable, to make sure the main loop will pick up the changes.

Children