How to deal with flag variables in embedded system -
i doing project on embedded system changing flag variable 'x_flag' upon interrupt. flag variable global , accessed in other source files check state of condition of events too.
i know troubles dealing global variables, ask how can access current state of 'x_flag' in other source files?
in c-file can define like
volatile int x_flag; void myisrfunction(void) { x_flag = 1; }
and in header file add declaration
extern volatile int x_flag;
then need include header file other c-file able access x_flag
but there should disable interrupt when accessing/modifing 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 if flag hardware register, may need more careful, disabling interrupts not stop hardware altering value of flag , clearing explicitly may or may not allowed. in case, you'll need @ hardware documentation find out , not safe (and/or talk whoever designing it, if you're working hardware engineers).
Comments
Post a Comment