Tag: MSP430 Tutorial

MSP430 Launchpad: Using Buttons

Posted by on December 21, 2010


Sooner or later you’ll want a user interface, whether you’re working on a game or just trying to figure things out one of the simplest methods for human interaction would be a button. We’ll discuss how to setup and read input bits in the following paragraphs.

The code:

The quick explanation:

We’ll deal with inputs very much the same way we would with an output, except we’re reading instead of writing. Notice how we define the bits we want as “high” on P1OUT to light up our LEDs, provided the bits are set on P1DIR to “high” then those bits will act as outputs and every time P1OUT has one of these bits “high”, your outputs will also go “high”.

Likewise with our inputs, if P1DIR is set “low” on our target bit then all we have to do is read P1IN to know whether a button is being held down or not. Simple eh?

To recap: On port direction bits low = Inputs, bits high = Outputs

A word on de-bouncing:

For those of you unaware of switch bounce, I would like to give you a simple explanation of what it is: whenever you actuate a switch it’s contacts bounce back and forth very rapidly as the metal vibrates after it’s been bent and pushed onto the opposite contact. This poses a problem with false triggering; You may press a button once but the software may detect 3 or more presses.

Notice how we don’t perform any sort of de-bouncing in the code, this is for simplicity and in the future we will tackle this, but for now we’re keeping it simple and down to the basics.

One simple way to de-bounce in software is to sample the input multiple times, hardware de-bounce is often proffered but it comes with it’s costs. For cheap consumer products more often than not this action is carried in software.

Where do we go from here?

Now that you can manipulate both outputs and inputs; it is time to create something useful. In the next lesson we’ll design a full blown bicycle safety light with what we’ve learnt!

For the next lesson you’ll need:

  • 3x General Purpose NPN Transistor ( PN2222, BC337, 3904, etc. Almost any will do )
  • 3x 4k7 Ohm 1/4W Resistors
  • 3x 100 1/4W Resistors ( Could be up to 470 Ohms, or even 1K if high brightness LEDs are used )
  • 1x 10K Ohms 1/4W Resistor (Optional)
  • 1x Tact Switch (Optional)
  • 3x 5mm or 3mm LEDs, Could be all of the same colour or one Red, Green and Blue. Your choice.
  • 1x Breadboard or any other prototyping method of your choice.

Something to download:

The “Using Buttons” project files.

Stay tuned.