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: There is a conflict between the shared global variables between the main loop and the interrupt service function

Part Number: TMS320F28335

Hi Team,

There's an issue from the customer need your help:

How can I avoid the access conflict between the main loop and the interrupt service function to global variables?

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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
}
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Suppose that the CPU execution sequence is in a certain scenario: at the beginning, the value of Status is OK, and the CPU uses the if statement in the main loop to determine that the condition is true, and the "Switch = ON" statement is about to be executed. At this point, an external interrupt is triggered, and the CPU goes to the interrupt service function and executes the "Switch = OFF" statement. After all interrupted service functions are executed, the CPU returns to the place where the main loop was interrupted and continues to execute the Switch = ON statement. The final value of the switch is ON.

Best Regards,

Ben

  • Hi,

    From our code snippet, I see the status is defined as a local variable. Can you define that as a global volatile variable?

    Regards,.

    Veena

  • Hi Veena,

    Please check this code snippet:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    #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
    }
    }
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Best Regards,

    Ben

  • Hi Ben,

    I still see that the status is defined inside main and hence would be a local variable

    Regards,

    Veena