Skip to content

Instantly share code, notes, and snippets.

@s-estay
Created September 7, 2016 23:31
Show Gist options
  • Save s-estay/5d33c1432b5ff96507a36820d2e7479c to your computer and use it in GitHub Desktop.
Save s-estay/5d33c1432b5ff96507a36820d2e7479c to your computer and use it in GitHub Desktop.
msp430: toggling LED2 when S2 is pressed
// Source: Programmable Microcontrollers with Applications, p.117
// Toggling the green LED when the push button is pressed
// MSP430 LaunchPad Rev.1.5
#include <msp430g2553.h>
#define LED BIT6 //P1.6 LED2
#define BUTTON BIT3 //P1.3 S2
void delay_ms(int); // declare function delay_ms
void main(void){
WDTCTL = WDTPW|WDTHOLD; // stop WDT
P1DIR = LED; // P1.0 set as output
P1REN = BUTTON; // internal resistor connected to S2 enabled
P1OUT = BUTTON; // internal resistor set as pull-up
/* check input from the push button */
while(1){
if((P1IN & BUTTON) == 0x00){ // check if S2 (active low) is pressed
delay_ms(5); // wait 5ms
if((P1IN & BUTTON) == 0x00){ // if S2 still pressed ..
P1OUT ^= LED; // .. toggle LED2
while((P1IN & BUTTON) == 0x00); // wait for the release of S2
}
}
}
}
void delay_ms(int a){ // define function delay_ms
while(a != 0){ // while variable a is different from 0
__delay_cycles(1000); // 1000 cycles at 1MHz (SMCLK) gives 1ms
a--; // decrement variable a
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment