DSPRelated.com
Forums

A very strange problem

Started by bing...@meidensg.com.sg June 9, 2008
HI,

I found a very strange problem. MY code as following. Running in debug mode, I found even variable A become more than 50(by watch window), the code is still running in the while loop, and can not run to the code after while loop.

Anybody can help me, I am mad already.

unsigned int A;//global variable
void main(void)

{
while(A<50)
{
}

//other code here
}

interrupt void XInt2_ISR(void)//a external interrupt
{
A++:
}


OMAP35x EVM jump-starts low-power apps
The modular and extensible OMAP35x Evaluation Module (EVM) enables developers to start building applications based on the OMAP35x architecture: http://www.DSPRelated.com/omap35x
HI

I think it is a optimiser reason of the compiler.
The keyword volatile tells to the compiler that a higher instance (in
your case the interrupt service routine) can change the value of A. If
you do not use this keyword volatile then the optimiser tries to make
shorter the code.
try
volatile unsigned int A;

Please check the asm file output of your compiler, then you can see what
happen.

Best rergards

Franz


-----Ursprgliche Nachricht-----
Von: c... [mailto:c...] Im Auftrag von
b...@meidensg.com.sg
Gesendet: Montag, 9. Juni 2008 09:57
An: c...
Betreff: [c28x] A very strange problem

HI,

I found a very strange problem. MY code as following. Running in debug
mode, I found even variable A become more than 50(by watch window), the
code is still running in the while loop, and can not run to the code
after while loop.

Anybody can help me, I am mad already.

unsigned int A;//global variable

void main(void)

{
while(A<50)
{
}

//other code here
}

interrupt void XInt2_ISR(void)//a external interrupt
{
A++:
}


OMAP35x EVM jump-starts low-power apps
The modular and extensible OMAP35x Evaluation Module (EVM) enables developers to start building applications based on the OMAP35x architecture: http://www.DSPRelated.com/omap35x
In your case, you must specify A as a volatile variable.
Why ? Because the optimizer will not see A variable changing in the main
source code.
It doesn't know that the varaible will be change outside ... so it must
re-read in each loop the variable A.

so, the fix is :

volatile unsigned int A;//global variable

have a look to the generated assembly code if you have a doubt !


Veuillez rondre b...@meidensg.com.sg
Envoypar : c...
Pour : c...
cc :

Objet : [c28x] A very strange problem

HI,

I found a very strange problem. MY code as following. Running in debug
mode, I found even variable A become more than 50(by watch window), the
code is still running in the while loop, and can not run to the code after
while loop.

Anybody can help me, I am mad already.

unsigned int A;//global variable
void main(void)

{
while(A<50)
{
}

//other code here
}

interrupt void XInt2_ISR(void)//a external interrupt
{
A++:
}

OMAP35x EVM jump-starts low-power apps
The modular and extensible OMAP35x Evaluation Module (EVM) enables developers to start building applications based on the OMAP35x architecture: http://www.DSPRelated.com/omap35x