Global variabel vs interrupt???? Somebody help me...

Hi, everyone…

After I succeed with my first interrupt program, I want develope that with the global variabel. Does everyone know how to make a global variabel works in main program? The global variabel must been updated in the interrupt routine. I make some program, but the global variabel that I’ve made, didn’t show updated in the main program.

here is my example program:

int a; /* a is a global variabel*/

int main (void)

{

while (1)

{

IOPIN = a;

}

}

void IRQ_Routine(void)

{

a–;

if(a==0x0)

a = 0xFFFE;

T1IR = 0x01;

VICVectAddr = 0xff;

}

from the example, the global variabel will be updated every time the interrupt occurs. Then, “a” will be shown (using IOPIN). But, what is happening is not like that. The “a” never been updated…

Did everyone can help me to solve my problem???

regards,

Hesanj

You need to use the keyword “volatile” when defining variable a.

The problem that you are running into is that your compiler is being too smart. When it is optimizing your while loop, it thinks that there is no way variable a can change, so it does not re-read variable a on every pass through the loop.

When you define a variable as volatile, you are explicitly telling the compiler that this variable may change at any time, so never optimize out a read.

  • Nathan

Thx a lot Nathan! I will try this one…

regards,

Hesanj