A volatile is an important qualifier in C programming. Here I am pointing some places where we need to use the volatile keyword.
Accessing the memory-mapped peripherals register or hardware status register.
#define COM_STATUS_BIT 0x00000006
uint32_t const volatile * const pStatusReg = (uint32_t*)0x00020000;
unit32_t GetRecvData()
{
unit32_t RecvData;
//Code to receive data
while (((*pStatusReg) & COM_STATUS_BIT) == 0)
{
// Wait until flag does not set
//Received data in RecvData
}
return RecvData;
}
Sharing the global variables or buffers between the multiple threads.
Accessing the global variables in an interrupt routine or signal handler.
volatile int giFlag = 0;
ISR(void)
{
giFlag = 1;
}
int main(void)
{
while (!giFlag)
{
//do some work
}
return 0;
}
Answer:
A volatile is an important qualifier in C programming. Here I am pointing some places where we need to use the volatile keyword.
- Sharing the global variables or buffers between the multiple threads.
- Accessing the global variables in an interrupt routine or signal handler.
need an explanation for this answer? contact us directly to get an explanation for this answer