




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
embedded system assembly language notes
Typology: Lecture notes
1 / 196
This page cannot be seen from the preview
Don't miss anything!





























































































This book is intended for use by Junior-level undergraduates, Senior-level undergraduates, and Graduate students in electrical engineering as well as practicing electrical engineers and hobbyists and seeks to provide a gentle introduction to embedded systems programming with the Microchip PIC16F877 microcontroller. After introducing the PIC16F877 and its programming, this book covers the fundamental techniques and advanced level techniques of embedded systems programming in a general sense. The general sense ESP techniques can be applied to any microcontroller. There is also an introduction to the fundamentals of digital signal processing (DSP) using the PIC16F877.
I would like to thank Dr. Dan Simon of the Cleveland State University Electrical Engineering Department for his kind and valuable help and suggestions in the preparations for this book. I would also like to thank John R. Owerko and James R. Jackson, both of A.R.F Products, Inc., for their expertise in the security systems market. I owe them both a great debt for my knowledge of security systems and for expanding my knowledge of the techniques of embedded systems programming in general.
Special thanks also go to Sister Renee Oliver who proofread the manuscript and offered many helpful suggestions. Thanks go to my friends Damian Poirier, Jim Strieter, Greg Glazer, Zarif Bastawros, Brian McGeever, Ted Seman, and Jim Chesebrough who offered many helpful suggestions.
Any errors that remain in the text are mine and I will correct them in the next edition.
Timothy D. Green November 2005 Cleveland, Ohio
ABS = Absolute Value ACCUM = Accumulator ADC = Analog-to-Digital Converter ADPCM = Adaptive Differential Pulse Code Modulation ALU = Arithmetic Logic Unit
Arccos = Arc-Cosine ASCII = American Standard Code for Information Interchange ATM = Automatic Teller Machine BOR = Brown Out Reset BPF = Band Pass Filter
CK = Clock Cos = Cosine CPU = Central Processing Unit D = Data DAC = Digital-to-Analog Converter
dB = Decibels DC = Direct Current DDS = Direct Digital Synthesis DIP = Dual Inline Package DPSK = Differential Phase Shift Keying
DSP = Digital Signal Processing DTMF = Dual Tone Multi-Frequency EEPROM = Electrically Erasable Programmable Read Only Memory EMC = Electro-Magnetic Compatibility EMI = Electro-Magnetic Interference
EPROM = Erasable Programmable Read Only Memory ESP = Embedded Systems Programming Fmax = Maximum Frequency Fosc = Oscillator Frequency (of the PIC) Freq = Frequency
FSK = Frequency Shift Keying HPF = High Pass Filter Hz = Hertz IIC or I2C = Inter-Integrated Circuit INT = Interrupt
I/O = Input/Output ISR = Interrupt Service Routine kHz = Kilohertz LED = Light Emitting Diode LPF = Low Pass Filter
Max = Maximum MHz = Megahertz Min = Minimum ms = Milliseconds MSSP = Master Synchronous Serial Port
OSC = Oscillator PC = Personal Computer or Program Counter PIC = Peripheral Interface Controller PISO = Parallel-In, Serial-Out (Shift Register) PLL = Phase-Locked Loop
POR = Power-On Reset PROM = Programmable Read Only Memory PSP = Parallel Slave Port PWM = Pulse Width Modulation Q = Flip-Flop, Counter, or Shift Register Output State (Data Out)
RAM = Random Access Memory (A Read/Write Memory) RC = Resistor/Capacitor (Time Constant or Circuit) RF = Radio Frequency RFI = Radio Frequency Interference ROM = Read Only Memory
Sin or sin = Sine SIPO = Serial-In, Parallel-Out (Shift Register) SPI = Serial Peripheral Interface sqrt = Square Root SR = Sampling Rate
IBM and IBM-PC are registered trademarks of International Business Machines, Inc. PIC, PIC16F87X, PIC16F877, MPLAB, MPASM, In-Circuit Debugger, In- Circuit Serial Programmer are registered trademarks of Microchip Technology, Inc.
CTI and CTI Speech Compressor are registered trademarks of Compressed Time, Inc. Touch Tone, Unix, C, and C++ are registered trademarks of AT&T, Inc.
Linux is a registered trademark of the Free Software Foundation.
I2C, IIC, and Inter-Integrated Circuit are registered trademarks of Philips Corp.
The PIC Instruction Set, Assembly Keywords, and Mnemonics are Copyrighted by Microchip Technology, Inc.
Maxim and MAX690CPA are registered trademarks of Maxim Corporation.
Borland, Turbo, Turbo C++ are registered trademarks of Inprise, Inc.
An embedded system is a product which uses a computer to run it but the product, itself, is not a computer. This is a very broad and very general definition. Embedded systems programming, therefore, consists of building the software control system of a computer-based product. ESP encompasses much more than traditional programming techniques since it actually controls hardware in advance of real time. ESP systems often have limitations on memory, speed, and peripheral hardware. The goals of ESP programmers are to get the “maximum function and features in the minimum of space and in minimum time”.
Embedded systems are everywhere! Name almost any appliance in your home or office and it may have a microprocessor or a microcomputer to run it. A watch, microwave oven, telephone, answering machine, washer, dryer, calculator, toy, robot, test equipment, medical equipment, traffic light, automobile computer, VCR, CD player, DVD player, TV, radio, and printer all have computers in them to run them.
These examples of embedded systems are simple but the concept of embedded systems applies to much larger systems as well. Overall, there are four levels of size, option, and complexity in embedded systems. These levels are:
A good example of a high level embedded system is an air-traffic control system. It would use a main-frame computer with many terminals and many users on a time- sharing basis. It would connect to several smaller computers, run the radar, receive telemetry, get weather information, have extensive communications sub-systems, and coordinate all of these function in an orderly, systematic way. It is necessarily a high- reliability system and may, therefore, have extensive backup systems. It would have a custom-built operating system that would be completely dedicated to controlling air- traffic.
An example of a medium level embedded system is a typical automatic teller machine (ATM) at any bank or bank terminal. It may use a more advanced microprocessor with many peripheral functions. Consider that it contains a video terminal, a keyboard, a card-reader, a printer, the money-dispensing unit, a modem, and many input/output ports. The ATM probably doesn’t use a custom operating system but would use something off the shelf, like Unix or Linux. The controlling software is probably written in a high-level language like C or C++.
Embedded systems programming at the low levels is necessarily a multi-disciplinary field. The programmers and designers must consider hardware issues, manufacturing issues, electromagnetic interference/electromagnetic compatibility problems, and noise limitations.
Low-level code doesn’t just consist of algorithms but the code, itself, is, at times, a great function of the code geometry. Getting optimum performance of low-level embedded code depends very much on how the instructions are placed in program memory.
The C/C++ language may be used in low-level embedded systems programming but not where “fine controls” or “high speeds” are required. The blanket statement that a “C compiler can produce code that is nearly as good as an assembly language program” is OK for traditional programs, high-level ESP, and medium-level ESP. It is not true for low-level ESP! Low-level ESP is special and has very exacting demands on its code. (This fact will be explained in detail in Chapter 6.)
The overall scope of this book is to show the reader how to program the PIC, use its peripheral functions, and provide the fundamentals of general ESP techniques.
The plan of this book is as follows:
Chapter 2 introduces microcontrollers in general and details the basic structure of the PIC16F877.
Chapter 3 introduces the most fundamental elements of programming the PIC in assembly language using a simple circuit and program. Several instructions are introduced here. (Chapter 3 is the “Hello World” Chapter.)
Chapter 4 covers the first half of the PIC instruction set by considering the design of a very simple home security system. This method illustrates not only what the instructions are but, also, how to use them in a practical way.
Chapter 5 covers the rest of the PIC instruction set in the same way as in Chapter 4 with a more advanced security system design which includes a keypad and a digit display interface.
Chapter 6 covers the fundamentals of general ESP and shows the reader the style, technique and art of ESP. These techniques and ideas apply to all processors and are the main-stay of all of low-end embedded systems programming. Examples of coding and programs are given in the PIC assembly language.
Chapter 7 covers more advanced ESP techniques, such as sine-wave generation, DTMF signaling, data compression, pulse-width modulation, and testing techniques.
Chapter 8 covers the PIC peripherals with complete examples of how to use them. These include counters, timers, interrupts, the analog-to-digital converter, the pulse-width modulators, measurement hardware, and event generation hardware.
Chapter 9 covers the PIC peripherals for serial data communications. These are the USART, a shift-register interface, and the “Inter-Integrated Circuit” serial interface.
Chapter 10 is an introduction to the fundamentals of Digital Signal Processing (DSP) in an intuitive way and with detailed examples. Filter designs and programs are given in a cookbook fashion. Some advanced and exotic DSP applications and techniques are given.
Appendix A is a detailed view of the PIC instruction set.
Appendix B is a set of useful C++ programs which help the reader design projects for the PIC.
Appendix C is a list of special-function registers and their bit-settings.
Appendix D is a register-file map.
Appendix E shows the PIC16F877 external pins and their functions.
Appendix F shows the sequence of instructions to save registers and restore them when doing a processor interrupt.
ALU
Registers
Data Bus (^) Address Bus
Control Unit
To / From System Memory
To Address System Memory
Microprocessor
Figure 2- uP^ Internal View Block Diagram
2.2 The PIC16F
The PIC16F877`s internal block diagram is shown in Figure 2-2. The PIC contains an ALU, which does arithmetic and logic operations, the RAM, which is also called the “register-file”, the program EEPROM (Flash Memory), the data EEPROM, and the “W” register. The “W” register is not a part of the register-file but is a stand-alone, working register (also called an “accumulator”).
The ALU, the RAM, the “W” register, and the data EEPROM each manipulate and hold 8-bit-wide data, which ranges in value from zero to 255 (or, in hexadecimal, from 0x00 to 0xFF).
The program EEPROM (Flash Memory) works with 14-bit-wide words and contains each of the user’s instructions.
It is not uncommon for microcontrollers to have different sizes of data memory and program memory (in the PIC: 8-bits for data and 14-bits for program words). More than that, the key is that the data and program memories occupy separate spaces. This allows access to each at the same time.
ALU
W Register
Timing & Control
In / Out Ports & Other Peripherals
RAM
(^128 128 128 ) Bytes Bytes Bytes Bytes
0 1 2 3 4- Banks
Program EEPROM 8192 Words (14-Bits Long)
Data EEPROM 256 Bytes
Figure 2-2 PIC16F877 Internal Block Diagram
The PIC’s RAM addresses range from zero to 511 but the user can only access a RAM byte in a set of four “banks” of 128 bytes each and only one bank at a time. Not all of this RAM is available to the user as read-write memory, however. Many addresses are dedicated to special functions within the processor but they “look-like” RAM and are accessed the same way.
The PIC’s program EEPROM (Flash Memory) has addresses that range from zero to 8191 (0x1FFF). The user’s program occupies this memory space.
2.3 Programming The PIC
All types of computer programs can be broken-down into four main sets of actions:
MAIN CALL SORT_SUB ; Sort the Data (No Colon in Label)
Section 3.1 introduces a simple PIC system and analyzes its controlling program. It looks at each instruction, examines its structure and coding, and sets a foundation for proper usage of assembly language. Section 3.2 summarizes the instructions and concepts covered in Section 3.1.
An external view of the PIC with its pins is shown in Figure 3-1. This is the most basic view of the PIC’s functions. Most pins have a second use, or, even a third use: to run the PIC’s peripherals, such as the ADC, the timers, and the serial ports, to name a few. These other functions will be shown later as they are needed.
The basic function of these pins is for digital inputs and digital outputs. The individual bits on each of the input/output ports (A-through-E) can each be selected as “input” or “output” by special configuration registers in the RAM. The software must set these bits before the ports can be used. This will be shown in more detail shortly.
An example circuit which uses the input/output (I/O) ports “B” and “C” is shown in Figure 3-2. This is a simple, bare minimum, PIC example circuit that serves to introduce some simple software and instructions. Port “B” has a set of 8 DIP switches with resistor pull-ups on it to allow data from these switches to be read into the PIC. Port “C” drives a set of 8 LEDs through resistors to allow the PIC to send out and display its data. The power supply must drive both sets of power pins as shown in Figure 3-2. The clock input, OSC1, is driven by an external oscillator module as shown. Also, a 10K Ohm pull-up resistor is used on Pin 1 (/MCLR) to keep the PIC out of its “Reset” state.
The following program can be used to run the circuit of Figure 3-2:
LIST P = 16F INCLUDE “P16F877.INC”
ORG 0x0000 ; Program starts at address zero. NOP BANKSEL PORTC ; Select Bank Zero MOVLW B’00000000’ ; Reset PORTC MOVWF PORTC