1
\$\begingroup\$

I started using MSP430 Launchpad and as I was examining the code for blinking a led I found something odd. The code is this and is a ready example:

#include  <msp430g2553.h>

 unsigned int i = 0;                         

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 

  P1DIR |= 0x01;                            

  for (;;)                                  
  {

    P1OUT ^= 0x01;                         

    for(i=0; i< 20000; i++);               

  }
}

In the beginning you have to initialize P1DIR so to set the pin in which the led is connected to output. I searched a lot and everywhere is says that the specific register is just to set the direction. So then I deleted a part of the code and the remaining was this one:

#include  <msp430g2553.h>

 unsigned int i = 0;                         

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                 

  P1DIR |= 0x01;                            

  for (;;)                                  
  {            

  }
}

Here is the odd part. The led is on!Shouldn't be off? The line of code with P1OUT isn't going to say to MSP if the led is on or off? With only P1DIR nothing should happening. What am I doing wrong?

\$\endgroup\$
2
\$\begingroup\$

You can download some code I wrote at:

ZIP file containing an LED Tutorial for MSP430

It covers everything from just turning an LED on, to causing it to evenly (to the eye) diminish and increase in intensity (which requires a geometric progression to achieve.)

In the meantime, what you didn't realize is the power-on value for P1OUT. Try two different statements BEFORE your for(;;) statement:

P1OUT &= ~0x01;

and

P1OUT |= 0x01;

And apply one or the other of the above with your latter code example (the one with the deleted code.) You will see two different behaviors. I'm not certain as I haven't researched it lately, but I suspect the P1OUT values are "random" upon power-on-reset. Regardless, it matters and you need to make sure that P1OUT values are "known" before starting your code.

In the case of your first code example that blinks the LED, the initial value didn't matter since the loop XORs the value over and over. You would never notice if it started as ON or OFF, because it is toggling all the time.

\$\endgroup\$
  • \$\begingroup\$ Thank you but what I am saying is without any other line of code and with only the P1DIR shouldn't the led be turned off? I mean there is no output command so to know the MPS what should the led do. \$\endgroup\$ – Adam Mar 19 '14 at 14:38
  • 1
    \$\begingroup\$ P1OUT is "randomly" set at power-on. See near top of page 333 of SLAU144J, the family guide for the device. If you don't set it, then it's "unknown." To see the effect of an unknown value, see page 42 of the datasheet, SALS735J, which is the schematic of the port pin. I already mentioned why in an earlier reply. These two pages clinch the deal. You need to set P1OUT if you care to know what its value is. If you don't care, don't be surprised if its value isn't known. \$\endgroup\$ – jonk Mar 19 '14 at 19:49
  • \$\begingroup\$ By "known" I meant "known a priori." \$\endgroup\$ – jonk Mar 19 '14 at 19:59
-1
\$\begingroup\$

P1DIR configures direction port pin (DIR) output or input. 1 is output, 0 is input.

P1OUT ^= 0x01;    //P1OUT register holds the status of the LED.
                  // 1 is ON, 0 is OFF

Use P1OUT statement before the for loop

Change your for(;;) to this so that you see LED toggles:

for(i=0; i < 50000; i++);     // Delay between Green LED toggles.
\$\endgroup\$
  • 3
    \$\begingroup\$ I think you missed the point. The OP is well aware that there would normally be an instruction to modify P1OUT. The question relates instead to the default state of P1OUT. Also, please use proper syntax and grammar in your posts. \$\endgroup\$ – Joe Hass Apr 2 '14 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.