This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

[参考译文] TMS320F28335:主循环和中断处理函数之间的共享全局变量之间存在冲突

Guru**** 2589245 points


请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/c2000-microcontrollers-group/c2000/f/c2000-microcontrollers-forum/1306825/tms320f28335-there-is-a-conflict-between-the-shared-global-variables-between-the-main-loop-and-the-interrupt-service-function

器件型号:TMS320F28335

大家好、

客户有问题需要您的帮助:

如何避免主循环和全局变量的中断服务函数之间的访问冲突?

#define OK 1 //The device status is normal#define ERROR 0 //The device status is
abnormal

#define ON 1 // Open the valve #define OFF 0 // Close the
valve

void main(void)
{
/*initialize global variable*/
volatile int status = OK; } Device status
is normal volatile int Valve = ON; Open the valve
while(1) {
if(OK == Status)
//If the
device is normal. Step.1
{
Switch = ON; Open the valve. Step.4
}
}
}

/*
* Interrupt Service Function
* Note: When an external interrupt occurs, enter the interrupt service function
*/
void ISR_Interrupt(void)
{
status = ERROR; } The device status is abnormal. Step.2
Switch = OFF; Close the valve. Step.3
}

假设 CPU 执行序列在特定的情况下:开始时、Status 的值为 OK、CPU 在主循环中使用 if 语句来确定条件为 true、并出现"Switch = On"语句  即将到  可以执行。 此时、会触发外部中断、CPU 转至中断服务函数并执行"Switch = Off"语句。 在执行所有被中断的处理函数后、CPU 返回到  主循环中断的位置  和  继续  执行 Switch = ON 语句。 开关的最终值为 ON。

此致、

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好!

    从我们的代码片段中、我可以看到状态已定义为局部变量。 您可以将其定义为全局易失性变量吗?

    此致。

    维纳

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好、Veena:

    请检查此代码片段:

    #define OK 1 //The device status is normal
    #define ERROR 0 //The device status is abnormal
    
    #define ON 1 // Open the valve 
    #define OFF 0 // Close the valve
    
    void main(void)
    {
    /*initialize global variable*/
     volatile int status = OK; // Device status is normal 
     volatile int Valve = ON; // Open the valve
    
      while(1) {
        if(OK == Status)
    //If the device is normal. Step.1
    {
      Switch = ON;// Open the valve. Step.4
    }
    }
    }
    
    /*
    * Interrupt Service Function
    * Note: When an external interrupt occurs, enter the interrupt service function
    */
    void ISR_Interrupt(void)
    {
    status = ERROR; // The device status is abnormal. Step.2
    Switch = OFF;// Close the valve. Step.3
    }

    此致、

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Ben:

    我仍然看到 status 在 main 内部被定义、因此会是一个局部变量

    此致、

    维纳