Button s2

961 views
Skip to first unread message

tfoutz99

unread,
Feb 7, 2012, 8:26:16 PM2/7/12
to TI Launchpad
Hi Everyone,
I have been playing with my new launchpad (Amazing!) and have been
really pleased.

However, I can't get button s2 to work. I am following a great book on
microcontrollers, and am having no luck getting s2 to do anything. I
even downloaded the following code from pastebin (http://pastebin.com/
aNfJUh6R); pushing button s2 still didn't do anything. I have 2
launchpads, and the behavior is the same. S2 works fine with the demo
program, so I know it is at least all wired up correctly.

Any help would be greatly appreciated!

// ============================================================= //
#include <msp430g2553.h> // gotta have this for the M430G2253 chip,
bro

void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR &= ~BIT3; // Push Port 1 P1.3 (push button) as input
P1DIR |= BIT6; // Set P1.6 (LED) to output direction
P1SEL &= ~BIT3; // Select Port 1 P1.3 (push button)
P1OUT &= ~BIT6; // Set the LED off

while( 1 ) {
if( (P1IN & BIT3) == 0) // Push button down when bit 3 == 0
P1OUT |= BIT6; // Set LED on when button down
else
P1OUT &= ~BIT6; // Set LED off when button off
}
}

octavian capatina

unread,
Feb 8, 2012, 7:34:46 AM2/8/12
to ti-lau...@googlegroups.com
Hi

While  playing with my new launchpad  for

#include <msp430g2231.h>
#define P1O P1OUT
#define P1I P1IN
#define P1D P1DIR

void main(void)
{
    P1D=65;
    P1O=0;
    R4=8;
    if(P1I&R4==8)
    if(P1I&R4==0)
    R4=8;
    R5=(R5+1);
    P1O=R5;
}   

comes out 2 errors: R4 and R5 are undefined !!!!
R4 and R5  are general purpose register and their named is reserved?!


Any help would be greatly appreciated!

Octavian



2012/2/8 tfoutz99 <tom....@gmail.com>



--
cu bine,  kind regards,  mit freundlichen Grüßen


mai incolo vorba marelui istoric David Prodan ...

Mikhail Koslowski

unread,
Feb 8, 2012, 10:02:06 PM2/8/12
to ti-lau...@googlegroups.com
Hi Tom

If you take a look at section 8.2 of slau144i (http://www.ti.com/litv/pdf/slau144i this is the datasheet for the whole msp430x2xx family) you will find some information about the operation of I/O ports and i will mention it in my explanations.

First, i would recommend first that you change this line:

       P1SEL &= ~BIT3;                                 // Select Port 1 P1.3 (push button)
for this:
       P1SEL &= ~(BIT3 | BIT¨);                       // P1.3 and P1.6 as I/O
This will select the I/O function of both pins (SEL stands for "function select"). see sec. 8.2.5 for more details.

Then, for your button to work properly you first have to ask "how it is wired?", and if you check the schematics, you will see that one tail is on p1.3 and the other is grounded, and now we reach the second question "how can a grounded button inputs a logical 1 (high) into the uC?" and the answer is "simple": Pull Up resistor.
I guess all Msp430s have internal resistors, enabled/disabled by software, for yours, taking a look at 8.2.4 you will see that there is a PxREN register, and there we are going to "enable" a resistor at p1.3:
       P1REN |= BIT3;                           // Enable a pull-up/down resistor.
After that, still in 8.2.4 we can see "The corresponding bit in the PxOUT register selects if the pin is pulled up or pulled down.",  and walking into 8.2.2 (PxOUT) we will find out how to set the resistor to pull UP:
       P1OUT &= ~BIT3;                           // Sets the resistor to pull-UP

Other parts of your code are fine! with this addons i think it should works as expected.

Hope it helps you and others.
If you still got any questions or if i said something wrong or not clear, feel free to tell us!

--
Mikhail

2012/2/7 tfoutz99 <tom....@gmail.com>:

Mikhail Koslowski

unread,
Feb 8, 2012, 10:40:46 PM2/8/12
to ti-lau...@googlegroups.com
Hi Octavian,

First i would recommend you opening another thread for asking your question, as it belong to another "subject". it would be easier for others to find it, answer it and to see the reply, without mixing with the other topic! =)

Then, answering your question. As far as i know, it is not possible to access this "General puppose registers" from C. if you want to lower the level, consider using assembly, else then what you can do is declare a variable, with the type that most fills your needs (char, int, float, short and so on...) and use it! anyway, if you want to "force" it to be stored in a register (for speed up) you can declare it as:
       register unsigned char var;
in the example above, i declared a variable named "var" typed "unsigned char", and the compiler (compliant with ANSI C) will try its best to fit this variable in a register to speed the process up!

Now, if you really want to access the register in C, maybe it can be done by using a #define with the
register "address". I tried to get a "pointer" to see the register "address" but Code Composer did not allowed me. Maybe registers are not memory mapped? don't know (and datasheet says nothing..)



--
Mikhail

2012/2/8 octavian capatina <oct....@gmail.com>

Mikhail Koslowski

unread,
Feb 8, 2012, 10:48:07 PM2/8/12
to ti-lau...@googlegroups.com
Sorry, could not attach the image showing CC Debug/Watch
here is the link to the image: https://docs.google.com/drawings/d/1HC5kpAvKtV1cF-OX8jvYU1tBXyVlnwKiw8QqnRsaEbg/edit

2012/2/9 Mikhail Koslowski <mikhail....@gmail.com>

octavian capatina

unread,
Feb 9, 2012, 8:05:07 AM2/9/12
to ti-lau...@googlegroups.com
Thanks Mikhail,
finnaly a have renounce to work with Ri register; I had worked symbolic with a define variable.

kind regards

2012/2/9 Mikhail Koslowski <mikhail....@gmail.com>

tfoutz99

unread,
Feb 9, 2012, 9:56:08 PM2/9/12
to TI Launchpad
Worked like a charm. I was missing that bit about setting a pull-up
resistor, like you said.

Also, reading section 8 of the manual helped a great deal in
understanding the digital I/O.

For any future readers, the line in Mikhail's response:
P1SEL &= ~(BIT3 | BIT¨);

Should read as:
P1SEL &= ~(BIT3 | BIT6);
(for some reason the 6 was rendered incorrectly)

Thank you so much for your help!

--Tom

On Feb 8, 10:02 pm, Mikhail Koslowski <mikhail.koslow...@gmail.com>
wrote:
> Hi Tom
>
> If you take a look at section 8.2 of slau144i (http://www.ti.com/litv/pdf/slau144ithis is the datasheet for the whole
> msp430x2xx family) you will find some information about the operation of
> I/O ports and i will mention it in my explanations.
>
> First, i would recommend first that you change this line:
>       * P1SEL &= ~BIT3;                                 // Select Port 1
> P1.3 (push button)*
> for this:
>       * P1SEL &= ~(BIT3 | BIT¨);                       // P1.3 and P1.6 as
> I/O*
> This will select the I/O function of both pins (SEL stands for "function
> select"). see sec. 8.2.5 for more details.
>
> Then, for your button to work properly you first have to ask "how it is
> wired?", and if you check the schematics, you will see that one tail is on
> p1.3 and the other is grounded, and now we reach the second question "how
> can a grounded button inputs a logical 1 (high) into the uC?" and the
> answer is "simple": Pull Up resistor.
> I guess all Msp430s have internal resistors, enabled/disabled by software,
> for yours, taking a look at 8.2.4 you will see that there is a PxREN
> register, and there we are going to "enable" a resistor at p1.3:
> *       P1REN |= BIT3;                           // Enable a pull-up/down
> resistor.*
> After that, still in 8.2.4 we can see *"The corresponding bit in the PxOUT
> register selects if the pin is pulled up or pulled down."*,  and walking
> into 8.2.2 (PxOUT) we will find out how to set the resistor to pull UP:
> *       P1OUT &= ~BIT3;                           // Sets the resistor to
> pull-UP*
>
> Other parts of your code are fine! with this addons i think it should works
> as expected.
>
> Hope it helps you and others.
> If you still got any questions or if i said something wrong or not clear,
> feel free to tell us!
>
> --
> Mikhail
>
> 2012/2/7 tfoutz99 <tom.fo...@gmail.com>:

Mikhail Koslowski

unread,
Feb 11, 2012, 9:32:21 PM2/11/12
to ti-lau...@googlegroups.com
Great!

I may have missed "6" in BIT6, thanks on pointing it!
and i also missed an space between datasheet link and the next word.. the correct is http://www.ti.com/litv/pdf/slau144i

-- Mikhail

2012/2/10 tfoutz99 <tom....@gmail.com>

josipP

unread,
Mar 5, 2012, 5:37:31 PM3/5/12
to TI Launchpad
Hi

I have a similiar problem in my msp, but that piece of code does not
work:

else
                        P1OUT &= ~BIT6;                     // Set LED
off when button off

When im releasing button, led is still on. What should I do to make
LED switched off ?

Best regards
JosipP

On 12 Lut, 03:32, Mikhail Koslowski <mikhail.koslow...@gmail.com>
wrote:
> Great!
>
> I may have missed "6" in BIT6, thanks on pointing it!
> and i also missed an space between datasheet link and the next word.. the
> correct ishttp://www.ti.com/litv/pdf/slau144i
>
> -- Mikhail
>
> 2012/2/10 tfoutz99 <tom.fo...@gmail.com>
>
>
>
>
>
>
>
> > Worked like a charm. I was missing that bit about setting a pull-up
> > resistor, like you said.
>
> > Also, reading section 8 of the manual helped a great deal in
> > understanding the digital I/O.
>
> > For any future readers, the line in Mikhail's response:
> > P1SEL &= ~(BIT3 | BIT¨);
>
> > Should read as:
> > P1SEL &= ~(BIT3 | BIT6);
> > (for some reason the 6 was rendered incorrectly)
>
> > Thank you so much for your help!
>
> > --Tom
>
> > On Feb 8, 10:02 pm, Mikhail Koslowski <mikhail.koslow...@gmail.com>
> > wrote:
> > > Hi Tom
>
> > > If you take a look at section 8.2 of slau144i (
> >http://www.ti.com/litv/pdf/slau144ithisis the datasheet for the whole

Vitali

unread,
Apr 23, 2012, 2:35:40 PM4/23/12
to ti-lau...@googlegroups.com
Hi,

would you mind posting the complete code working on the M430G2553? 'Cause I've followed that discussion and made the needed changes and the sample still does not work...

Thanks in advance,
Vitali

Mikhail Koslowski

unread,
Apr 23, 2012, 5:34:14 PM4/23/12
to ti-lau...@googlegroups.com
JosiP, can you please post your main function code?

Vitali, what happens when you run your modified code?


2012/3/5 josipP <woj...@gmail.com>

Sisyphos

unread,
Jun 28, 2012, 3:07:11 AM6/28/12
to ti-lau...@googlegroups.com
Hi. Im begginer user. Im not good at electronics but i liked launchpad very much. Its very good toy for summer holiday :). I read many but still dont understand GPIO. I have Rev. 1.5 launchpad. I know C a bit but i dont understad bitwise operations. Its hard to learn it in C so i decide to learn Assembly for make it clear. Im using msp430 Microcontroller basics. Im stuck on this button problem. I used modified codes and result was BIT6 led on and button doesnt work. Here is the modified codes i used. Please correct it.

#include "msp430g2553.h"


void main(void)
{
        WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer
        P1DIR &= ~BIT3;                                        // Push Port 1 P1.3 (push button) as input
        P1DIR |= BIT6;                                         // Set P1.6 (LED) to output direction
        P1SEL &= ~(BIT3 | BIT6);                              //MODIFIED Select Port 1 P1.3 (push button)

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

        while( 1 ) {
                if( (P1IN & BIT3) == 0)         // Push button down when bit 3 == 0
                        P1OUT |= BIT6;                        // Set LED on when button down
                else
                        P1OUT &= ~BIT6;                        // Set LED off when button off
        }
}

Why we are defining P1DIR twice? If we write P1DIR=0x40 BIT3's bit would be 0. where am i wrong? Whats the true code?

24 Nisan 2012 Salı 00:34:14 UTC+3 tarihinde Mikhail yazdı:

Mikhail Koslowski

unread,
Jun 28, 2012, 3:25:54 PM6/28/12
to ti-lau...@googlegroups.com
Hi Sisyphos,

Actually you are coding in C. when you write something like "WDTCTL" for example, you are just using a predefined macro so your code will be "readable". If you were doing it in assembly, instead of using "ifs, whiles, fors" and so on, you would be using instructions defined in msp instruction set.

Now, about the code.
I've tested it in a rev1.4 and it worked fine, but there are many points that we may address:




--
Mikhail



2012/6/28 Sisyphos <cemer...@gmail.com>

Mikhail Koslowski

unread,
Jun 28, 2012, 3:33:44 PM6/28/12
to ti-lau...@googlegroups.com
sorry.. send the message without finishing..

1 - it worked in 1.4rev with g2231, not the same setup as yours
2 - as far as i remember, rev1.4 already have somthing external to pull p1.3 up, so it is not needed to turn on the internal pull up, and this is missing in the code.

what you can do to have this code working is:
1 - Set P1SEL2, so the IO function will be properly set (do it right after P1SEL line):
     P1SEL2 &= ~(BIT3 | BIT6);
2 - Set a pull up resistor on p1.3, so you'll not need any extarnal circuit to do it:
    P1REN |= BIT3; // Enable a pull-up/down resistor in P1.3 (do it outside the while loop)
    P1OUT |= BIT3; // Sets the resistor to pull-up


Please test it and tell us the result.

and about Bitwise operations, i would recommend you to read this: http://www.cprogramming.com/tutorial/bitwise_operators.html

--
Mikhail



2012/6/28 Mikhail Koslowski <mikhail....@gmail.com>

Sisyphos

unread,
Jun 28, 2012, 9:11:41 PM6/28/12
to ti-lau...@googlegroups.com
Its perfectly working :D. Thank you mikhail.

Sisyphos

unread,
Jun 29, 2012, 7:16:04 AM6/29/12
to ti-lau...@googlegroups.com
I just wanted to simplify the bitwise operated codes for the amatours and the person who isnt engineer like me :D. I think Button like led and used this codes:


#include "msp430g2553.h"

void main(void)
{
        WDTCTL = WDTPW + WDTHOLD;                
        P1DIR = BIT6;                                   
        P1REN = BIT3;            
        P1OUT = BIT3;            
      
        while( 1 ) {
               
                if( (P1IN & BIT3) == 0)                
                {
                        P1OUT = BIT3+ BIT6;                     
                }
                else
                {
                        P1OUT = BIT3;
                }
        }
}


Please warn if something bad idea. Thank you again.


On Thursday, 28 June 2012 22:33:44 UTC+3, Mikhail wrote:

Mikhail Koslowski

unread,
Jun 29, 2012, 6:32:21 PM6/29/12
to ti-lau...@googlegroups.com
Hi Sisyphos,

Bitwise operations are somehow "masked" operations.
When you write: P1OUT |= BIT3, you are doing the same as P1OUT = P1OUT | BIT3. They both are "bitwise", but the first one is a short form (avaiable in C language).

When you do not use bitwise operations, for example P1OUT = BIT3, despite having done what you wanted (set bit3 of p1out high), you are also "implicity" setting all other bits to 0, and thus you are risking messing something else. Following the same example of P1OUT. While you only wish to turn the led on (setting P1.6), if you do this with only P1OUT = BIT6 you will be messing with the configuration of the pull-up resistor on P1.3, setting it to pull-down instead.

Then you will say to me: "Ok, i know that i cannot mess P1.3 setup, so i will set it everytime i change something on P1OUT". Right, this will work! but this example is only 1 led and 1 button, and if you try to do anything a little "bigger" (2 leds and 2 buttons for example), it starts becoming harder to track how each bit should be on each time you change something.. (and if you go further, using in the same port I/O pins with different setups and other peripherals at the same time, chances are even greater that something will go wrong).

Another risk of doing this implicity changes (without noticing them of course) is if you, for example, wrongly sets P1.3 as output, set it output value to high (thinking you will be changing the resistor to pull-up), and then press the push-button on the board. you will be grounding a high output and this is the same as a short-circuit, wich can damage your board.

If you always do it bitwise, you really don't need to care of "which values others bits will have here??" cause you will be doing something like P1OUT = P1OUT | BIT6 (or in the short form P1OUT |= BIT6), and this can be read as "put ou P1OUT whatever it already have and set Bit6"..

So, i think it is really worthy to understand and master bitwise operations :)
--
Mikhail



2012/6/29 Sisyphos <cemer...@gmail.com>

Sisyphos

unread,
Jun 29, 2012, 11:53:54 PM6/29/12
to ti-lau...@googlegroups.com
Its good to learn that. Thank you Mikhail.

Mikhail Koslowski

unread,
Jun 30, 2012, 10:50:23 PM6/30/12
to ti-lau...@googlegroups.com
You are welcome!

--
Mikhail



2012/6/30 Sisyphos <cemer...@gmail.com>

Rajeev S

unread,
Feb 1, 2013, 7:04:28 PM2/1/13
to ti-lau...@googlegroups.com
Hi,

I tried the above code in my MSP430G2553 and it did not work.
The code:

#include  <msp430g2553.h> 

void main(void) 
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR &= ~BIT3; // Push Port 1 P1.3 (push button) as input
P1DIR |= BIT6; // Set P1.6 (LED) to output direction
        P1REN |= BIT3;
P1SEL &= ~BIT3; // Select Port 1 P1.3 (push button)
P1OUT &= ~BIT6; // Set the LED off


while( 1 ) {
if( (P1IN & BIT3) == 0) // Push button down when bit 3 == 0
P1OUT |= BIT6; // Set LED on when button down
else 
P1OUT &= ~BIT6; // Set LED off when button off
}

I had used the G2231 before and many of those programs that involve the S2 diesnt work on the 2553,Is my switch faulty?

I would like to add that the voltage between the terminals when the switch is open is 0V.

Peter Johansson

unread,
Feb 1, 2013, 8:02:58 PM2/1/13
to ti-lau...@googlegroups.com
The rev 1.5 launchpads do *not* have a physical pull-up. You need to
enable the internal pull-up to use the switch.

-p.

Kimi Wang

unread,
Feb 16, 2016, 12:16:49 PM2/16/16
to TI Launchpad
Hi Mikhail,

do you mind to take a look my main function?
I did the step by step trace, and it seems like the program never goes to "else" statement, and even I don't press the S2 button, the LED2 is turned on.

void main(void)
{
        WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer
        P1REN |= BIT3;
        P1OUT &= ~BIT3;
        P1DIR &= ~BIT3;                                        // Push Port 1 P1.3 (push button) as input
        P1DIR |= BIT6;                                         // Set P1.6 (LED) to output direction
        P1SEL &= ~(BIT3 | BIT6);                                       // Select Port 1 P1.3 (push button)
        P1OUT &= ~BIT6;                                 // Set the LED off

        while( 1 ) {
                if( (P1IN & BIT3) == 0)         // Push button down when bit 3 == 0
                        P1OUT |= BIT6;                        // Set LED on when button down
                else
                        P1OUT &= ~BIT6;                        // Set LED off when button off
        }
}

Thanks!

在 2012年4月23日星期一 UTC-5下午4:34:14,Mikhail写道:

Mikhail Koslowski

unread,
Feb 16, 2016, 12:45:59 PM2/16/16
to ti-lau...@googlegroups.com
Hi,

It has been a long time since I posted in this thread (almost 4 years!). But I will try to help you.

First, check in the schematics of your board if the LED and the Button are on the same pins. If you are using Launchpad, they may have changed something on the newest versions of the board.
Then, if the schematics are Ok, try swapping the order of the lines that set the PullUp resistor so it is done after setting the pin to IO function.

Also, try inspecting the voltage level of the pin with a multimeter with the button on/off. (I'm not sure if the internal pull-up "shows" it's voltage to the external pin).

You can also inspect the value of the whole P1IN register to see what is happening with the button press.

Try those things and let me know what happened.

Mikhail


--
You received this message because you are subscribed to the Google Groups "TI Launchpad" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ti-launchpad...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages