Professional Documents
Culture Documents
Lecture 6:
Finite state machines
7-1
Finite State Machine (FSM)
❑Finite State Machines (FSMs)
❖Set of inputs, outputs, states and transitions
❖State graph defines input/output relationship
7-2
Finite State Machine (FSM)
7-3
Finite State Machine (FSM)
❑What is a finite state machine?
❖Inputs (sensors)
❖Outputs (actuators)
❖Controller
❖State graph
7-4
Finite State Machine (FSM)
7-5
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
7-6
Finite State Machine (FSM)
7-7
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-8
Traffic Light Control
7-11
Traffic Light Control
7-12
Linked Data Structure
goN DCD 0x21 ;North green, East red
DCD 3000 ;30 sec
DCD goN,waitN,goN,waitN
waitE Base address+12 goN Base address waitN Base address+12 goE Base address
waitE Base address+8 goN Base address waitN Base address+8 goE Base address
#define goN 0
#define waitN 1
#define goE 2
#define waitE 3
State 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-21
# define INPUT (*((volatile uint32_t *) 0x400243FC))
Homework:
How would you redefine INPUT and LIGHT using bit specific addressing:
Bit 0 and 1 of PORTE
Bit 0,1,2,3,4,5,6 of PORTB
7-22
address1
# define PORTE 0x400243FC
address2 0x400243FC
address3
# define PORTE (uint32_t *) 0x400243FC address4
address5
address6
# define PORTE (*((uint32_t *) 0x400243FC))
ROM
0x400243FC GPIO_PORTE_DATA_R
RAM
7-23
# define PORTE (* ((uint32_t *) 0x400243FC))
Danger:
GPIO_POR RAM may contain
Processor RAM TE_DATA_ outdated copy
R of PORT E data
7-24
FSM Engine in C (Indexes)
void main(void) {
uint32 CS; // index of current state
uint32_t Input;
&goE goN
&goE goN
&goE goN
&goE goN
Time = 500 Time = 500
&FSM[1] = waitN Out = 0x22 &FSM[3] = waitE Out = 0x14
7-27
Food of thought
7-28
FSM Engine in C (Pointers)
void main(void) {
State *Pt; // state pointer
uint32_t Input;
waitN
// initialize ports and timer goN
… waitN
Pt
goN
Pt = goN; // start state
Time = 3000
while(1) {
Out = 0x21
LIGHT = Pt->Out; // set lights
SysTick_Wait10ms(Pt->Time);
Input = SENSOR; // read sensors
Pt = Pt->Next[Input];
}
} 7-29
FSM for adjusting duty cycle of LED
waveform
❑ 1) The system starts with 90% duty-cyle
❑ 2) Turn the LED on/off at current duty-cycle
❑ 3) If the switch is pressed, then switch duty-cycle
❖Cycle: 50->70->90->10->30->50...
❑ 4) Steps 1 and 2 are repeated over and over
7-30
FSM for adjusting duty cycle of LED waveform
0
High Low
1 0
0
450ms 50ms
0
High Low
1 0
0
350ms 150ms
0
High Low
1 0
0
250ms 250ms
0
High Low
1 0
0
150ms 350ms
0
High Low
1 0
0
50ms 450ms
7-31
FSM for adjusting duty cycle of LED
waveform
0
High Low 1
1 0 0 1
450ms 50ms Change0
1
0
50ms
0 0
High Low
1 0 0
350ms 150ms
0
High Low
1 0 0
250ms 250ms
0
High Low
1 0 0
150ms 350ms
0
High Low
1 0 0
50ms 450ms
7-32
FSM for adjusting duty cycle of LED waveform
0
High Low 1
1 0 0 1
450ms 50ms Change0
1
0
50ms
0 0 1
High Low
1 1
0 0
350ms 150ms Change1
1
0
50ms
0 0
High Low
1 0 0
250ms 250ms
0
High Low
1 0 0
150ms 350ms
0
High Low
1 0 0
50ms 450ms
7-33
FSM for adjusting duty cycle of LED waveform
0
High Low 1
1 0 0 1
450ms 50ms Change0
1
0
50ms
0 0 1
High Low
1 1
0 0
350ms 150ms Change1
1
0
50ms
0 0
High Low 1
1 0 0 1
250ms 250ms Change2
1
0
50ms
0 0
High Low 1
1 0 1
0
150ms 350ms Change3
1
0
50ms 0 1
0 0
High Low
1 0 0 Change4
1
50ms 450ms 0
1 7-34
50ms
Homework
7-35
FSM Engine in C (Pointers)
void main(void) {
State_t *Pt; // state pointer
uint32_t Input; Exact same
controller as
before
// initialize ports and timer
…
7-37
Thank You
Q&A
7-38
SysTick Timer in C
#define NVIC_ST_CTRL_R(*((volatile uint32_t *)0xE000E010))
#define NVIC_ST_RELOAD_R(*((volatile uint32_t *)0xE000E014))
#define NVIC_ST_CURRENT_R(*((volatile uint32_t *)0xE000E018))
void SysTick_Init(void){
NVIC_ST_CTRL_R = 0; // 1) disable SysTick during setup
NVIC_ST_RELOAD_R = 0x00FFFFFF; // 2) maximum reload value
NVIC_ST_CURRENT_R = 0; // 3) any write to CURRENT clears it
NVIC_ST_CTRL_R = 0x00000005; // 4) enable SysTick with core clock
}