HomeArticlesProjectsBlogContact
Articles
Talking About Micros - State Machine
Colin Mitchell
Colin Mitchell
June 12, 2014
Make sure to subscribe to our newsletter and be the first to know the news.

TalkingAboutMicrosHeading

Chapter 1

Chapter 2a - State Machine

What is a STATE MACHINE and do we need to learn about it?

Before we get into our simple method of programming a microcontroller, we will look at the term: STATE MACHINE and see if it is something FANTASTIC that we should cover.
Learning about a “MACHINE that has STATES” also involves learning about FLOW DIAGRAMS and both of these are a way to break a complex task into simple sections before you start to program.
The only problem is this:
They both involve new terms and terminology such as drawing diagrams with squares and rectangles and diamonds and words such as STATE and EVENT.
In our method of “cut-and-paste” programming you are creating a linear program where the micro advances down the program and carries out an operation according to the instruction it is presently interpreting.
In fact, you are producing a FLOW DIAGRAM without using any of the technical terminology and creating a STATE MACHINE without even realising.
Thus you are not missing out on any BRILLIANT STRATEGY or CLEVER PROGRAMMING TRICKS.
The only BRILLIANT and CLEVER programming comes from some of the wizards of programming in the form of sub-routines to MULTIPLY and DIVIDE and these are contained in our library of sub-routines.

To show you are not missing out on anything, here is an article on STATE MACHINE:

One common way of conquering difficult software design problems is to use a STATE MACHINE. First you figure out all the states the software can be in. Then you determine all the inputs to the state machine all the events that can cause the state machine to take some action or to change states. Finally you determine the state machine outputs all the actions that the state machine can perform.

When your state machine design is done, you’ll have a list of states, a list of events (inputs), and a set of action procedures for each state that describe what the state machine does for each event (outputs).

There are two ways to code a state machine. One way uses a set of nested statements. The outer has a case for each possible state. Each of these outer case has an inner with a case for each possible event. The actual code that gets selected performs the actions for that state/event. Alternately, the outer statement could have a case for each event, and the inner could have a case for each state.

Another more concise way of coding is to use a lookup table. First, number all your states consecutively, starting with 0 is a convenient way to do this. Do the same for your events. Then make up a set of tables, one table per state. Each table has one entry per event, in the same order as the event. Then the entire set of tables is arranged in the same order as the state. Each item in a table is the function to execute to perform the action for that particular event in that particular state.

enum states { STATE_1, STATE_2, STATE_3, MAX_STATES }

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values we have.
enum is short for enumeration - meaning to “count-off” or name one-by-one.

Introducing STATE MACHINE just produces more complexity to a discussion but you will learn lots of new and complex words.
However, we are here to keep things simple - not to show how complex we can get.

(C) 2003 Hank Wallace
What Are State Machines?

Programmers today need every trick they can muster to reduce time to market and increase code reliability and maintainability. One of the tools that many programmers are somewhat familiar with but do not use often is the state machine. State machines are one of the most useful simple tools that we have at our disposal. They are not so much a different computing mechanism as a different thinking mechanism, allowing us to divide complex or tedious algorithms into manageable pieces.

The state machine is an abstract mechanism that has a number of well defined resting states. Inputs to the state machine cause the resting state to change depending on the intended purpose. We are concerned here with finite state machines (which have a finite number of states) implemented in software and hardware.

Small state machines are represented by bubble diagrams, with each bubble representing a resting state. Lines between the states are called edges or transitions. The state machine can occupy only one state at a time and can only transition from one state to another along the edges leading to other states, and only under the labeled conditions.

In our programming, we show how to deal with a complex problem by simply looking at each requirement as the micro advances down the program and dealing with the condition by going to a sub-routine.
This simplifies everything so you don’t need to draw-up complex diagrams or learn anything else.
There is more than one way to do solve a problem.


Tags

#elektor
Colin Mitchell

Colin Mitchell

Expertise

electronics
writing
PIC-Chips

Social Media

instagramtwitterwebsite

Related Posts

TODO
PIC Elmer 160
March 06, 2012
© 2021, All Rights Reserved.

Quick Links

Advertise with usAbout UsContact Us

Social Media