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.

Variables - a basic question

Hi

I have a strange behavior of my code depending on a place where my variables are defined.

Example 1: I have a variable called defined as: int conversion_count=0;

It is used to count each ADC conversion. If this variable is defined at top of my code outside the ADC ISR it is working fine, but if it is defined inside the ADC_ISR it is not working. Why?

Code example:

DATA_array[conversion_count] = AdcRegs.ADCRESULT0 >>4; 

if(conversion_count == 99)
    {
        conversion_count = 0;
    }
    else conversion_count++;

Example 2: a different situation is with another variable defined as: int average = 1;

It is used to collect a number of samples to calculate an average

When it is defined inside the ADC ISR everything works fine, but it it is defined outside, it doesn't work well. Why

Code example:

if (average == 3)                                // average of 3 samples
{
        DATA = (    DATA_array[conversion_count] +
                           DATA_array[conversion_count-1] +
                           DATA_array[conversion_count-2])/average;

}

else average++;

So to summarize two similar situations - one work if the variable is defined outside the ADC ISR, another works when the variable is defined inside the ADC ISR. Why?

Thanks

  • Hi Alex,


    I think the issue is probably with the scope of your variable.  If you declare it outside of main, the variable has global scope and the tool chain will allocate a fixed SRAM location for your variable.  This location won't ever change, so if you manipulate it within subsequent ISR calls the state will persist.


    When you declare a variable inside a function, the scope is local to the function.  The tool chain will allocate space on the stack when the function or ISR is called.  When the ISR ends, the stack space is reclaimed and the contents of your variable are lost.

  • Hi Devin,

    Thank you for your answer.

    Still why this explains the behavior that I see.

    When the conversion_count is declared inside the ISR and I have a toggle as below during debug the program is never stops there but it always stops there when the variable is declared outside the main() and equal to 99.

    if(conversion_count == 99)
        {
      "toggle"       conversion_count = 0;
        }
        else conversion_count++;

  • Alex,

    As suggested by Devin, when you define the variable inside the ISR then scope is only limited to that ISR during ISR execution. Once you come out of the ISR, the value is no more retained. I see you are incrementing the variable inside the ISR but if it's defined inside the ISR then that increment has no meaning. Every time code comes out of ISR current value the variable is lost and when ISR is called again, the variable will start over again hence count will never reach to 99. If you define it global variable then it's value will be retained. Hope this helps.

    Regards,

    Vivek Singh 

  • Hi Vivek
    Thanks for this, now I understand