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.

CCS/TMS320F28377D: voltatile variable

Part Number: TMS320F28377D

Tool/software: Code Composer Studio

Hello community

I want to know if it is necessary to declare my variables as "voltatile " in my code.

I have some variables which are calculated inside the ISR (their value is changed there) and I want them to keep these values until next time that this ISR happens. 

I have declared them as float (e.g. float x=0;) in the top of my code (outside the ISR and main function)

Q1-- Is this way correct? or I should declare them as voltatile  or other declaration?

If I have some loops (for loops) or switch case inside the ISR, where should I declare their variable, i.e., for( n = 0; n<7; n++ ) where should I declare "n". I have decleard it as Uint16 n; roght at the top of "for loop" inside the ISR.

Q2--Is it correct too?

Regards

  • Lio,

    lio messi said:

    I have some variables which are calculated inside the ISR (their value is changed there) and I want them to keep these values until next time that this ISR happens. 

    I have declared them as float (e.g. float x=0;) in the top of my code (outside the ISR and main function)

    Q1-- Is this way correct? or I should declare them as voltatile  or other declaration?

    You should declare these variables as static, not volatile.  For example:

      static float x=0;

    lio messi said:

    If I have some loops (for loops) or switch case inside the ISR, where should I declare their variable, i.e., for( n = 0; n<7; n++ ) where should I declare "n". I have decleard it as Uint16 n; roght at the top of "for loop" inside the ISR.

    Q2--Is it correct too?

    Loop counters are fine being local variables in the ISR.  What you have done is one way to do this.

    Regards,

    David

  • Hello David

    Thanks for reply

    So I should declare many of my variables as "static"! because almost all of them are varied in the main ISR. for example I have used SDFM for reading the current in that ISR. then should I declare phase "A" current as " static float IA" in the top of my code?

    What happens if I don't use "static". 

    regards

  • Lio,

    lio messi said:

    So I should declare many of my variables as "static"! because almost all of them are varied in the main ISR. for example I have used SDFM for reading the current in that ISR. then should I declare phase "A" current as " static float IA" in the top of my code?

    What happens if I don't use "static". 

    To be clear, static is applied to local variables.  It makes them persistent, but not visible outside the scope of the function.  I think you can also apply static to a global variable, which would make them persistent but only visible to the functions in that source file.

    Static variables are allocated into the memory space on the processors (so they can be persistent).  Local variables (non-static) are on the stack.  If you declare everything as static, then you will consume a lot of extra memory (versus non-static locals which use stack space that gets de-allocated when the functions exits).

    If you don't use static on a local variable then its value is gone upon exit from the function.

    Regards,

    David

  • Thank you David
    Till now, I have used global variables (declared on the top of my code outside the main) for variable which their values is changed as used in an ISR which is executed periodically with a PWM. I have not used "static" for these variables. But their value doesn't change wrong and I think my code is working correct (i.e., they take the new value in ISR and keep it till next time this ISR is executed)
    Is this because I declared these variables as global?
    regards
  • Lio,

    Global vars and static vars are similar in that they are both persistent (retain their value), and have a permanent memory allocation.  Static vars are limited in scope however.  Static globals are only visible to code within the same source file, and static locals are only visible to the function in which they are declared.  This is standard C stuff.

    Global vars are one way to do what you want.  If you want to be more strict with your C code, then a static var that is visible only to the function(s) that need it is appropriate.

    Regards,

    David

  • Hello again
    I appreciate your reply.
    regards