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.

msp430g2553 Key pressed working code

#include <msp430.h>

int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= 0x41; // Set P1.0 to output direction
P1DIR &= ~0x08;
P1REN |=0x08; // needed to use key to enable pullup resistor
P1SEL |=0x08; // general statement but needed to select particular pins
P1OUT |= 0x00; //turning all pins in off
while(1)
{
if(!(P1IN &0x08))
{
P1OUT ^= 0x01; // Toggle P1.0 using exclusive-OR
__delay_cycles(250000);
}
else
{

P1OUT ^= 0x40; // Toggle P1.0 using exclusive-OR
__delay_cycles(250000);
}
}
}

  • Is there a question here?

    I do see

    P1SEL |=0x08;

    which specifies an alternate function for P1.3. Looking at Table 17 (slas735j), I'm not quite sure which one that is. I suspect you don't want that line at all.

  • And this;

    Nandish H S said:
    P1OUT |= 0x00; //turning all pins in off

    Does nothing!

  • Hi Nandish ,

     

    In while(1) loop, you have P1OUT ^=0X01; below this line type P1OUT |=0X08;

    also in else statement you have P1OUT ^=0X40; below that line type P1OUT |=0X08;

    try this out

  • Hi Nandish!

    Your code looks a little bit weird. What do you want to do? It seems as if you want to use the push-button on P1.3 of the launchpad to control the LEDs on P1.0 and P1.6, right?

    Try this:

    void main( void )
    {
      WDTCTL = ( WDTPW | WDTHOLD ); // Stop watchdog timer
      P1DIR  = 0x41;                // Set P1.0 and P1.6 to output
      P1REN  = 0x08;                // Enable resistor on P1.3
      P1OUT  = 0x08;                // Resistor to pull-up
    
      while( 1 )
      {
        if( !(P1IN & 0x08) )        // Button on P1.3 (S2) pressed
        {
          P1OUT ^= 0x01;            // Toggle LED on P1.0
        }
        else                        // Button released
        {
          P1OUT ^= 0x40;            // Toggle LED on P1.6
        }
    
        __delay_cycles( 250000 );   // Delay to see blinking
      }
    }

    This should toggle one of your LEDs, depending on the button. If it is pressed, LED on P1.0 toggles, if not LED on P1.6 toggles. The other LED remains in it's state.

    Maybe it "looks" better, if you disable the other LED by writing:

    if( !(P1IN & 0x08) )        // Button on P1.3 (S2) pressed
    {
      P1OUT &= ~0x40;           // P1.6 off
      P1OUT ^=  0x01;           // Toggle LED on P1.0
    }
    else                        // Button released
    {
      P1OUT &= ~0x01;           // P1.0 off
      P1OUT ^=  0x40;           // Toggle LED on P1.6
    }
  • Nandish,

    P1SEL|= 0x08 will switch pin 3 into module mode. Depending on the particular MSP port logic, this may sewer P1IN.3 from the physical pin. Luckily not in the G2553. However, it switches the pin into an undefined mode (ADC10CLK is an output, there is no module input for this pin). Surely not your intention. Simply leave P1SEL clear for generic digital I/O.
    And Leo is right, |=0x00 does nothing. It sets all bits that are set in 0x00 (= none). You probably meant a simple ‘=’ to assign 0x00 to the register.

     Bruce,
    Apparently, Nandish has posted his (more or less) working code (hence the thread title), for others to use. No question here, just sharing code.

**Attention** This is a public forum