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 programming in Code Composer Studio

Other Parts Discussed in Thread: ENERGIA, MSP430G2553

Hello Software Masters. I'm just a beginner in using CCS and I was so confuse on how to program using this software.

I previously used the energia software. Kindly help me on how to program below. I need your expert ideas for me to learn on how to use this software.

Scene:

When I press the button in P1.3 the LED at P1.0 will turn on and will not turn off unless I press again the same button.

For those who will answer my inquiry, Thank you so much.

  • Hi Sharmaine!

    This is a very simple task and I guess it would be the best if you try to solve it on your own, getting help here in the forum when something does not work as expected. So what do you already know? You can split this task into smaller ones, like first control only the LED without the button.

    Configure P1.0 to output direction and set the output pin high or low to switch the LED on or off.

    Are you able to do that? How would you have done it in Energia?

    Dennis

  • Thanks for your suggestion. Yes I already did that. Below is what I programmed in energia

    const int BUTTON = 5; //use button on board
    const int LED = 14; //use red led on board

    int ledState = LOW; //current ledState
    int buttonState; //current buttonState
    int lastButtonState = LOW; //previous buttonState

    long time = 0; //last time switch is pressed
    long debounce = 50; //debounce time

    void setup()
    {
    //set I/O pins
    pinMode(BUTTON, INPUT_PULLUP);
    pinMode(LED, OUTPUT);
    }

    void loop()
    {
    int buttonState = digitalRead (BUTTON); //is switch press?

    //when button is press & (previous & current state is different) & (previous - current time > debounce)
    if (buttonState == HIGH && lastButtonState == LOW && millis() - time > debounce)
    {
    if (ledState == HIGH) //when LED IS ON
    {
    ledState = LOW; //turn LED OFF
    }
    else //when LED is OFF
    {
    ledState = HIGH; //turn it ON
    }
    time = millis(); //save time after releasing the button
    }
    digitalWrite (LED, ledState); //output LED
    lastButtonState = buttonState; //save the previous buttonState to current buttonState
    }
  • OK, so although I do not know about those Energia functions, this doesn't look that bad at all. I would now recommend to read chapter 8 of the user's guide. It describes the Digital I/O of the microcontroller. If you know this, half of your project is already done. In addition you should learn about bitwise operations.

  • Hi Mr. Dennis,

    What have I done now is to turn on led when button is press and even when I release the button, it will stay on its state.
    Now, my problem is, when I press again the button, the LED will not turn off anymore.
    Can you suggest what to do?
  • Hi Sharmaine!

    Sounds good. If you share the code with us we can see what is wrong / missing.

    (When posting code please use rich formatting which appears on the lower right when pressing on reply and then use the small </> icon)

    Dennis

  • #include <msp430g2553.h>

    void main(void)

    {

      WDTCTL = WDTPW + WDTHOLD;        // Stop watchdog timer

      P1DIR |= BIT6;                   // Set P1.6 (green LED) to output direction, 1 is output

      P1OUT &= ~BIT6;                  // Set the green LED off

      P1DIR &= ~BIT3;                  // Port 1 P1.3 (push button) as input, 0 is input

      P1REN |=  BIT3;                  // Enable Port 1 P1.3 (push button) pull-up resistor

      P1OUT |= BIT3; // The button is up

    while(1)

    {

          if( (P1IN & BIT3 ) == 0)      // Push button down when bit 3 == 0

          {

          if ((P1OUT | BIT6) == 0)

          {

          P1OUT &= ~BIT6;

          }

          else

          {

          P1OUT |= BIT6;

          }

          }

    }

    }

  • Hi Sharmaine!

    Dennis Eichmann said:
    (When posting code please use rich formatting which appears on the lower right when pressing on reply and then use the small </> icon)

    This is how it looks like when using rich formatting and the code insertion tool:

    #include <msp430g2553.h>
    
    void main( void )
    {
      WDTCTL = WDTPW + WDTHOLD;        // Stop watchdog timer
    
      P1DIR |=  BIT6;                  // Set P1.6 (green LED) to output direction, 1 is output
      P1OUT &= ~BIT6;                  // Set the green LED off
      P1DIR &= ~BIT3;                  // Port 1 P1.3 (push button) as input, 0 is input
      P1REN |=  BIT3;                  // Enable Port 1 P1.3 (push button) pull-up resistor
      P1OUT |=  BIT3;                  // The button is up
    
      while( 1 )
      {
        if( (P1IN & BIT3 ) == 0 )      // Push button down when bit 3 == 0
        {
          if( (P1OUT | BIT6) == 0 )
          {
            P1OUT &= ~BIT6;
          }
          else
          {
            P1OUT |= BIT6;
          }
        }
      }
    }

    Now there are line numbers you can refer to, for example.

    What do you want to do here?

    if( (P1OUT | BIT6) == 0 )

    I guess you want to test if P1.6 is actually low, right? The output register can be read equally to the input register:

    if( (P1OUT & BIT6) == 0 )

    Dennis

  • Hello sir.

    Yes. I want to test if P1.6 is low. but if I change

    if( (P1OUT | BIT6) == 0 )

    to

    if( (P1OUT & BIT6) == 0 )

    the LED will not light anymore when the switch is pressed?
    What to do?

**Attention** This is a public forum