Created
April 29, 2011 14:10
-
-
Save scsibug/948352 to your computer and use it in GitHub Desktop.
MSP430 button and LED demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*********************************************************** | |
* TI MSP-430 LaunchPad Button & LED-Blinking Demo | |
* | |
* Simple example of using a button to manipulate LEDs. | |
* | |
* Pressing the button (P1.3) will toggle a pattern of the | |
* red/green LEDs alternating on/off. | |
* | |
* Tested on an MSP430G2231, built with mspgcc toolchain. | |
**********************************************************/ | |
#include <io.h> | |
#include <signal.h> | |
#define BUTTON BIT3 // Port 1.3 | |
#define REDLED BIT0 // Port 1.0 | |
#define GRNLED BIT6 // Port 1.6 | |
int doflip = 1; | |
int main(void) { | |
// Watchdog setup | |
WDTCTL = WDTPW + WDTHOLD; // stop watchdog (password + hold counter) | |
// LED initial setup | |
P1DIR |= REDLED + GRNLED; // set P1.0 and P1.6 as output (1) pins | |
P1OUT |= REDLED; // Enable REDLED | |
P1OUT &= ~GRNLED; // Disable GRNLED | |
// Button setup | |
P1DIR &= ~BUTTON; // button is an input | |
P1OUT |= BUTTON; // pull-up resistor | |
P1REN |= BUTTON; // resistor enabled | |
P1IES |= BUTTON; // interrupt on low-to-high transition | |
P1IE |= BUTTON; // interrupt enable | |
// Timer setup | |
BCSCTL3 |= LFXT1S_2; // Set clock source to VLO | |
TACCR0 = 2500; // Count to this, then interrupt | |
TACTL = TASSEL_1 | MC_1; // Clock source ACLK | |
TACCTL1 = CCIE; // Timer A interrupt enable | |
// Shut down CPU, General Interrupt Enabled | |
__bis_SR_register(CPUOFF | GIE); | |
return 0; | |
} | |
interrupt(TIMERA1_VECTOR) ta1_isr(void) { | |
TACCTL1 &= ~CCIFG; // reset the interrupt | |
if (doflip) | |
P1OUT ^= (REDLED + GRNLED); // flip LED output | |
} | |
interrupt(PORT1_VECTOR) port1_isr(void) { | |
P1IFG = 0; // reset interrupt | |
doflip ^= 1; // toggle LED flipping | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tjo