Q:

The Proper place to use the volatile keyword?

0

The Proper place to use the volatile keyword?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

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;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Embedded C interview questions and answers (2022)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What is ISR?... >>
<< Can we have a volatile pointer?...