Professional Documents
Culture Documents
Lec4 FSM
Lec4 FSM
Lecture 4:
Finite State Machines (FSMs)
7-1
Finite State Machine (FSM)
Finite State Machines (FSMs)
Set of inputs, outputs, states and transitions
State graph defines input/output relationship
What is a state?
Description of current conditions
What is a state graph?
Graphical interconnection between states
What is a controller?
Software that inputs, outputs, changes state
Accesses the state graph
https://www.youtube.com/playlist?list=PLyg2vmIzGxXEle4_R2VA_J5uwTWktdWTu
7-2
Finite State Machine (FSM)
7-3
Finite State Machine (FSM)
Moore FSM
output value depends only on the current state,
inputs affect the state transitions
significance is being in a state
Input: when to change state
Output: definition of being in that state
Next if input is 01 or 11 00,01
00,01,
00,10 01,11 10,11 10,11 Output
goN waitN goE waitE
100001 100010 001100 010100
30 5 30 5
Wait time 00,01,10,11
7-4
Finite State Machine (FSM)
7-5
Finite State Machine (FSM)
Mealy FSM
output value depends
on input and current
state
inputs affect the state
Inputs: Control
transitions. PA2 Brake
LM3S/TM4C Machine
significance is the state PA1 Gas
PA0 Control
transition
Gas
Input: when to change Brake
Control
0/00 0/10 Initial
Stop
state Go
1/01 1ms 1/00 1ms
Output: how to
change state Outputs: Brake, Gas
7-6
Finite State Machine (FSM)
7-7
Finite State Machine (FSM)
7-8
FSM Implementation
Data Structure embodies the FSM
multiple identically-structured nodes
statically-allocated fixed-size linked structures
one-to-one mapping FSM state graph and
linked structure
one structure for each state
Linked Structure
pointer (or link) to other nodes (define next
states)
Table structure
indices to other nodes (define next states)
7-9
Example: Traffic Light Control
PE1=0, PE0=0 means no cars exist on either road
PE1=0, PE0=1 means there are cars on the East road
PE1=1, PE0=0 means there are cars on the North road
PE1=1, PE0=1 means there are cars on both roads
PE1 North
LM3S or
TM4C PE0
PB5 R
PB4 Y East
PB3 G
PB2
PB1 R Y G
PB0
goN, PB5-0 = 100001 makes it green on North and red on East
waitN, PB5-0 = 100010 makes it yellow on North and red on East
goE, PB5-0 = 001100 makes it red on North and green on East
waitE, PB5-0 = 010100 makes it red on North and yellow on East
7-10
Example: Traffic Light Control
http://youtu.be/kgABPjf9qLI
7-11
FSM Data Structure in C
const struct State { // State data structure
uint32_t Out; // Output
uint32_t Time; // Time in 10 ms units
uint32_t Next[4]; // list of next states from
// state transition table
};
typedef const struct State STyp;
STyp FSM[4] = {
{0x21, 3000, {goN,waitN,goN,waitN}},
{0x22, 500, {goE,goE,goE,goE}},
{0x0C, 3000, {goE,goE,waitE,waitE}},
{0x14, 500, {goN,goN,goN,goN}}
}; 7-12
FSM Engine in C
void main(void) {
uint32 CS; // index of current state
uint32_t Input;
while(1) {
LIGHT = FSM[CS].Out; // set lights according to state
SysTick_Wait10ms(FSM[CS].Time); //Timer delay
Input = SENSOR; // read input sensors
CS = FSM[CS].Next[Input]; // select next state from
// table according to input
}
} 7-13