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.

Why are flashing LED that way?

Other Parts Discussed in Thread: MSP430F2012

Hi, I play with Launchpad and I do not know why in the end comes a moment when both LEDs light simultaneously. Thanks.

 

 

#include "msp430f2012.h"

#include "MyDelay.h" 

 

 

#define LED1_OFF P1OUT ^=  BIT0

#define LED1_ON  P1DIR |=  BIT0

#define LED2_OFF P1OUT ^=  BIT6

#define LED2_ON  P1DIR |=  BIT6

 

void main(void)

{

  WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer

  BCSCTL1 = CALBC1_16MHZ;

  DCOCTL = CALDCO_16MHZ; 

  P1OUT = 1;

 

  for (;;)

  {

    void Blink(unsigned second);

    {

     DelayS(1);

LED1_ON;

DelayS(1);

LED1_OFF;

DelayS(1);

   LED2_ON;

DelayS(1);

LED2_OFF;

   }

  }

}

 

  • maybe simply a crash.
    The code looks very strange. Why do you declare a funciton blink inside the for loop?
    What does DelayS(1) do? (I guess, waiting 1 second, but how)?

    Also, your defines are not symmetrical.
    With LEDx_ON, you set the direction to output (which is superfluous after you did it the first time), while LEDx_OFF actually toggles the output. Which isn't what the name indicates.

    it should rather be:

    #define LED1_INIT P1DIR|=BIT0; // to be used once
    #define LED1_ON P1OUT |= BIT0;
    #define LED1_OFF P1OUT &=~BIT0;

    The way it is in your code halves the blinking frequency and containes redundant/superfluous actions.

  • While I was playing with my own launchpad i wrote this as a personal note in the comments for later.
    And I think it might be usefull for you (or other people)

    //If you wish to set a single bit to '1' without changing the state
    // of the other bit-registers, you have to perform a "OR"-operation.
    //
    // You can do this on 2 ways, the short or the long one
    //
    // P1DIR |= 0x01;             (short)        (0x01 = bin "0000 0001")
    // P1DIR = P1DIR | 0x01   (long )
    //
    // which results in xxxx xxx1 (where x means no state change)

    // If you wish to reset a single bit to '0' without changing the state
    // of the other bit-registers, you have to perform a "AND"-operation.
    //
    // P1DIR &= 0xFE              (short) (0xFE = bin "1111 1110")
    // P1DIR = P1DIR & 0xFE   (long )
    //
    // which results in xxxx xxx0 (where x means no state change)

    Toggling the LED's whit the XOR is also possible:

    P1OUT ^= 0x01; // Toggle P1.0 using exclusive-OR (0x01 = bin 0000 0001)
    P1OUT ^= 0x40; // Toggle P1.4 using exclusive-OR (0x40 = bin 0000 1000)

    // Logic Table of an 'EXOR' function
    //
    // Input||Output
    // A B || Q
    //------||----
    // 0 0 || 0
    // 0 1 || 1
    // 1 0 || 1
    // 1 1 || 0

    And like Jens-Micheal said, you don't have to calculate the inverse hex of the binary value like i do, just use the bit location like he said.
    P1OUT &=~BIT0.

    Where the '~' means a NOT instruction.
    And BIT0 is 0x0001 (see the header file of you microcontroller)

    If you wish to, you can follow al my 'findings' about the launchpad on my blog (it's a school project):
    http://project-biosensor.blogspot.com/ (check blink-a-led prog)

  •  

    So I did as advised Jens and not blinking. Other wise, the delay: I am in the librarywhere their time constants are defined depending on the frequency. Is it just for the needs of different flashing so no need accuracy.

     

     

     

     

    #include "msp430f2012.h"

    #include "MyDelay.h" 

     

     

    #define LED1_INIT P1DIR|=BIT0+BIT6; // to be used once

    #define LED1_ON P1OUT |= BIT0;

    #define LED1_OFF P1OUT &=~BIT0;

    #define LED2_ON P1OUT |= BIT6;

    #define LED2_OFF P1OUT &=~BIT6;

     

    void main(void)

    {

      WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer

      BCSCTL1 = CALBC1_16MHZ;

      DCOCTL = CALDCO_16MHZ; 

      for (;;)

      {

        void Blink(unsigned ms);

        {

         DelayS(1);

    LED1_ON;

    DelayS(1);

    LED1_OFF;

    DelayS(1);

       LED2_ON;

    DelayS(1);

    LED2_OFF;

       }

      }

    }

     

**Attention** This is a public forum