Fast / slow blinking LED example for MSP430 and switch

This is a simple switch input example for the MSP430G2553. A pull-down resistor keeps digital input pin P2.3 at 0V normally. However, when the switch is pressed, the pin is short-circuited to 3V. The program running on the MSP430 blinks and LED on and off. The duration of the delay between flashes is controlled by the state of P2.3.

Thanks to David Coyle and Conor Mahony for lending me their switch circuit and code.

MSP430 switch input example circuit

//
// Blinking LED example for MSP430G2553 or MSP430G2452
// Written by David Coyle & Conor Mahony - Last modified 7-2-2014
// LED blinks fast or slow depending on whether P2.3 is high or low
//
  
#include <msp430.h>
  
int main( void )
{
    int sw;
     
    WDTCTL = WDTPW + WDTHOLD; // Disable watchdog timer
      
    P1DIR = 0b00000001; // P1.0 is an output, P1.1-7 are inputs
    P2DIR = 0b00000000; // P2.0-7 are inputs
      
    // Main loop repeats forever
    while(1)
    {
        // read switch state (P2.3) into a variable
        sw = P2IN & 0b00001000;
         
        P1OUT = 0b00000001; // LED on
        __delay_cycles(100000);
         
        P1OUT = 0b00000000; // LED off
         
        if (sw == 0) __delay_cycles(500000);
        else __delay_cycles(100000);
    }
      
    // The program never actually reaches this point
    return 0;
}

Photos of circuit:

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s