Programming the Finite State Machine with 8-Bit PICs in Assembly and C - Andrew Pratt - E-Book

Programming the Finite State Machine with 8-Bit PICs in Assembly and C E-Book

Andrew Pratt

0,0
27,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

This practical guide is aimed at electronics students and hobbyists. It is intended to be a valuable aid in writing programs using Finite State Machines (FSMs) in assembly language using 8-bit PIC microcontrollers. The last two chapters introduce the use of the C programming language and make a direct comparison with development in Assembly. An FSM is a way of writing a program to make it easier to produce and modify. The machine is abstract in that it is just the structure of the program. This abstract machine can be represented by drawing a diagram on paper. The diagram is independent of the programming language used. The FSM chart gives a complete description of what the program does. It can then be implemented as source code. The book should appeal to those with an interest in the combination of electronics and software and have an interest in how things work. The book will describe writing code for two particular microcontrollers: The 12F1822 and 16F1823. Both are mid-range and inexpensive. To read and write the programs to and from the PICs, all that is required is an FTDI TTL level USB lead (TTL-232R-5V-WE) in addition to two programs that are both available for free download as executable files and source code from Elektor. Microsoft Windows or Linux can be used. The PIC programs are written in assembly language. This goes against the conventional wisdom of using a higher-level language such as C. One reason for this is that assembly is a good way of learning what is happening at the lowest level. This is important as microcontroller programming requires an understanding of the chip. Another reason for using the finite state machine approach is that it makes assembly programs surprisingly easy to follow. One of the main obstacles in the way of getting started with embedded programming is the installation and learning of new software tools. The emphasis of this book is on making things straightforward with as little complication as possible. Therefore you can concentrate on understanding the code. Real projects aren't just about coding: our software has to do something real. As a consequence, a chapter deals with a method of circuit board construction. All coding is done in a text editor of your choosing. The command line is used for running programs. If you are a Windows user, you might look at this as old fashioned. This is actually an efficient way of doing things: simple scripts for repetitive tasks save lots of mouse clicks. The last two chapters give an introduction to programming in C using the XC8 compiler. Again this is done using a text editor and the command line. The intention has been to achieve results using an inexpensive microcontroller with simple command line tools. Much emphasis is placed on using Microchip's datasheet as this is the best place to get correct detailed information

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB

Seitenzahl: 339

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Programming the Finite State Machine with 8-Bit PICs in Assembly and C

an Elektor Publication

Andrew Pratt

●This is an Elektor Publication. Elektor is the media brand of Elektor International Media B.V.78 York StreetLondon W1H 1DP, UKPhone: (+44) (0)20 7692 8344© Elektor International Media BV 2020First published in the United Kingdom 2020

●All rights reserved. No part of this book may be reproduced in any material form, including photocopying, or storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this publication, without the written permission of the copyright holder except in accordance with the provisions of the Copyright, Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London, England W1P 9HE. Applications for the copyright holder’s written permission to reproduce any part of this publication should be addressed to the publishers. The publishers have used their best efforts in ensuring the correctness of the information contained in this book. They do not assume, and hereby disclaim, any liability to any party for any loss or damage caused by errors or omissions in this book, whether such errors or omissions result from negligence, accident or any other cause.

●British Library Cataloguing in Publication DataCatalogue record for this book is available from the British Library

●ISBN: 978-1-907920-92-9

●EISBN: 978-3-89576-356-4

●EPUB: 978-3-89576-357-1

Prepress production: DMC ¦ [email protected]

Printed in the Netherlands by Wilco

Elektor is part of EIM, the world’s leading source of essential technical information and electronics products for pro engineers, electronics designers, and the companies seeking to engage them. Each day, our international team develops and delivers high-quality content - via a variety of media channels (e.g., magazines, video, digital media, and social media) in several languages - relating to electronics design and DIY electronics. www.elektor.com

● Table Of Contents

●5

Table of Contents

• Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Chapter 1 • Getting Started. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

1.1 •Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

1.2 •Practical Implementation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.2.1 • Choice of Operating System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

1.2.2 • Machine Language and Assembly Source Files. . . . . . . . . . . . . . . . . . . . 11

1.2.3 • Using the Applications on Microsoft Windows. . . . . . . . . . . . . . . . . . . . . 13

1.2.4 ● Installing the Applications on Linux. . . . . . . . . . . . . . . . . . . . . . . . . . . 14

1.2.5 • The FTDI Lead and Testing the Programming Chain. . . . . . . . . . . . . . . . . 15

1.3 • Some Fundamentals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

1.3.1 • Bits and Bytes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

1.3.2 • The Hexadecimal Numbering System. . . . . . . . . . . . . . . . . . . . . . . . . . 20

1.3.3 • Boolean Logic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

1.3.4 • Bitwise logic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

1.3.5 • PIC Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

1.3.6 • Data Memory Organization. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

1.3.7 • An Assembly Program Snippet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

1.4 • Program Memory. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

1.5 • Hello World. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

1.5.1 • Some Key Points. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

1.5.2 • Assembling the Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

Chapter 2 • The Assembly Program as a Finite State Machine. . . . . . . . . . . . . . . . . 32

2.1 • Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

2.2 • A State Machine Framework for Assembly Language. . . . . . . . . . . . . . . . . . . . 32

2.3 • Timer0. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

2.4 • Interrupts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

2.5 • A More Complicated LED Flasher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

2.6 • Running More Than One Machine in a Program. . . . . . . . . . . . . . . . . . . . . . . 43

2.7 • Driving a Seven Segment LED Display. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

2.8 • The Differences Between the PIC 12F1822 and the 16F1823. . . . . . . . . . . . . . 51

2.9 • Interrupts and State Diagrams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

Chapter 3 • Macros, Subroutines and Bank Switching . . . . . . . . . . . . . . . . . . . . . . . 53

3.1 • Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53

3.2 • Create Your Own Instruction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

Programming the Finite State Machine with 8-Bit PICs in Assembly and C

●6

3.2.1 • Use an Include File. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

3.2.2 • More Macros. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

3.2.3 • Conditional Assembly. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60

3.3 • Subroutines. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

3.3.1 • An Example of a Subroutine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61

3.3.2 • Return Address and the Stack. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

3.3.3 • Calculating the Delay. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

3.3.4 • Calling Subroutines from Subroutines. . . . . . . . . . . . . . . . . . . . . . . . . . 64

3.4 • Bank Switching. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

Chapter 4 • Inputs and Outputs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

4.1 • Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

4.2 • Serial Output to a Computer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

4.2.1 • TTL Level Serial Communications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

4.2.2 • Configuring the EUSART to Transmit a Byte. . . . . . . . . . . . . . . . . . . . . . 68

4.2.3 • Serial Output Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

4.3 • Serial Input from a Computer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72

4.3.1 • Configuring the EUSART to Receive Bytes. . . . . . . . . . . . . . . . . . . . . . . 73

4.3.2 • Interrupt Service Routine. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

4.3.3 • Serial Input Example. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

4.4 • Analog Inputs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

4.4.1 ● Setting ADCON0 and ADCON1. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78

4.4.2 ● Circuit and State Diagram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

4.5 ● Pulse with modulated outputs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

4.5.1 ● LED with Pulsing Brightness. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

4.6 ● Digital Inputs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

4.6.1 ● Counting Input Pulses From a Switch. . . . . . . . . . . . . . . . . . . . . . . . . . 85

4.6.2 ● First method eliminating the effect of switch bounce. . . . . . . . . . . . . . . . 88

4.6.3 ● A Better Method of Eliminating the Effect of Switch Bounce. . . . . . . . . . . 90

Chapter 5 • Project Hardware Construction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

5.1 • Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

5.2 • Overview of the Suggested Method. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93

5.3 • Cutting and Drilling the board. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

5.4 • Populating and Wiring the Board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

5.5 • The Circuit Board Test Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97

5.5.1 • Analog Configuration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

● Table Of Contents

●7

Chapter 6 • Binary Arithmetic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

6.1 • Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

6.2 • Binary Addition of Unsigned Numbers. . . . . . . . . . . . . . . . . . . . . . . . . . . . 103

6.2.1 • Adding Two Eight Bit Positive Integers. . . . . . . . . . . . . . . . . . . . . . . . 104

6.2.2 • Serial Read Program Command Line Arguments. . . . . . . . . . . . . . . . . . 107

6.2.3 • Adding Two Sixteen Bit Positive Numbers. . . . . . . . . . . . . . . . . . . . . . 108

6.3 • Binary Subtraction of unsigned integers. . . . . . . . . . . . . . . . . . . . . . . . . . . 111

6.4 • Binary Subtraction with Negative Results. . . . . . . . . . . . . . . . . . . . . . . . . . 113

6.5 • Negative numbers in binary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

6.6 • Binary Multiplication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

6.7 • Binary Division. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

Chapter 7 • Digital Voltmeter Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

7.1 • Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

7.2 • The State Diagrams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

7.3 • Scaling the Raw Analog Value. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133

7.4 • Extracting the individual figures for the display. . . . . . . . . . . . . . . . . . . . . . 135

7.5 • Detecting No Input Voltage. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

7.6 • Recalibration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

Chapter 8 • Troubleshooting and Planning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

8.1 • Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

8.2 • Have an Overview of the Project. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

8.2.1 • State Diagrams and Flow Charts. . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

8.3 • Break Big Problems Down Into Smaller Ones. . . . . . . . . . . . . . . . . . . . . . . . 137

8.4 • Read Through Your Code in Detail and Add Comments. . . . . . . . . . . . . . . . . 137

8.5 • Debugging a Running Program. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137

8.6 • Traffic Lights. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

8.6.1 • A Circuit Diagram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

8.6.2 • Separate Different Problems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140

8.6.3 • Producing the Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141

8.7 • Using Debug Macro on the Voltmeter Programmable. . . . . . . . . . . . . . . . . . . 148

8.8 • A List of Things to Remember. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150

Chapter 9 • A Comparison with C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

9.1 • Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

9.2 • The Microchip XC8 Compiler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

9.2.1 • XC8 for Microsoft Windows. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

9.2.2 • XC8 for 32 Bit Debian-Based Distributions. . . . . . . . . . . . . . . . . . . . . . 151

9.2.3 • XC8 for 64 Bit Debian-Based Distributions. . . . . . . . . . . . . . . . . . . . . . 152

9.2.4 • XC8 for 64-Bit Fedora. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152

9.3 • Introduction to C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

Programming the Finite State Machine with 8-Bit PICs in Assembly and C

●8

9.3.1 • Hello World in C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

9.3.2 • Using the XC8 Compiler in Microsoft Windows. . . . . . . . . . . . . . . . . . . 156

9.3.3 • Using the XC8 Compiler in Linux. . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

9.3.4 • Emitted Code Assembly vs C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157

9.3.5 • Interrupts in C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

9.3.6 • The __delay() functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161

9.3.7 • Extending the If Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

9.3.8 • The Switch Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

9.3.9 • An Experiment to Measure Code Speed. . . . . . . . . . . . . . . . . . . . . . . 167

9.4 • Serial Communication. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

9.4.1 • Serial Byte Transmission. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

9.4.2 • Serial Byte Reception. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169

Chapter 10 • Further C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

10.1 • Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

10.2 • Data Types. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

10.3 • More on Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172

10.4 • Integer Arithmetic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

10.4.1 • Transmitting a Four-Byte Integer. . . . . . . . . . . . . . . . . . . . . . . . . . . 173

10.4.2 • The "for" loop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175

10.5 • The Voltmeter in C. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179

10.5.1 • Turning Bits On and Off Using Bit Type. . . . . . . . . . . . . . . . . . . . . . . 179

10.5.2 • Turning Bits On and Off Using Bitwise Operators . . . . . . . . . . . . . . . . 183

10.5.3 • Turning Bits On and Off Using Bitfields. . . . . . . . . . . . . . . . . . . . . . . 183

10.6 • Summary of Assembly, C, and Finite State Machines. . . . . . . . . . . . . . . . . 184

• Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

● Preface

●9

• Preface

This practical guide is aimed at electronics students and hobbyists. It is intended to be a valuable aid in writing programs using Finite State Machines (FSMs) in assembly language using 8-bit PIC microcontrollers. The last two chapters introduce the use of the C programming language and make a direct comparison with development in Assembly.

An FSM is a way of writing a program to make it easier to produce and modify. The machine is abstract in that it is just the structure of the program. This abstract machine can be represented by drawing a diagram on paper. The diagram is independent of the programming language used. The FSM chart gives a complete description of what the program does. It can then be implemented as source code.

The book should appeal to those with an interest in the combination of electronics and software and have an interest in how things work. The book will describe writing code for two particular microcontrollers: The 12F1822 and 16F1823. Both are mid-range and inexpensive. To read and write the programs to and from the PICs, all that is required is an FTDI TTL level USB lead (TTL-232R-5V-WE) in addition to two programs that are both available for free download as executable files and source code from Elektor. Microsoft Windows or Linux can be used.

The PIC programs are written in assembly language. This goes against the conventional wisdom of using a higher-level language such as C. One reason for this is that assembly is a good way of learning what is happening at the lowest level. This is important as microcontroller programming requires an understanding of the chip. Another reason for using the finite state machine approach is that it makes assembly programs surprisingly easy to follow. One of the main obstacles in the way of getting started with embedded programming is the installation and learning of new software tools.

The emphasis of this book is on making things straightforward with as little complication as possible. Therefore you can concentrate on understanding the code. Real projects aren’t just about coding: our software has to do something real. As a consequence, a chapter deals with a method of circuit board construction.

All coding is done in a text editor of your choosing. The command line is used for running programs. If you are a Windows user, you might look at this as old fashioned. This is actually an efficient way of doing things: simple scripts for repetitive tasks save lots of mouse clicks. The last two chapters give an introduction to programming in C using the XC8 compiler. Again this is done using a text editor and the command line.

The intention has been to achieve results using an inexpensive microcontroller with simple command line tools. Much emphasis is placed on using Microchip’s datasheet as this is the best place to get correct detailed information.

Programming the Finite State Machine with 8-Bit PICs in Assembly and C

●10

Chapter 1 •Getting Started

1.1 •Introduction

Don’t be put off by the technical-sounding words ‘Finite State Machine’ (FSM). The principle of how they work is really easy to understand. Firstly, the machine is not an actual machine that you have to build. It is merely a structure your program has. The idea is that the program is broken down into a set number of states. When the program is in one of these states, it can respond to a set of inputs or events resulting in the program jumping to a different state. The outputs from the program depend only on the state (Moore machine) or on the state and the inputs (Mealy machine). Let’s start with the basic ‘Hello World’ microcontroller program to flash an LED as an example of a state machine with two states, LED ON and OFF.

Figure 1-1 displays what the program does and how to write it.

What the program does is:

After power up, the initial conditions are the LED is ON and the timer is reset and started.

After the timer expires, the LED turns OFF and the timer is reset and started.

After the timer expires again, the LED turns ON and the timer is reset and started.

This continues until the microcontroller is powered down

In this example, there is only one event that triggers a change of state: This being the expiry of the timer. The effect of the expiry depends on the current state - it might jump to the LED ON state or the LED OFF state. The fact that an input or event is only used by the current state helps to make a more complicated program robust and easier to follow. This example is a Mealy machine, where outputs or actions are determined by the current state and inputs. On the state diagrams, the transitions between states are shown by arrows. Alongside these are the input conditions that trigger transitions and the resulting outputs. In a Moore machine, the outputs depend only on the current state of the machine. The outputs would be shown on the diagram inside the states. This book is specifically about practical programs. I will not try to adhere to strict definitions. As for how

Figure 1-1 A two-state machine

Chapter 1 ● Getting Started

●11

to write the program, you have already done the important bit by planning. All that has to be done now is to implement the diagram as code and this will be less prone to error as you can concentrate on local detail. The book will mostly use the Mealy type machine. Inputs causing a transition will be separated by a forward slash from the outputs. This will be written next to the transition between two states. With