PICkit ? 1 Flash Starter Kit User’s Guide
C.4
LESSON 2 – INTRODUCTION TO STATE MACHINES
State Machines are an integral part of software programming. State machines make
code more efficient, easier to debug and help organize the program flow. State
machines are not limited to just firmware, they can be used to streamline any system.
However, this document limits the scope of state machines to microcontroller firmware.
C.4.1
C.4.1.1
Design
WHAT IS A STATE MACHINE
A Finite State Machine (FSM) is based on the idea of there being finite number of states
for a given system. For instance, when an application turns an LED on and off, two
states exist; one state is when the LED is on and the other is when it is off. The example
firmware that this document refers to turns on eight LEDs sequentially. Only one LED
is on at a time, therefore eight states exist. Each state consists of one LED being turned
on while all the rest are off.
State machines require a State Variable (SV). The SV is essentially a pointer that keeps
track of the state that the microcontroller is in, and directs the program flow to the
corresponding software module. The SV can be modified in the software modules (or
states) themselves or by an outside function. The example firmware uses an outside
function which detects a button press to advance through the states.
C.4.1.2
BENEFITS OF STATE MACHINES
The introduction briefly mentioned some advantages of state machines. These advan-
tages are worth studying in greater detail because they are what make programming
with state machines so rewarding and beneficial to firmware developers.
The first advantage is using state machines inherently promotes good firmware design
techniques. When beginning to implement an application, think about what states are
necessary to make the application work. List all the pieces, or states, of an application
and then explore how they tie to one another. This will help prevent developing bugs in
the code. This line of thinking also leads to the development of a very useful engineer-
ing tool – the flow chart. The following paragraph covers state machine development in
greater detail.
State machines have one characteristic that cause them to be very beneficial. They
always return to one spot (or jump station) in the code at which the program flow is
channeled, by the state variable, to the corresponding software module. This provides
several advantages. First, this characteristic makes calling repetitive tasks on a regular
basis quite simple. Clearing the watchdog timer, checking for I/O button presses or
communicating with a host that requires periodic communication are examples of
repetitive tasks. The alternative to using state machines is to use looping code. In order
for looping code to handle repetitive tasks, the functions that handle these tasks must
be distributed throughout the code in each of the loops. This is not only highly inefficient
but also confusing to understand. Figure C-5 shows a block diagram of what the
example code would look like if a state machine where not used. Compared to
Figure C-6, which shows a block diagram of state machine based code, it is clear that
using a state machine cuts down on code space and the likelihood of missing a
repetitive task call.
DS40051D-page 46
? 2004 Microchip Technology Inc.
相关PDF资料
DV164120 KIT STARTER PICKIT 2
DV164121 KIT PICKIT 2 DEBUG EXPRESS
DV164122 ANALYZER SRL PICKIT W/DEMO BOARD
DV164131 KIT STARTER PICKIT 3
DV164132 KIT EVAL F1 FOR PIC12F1/PIC16F1
DV243003 KIT STARTER FOR SRL MEM PRODUCTS
DVA1001 ADAPTER FOR PIC16F716 18DIP
DVA1004 DEVICE ADAPTER 8/14/20DIP
相关代理商/技术参数
DV164101 制造商:Microchip Technology Inc 功能描述:TOOLS: FLASH MICROCONTROLLER (
DV164102 功能描述:开发板和工具包 - 无线 rfPICkit RoHS:否 制造商:Arduino 产品:Evaluation Boards 工具用于评估:AT32UC3L 核心:AVR32 频率: 接口类型:USB 工作电源电压:5 V
DV164120 功能描述:电路内置调试器 PICkit 2 8/14/20P Flash RoHS:否 制造商:Microchip Technology 产品:In-Circuit Debugger Kits 工具用于评估:PIC MCUs, dsPIC DSCs 用于:07-00024, AC164113 核心:dsPIC, PIC 接口类型:USB 工作电源电压:3 V to 5 V
DV164121 功能描述:电路内置调试器 PICkit 2 Debug Express RoHS:否 制造商:Microchip Technology 产品:In-Circuit Debugger Kits 工具用于评估:PIC MCUs, dsPIC DSCs 用于:07-00024, AC164113 核心:dsPIC, PIC 接口类型:USB 工作电源电压:3 V to 5 V
DV164121 制造商:Microchip Technology Inc 功能描述:ICPICKIT2 PROGRAMMER/DEBUGGER ((NW))
DV164121+TEFLCST3 制造商:Microchip Technology Inc 功能描述:KIT PICKIT2+FLOWCODE-HOME BUNDLE 制造商:Microchip Technology Inc 功能描述:ICD, PICKIT 2, FLOW CODE, PIC, DSPIC 制造商:Microchip Technology Inc 功能描述:ICD, PICKIT 2, DEBUG EXP, FLOW CODE, PIC, DSPIC; Silicon Family Name:PIC12F6xx, PIC16F5xx; Core Architecture:PIC; Core Sub-Architecture:PIC12, PIC18, PIC24; IC Product Type:Debugger / Programmer; Series:PICkit 2 ;RoHS Compliant: Yes
DV164122 功能描述:界面开发工具 PICkit Ser Analyzer RoHS:否 制造商:Bourns 产品:Evaluation Boards 类型:RS-485 工具用于评估:ADM3485E 接口类型:RS-485 工作电源电压:3.3 V
DV164126 功能描述:开发板和工具包 - PIC / DSPIC USB Kit w/ PICkit LowPinCount RoHS:否 制造商:Microchip Technology 产品:Starter Kits 工具用于评估:chipKIT 核心:Uno32 接口类型: 工作电源电压: