#include #define LEDul 0x01 //The upper left LED is connected to bit 0 #define LEDml 0x02 //The middle left LED is connected to bit 1 #define LEDll 0x04 #define LEDc 0x08 //The center LED is connected to bit 3 #define LEDur 0x10 #define LEDmr 0x20 #define LEDlr 0x40 #define PB 0x40 #define DIESTATE1 1 //Note: these numbers are abitrary, #define DIESTATE2 2 // they just need to be unique. #define DIESTATE3 3 #define DIESTATE4 4 #define DIESTATE5 5 #define DIESTATE6 6 //Also... if you know C, a quicker way to do this is to create an enumerated type (enum) void LED_Die(int val) { switch (val) { case 0: P1OUT = 0; break; case 1: P1OUT = LEDc; break; case 2: P1OUT = LEDur | LEDll; break; case 3: P1OUT = LEDc | LEDul | LEDlr; break; case 4: P1OUT = LEDul | LEDll | LEDur | LEDlr; break; case 5: P1OUT = LEDc | LEDul | LEDll | LEDur | LEDlr; break; case 6: P1OUT = LEDul | LEDll | LEDur | LEDlr | LEDml | LEDmr; break; default: P1OUT = 0; // This should never happen! } } //********************************************************************* void main(void) { volatile int myState; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog P1DIR = P1DIR | LEDc |LEDul | LEDll | LEDur | LEDlr | LEDml | LEDmr; // P1.0-P1.6 are output P2SEL = P2SEL & ~PB; //Clear select bit (makes P2.6 an input) myState = DIESTATE1; while (1) { //Do this forever //************************************************************** //Code for state transitions. This code only figures out what //state transitions to make, and then makes them. switch (myState) { case DIESTATE1: if (!(PB & P2IN)) myState = DIESTATE2; break; case DIESTATE2: if (!(PB & P2IN)) myState = DIESTATE3; break; case DIESTATE3: if (!(PB & P2IN)) myState = DIESTATE4; break; case DIESTATE4: if (!(PB & P2IN)) myState = DIESTATE5; break; case DIESTATE5: if (!(PB & P2IN)) myState = DIESTATE6; break; case DIESTATE6: if (!(PB & P2IN)) myState = DIESTATE1; break; default: myState=DIESTATE1; // This should never happen! } //************************************************************** //Code for state outputs. This just decides on system outputs //based on the current state and inputs to system. switch (myState) { case DIESTATE1: if (!(PB & P2IN)) LED_Die(0); else LED_Die(1); break; case DIESTATE2: if (!(PB & P2IN)) LED_Die(0); else LED_Die(2); break; case DIESTATE3: if (!(PB & P2IN)) LED_Die(0); else LED_Die(3); break; case DIESTATE4: if (!(PB & P2IN)) LED_Die(0); else LED_Die(4); break; case DIESTATE5: if (!(PB & P2IN)) LED_Die(0); else LED_Die(5); break; case DIESTATE6: if (!(PB & P2IN)) LED_Die(0); else LED_Die(6); break; default: LED_Die(0); // This should never happen! } } }