This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problems with static and global variables

I'm just starting to learn the ins and outs of the Infineon C167CS-40M and have come across a roadblock in my programming. Any time I try to use global or static variables in my ISR, the data in the variables gets cleared out or corrupted somehow.

Am I not allowed to use static and global variables for ISRs? Is there some initialization code for it I'm missing? Or is there some other way to pass variables to the ISR from the main program and vice versa?

Any help is greatly appreciated.

  • Am I not allowed to use static and global variables for ISRs?

    There is no such limitation. Global and static variables are OK to use in ISRs and normal code.
    My guess is you get memory corruption for reasons unrelated to ISR. Could be memory manipulations with erroneously calculated pointers. Or too little memory allocated for stack, and it grows and wipes out variables. Or something else...

  • Ah, yes, thank you. I tried using static and global variables in my main program and the same problem occurred. So that rules out the ISR, at least.

  • Are your variables clobbered instantly or just now and then? If clobbered instantly - maybe you have an incorrect memory map so that the compiler places the variables where there are no memory. Basically putting down the coffe cup outside the table.

  • It's odd... if I define it like this:

    static char y = 'r';
    

    It is instantly cleared out. But I can define it afterward and it has the correct value. Like this works:

    static char y;
    y = 'r';
    

    But after they are defined, they get cleared out after 'if' statements; more specifically after 'if' statements that include the variable in the code to be executed but not in the condition statement.

    So this statement would corrupt y:

    char x = 't';
    static char y;
    y = 'r';
    if(x == 'p'){
    y = 'i';
    }
    

    But this statement would not corrupt y:

    static char y;
    y = 'r';
    if(y == 'p'){
    y = 'i';
    }
    

    And neither would this:

    char x = 't';
    static char y;
    y = 'r';
    if(y == 'p'){
    x = 'i';
    }
    

    Nor this:

    char x = 't';
    static char y;
    y = 'r';
    if(x == 'p'){
    x = 'i';
    }