I am experiencing this weird behavior with a for loop. I want to use a decrementing loop for efficiency's sake. In this case just for a timer delay.
When I run this it works fine. for(i=0; i< 20000; i++);
When I run this it fails to count and drops out of the loop immediately. for(i=20000; i>0; i--);
I'm compiling with CCS v5.5 and the full code for main.c is..
#include <msp430.h>
unsigned int i = 0;
void main(void) {
WDTCTL = WDTPW + WDTHOLD;
P1DIR |= 0x41;
P1OUT = 0x40;
for (;;)
{
P1OUT ^= 0x41;
for(i=20000; i>0; i--); // problem line here
}
}
I'm new to CCS and the MSP430 but I'm pretty sure there is nothing wrong with the for loop right?. One thing I did note is that if i open up the for loop by adding some irrelevant step inside brackets such as assigning another variable j = i then the decrementing loop works.
for(i=20000; i>0; i--){
j = i;
}
What's going on here?