Electronic – Using single delay in LED blinking

msp430

The following code basically blinks yellow LED on MSP430 Launch Pad on port 1.6.

I had to use two

do-while

loops for providing delays. Is there a way I can do this using a single do-while ?

int main(void)
{
      P2DIR = 0xFF;
      P2OUT = 0x00;
          P3DIR = 0xFF;
          P3OUT = 0x00;
        volatile unsigned int i;    // volatile to prevent optimization
       WDTCTL = WDTPW + WDTHOLD;       // Stop watchdog timer
       P1DIR |= 0x40;                  // Set P1.6 (Green LED) to output
       while( 1 ) {
           i = 9000;                    // SW Delay
           do i--;
               while(i != 0);
           P1OUT |= 0x40;              // Green LED On
           i = 9000;                    // SW Delay
             do i--;
       while(i != 0);

       P1OUT &= ~0x40;             // Green LED Off
   }
}

Best Answer

As you want to blink the LED, you can use X-OR rather than & and OR so, it will be like:

 while( 1 )
   {
           i = 9000;                    // SW Delay
           do 
           {
              i--;
           } while(i != 0);
           P1OUT ^= 0x40;              // Toggle Green LED
    }

XOR is normally used for Toggling.