Title: Electronics II Physics 3620 / 6620
1Electronics IIPhysics 3620 / 6620
- Jan 28, 2009
- Part 1
- Finite State Machine (continued)
2Example I Even or odd
- The following machine determines whether the
number of As in a string is even or odd - A circles represents a state (also known as a
vertex) an arrow (or arc) represents a
transition
- This is an example of a State Diagram
- In Labview we represent different states using
the case structure - Inputs are the characters of a string
- The output is the resultant state see in-class
example implemented in Labview (even_odd.vi)
3Labview Implementation
- Implementation in Labview is based on a case
structure (whi8ch represents the state machine
itself, and a while-loop around it
- The actual state is kept by a shift register
within the while loop - The shift register shows up as an input
(initialize) and output pair
Programming ? Numeric ? Enum Const,.
- In some cases you can specify an exit state (as
shown) to stop
4State Transition
- There are different methods to determine which
state to transition to next. Four common methods
are discussed below (Note in the following
method examples, Init could transition to any
state)
- Case 2 the init state has two possible
transitions, Shut Down or Power Up.
- Case 1 the Init state that has only one
possible transition.
Programming ? Comparison ? Select
5More Cases
- Case 4
- Init state using an inner loop and case
structure to transition to the next state. The
inner case structure contains one diagram for
each transition that leaves the current state.
Each of the cases in the inner case structure has
two outputs a boolean value, which specifies
whether or not the transition should be taken,
and an enumerated constant, which specifies the
state to which the transition goes. By using the
loop index as an input to the case structure,
this code effectively runs through each
transition case one by one, until it finds a
diagram with a True boolean output. After the
True boolean output is found, the case outputs
the new state to which the transition goes.
Though this method may appear slightly more
complicated than the previous methods, it does
offer the ability to add names to transitions by
casting the output of the loop index to an
enumerated type. This benefit allows you to add
automatic documentation to your transition
code.
- Case 3
- Init state using a boolean array along with an
array of enum constants. There is a boolean in
the boolean array corresponding to each
transition Init can make. The array of enum
constants represents each transition name. The
index of the first True boolean in the boolean
array corresponds to the index of the new state
in the array of enums. - See programming ? Arrays ? to see the different
types of arrays
6Error states
- Some state machines may have a error state with
the following characteristics - An unexpected input will cause a transition to
the error state - All subsequent inputs cause the state machine to
remain in the error state
7Simplifying State Diagrams I
- State machines can get pretty complicated
- We can simplify the State Diagram by leaving
out the error state - The error state is still part of the machine
- Any input without a transition on our drawing is
assumed to go to the error state - Another simplification Use to indicate all
other characters - This is a convention when drawing the machineit
does not mean we look for an asterisk in the
input
8Example II Nested parenthesis
- The following example tests whether parentheses
are properly nested (up to 3 deep)
- How can we extend this machine to handle
arbitrarily deep nesting?
9Nested parentheses II
- Question How can we use a state machine to check
parenthesis nesting to any depth? - Answer We cant (with a finite number of states)
- We need to count how deep we are into a
parenthesis nest 1, 2, 3, ..., 821, ... - The only memory a pure state machine has is
which state it is in - However, if we arent required to use a pure
state machine, we can add memory (such as a
counter) and other features
10Nested parentheses III
- This machine is based on a state machine, but it
obviously is not just a state machine
11Example from NI Coke Machine
- Specifications
- This application has the following requirements
- All Coke products are sold for 50 cents (This was
still true on campus in 2004) - The machine only accepts nickels, dimes, and
quarters. - Exact change is not needed.
- Change can be returned at anytime during the
process of entering coins.
- The States
- 1.) INIT initialize our Coke Machine2.) WAIT
FOR EVENT where the machine waits for coins3.)
RETURN CHANGE where the machine returns
change4.) COKE PRODUCT when the machine has
received 50 or more cents it will dispense the
beverage5.) QUARTER when the customer enters a
quarter6.) DIME when the customer enters a
dime7.) NICKLE when the customer enters a
nickel8.) EXIT after the change is returned
and/or beverage dispensed, the machine will power
down (application will terminate)
12The State Diagram
13Mealy and Moore machines
- Moore machine
- Associates its outputs with states
- The outputs are represented either within the
vertex corresponding to a state or adjacent to
the vertex - Mealy machine
- Associates its outputs with the transitions
- In addition to the input values, each arc also
shows the output values generated during the
transition the format of the label of each arc
is Inputs/Outputs - Both can be used to represent any sequential
system and each has its advantages.
14Example Alarm clock state table
Present State Alarm Weekday Next State Turn off alarm
Asleep On X Awake in bed Yes
Awake in bed Off Yes Awake and up No
Awake in bed Off No Asleep No
- When you are asleep and alarm goes on, you go
from being asleep to being awaked in bed you
also turn off the alarm - The next two rows encode your actions
- You get up
- You go back to sleep
- This table doesnt cover what you wouldnt
do(i.e. if you are asleep and the alarm doesn't
go off, you remain asleep, etc..)
15Alarm clock state table
Present State Alarm Weekday Next State Turn off alarm
Asleep Off X Asleep No
Asleep On X Awake in bed Yes
Awake in bed On X Awake in bed Yes
Awake in bed Off Yes Awake and up No
Awake in bed Off No Asleep No
Awake and up X X Awake and up No
- Covers all the cases
- First row covers the situation you are asleep,
the alarm doesnt go off and you remain asleep - Last row covers the situation you are awake and
up and you remain awake and up - The third row covers the case you are already up
and the alarm goes off. You turn it off and
remain Awake in bed
16Moore machine diagram
- Self arcs can be missing (since it outputs are
associated with the states and not with the arcs) - Offers a simpler implementation when the output
values depend only on the state and not on the
transitions - It requires less hardware to produce the output
values than does a Mealy machine, since its
outputs depend only on its state and its input
values - It is well suited for representing the control
units of microprocessors
17Mealy machine diagram
- Self arcs must be shown (because the output
values are shown on the arcs) - Can be more compact than Moore machine,
especially when two or more arcs with different
output values go into the same state