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?
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
...