X
As the ISR starts, it will save some or all internal registers somewhere - typically on the stack. It is then free to use those internal registers in the ISR to execute your code. When that is done it will restore the saved values of the internal registers - typically copy them back from the stack.
Now when an ISR is about to run ...#
* the compiler knows the ISR uses R1, R5, R17, R18 (just as example)
* but these registers currently contain important values for operations performed in main(), they must have the same values when the ISR is finished for the main() to work correctly.
* so before performing operations in ISR it needs to "save" the main()_contents of R1, R5, R17, R18
* now the ISR can change the content of R1, R5...... to perform ISR operations
* after the ISR has finished it´s job it needs to restor the (old) values for the main loop.
label:
move the value of variable 'm' to internal register A
if A != 1 then go to done
go to label
done:
The reason you're seeing a strange difference is because, due to optimizations (and this is pure speculation), the A in main isn't the same A as in the ISR internally anymore.
No there are not two different variables "A".So like two different "A" which is a variable name now and not register name.
No there are not two different variables "A".
But there is the "A" in SRAM (the real variable) and main() copies the content into a register.
While the ISR correctly updates the variable in SRAM...
the main misses to update it´s copy in the register (if not set to "volatile") and works with the mature value of "A" in the register.
Back to C:
I´m not sure what the debugger shows. I guess you can see the value of variables.
But are you able to see the contents of the raw microcontroller_registers?
int A = 0;
int main()
{
while(A != 1)
{
//Do something
}
}
void ISR()
{
A++; <------ breakpoint
}
int A = 0;
int main()
{
while(A != 1) <------ breakpoint
{
//Do something
}
}
void ISR()
{
A++;
}
seems you don´t see the register at all. (in the debugger)
Register is machine level (assembler)
C is not at register level.
I try to find an example:
Let´s say in a restaurant you ask for coffee.
The waiter comes with a cup and a pot. He pours the coffe from the pot into your cup.
Now you complain that the coffee is too cold.
The waiter puts the pot on the oven to make it hot.
Still the coffee in your cup stays cold. ;-)
Same with me.I don't know if what I say is understandable,
But I think it´s clear now that a "volatile" statement makes sense.
Heavy discussions, today.
I don´t have experience with the debugger. So I can´t say whether the debugger shows the SRAM value or the register value.
one problem in explaining these things is that there are many different microcontrollers.
The one works this way, the other works differently.
The one saves registers in stack, the other in SRAM, the other does a complete register set swap.
And there are different people, with different ways to learn things. I´m the one who learns with "pictures". I`m not good in learning text. But as soon as I can transform a problem into a picture, it´s burnt in. (this does not mean that I don´t forget or don´t make mistakes. sadly).
Others find "pictures" like the cup of coffee annoying.
I´m not sure, but a think there is a misunderstanding.Although there is another problem. Let's say this 1s passed, then the main loop sees this flag is on then the function of updating the screen is being executed so some time passes and it is way past 1s and is propagating more and more times the screen refreshes to the point where the delay is not 1 s but it can be 1 minute.
Another thing is what if I have more stuff to do inside main loop ? The time delay might be even bigger.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?