Craig Evans
/
2645_FSM_Counter
Demonstrates how to implement a simple FSM counter
main.cpp
- Committer:
- eencae
- Date:
- 2016-01-06
- Revision:
- 1:81b129bec569
- Parent:
- 0:e8be38c50b44
- Child:
- 2:bdd8b01141d1
File content as of revision 1:81b129bec569:
/* 2645_FSM_Counter Sample code from ELEC2645 Week 16 Lab Demonstrates how to implement a simple FSM counter (c) Craig A. Evans, University of Leeds, Jan 2016 */ #include "mbed.h" // K64F on-board LEDs DigitalOut r_led(LED_RED); DigitalOut g_led(LED_GREEN); DigitalOut b_led(LED_BLUE); // K64F on-board switches InterruptIn sw2(SW2); InterruptIn sw3(SW3); // LEDs to display counter output // connect up external LEDs to these pins with appropriate current-limiting resistor BusOut output(PTB2,PTB3,PTB10,PTB11); // array of states in the FSM, each element is the output of the counter int fsm[4] = {1,2,4,8}; // function prototypes // error function hangs flashing an LED void error(); // set-up the on-board LEDs and switches void init_K64F(); int main() { init_K64F(); // set inital state int state = 0; while(1) { // loop forever output = fsm[state]; // output current state // check which state we are in and see which the next state should be switch(state) { case 0: state = 1; break; case 1: state = 2; break; case 2: state = 3; break; case 3: state = 0; break; default: error(); //invalid state - call error routine // or could jump to starting state i.e. state = 0 break; } wait(0.2); // small delay } } void init_K64F() { // on-board LEDs are active-low, so set pin high to turn them off. r_led = 1; g_led = 1; b_led = 1; // since the on-board switches have external pull-ups, we should disable the internal pull-down // resistors that are enabled by default using InterruptIn sw2.mode(PullNone); sw3.mode(PullNone); } void error() { while(1) { // if error, hang while flashing error message r_led = 0; wait(0.2); r_led = 1; wait(0.2); } }