int main(void) { SYSTEMConfigPerformance(10000000); initTimer1(); initSW2(); initLEDs(); enableInterrupts(); state = led1; while(1) { //TODO: Using a finite-state machine, define the behavior of the LEDs //Debounce the switch switch(state) { case led1: turnOnLED(1); stateNext = led2; break; case led2: turnOnLED(2); stateNext = led1; break; case deBounce1: delayUs(10000); break; case deBounce2: delayUs(10000); break; } } return 0; }
int main(void) { enableInterrupts(); initLEDs(); initSW2(); initTimer2(); updateLEDState(); while(1) { switch (currentState) { case wait: if(buttonState==risingEdge)//button is pressed { currentState=debouncePress; buttonState=Idle; } // if(IFS1bits.CNDIF==1) // //if(PORTDbits.RD6==0) //!!! use change notifications, not polling // { // currentState=debouncePress; // } break; case debouncePress: delayUs(50); currentState=waitForRelease; break; case waitForRelease: //!!! use change notifications, not polling if(buttonState==fallingEdge)//button released { currentState=debounceRelease; buttonState=Idle; } // if(IFS1bits.CNDIF==1) // //if(PORTDbits.RD6==1)//released // { // currentState=debounceRelease; // } break; case debounceRelease: delayUs(50); updateLEDState(); currentState=wait; break; } } return 0; }
/* Please note that the configuration file has changed from lab 0. * the oscillator is now of a different frequency. */ int main(void) { SYSTEMConfigPerformance(40000000); enableInterrupts(); initLEDs(); initSW2(); state = RUN; while(1) { switch(state) { case RUN: led1 = ON; led2 = OFF; lastState = RUN; break; case STOP: led1 = OFF; led2 = ON; lastState = STOP; break; case debouncePress: delayMs(5); if(lastState==RUN) state = STOP; else if(lastState==STOP) state = RUN; else state = RUN; break; case debounceRelease: delayMs(5); state = lastState; break; } } return 0; }
int main(void) { SYSTEMConfigPerformance(10000000); enableInterrupts(); initLEDs(); initSW2(); int PrevState=1; int state=ledrun; while(1) { switch(state){ //LED 1 is on case ledrun: LEDRUN=ON; LEDSTOP=OFF; PrevState=1; if(switchFlag==1) //if switch is pressed { switchFlag=0; state=debouncePress; } break; case ledstop: //LED 2 is on LEDRUN=OFF; LEDSTOP=ON; PrevState=2; if(switchFlag==1) //if switch is pressed { switchFlag=0; state=debouncePress; } break; case debouncePress: //calls delay and moves to wait state delayMs(50); state=wait; break; case debounceRelease: //calls delay and moves to wait 2 aka logic state delayMs(50); state=wait2; break; case wait: //waits until switch is released if(switchFlag==0) { state=wait; } else { switchFlag=0; state=debounceRelease; } break; case wait2: //uses flag that is triggered if switch is held for longer than if(PrevState==1) { state=ledstop; } else if(PrevState==2) { state=ledrun; } break; } } return 0; }