Programming the Finite State Machine with 8-Bit PICs in Assembly and C
By Andrew Pratt
An Elektor Publication
Table of Contents
- Preface
- Chapter 1: Getting Started
- 1.1 Introduction
- 1.2 Practical Implementation
- 1.2.1 Choice of Operating System
- 1.2.2 Machine Language and Assembly Source Files
- 1.2.3 Using the Applications on Microsoft Windows
- 1.2.4 Installing the Applications on Linux
- 1.2.5 The FTDI Lead and Testing the Programming Chain
- 1.3 Some Fundamentals
- 1.3.1 Bits and Bytes
- 1.3.2 The Hexadecimal Numbering System
- 1.3.3 Boolean Logic
- 1.3.4 Bitwise logic
- 1.3.5 PIC Architecture
- 1.3.6 Data Memory Organization
- 1.3.7 An Assembly Program Snippet
- 1.4 Program Memory
- 1.5 Hello World
- 1.5.1 Some Key Points
- 1.5.2 Assembling the Program
- Chapter 2: The Assembly Program as a Finite State Machine
- 2.1 Introduction
- 2.2 A State Machine Framework for Assembly Language
- 2.3 Timer0
- 2.4 Interrupts
- 2.5 A More Complicated LED Flasher
- 2.6 Running More Than One Machine in a Program
- 2.7 Driving a Seven Segment LED Display
- 2.8 The Differences Between the PIC 12F1822 and the 16F1823
- 2.9 Interrupts and State Diagrams
- Chapter 3: Macros, Subroutines and Bank Switching
- 3.1 Introduction
- 3.2 Create Your Own Instruction
- 3.2.1 Use an Include File
- 3.2.2 More Macros
- 3.2.3 Conditional Assembly
- 3.3 Subroutines
- 3.3.1 An Example of a Subroutine
- 3.3.2 Return Address and the Stack
- 3.3.3 Calculating the Delay
- 3.3.4 Calling Subroutines from Subroutines
- 3.4 Bank Switching
- Chapter 4: Inputs and Outputs
- 4.1 Introduction
- 4.2 Serial Output to a Computer
- 4.2.1 TTL Level Serial Communications
- 4.2.2 Configuring the EUSART to Transmit a Byte
- 4.2.3 Serial Output Example
- 4.3 Serial Input from a Computer
- 4.3.1 Configuring the EUSART to Receive Bytes
- 4.3.2 Interrupt Service Routine
- 4.3.3 Serial Input Example
- 4.4 Analog Inputs
- 4.4.1 Setting ADCON0 and ADCON1
- 4.4.2 Circuit and State Diagram
- 4.5 Pulse with modulated outputs
- 4.5.1 LED with Pulsing Brightness
- 4.6 Digital Inputs
- 4.6.1 Counting Input Pulses From a Switch
- 4.6.2 First method eliminating the effect of switch bounce
- 4.6.3 A Better Method of Eliminating the Effect of Switch Bounce
- Chapter 5: Project Hardware Construction
- 5.1 Introduction
- 5.2 Overview of the Suggested Method
- 5.3 Cutting and Drilling the board
- 5.4 Populating and Wiring the Board
- 5.5 The Circuit Board Test Program
- 5.5.1 Analog Configuration
- Chapter 6: Binary Arithmetic
- 6.1 Introduction
- 6.2 Binary Addition of Unsigned Numbers
- 6.2.1 Adding Two Eight Bit Positive Integers
- 6.2.2 Serial Read Program Command Line Arguments
- 6.2.3 Adding Two Sixteen Bit Positive Numbers
- 6.3 Binary Subtraction of unsigned integers
- 6.4 Binary Subtraction with Negative Results
- 6.5 Negative numbers in binary
- 6.6 Binary Multiplication
- 6.7 Binary Division
- Chapter 7: Digital Voltmeter Project
- 7.1 Introduction
- 7.2 The State Diagrams
- 7.3 Scaling the Raw Analog Value
- 7.4 Extracting the individual figures for the display
- 7.5 Detecting No Input Voltage
- 7.6 Recalibration
- Chapter 8: Troubleshooting and Planning
- 8.1 Introduction
- 8.2 Have an Overview of the Project
- 8.2.1 State Diagrams and Flow Charts
- 8.3 Break Big Problems Down Into Smaller Ones
- 8.4 Read Through Your Code in Detail and Add Comments
- 8.5 Debugging a Running Program
- 8.6 Traffic Lights
- 8.6.1 A Circuit Diagram
- 8.6.2 Separate Different Problems
- 8.6.3 Producing the Code
- 8.7 Using Debug Macro on the Voltmeter Programmable
- 8.8 A List of Things to Remember
- Chapter 9: A Comparison with C
- 9.1 Introduction
- 9.2 The Microchip XC8 Compiler
- 9.2.1 XC8 for Microsoft Windows
- 9.2.2 XC8 for 32 Bit Debian-Based Distributions
- 9.2.3 XC8 for 64 Bit Debian-Based Distributions
- 9.2.4 XC8 for 64-Bit Fedora
- 9.3 Introduction to C
- 9.3.1 Hello World in C
- 9.3.2 Using the XC8 Compiler in Microsoft Windows
- 9.3.3 Using the XC8 Compiler in Linux
- 9.3.4 Emitted Code Assembly vs C
- 9.3.5 Interrupts in C
- 9.3.6 The _delay() functions
- 9.3.7 Extending the If Statement
- 9.3.8 The Switch Statement
- 9.3.9 An Experiment to Measure Code Speed
- 9.4 Serial Communication
- 9.4.1 Serial Byte Transmission
- 9.4.2 Serial Byte Reception
- Chapter 10: Further C
- 10.1 Introduction
- 10.2 Data Types
- 10.3 More on Functions
- 10.4 Integer Arithmetic
- 10.4.1 Transmitting a Four-Byte Integer
- 10.4.2 The "for" loop
- 10.5 The Voltmeter in C
- 10.5.1 Turning Bits On and Off Using Bit Type
- 10.5.2 Turning Bits On and Off Using Bitwise Operators
- 10.5.3 Turning Bits On and Off Using Bitfields
- 10.6 Summary of Assembly, C, and Finite State Machines
- Index
Chapter 1: Getting Started
This chapter introduces the fundamental concepts required for programming 8-bit PIC microcontrollers, focusing on the practical aspects of setting up the development environment and understanding basic programming constructs.
1.1 Introduction
Provides an overview of the book's scope and the importance of finite state machines in embedded systems.
1.2 Practical Implementation
Details the practical steps involved in implementing finite state machines on PIC microcontrollers.
1.2.1 Choice of Operating System
Discusses the considerations for choosing an operating system for microcontroller development, including options like Microsoft Windows and Linux.
1.2.2 Machine Language and Assembly Source Files
Explains the difference between machine language and assembly language, and the structure of assembly source files.
1.2.3 Using the Applications on Microsoft Windows
Guides users on utilizing development tools and applications within the Microsoft Windows environment.
1.2.4 Installing the Applications on Linux
Provides instructions for installing and configuring development tools on Linux-based systems.
1.2.5 The FTDI Lead and Testing the Programming Chain
Covers the use of FTDI leads for programming PIC microcontrollers and testing the overall programming chain.
1.3 Some Fundamentals
Covers essential foundational knowledge for microcontroller programming.
1.3.1 Bits and Bytes
Explains the basic units of digital information: bits and bytes, and their representation.
1.3.2 The Hexadecimal Numbering System
Details the hexadecimal numbering system and its application in programming.
1.3.3 Boolean Logic
Introduces the principles of Boolean logic, including AND, OR, and NOT operations.
1.3.4 Bitwise Logic
Explains bitwise operations such as AND, OR, XOR, and bit shifting.
1.3.5 PIC Architecture
Provides an overview of the architecture of PIC microcontrollers.
1.3.6 Data Memory Organization
Describes how data is organized and stored in the memory of PIC microcontrollers.
1.3.7 An Assembly Program Snippet
Presents a small example of an assembly language program for PIC microcontrollers.
1.4 Program Memory
Discusses the structure and usage of program memory in PIC microcontrollers.
1.5 Hello World
Introduces a basic "Hello World" program as a starting point for learning.
1.5.1 Some Key Points
Highlights important considerations for the "Hello World" program.
1.5.2 Assembling the Program
Explains the process of assembling the "Hello World" program.
Chapter 2: The Assembly Program as a Finite State Machine
This chapter focuses on implementing finite state machines using assembly language programming for PIC microcontrollers.
2.1 Introduction
Introduces the concept of using assembly language to create finite state machines.
2.2 A State Machine Framework for Assembly Language
Presents a structured framework for building state machines in assembly code.
2.3 Timer0
Explains the functionality and usage of the Timer0 peripheral in PIC microcontrollers.
2.4 Interrupts
Covers the concept of interrupts and how they are handled in assembly programs.
2.5 A More Complicated LED Flasher
Details the creation of a more complex LED flasher program using state machines.
2.6 Running More Than One Machine in a Program
Discusses techniques for managing multiple state machines within a single program.
2.7 Driving a Seven Segment LED Display
Explains how to control a seven-segment LED display using assembly language.
2.8 The Differences Between the PIC 12F1822 and the 16F1823
Highlights the key differences between the PIC 12F1822 and PIC 16F1823 microcontrollers.
2.9 Interrupts and State Diagrams
Explores the relationship between interrupts and state diagrams in program design.
Chapter 3: Macros, Subroutines and Bank Switching
This chapter delves into advanced assembly programming techniques, including macros, subroutines, and bank switching.
3.1 Introduction
Introduces the topics of macros, subroutines, and bank switching in assembly programming.
3.2 Create Your Own Instruction
Explains how to create custom instructions or macros to simplify coding.
3.2.1 Use an Include File
Demonstrates the use of include files to organize and reuse code.
3.2.2 More Macros
Covers the creation and usage of more complex macros.
3.2.3 Conditional Assembly
Explains how to use conditional assembly directives to control code compilation.
3.3 Subroutines
Details the concept and implementation of subroutines (functions) in assembly language.
3.3.1 An Example of a Subroutine
Provides a practical example of creating and using a subroutine.
3.3.2 Return Address and the Stack
Explains how the stack is used to manage subroutine calls and return addresses.
3.3.3 Calculating the Delay
Shows methods for calculating and implementing delays in assembly programs.
3.3.4 Calling Subroutines from Subroutines
Demonstrates how to nest subroutine calls.
3.4 Bank Switching
Explains the concept of bank switching for accessing memory in PIC microcontrollers.
Chapter 4: Inputs and Outputs
This chapter covers how to interface PIC microcontrollers with external devices through various input and output methods.
4.1 Introduction
Introduces the chapter's focus on input and output operations.
4.2 Serial Output to a Computer
Details how to send data from a PIC microcontroller to a computer via serial communication.
4.2.1 TTL Level Serial Communications
Explains the principles of TTL level serial communication.
4.2.2 Configuring the EUSART to Transmit a Byte
Guides on configuring the EUSART (Enhanced Universal Synchronous/Asynchronous Receiver/Transmitter) module for transmitting data.
4.2.3 Serial Output Example
Provides an example of a program that outputs data serially.
4.3 Serial Input from a Computer
Covers receiving data from a computer to a PIC microcontroller via serial communication.
4.3.1 Configuring the EUSART to Receive Bytes
Details the configuration of the EUSART module for receiving data.
4.3.2 Interrupt Service Routine
Explains the use of interrupt service routines for handling serial data reception.
4.3.3 Serial Input Example
Provides an example of a program that receives data serially.
4.4 Analog Inputs
Discusses how to read analog signals using the PIC microcontroller's Analog-to-Digital Converter (ADC).
4.4.1 Setting ADCON0 and ADCON1
Explains the configuration of the ADCON0 and ADCON1 registers for ADC operation.
4.4.2 Circuit and State Diagram
Presents a circuit diagram and state diagram related to analog input processing.
4.5 Pulse with modulated outputs
Covers generating modulated pulse outputs.
4.5.1 LED with Pulsing Brightness
Details how to control an LED's brightness using pulse width modulation (PWM).
4.6 Digital Inputs
Explains how to read digital signals from external sources.
4.6.1 Counting Input Pulses From a Switch
Demonstrates how to count pulses generated by a switch.
4.6.2 First method eliminating the effect of switch bounce
Presents a method for debouncing a mechanical switch.
4.6.3 A Better Method of Eliminating the Effect of Switch Bounce
Offers an improved technique for switch debouncing.
Chapter 5: Project Hardware Construction
This chapter provides guidance on constructing the hardware for projects discussed in the book.
5.1 Introduction
Introduces the hardware construction phase of the projects.
5.2 Overview of the Suggested Method
Outlines the recommended approach for building the project hardware.
5.3 Cutting and Drilling the board
Provides instructions for preparing the printed circuit board (PCB).
5.4 Populating and Wiring the Board
Details the process of placing components and making connections on the PCB.
5.5 The Circuit Board Test Program
Describes a test program to verify the functionality of the constructed circuit board.
5.5.1 Analog Configuration
Explains the analog configuration required for the test program.
Chapter 6: Binary Arithmetic
This chapter covers various arithmetic operations performed using binary numbers, relevant to microcontroller programming.
6.1 Introduction
Introduces the topic of binary arithmetic.
6.2 Binary Addition of Unsigned Numbers
Explains how to perform addition with unsigned binary numbers.
6.2.1 Adding Two Eight Bit Positive Integers
Demonstrates the addition of two 8-bit positive integers.
6.2.2 Serial Read Program Command Line Arguments
Discusses reading program command line arguments serially.
6.2.3 Adding Two Sixteen Bit Positive Numbers
Shows how to add two 16-bit positive integers.
6.3 Binary Subtraction of unsigned integers
Explains the process of subtracting unsigned binary numbers.
6.4 Binary Subtraction with Negative Results
Covers binary subtraction that results in negative numbers.
6.5 Negative numbers in binary
Discusses different methods for representing negative numbers in binary, such as two's complement.
6.6 Binary Multiplication
Explains the algorithms for binary multiplication.
6.7 Binary Division
Covers the algorithms for binary division.
Chapter 7: Digital Voltmeter Project
This chapter details a project to build a digital voltmeter using PIC microcontrollers.
7.1 Introduction
Introduces the digital voltmeter project.
7.2 The State Diagrams
Presents the state diagrams that define the operation of the digital voltmeter.
7.3 Scaling the Raw Analog Value
Explains how to scale the raw analog readings from the ADC to represent voltage values.
7.4 Extracting the individual figures for the display
Details the process of extracting individual digits for display on an output device.
7.5 Detecting No Input Voltage
Describes methods for detecting when there is no input voltage.
7.6 Recalibration
Covers the process of recalibrating the digital voltmeter for accuracy.
Chapter 8: Troubleshooting and Planning
This chapter provides guidance on troubleshooting common issues and planning microcontroller projects.
8.1 Introduction
Introduces the topics of troubleshooting and project planning.
8.2 Have an Overview of the Project
Emphasizes the importance of having a clear understanding of the project's goals and structure.
8.2.1 State Diagrams and Flow Charts
Discusses the use of state diagrams and flow charts in project planning and debugging.
8.3 Break Big Problems Down Into Smaller Ones
Advises on a systematic approach to problem-solving by breaking down complex issues.
8.4 Read Through Your Code in Detail and Add Comments
Stresses the importance of code readability and documentation through detailed review and commenting.
8.5 Debugging a Running Program
Offers techniques for debugging programs that are currently running on the microcontroller.
8.6 Traffic Lights
Uses a traffic light simulation as a case study for troubleshooting and implementation.
8.6.1 A Circuit Diagram
Presents a circuit diagram for the traffic light project.
8.6.2 Separate Different Problems
Discusses isolating and addressing different issues within the project.
8.6.3 Producing the Code
Covers the process of generating the final code for the traffic light system.
8.7 Using Debug Macro on the Voltmeter Programmable
Explains how to use debug macros with the voltmeter project.
8.8 A List of Things to Remember
Provides a summary of key points and best practices.
Chapter 9: A Comparison with C
This chapter compares programming in Assembly language with programming in C for PIC microcontrollers.
9.1 Introduction
Introduces the comparison between Assembly and C programming.
9.2 The Microchip XC8 Compiler
Discusses the Microchip XC8 compiler, a C compiler for PIC microcontrollers.
9.2.1 XC8 for Microsoft Windows
Details the usage of XC8 on the Windows platform.
9.2.2 XC8 for 32 Bit Debian-Based Distributions
Covers XC8 installation and usage on 32-bit Debian-based Linux systems.
9.2.3 XC8 for 64 Bit Debian-Based Distributions
Covers XC8 installation and usage on 64-bit Debian-based Linux systems.
9.2.4 XC8 for 64-Bit Fedora
Details XC8 usage on 64-bit Fedora Linux systems.
9.3 Introduction to C
Provides an introduction to the C programming language for embedded systems.
9.3.1 Hello World in C
Presents a "Hello World" program written in C.
9.3.2 Using the XC8 Compiler in Microsoft Windows
Explains how to use the XC8 compiler within the Windows environment.
9.3.3 Using the XC8 Compiler in Linux
Guides on using the XC8 compiler on Linux systems.
9.3.4 Emitted Code Assembly vs C
Compares the assembly code generated by the C compiler with hand-written assembly.
9.3.5 Interrupts in C
Discusses how to handle interrupts using C programming.
9.3.6 The _delay() functions
Explains the use of delay functions in C.
9.3.7 Extending the If Statement
Covers advanced usage of the 'if' statement in C.
9.3.8 The Switch Statement
Details the 'switch' statement for multi-way branching.
9.3.9 An Experiment to Measure Code Speed
Describes an experiment to measure the execution speed of C code.
9.4 Serial Communication
Covers serial communication protocols in C.
9.4.1 Serial Byte Transmission
Explains how to transmit data byte by byte serially in C.
9.4.2 Serial Byte Reception
Details how to receive data byte by byte serially in C.
Chapter 10: Further C
This chapter explores more advanced C programming concepts relevant to PIC microcontroller development.
10.1 Introduction
Introduces additional C programming topics.
10.2 Data Types
Discusses various data types available in C and their usage.
10.3 More on Functions
Covers advanced aspects of function definition and usage in C.
10.4 Integer Arithmetic
Explains integer arithmetic operations in C.
10.4.1 Transmitting a Four-Byte Integer
Demonstrates how to transmit a 4-byte integer value.
10.4.2 The "for" loop
Explains the usage of the 'for' loop for iterative tasks.
10.5 The Voltmeter in C
Details the implementation of the digital voltmeter project using C programming.
10.5.1 Turning Bits On and Off Using Bit Type
Shows how to manipulate individual bits using bit types.
10.5.2 Turning Bits On and Off Using Bitwise Operators
Explains the use of bitwise operators for bit manipulation.
10.5.3 Turning Bits On and Off Using Bitfields
Covers the use of bitfields for efficient data packing.
10.6 Summary of Assembly, C, and Finite State Machines
Provides a summary comparing Assembly, C, and finite state machine concepts.
Index
An index of terms and topics covered in the book.
Related Documents
![]() |
Starting Electronics: Microcontroller Basics with PIC A comprehensive guide to microcontroller basics using PIC devices, covering assembly and C programming, interrupts, addressing, and hardware interfaces. |
![]() |
Gids voor PIC Microcontroller Projecten Een uitgebreide gids met diverse projecten voor PIC microcontrollers, inclusief hardware, software, sensoren, communicatie, en meer. Bevat tutorials en appendices. |
![]() |
Explore ATtiny Microcontrollers: C and Assembly Programming Guide Learn AVR architecture and ATtiny microcontroller programming with this in-depth guide covering C and Assembly languages, development tools, and practical examples from Elektor. |
![]() |
AVR Hardware en C-Programmering in de Praktijk: Een Gids voor Ontwikkelaars Leer AVR microcontrollers programmeren in C met deze uitgebreide gids. Behandelt hardware, ontwikkelomgevingen, timers, displays, seriële communicatie en praktijkvoorbeelden van Florian Schäffer, uitgegeven door Elektor. |
![]() |
PIC Microcontrollers: A Comprehensive Guide Explore the world of PIC microcontrollers with this guide, covering essential concepts, hardware, software, and practical projects from basic switches to advanced sensors and communication. |
![]() |
Microcontrollers Programmeren: Leer PIC-microcontrollers programmeren met JAL Leer PIC-microcontrollers programmeren met de JAL taal in 60 lessen, met praktische opdrachten en uitleg over elektronica en programmering. Een Elektor publicatie. |
![]() |
Elektor Schaltungsverzeichnis: Elektronikprojekte für Hobby und Beruf Umfassendes Verzeichnis von Elektronikprojekten und Schaltungen, kategorisiert nach Audio/Video, Computer, Haus & Garten, Hobby & Modellbau, Messen & Testen, Mikrocontroller und Stromversorgung. Ideal für Maker und Elektronik-Enthusiasten. |
![]() |
Elektor Special Project Mikrocontroller 2 - November 2007 Overview of Elektor's Special Project Mikrocontroller 2 issue from November 2007, featuring articles on microcontrollers, robotics, sensors, and software development, with additional content available on CD. |