Ayala - The 8051 Micro Controller

download Ayala - The 8051 Micro Controller

If you can't read please download the document

  • date post

  • Category


  • view

  • download


Embed Size (px)


all about microcontrollers

Transcript of Ayala - The 8051 Micro Controller


Kenneth J. AyalaWestern Carolina University


Copyediting: Techntcal Texts. Inc. Text and Cover Design: Roslyn Stendahl. Dapper Design Cover Image: Christopher Springmann. The Stock Market Composition: G & S Typesetters, Inc. Artwork: George Barlle, Accurate Art


WEST PUBLISHING COMPANY 50 W. Kellogg Boulevard P.O. Box 64526 St. Paul. MN 55164-0526

All rights reserved Printed in the Untted States of America @

Library of Congress Cataloging-in-Publication Data Ayala, Kenneth J. The 8051 microcontroller . architecture, programming, and applications / Kenneth J. Ayala. cm. p. Includes index. ISBN 0-314-77278-2 (soft) 1. Intel 8051 (Computer) 2. Digital control systems. I. Title. QA76.8 127A93 1991 004.165-dc2O 90-12928 CIP

To John Jamison of VMI and John Peatman of Georgia Tech. both of whom made this book possible

Contents1MICROPROCESSORS AND MICROCONTROLLERS 1 lntroduction 1 Microprocessorsand Microcontrollers 2 Microprocessors 2 Microcontrollers 3 Comparing Microprocessors and Microcontrollers 4 The 280 and the 8051 4 A Microcontroller Survey 5 Four-Bit Microcontroller 5 Eight-Bit Microcontrollers 6 Sixteen-Bit Microcontrollers 7 Thirty-Two Bit Microcontrollers 8 Development Systems for Microcontrollers 9 Summary 9 Questions 10 THE 8051 ARCHITECTURE 11 lntroduction 11 11 8051 Microcontroller Hardware The 8051 Oscillator and Clock 16 Program Counter and Data Pointer 17 A and B CPU Registers 17 Flags and the Program Status Word (PSW) Internal Memory 19 Internal RAM 19 The Stack and the Stack Pointer 19 Special Function Registers 21 Internal ROM 22 InputlOutput Pins, Ports, and Circuits 22 Port 0 23 Port 1 25





Port 2 25 Port 3 25 External Memory 26 Connecting External Memory 26 Counter and Timers 28 Timer Counter lnterrupts 29 Timing 30 Timer Modes of Operation 30 Timer Mode 0 30 Timer Mode 1 30 Timer Mode 2 31 Timer Mode 3 32 Counting 32 Serial Data InputlOutput 32 Serial Data lnterrupts 32 Data Transmission 34 Data Reception 34 Serial Data Transmission Modes 34 Serial Data Mode 0-Shift Register Mode Serial Data Mode 1-Standard UART 35 Mode 1 Baud Rates 36 Serial Data Mode 3 37 lnterrupts 37 Timer Flag lnterrupt 39 Serial Port lnterrupt 39 External lnterrupts 39 Reset 40 lnterrupt Control 40 lnterrupt EnablelDisable 40 lnterrupt Priority 41 lnterrupt Destinations 41 Software Generated lnterrupts 41 Summary 41 Questions 42




44 Introduction 44 Addressing Modes 45 lmmediite Addressing Mode 45 Reaister Addressina Mode 45 ~ i 6 cAddressing ode t 47 Indirect Addressing Mode 49 External Data Moves 50 Code Memory Read-Only Data Moves PUSH and POP Opcodes 52 Data Exchanges 53 Example Programs 54 Summary 56 Problems 57





LOGICAL OPERATIONS 59 lntroduction 59 Byte-Level Logical Operations 60 Bit-Level Logical Operations 62 Internal RAM Bit Addresses 62 S R Bit Addresses F 62 Bit-Level Boolean Operations 63 Rotate and Swap Operations 66 Example Programs 68 Summary 69 Problems 70


ARITHMETIC OPERATIONS 71 lntroduction 71 Flags 72 tnstructions Affecting Flags 72 lncrementing and Decrementing 73 Addition 74 Unsigned and Signed Addition 74 Signed Addition 75 Multiple-Byte Signed Arithmetic 76 Subtraction 77 Unsigned and Signed Subtraction 78 Unsigned Subtraction 78 Signed Subtraction 78 Multiplication and Division 80 Multiplication 80 Division 80 Decimal Arithmetic 81 Example Programs 82 Summary 84 Problems 85


JUMP AND CALL OPCODES 86 lntroduction 86 The Jump and Call Program Range Relative Range 87 Short Absolute Range 88 Long Absolute Range 88 Jumps 89 Bit Jumps 89 Byte Jumps 90 Unconditional Jumps 90 Calls and Subroutines 92 Subroutines 92 Calls and the Stack 92


Calls and Returns 93 Interrupts and Returns 94 Example Problems 95 Summary 97 Problems 98


AN 8051 MICROCONTROLLER DESIGN 100 lntroduction 100 A Microcontroller Specification 101 A Microcontroller Design 102 External Memory and Memory Space Decoding 102 Reset and Clock Circuits 102 Expanding 110 103 Memory-Mapped 110 104 Part Speed 106 Production Concerns 106 Testing the Design 107 Crystal Test 107 ROM Test 107 RAM Test 108 Timing Subroutines 110 Time Delays 110 Pure Software Time Delay 111 Software Polled Timer 112 Pure Hardware Delay 114 Lookup Tables for the 8051 117 P as a Base Address C 118 DPTR as a Base Address 120 Serial Data Transmission 121 Character Transmission Using a Time Delay 123 Character Transmission by Polling 124 Interrupt-Driven Character Transmission 125 Receiving Serial Data 126 Polling for Received Data 126 Interrupt-Driven Data Reception 127 Summary 128 Problems 129


APPLICATIONS 131 lntroduction 131 Keyboards 132 Human Factors 132 Key Switch Factors 132 Key Configurations 133 Programs for Keyboards 134 A Scanning Program for Small Keyboards 136 Interrupt-Driven Programs for Small Keyboards


Program for a Large Matrix Keyboard 147 Displays 151 Seven-Segment Numeric Display 151 Intelligent LCD Display 155 Pulse Measurement 158 Measuring Frequency 158 Pulse Width Measurement 161 DIA and AID Conversions 162 DIA Conversions 163 AID Conversion 165 Multiple lnterrupts 166 Hardware Circuits for Multiple lnterrupts 173 177 Putting it all Together Summary 181 Problems 182


SERIAL DATA COMMUNICATION 185 Introduction 185 Network Configurations 186 8051 Data Communication Modes 189 Mode 0: Shift Register Mode 189 Mode 1: Standard 8-Bit UART Mode 192 Modes 2 and 3: Multiprocessor 197 Summary 202 Problems 202

Appendix A Appendix B Appendix C Appendix D Appendix E Index 238

8051 Operational Code Mneumonics 203 H o w t o Use the Assembler 212 How t o Use the Simulator 220 The 8255 Programmable I10 Port 233 Control Registers 236


The microprocessor has been with us for some fifteen years now, growing from an awkward 4-bit child to a robust 32-bit adult. Soon, 64- and then 128-bit wizards will appear to crunch numbers, spreadsheets, and, CAD CAM. The engineering community became aware of, and enamored with, the 8-bit microprocessors of the middle to late 1970's. The bit size, cost, and power of these early CPUs were particularly useful for specific tasks involving data gathering, machine control, human interaction, and many other applications that granted a limited intelligence to machines and appliances. The personal computer that was spawned by the 8-bit units predictably became faster by increasing data word size and more complex by the addition of operating system hardware. This process evolved complex CPUs that are poorly suited to dedicated applications and more applicable to the generic realm of the computer scientist and system programmer. Engineering applications, however, did not change; these applications continue to 1 be best served by 8-bit CPUs with limited memory size and 1 0 power. Cost per unit also continues to dominate processing considerations. Using an expensive 32-bit microprocessor to perform functions that can be as efficiently served by an inexpensive 8-bit microcontroller will doom the 32-bit product to failure in any competitive marketplace. Many designers continue to use the older families of 8-bit microprocessors. The 8085,6502,6800, and 280 are familiar friends to those of us who had our first successes with these radical new computers. We know their faults and idiosyncrasies; we have, quite literally, tons of application software written for them. We are reluctant to abandon this investment in time and money. New technology makes possible, however, a better type of small computer-one with not only the CPU on the chip, but RAM, ROM, Timers, UARTS, Ports, and other com1 mon peripheral 1 0 functions also. The microprocessor has become the microcontroller. Some manufacturers, hoping to capitalize on our software investment, have brought out families of microcontrollers that are software compatible with the older microprocessors. Others, wishing to optimize the instruction set and architecture to improve speed and reduce code size, produced totally new designs that had little in common with their earlier microprocessors. Both of these trends continue.

This hook has been written for a diverse audience. It is meant for use primarily by those who work in the area of the electronic design and assembly language programming of small, dedicated computers. An extensive knowledge of electronics is not required to program the microcontroller. Many practitioners in disciplines not normally associated with computer electronicstransportation. HVAC, mechanisms, medicine, and manufacturing processes of all typescan benefit from a knowledge of how these "smart chips" work and how they can be used to improve their particular product. Persons quite skilled in the application of classical microprocessors, as well as novice users who have a basic understanding of computer operation but little actual experience, should all find this book useful. The seasoned professional can read Chapter 2 with some care, glance at the mnemonics in Chapters 3 through 6, and inspect the applications in Chapters 7 . 8, and 9. The student may wish to quickly read Chapter 2, study the mnemonics and program examples carefully in Chapters 3 through 6. and then exercise the example programs in Chapters 7, 8, and 9 to see how it all works. The text is suitable for a one- or two-semester course in microcontrollers. A two semester course sequence could involve the study of Chapters I to 6 in the first semester and Chapters 7. 8. and 9 in the second semester in conjunction with several involved student programs. A one-semeste