0

I am asking this because the code below is what I'm using to make the 2 two leds perfectly alternate their flashing. But it doesn't really make sense as to why this works. This loop with 2 xor's has 2 states one with pin1(red lit only) active and another with both pin 6 and pin 1 active(both red and green lit). But the lights flash like they are alternating on and off perfectly between each other.

#include <msp430g2553.h> 

// counter as a global variable
unsigned int i = 0;  

void main(void) 
{ 
    // stop the watchdog timer
    WDTCTL = WDTPW + WDTHOLD;  

    // set the direction register for LED1 and LED2
    P1DIR |= 0x41;  

    // initialize LED1 and LED2 to off
    P1OUT &= 0xBE;  

    //empty for loop is an infinite loop
    for (;;) 
    { 
        P1OUT ^= 0x01;

        // create a delay between toggles
        for(i=0; i< 20000; i++)  
        {    
            // empty statement, do nothing
        }

        P1OUT ^= 0x40;
    }
}

Is it possible the delay for the main for loop is causing this illusion?

Clifford
  • 88,407
  • 13
  • 85
  • 165
Destreation
  • 105
  • 1
  • 2
  • 12
  • cant be determined from what you have provided. – old_timer Jun 04 '17 at 02:14
  • To better understand what happens, add a copy of the delay loop before the end of the infinite loop. – CL. Jun 04 '17 at 11:12
  • If you want others to read your code, it is probably polite and more likley to attract an audience if it is formatted for readability. Whitespace and indentation help comprehension for you too. Reformatted for you. – Clifford Jun 04 '17 at 19:56
  • 1
    What is the purpose of making `i` _global_? If you must have a global variable, do not call it `i`! In this case not only does it not need ot be global, it could be localised to just the for loop: `for( volatile int i = 0; i < 20000; i++ )`. And it should be declared volatile in any event if you don't want the loop to be potentially optimised away. – Clifford Jun 04 '17 at 20:03

1 Answers1

4

The answer is that it is VERY FAST to get back to the start of the loop.

This code looks like it should alternate the LEDs to me. Your comments look like they're badly lined up though.

The main part of it is in your loop. You start out with both LEDs off. Then do this (I've changed the comments to what I think they do):

for (;;) {                   //  infinite loop
    P1OUT ^= 0x01;           // toggle state of LED1
    for(i=0; i< 20000; i++)  // create a delay 
        ;
    P1OUT ^= 0x40;           // toggle state of LED2
}

So what that does, flattening out the loop is:

                      LED1          LED2
                      off           off

start into loop

Toggle LED1           on            off
wait
wait
wait
Toggle LED2           on            on    { go back to start of loop - quickly }
Toggle LED1           off           on 
wait
wait
wait
Toggle LED2           off           off 
Toggle LED1           on            off
wait 
...
GregHNZ
  • 7,946
  • 1
  • 28
  • 30
  • Yeah I'm sorry about the comments it was textbook code I edited but I did not modify the comments. Thanks your example really helped me visualize it. – Destreation Jun 04 '17 at 13:26