Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- eencae
- Date:
- 2016-01-07
- Revision:
- 0:49ec0f45aa1c
- Child:
- 1:9d9040c75f6b
File content as of revision 0:49ec0f45aa1c:
/*
2645_FSM_UpDown_Counter_Transition_Table
Sample code from ELEC2645 Week 16 Lab
Demonstrates how to implement a simple FSM up/down counter using a transition table
(c) Craig A. Evans, University of Leeds, Jan 2016
*/
#include "mbed.h"
// pre-processor directives
// defines directions as 0/1. Note UPPERCASE
#define UP 0
#define DOWN 1
// 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);
// struct for state
struct State {
int output; // output value for current state
float time; // wait time for state
int next_state[2]; // next state (depending on direction 0 - UP, 1 - DOWN)
};
typedef const struct State STyp;
// array of states in the FSM, each element is the output of the counter, wait time, next state(s)
STyp fsm[4] = {
{1,0.2,{1,3}},
{2,0.2,{2,0}},
{4,0.2,{3,1}},
{8,0.2,{0,2}}
};
// flag - must be volatile as changes within ISR
// g_ prefix makes it easier to distinguish it as global
volatile int g_sw2_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();
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);
// set initial state
int state = 0;
// set initial direction
int direction = UP;
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
// swap direction when button has been pressed
direction = ! direction;
}
// set output of current state (use dot syntax to access struct members)
output = fsm[state].output;
// implement required delay
wait(fsm[state].time);
// set the next state depending on direction
state = fsm[state].next_state[direction];
}
}
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);
}
}
// SW2 event-triggered interrupt
void sw2_isr()
{
g_sw2_flag = 1; // set flag in ISR
}