Skip to content

Instantly share code, notes, and snippets.

@jimmitt
Created February 5, 2014 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimmitt/8815159 to your computer and use it in GitHub Desktop.
Save jimmitt/8815159 to your computer and use it in GitHub Desktop.
/*
* ButtonTest by James Trimble, Capt, USAF
* USAFA ECE Automata Club
* Description: Uses polling to detect button presses, then increments a 2-bit counter
* which is displayed on the on-board LEDs tied to P1.0 and P1.6
*/
#include <msp430.h>
/*
* main.c
*/
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1DIR |= 0x41; // Configure P1.0 and P1.6 for output
P1DIR &= ~0x08; // Configure P1.3 for input
P1REN |= 0x08; // Enable the resistor for P1.3
char counter = 0x00;
while (1==1) {
if (!(P1IN & 0x08)) { // If bit 3 is low
int i = 0;
for (i; i < 10000; i++); // Delay for debouncing
if (!(P1IN & 0x08)) { // If bit 3 is low
counter++;
if (counter == 4) {
counter = 0;
}
if (counter & 0x02) { // If bit 1 is set
P1OUT |= 0x01; // Turn on bit 0
}
else {
P1OUT &= ~0x01; // Turn off bit 0
}
if (counter & 0x01) { // If bit 0 is set
P1OUT |= 0x40; // Turn on bit 6
}
else {
P1OUT &= ~0x40; // Turn off bit 6
}
}
// wait for the button to be released
while (!(P1IN & 0x08));
}
}
return 0;
}
@toddbranch
Copy link

Code might be slightly clearer:

P1DIR |= BIT6 | BIT0; // Configure P1.0 and P1.6 for output

Could use that throughout. No change in functionality, but maybe a little more readable.

@jimmitt
Copy link
Author

jimmitt commented Feb 6, 2014

I agree. I'll rewrite it to make it more clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment