Q:

How to deal with flag variables in embedded system

I am doing a project on embedded system where I am changing a flag variable 'x_flag' upon interrupt. This flag variable is global and accessed in other source files to check the state of condition of events too.
I know the troubles dealing with global variables, but I would like to ask you about how can I access the current state of 'x_flag' in other source files?

A:

The correct way to expose variables is to make a GetXFlagState() function in the class which owns it. That way the owner class remains in control.

A:

In your c-file you can define it like
volatile int x_flag;

void myISRFunction(void)
{
    x_flag = 1;
}

And in your header file you add a declaration
extern volatile int x_flag;

Then you only need to include the header file from the other c-file to be able to access x_flag
But there you should also disable the interrupt when accessing/modifing the flag.
#include "myISR.h"

void someFunc()
{
  int local_x_flag;

  disableInterrupts();
  local_x_flag = x_flag;
  x_flag = 0;
  enableInterrupts();

  if ( local_x_flag )
    doSomething();
}

Note that if the flag is really a hardware register, you may need to be more careful, as disabling interrupts will not stop the hardware from altering the value of the flag and clearing it explicitly may or may not be allowed.  In that case, you'll need to look carefully at the hardware documentation to find out what is and is not safe (and/or talk to whoever is designing it, if you're working with hardware engineers).

