Hello all,
I am trying to best understand use of volatile variables. To the best of my understanding, a variable should only be declared as volatile if its value will change outside the main program's flow (like in an ISR).
Let's assume we are reading from the UART by utilizing the UART_RX ISR and reading a keyboard input from the user.
Let's assume we have the following variables:
Value = the byte value in the UART_RX buffer register
Count = the number of bytes received
When the user presses a key, the program enters the UART_RX ISR, where count is incremented, and Value is changed to the user input.
From my understanding, since both variables are updated/changed inside the ISR, both of them should be declared as volatile. Is this true? Also, if Value were an array of characters, whose values would get incrementally updated each time the user types a key, this array should also be declared volatile.
Would there be another type of situation that a variable should be volatile? I have seen some posts on the forum mention the case of hardware registers (e.g. reading P1IN or writing P1OUT). But to my understanding, these are already declared in the header file (if using C), and in general one should not have to worry about it. If this is true, are volatiles used mainly used in conjunction with ISR's and other peripherals like the DMA module?
Thanks in advance.
Sincerely,
Mehdi