MSP430 LaunchPad PushButton

From Texas Instruments Wiki
Jump to: navigation, search

by Aldo Briano

This module configures a pushbutton to turn on and off an LED. By pressing the button, the LED turns on. When the button is released, the LED turns off. Interrupts are used to read the input from the digital port. Two examples are explained, using the onboard button and LED of the LaunchPad kit, and interfacing external hardware components to the LaunchPad.


Related Projects


Quick Start

  1. Get external components.
    • 1 Resistance33KOhm
    • 1 Capacitor470nF
    • External switch / button
    • Small breadboard and jumper cables
  2. Create new project in CCS and copy/paste code
  3. Build circuit.


Development 

This example uses the onboard LED at P1.0. The external button should be placed in pin P1.3.  It will override the onboard button in the LaunchPad.

Schematic

The button used was Tactile Switch B3F-4000 with a max bounce time of 5 ms. Nevertheless, I assumed a 10 ms bouncing time to be extra safe.

 

                                                     Msp430 launchpad button debounce.png                                                     Launchpad images PushButtonBreadboard small.png

                                                             Fig. 3: Schematic for Circuit                                                                Fig. 4:  Circuit Built using a Breadboard and external components

Code

//***************************************************************************************
// MSP430 PushButton that toggles LED at P1.0 On and OFF
//
// Description; PushButton in P1.3 through interrupt turns on and off the LED in P1.0
// By changing the P1.3 interrupt edge, the interrupt is called every time the button
// is pushed and pulled; toggling the LED everytime. 
// ACLK = n/a, MCLK = SMCLK = default DCO
//
// MSP430x2xx
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// | P1.0|-->LED
//
// Aldo Briano
// Texas Instruments, Inc
// June 2010
// Built with Code Composer Studio v4
//***************************************************************************************
#include <msp430x20x2.h> 

#define LED0 BIT0
#define LED1 BIT6 
#define BUTTON BIT3



int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= (LED0 + LED1); // Set P1.0 to output direction 
// P1.3 must stay at input
P1OUT &= ~(LED0 + LED1); // set P1.0 to 0 (LED OFF)
P1IE |= BUTTON; // P1.3 interrupt enabled

P1IFG &= ~BUTTON; // P1.3 IFG cleared

__enable_interrupt(); // enable all interrupts
for(;;)
{}
}


// Port 1 interrupt service routine
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
P1OUT ^= (LED0 + LED1); // P1.0 = toggle
P1IFG &= ~BUTTON; // P1.3 IFG cleared
P1IES ^= BUTTON; // toggle the interrupt edge,
// the interrupt vector will be called
// when P1.3 goes from HitoLow as well as
// LowtoHigh
}

Theoretical Background

Programming

Input / Output ports are used to communicate the microcontroller with the outside world. It’s the traditional way humans interact with embedded systems. Digital I/O programming is simple with the LaunchPad microcontroller. Since the MSP430 has been designed with Memory Mapped I/O, its ports can be treated as cpu registers. When configuring a pin in a port (remember P1.0 is different from P1.1), you have to consider whether it will be used for input or output. For reading pushbuttons the pin must be set to input by setting its PxDIR to 0 (default). For LEDs, the P1DIR must be set to 1 (output).

This example uses interrupt routines. The difference between interrupts and polling is explained in Interrupts vs Polling. In summary, interrupts are more efficient, consuming less power, and enabling the mcu to process inputs only when necessary. Interrupts can be configured to occur when there is a change of input (high to low or low to high). This is called the pin edge and is set through PxIES.

PxIES = 0 means a change from low to high (0 to 1) will create an interrupt flag
PxIES = 1 means a change from high to low (1 to 0) will create an interrupt flag

 

For a detailed explanation of Digital I/O using the MSP430 please read MSP430x2xx Family User Guide.

                                                                              Launchpad datasheets Pxin.png 

                                                                               Launchpad datasheets Pxinterrupt.png

                                                                                                              MSP430x2xxx Familiy User Guide: page 8-3 


Each interrupt capable pin has the following registers available for configuration:

  • PxDIR : sets input or output direction
  • PxIN : current input value in that pin
  • PxOUT : sets the value to output in the pin
  • PxIES : sets the direction of the interrupt edge
  • PxIE : enables or disables interrupts for that pin
  • PxIFG : shows whether an interrupt flag was set

--registers in italic are only used with inputs

Hardware Interfacing

The LaunchPad kit includes two onboard pushbuttons and two LEDs. Nevertheless, it is essential to understand how to interface external hardware components to your MSP430. In this example, I’ll explain how to interface an external pushbutton.

There are two configurations: active low or active high. Their hardware configurations are depicted below. A pull-up or pull-down is necessary because it will force the pin to a default state when the button is de-pressed. If they are not present, the pin could depict erroneous values. An active low configuration means that the pin will read 1 when button is depressed and 0 when the button is pressed. It is “active” (pressed) when low (0). On the other hand, active high means 0 when depressed, 1 when pressed.

                          Active High:                   Msp430 launchpad Pull-downButton.png                                                                 Active Low:        Msp430 launchpad Pull-upButton.png

This illustrations show the two configurations.  For the sake of this example, I will use the active low configuration.

Buttons suffer from a mechanical issue that is called bouncing. When a button is pressed, mechanically, it bounces up and down for a couple of milli seconds before stabilizing. This means that instead of reading a 1 the mcu will receive a combination of lows and highs until stabilizing to 1. Each button has its own bouncing time, refer to the manufacturer datasheets for the specific value.

This issue can be handled through a process called debouncing. Software debouncing mainly delays reading the pin until the time has passed, or the value has stabilized. I will focus on hardware debouncing instead. For further information please refer to these websites. I use a simple circuit for debouncing, which I don’t believe is the best, yet does the job with minimal components. It involves an extra capacitor that charges and discharges as the button is pressed / depressed. The trick is to find the value for the capacitor which will not completely discharge while the button is bouncing. The equation for this RC circuit is the following. To calculate values, you need to assume or fix two of the three variables (I always calculate C from tbounce and R).

   

                                                                                                                  Equation of RC debouncing circuit

The following are snapshots of a switch pressed on before and after applying the debouncing circuit.

                              Fig. 1: Illustrates the bouncing of a push button                      Tactile switch debouncing oscilloscope.PNG

                               Fig. 1: Switch bouncing                                                                                    Fig. 2:  Switch after debouncing circuit                   

                 


Future Ideas

  • Add another button to toggle onboard LED at P1.6
  • Use external LEDs

References

  1. MSP430x2xx Family User Guide
  2. MSP430G2x31 Datasheet