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.

Parents Reply Children
  • 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';
    }