Craig Evans
/
2645_FSM_UpDown_Counter
Sample code from ELEC2645 Week 16 Lab Demonstrates how to implement a simple FSM up/down counter
Revision 1:fb324de02b7f, committed 2017-12-20
- Comitter:
- eencae
- Date:
- Wed Dec 20 11:53:00 2017 +0000
- Parent:
- 0:c5278b91719f
- Commit message:
- Updated mbed library and pins/outputs for Gamepad.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Jan 07 10:00:24 2016 +0000 +++ b/main.cpp Wed Dec 20 11:53:00 2017 +0000 @@ -2,7 +2,7 @@ 2645_FSM_UpDown_Counter -Sample code from ELEC2645 Week 16 Lab +Sample code from ELEC2645 Demonstrates how to implement a simple FSM up/down counter @@ -25,35 +25,36 @@ // K64F on-board switches InterruptIn sw2(SW2); InterruptIn sw3(SW3); +// Gamepad Button A +InterruptIn buttonA(PTB9); -// LEDs to display counter output -// connect up external LEDs to these pins with appropriate current-limiting resistor -BusOut output(PTB2,PTB3,PTB10,PTB11); +// LEDs on Gamepad (1 to 4) - active-low 0 = on and 1 = off +BusOut output(PTA1,PTA2,PTC2,PTC3); // array of states in the FSM, each element is the output of the counter -int fsm[4] = {1,2,4,8}; +// set the output in binary to make it easier, 0 is LED on, 1 is LED off +int fsm[4] = {0b0111,0b1011,0b1101,0b1110}; // flag - must be volatile as changes within ISR // g_ prefix makes it easier to distinguish it as global -volatile int g_sw2_flag = 0; +volatile int g_buttonA_flag = 0; // function prototypes -// error function hangs flashing an LED -void error(); // set-up the on-board LEDs and switches void init_K64F(); -// SW2 interrupt service routine -void sw2_isr(); +// Button A interrupt service routine +void buttonA_isr(); int main() { // initialise on-board LED and switches init_K64F(); - // SW2 has a pull-up resistor, so the pin will be at 3.3 V by default - // and fall to 0 V when pressed. We therefore need to look for a falling edge - // on the pin to fire the interrupt - sw2.fall(&sw2_isr); + // Button A is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister + buttonA.mode(PullDown); + + // It will return 0 by default and a 1 when pressed i.e. cause a rising edge + buttonA.rise(&buttonA_isr); // set inital state int state = 0; @@ -63,8 +64,8 @@ while(1) { // loop forever // check if flag i.e. interrupt has occured - if (g_sw2_flag) { - g_sw2_flag = 0; // if it has, clear the flag + if (g_buttonA_flag) { + g_buttonA_flag = 0; // if it has, clear the flag // swap direction when button has been pressed // (could just use ! but want this to be explicit to aid understanding) @@ -121,12 +122,12 @@ } break; default: // default case - error(); //invalid state - call error routine + error("Invalid state!"); //invalid state - call error routine // or could jump to starting state i.e. state = 0 break; } - wait(0.2); // small delay + wait(0.5); // small delay } @@ -146,18 +147,8 @@ } -void error() +// Button A event-triggered interrupt +void buttonA_isr() { - while(1) { // if error, hang while flashing error message - r_led = 0; - wait(0.2); - r_led = 1; - wait(0.2); - } -} - -// SW2 event-triggered interrupt -void sw2_isr() -{ - g_sw2_flag = 1; // set flag in ISR + g_buttonA_flag = 1; // set flag in ISR } \ No newline at end of file
--- a/mbed.bld Thu Jan 07 10:00:24 2016 +0000 +++ b/mbed.bld Wed Dec 20 11:53:00 2017 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/4336505e4b1c \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/e7ca05fa8600 \ No newline at end of file