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.

MSP430G2553-Q1: While loop hangs with optimization greater than 1

Part Number: MSP430G2553-Q1

This question applies to the latest CCS version. I have a routine that delays an adjustable count of ms. An ISR increments MsCount every ms. The following c code waits for mSDuration ms before returning:

// c code routine waits a set count of ms
// a timer ISR increments MxCount every ms
WaitTimer = MsCount; // initialize count
// when MsCount grows to exceed WaitTime by mSDuration, the wait is over
while ((MsCount - WaitTimer) < mSDuration);

This delay works with optimization level 1: The following is the disassembly for optimization level 1. The code loads MsCount into R15 (e1be), then subtracts WaitTimer (e1c2) and compares the result to mSDuration (e1c6).

171 WaitTimer = MsCount;
e1b8: 4292 02F4 02E6 MOV.W &MsCount,&WaitTimer
172 while ((MsCount - WaitTimer) < mSDuration)
$C$L2:
e1be: 421F 02F4 MOV.W &MsCount,R15
e1c2: 821F 02E6 SUB.W &WaitTimer,R15
e1c6: 912F CMP.W @SP,R15
e1c8: 2BFA JLO ($C$L2)

Optimization 2 skips the subtraction and R15 never changes. The while loop is stuck.
171 WaitTimer = MsCount;
e12e: 4292 02F4 02E6 MOV.W &MsCount,&WaitTimer
e134: 430F CLR.W R15
172 while ((MsCount - WaitTimer) < mSDuration);
$C$L2:
e136: 912F CMP.W @SP,R15
e138: 2BFE JLO ($C$L2)

Why would optimization leave essential steps out? Any suggestions?

**Attention** This is a public forum