Forum Discussion
Altera_Forum
Honored Contributor
19 years ago --- Quote Start --- originally posted by heavenscape@Aug 26 2006, 04:35 AM ... i am not quite sure in what cases should i use this keyword. --- Quote End --- heavenscape, a volatile variable can change its value by intervention of an event. For example, if you write a wait function, that waits until a port bit is going low:
while ( *((unsigned short *)PORT_BASE) & 0x0001 )
{
; // wait until not set
} Optimized code reads from PORT_BASE only once and then checks bit 0, which will never change. You have to declare this value to be volatile in order to force the software always to reread the value before checking it: while ( *((volatile unsigned short *)PORT_BASE) & 0x0001 )
{
; // wait until not set
} If you use a variable that could be changed while it is being processed (e.g. by an interrupt handler), this variable also is volatile, it must always be reread before checked. Mike