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

How can I use a global variable in the Interrupt routine?

The variable is defined in *.h:

static unsigned char cPwLe[3] = {0};

Its value is changed in the Main():
cPwLe[cCh] = Power_Level(cP[cCh]);  //Here  value is right

Its value is transfered to T0 Interrutpt service routine:
cPower = cPwLe[cCh]; //Here value is  always 0

How can I correct the errors?
Thanks all!

Parents
  • Can you tell why?
    Just a MOD caculating result in the mistake:

    if (cC%(cPower+1)==0)
    ...
    
    When be corrected to if (){} and add statement,that variable work well.


    To my surprise,
    In the same routine,another global variable cSV which value be changed in UART_INT Service routine,can be used in MOD caculating orderly:
    if (cC%(cSV+1)==0)
    ...
    

    Thanks everyone!

Reply
  • Can you tell why?
    Just a MOD caculating result in the mistake:

    if (cC%(cPower+1)==0)
    ...
    
    When be corrected to if (){} and add statement,that variable work well.


    To my surprise,
    In the same routine,another global variable cSV which value be changed in UART_INT Service routine,can be used in MOD caculating orderly:
    if (cC%(cSV+1)==0)
    ...
    

    Thanks everyone!

Children
  • Can you tell why?

    No. Nor can anybody else, from what you wrote. You're not giving anywhere nearly enough information for that.

    Just a MOD caculating result in the mistake:

    In what mistake?

    When be corrected to if (){} and add statement

    There's nothing to be corrected about what you wrote --- ripped completely out of context as it is, it's impossible for anyone not sitting on your chair to find out what "correct" might possibly mean, for this snippet of code.

  • Study the meaning and use of the volatile keyword. Code generated for the main routine might not use the latest value of a variable updated by an ISR unless that variable is declared volatile.