是否可以“关闭”或“打开” C变量的波动性?

Wil*_*ult 2 c warnings volatile

我在C中有两个静态的volatile变量,我想在一个逻辑语句中检查它们两个。但是,当我收到警告时,“未定义行为:此语句1037中未定义易失性访问的顺序”是否可以在很短的时间内暂停C变量的波动性以确保获得良好的数据?

这是代码:

static volatile unsigned char b;
static volatile unsigned char a;

//update the states of the two volatile variables 
update_vars( &a);
update_vars( &b);

// check them in a logical statement
// Can I suspend the volatile lable??  
if((addr_bit & (a | b)) == 0){
// update another variables
}
else{
// another action
}
Run Code Online (Sandbox Code Playgroud)

我在相同的中断环境中考虑此问题,但是如果希望在准确的时刻对数据进行稳定的评估,则可以暂时将其挂起。谢谢!

dbu*_*ush 5

volatile不能禁用变量的属性。

您需要为每个文件创建一个非易失性副本,然后对其进行操作。

unsigned char a_stable = a;
unsigned char b_stable = b;

if((addr_bit & (a_stable | b_stable)) == 0){
    ...
Run Code Online (Sandbox Code Playgroud)