The 8051 Microcontroller. A System Approach - Muhammad Ali Mazidi - Full

You might also like

Download as pdf
Download as pdf
You are on page 1of 650
the 8051 microcontroller a systems approach MUHAMMAD AL! MAZIDI JANICE GILLISPIE MAZIDI ROLIN D. MCKINLAY THE 8051 MICROCONTROLLER A Systems Approach Muhammad Ali Mazidi Janice Gillispie Mazidi Rolin D. McKinlay PEARSON ‘olumbus Indian nm Dobai Sto Paulo Editorial Director: Vernon R. Anthony Senior Acquisitions Editor: Lindsey Prudhomme Development Editor: Dan Trudden Editorial Assistant: Yveste Schlarmant Director of Marketing: David Gesell Senior Marketing Assistant: Les Roberts Production Manager: Holly Shufeldt Art Director: Jayne Conte Cover Designer: Suzanne Behnke Cover image: Fotolia Full-Service Project Management/Composition: Moganambigai Sundaramurthy, Integra Software Services Pvt. Ltd. Text and Cover Printer: Edwards Brothers Malloy Credits and acknowledgments borrowed from other sources and reproduced, with permis- sion, in this textbook appear on the appropriate page within text. Copyright © 2013 hy Pearson Education, Ine. All rights reserved. Manufactured in the United States of America. This publication is protected by Copyright, and permission should be Obtained from the publisher prior to any prohibited reproduction, storage in a retrieval syS- tem, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission(s) to use material from this work, please submit a ‘written request to Pearson Education, Inc., Permissions Department, One Lake Street, Uppet Saddle River, New Jersey 07458, or you may fax your request t0 201-236-3290. Library of Congress Catafoging-in-Publication Data Mazidi, Muhammad Al The 8051 microcontroller: a systems approach / Muhammad Ali Mazidi, Janice Gillispie Mazidi, Rolin D. McKinlay. p.m. ISBN-13: 978-0-13-508044-3 (alk. paper) ISBN-10: 0-13-508044-4 (alk. paper) { Intel 8051 (Microcontroller) 2. Programmable controllers. 3. Mictocontrollers. 4, Embedded computer systems, 1. Mazidi, Janice Gillispie. U1. McKinlay, Rolin D. Title. 3.P76M378132 2013 de23 2012011584 PEARSON ISBN 10: 0-13-508044-4 ISBN 13: 978-0-13-508044-3, This volume is dedicated to the memory of Dr. A, Davoodi, Professor of Tehran University, who in the tumultuous years of my youth taught me the importance of an independent search for truth. Muhammad Ali Mazidi To Betsy, for all the love, encouragement, and sacrifice she has made for me. — Rolin D. McKinlay Regard man as a mine rich in gems of inestimable value, Education can, alone, cause it to reveal its treasures, and enable mankind to benefit therefrom Baha’u'llah Preface xi BRIEF CONTENTS About the Authors xiv Chapter 0 Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Chapter 16 Chapter 17 Chapter 18 Appendix A Appendix B Appendix C Appendix D Appendix E Appendix F Appendix G Introduction to Computing 1 the 8051 Microcontrollers 51 8051 Assembly Language Programming 65 Jump, Loop. and Call Instructions 101 HO Port Programming 125 8051 Addressing Modes 141 Arithmetic, Logie Instructions. nd Programs 171 8051 Programming in C 213 8051 Hardware Connection aad Intel Hex File 249 8051 Timer Programming in Assembly and C271 8051 Serial Port Programming in Assembly and C309 Interrupts Programming in Assembly and C349 LCD and Keyboard Interfacing 383 ADC, DAC, and Sensor Interfacing 405 8051 Interfacing to External Memory 445 Relay, Optoisolator, and Stepper Motor 469 DS12887 RTC Interfacing and Programming 487 DC Motor Control and PWM S11 SPI and I2C Protocols 525 3s 543 8051 Instructions, Timing, and Regi Basics of Wire Wrapping 583 IC Technology and System Design Issues 587 Flowcharts and Pseudocode 609 8051 Primer for x86 Programmers 615 ASCH Codes 617 Assemblers, Development Resources, and Suppliers 619 BRIEF CONTENTS CONTENTS Preface xi About the Authors — xiv CHAPTER 0 Introduction to Computing 1 0.1 Numbering and Coding Systems 2 0.2 Digital Primer 9 0.3 Semiconductor Memory 12 0.4 Bus Designing and Address Decoding 24 0.5 W/O Address Decoding and Design 33 0.6 CPU Architecture 39 CHAPTER 1 The 8051 Microcontrollers 51 I. Microcontrollers and Embedded Processors 52 1.2 Overview of the 8051 Family 56 CHAPTER 2 8051 Assembly Language Programming 65 2.1 Inside the 8051 66 2.2 Introduction to 8051 Assembly Programming 69 2.3 Assembling and Running an 8051 Program 72 2.4 The Program Counter and ROM Space in the 805174 2.5 8051 Data Types and Directives 77 2.6 8051 Flag Bits and the PSW Register 80 2.7 8051 Register Banks and Stack 83 2.8 RISC Architecture 92 CHAPTER 3 Jump, Loop, and Call Instructions 101 3.1 Loop and Jump Instructions 102 3.2 Call Instructions 108 3.3 Time Delay for Various 8051 Chips 113 CHAPTER 4 I/O Port Programming 125 4.1 8051 1/0 Programming 126 4.2 1/0 Bit-Manipulation Programming 132 vii CHAPTER 5 8051 Addressing Modes 141 5.1 Immediate and Register Addressing Modes 142 5.2. Accessing Memory Using Various Addressing Modes 144 5.3 Bit Addresses for /O and RAM 154 5.4 Extra 128-Byte On-Chip RAM in 8052 163 CHAPTER 6 Arithmetic, Logic Instructions, and Programs 171 6.1 Arithmetic Instructions 172 6.2 Signed Number Concepts and Arithmetic Operations 182 6.3 Logic and Compare Instructions 187 6.4 Rotate Instruction and Data Serialization 193 6.5 BCD, ASCII, and Other Application Programs 199 CHAPTER 7 8051 Programming inC 213 7.1, Data Types and Time Delay in 8051 C214 7.2 WO Programming in 8051 C220 7.3 Logic Operations in 8051 C 226 7.4 Data Conversion Programs in 8051 C231 7.5 Accessing Code ROM Space in 8051 C236 7.6 Data Serialization Using 8051 C241 CHAPTER 8 8051 Hardware Connection and Intel Hex File 249 8.1 Pin Description of the 8051 250 8.2 Design and Test of DS89C4x0 Trainer 256 8.3 Explaining the Intel Hex File 264 CHAPTER 9 8051 Timer Programming in Assembly andC 274 9.1 Programming 8051 Timers 272 9.2 Counter Programming 287 9.3 Programming Timers 0 and | in 8051C 292 CONTENTS CHAPTER 10 8051 Serial Port Programming in Assembly andC 309 10.1 Basics of Serial Communication 310 10.2 8051 Connection to RS232_ 317 10.3 8051 Serial Port Programming in Assembly 319 10.4 Programming the Second Serial Port 332 10.5 Serial Port Programming in C 338 CHAPTER 11 Interrupts Programming in Assembly andC 349 11.1 8051 Interrupts 350 11.2 Programming Timer Interrupts 354 11.3 Programming External Hardware Interrupts 356 11.4 Programming the Serial Communication Interrupt 365 11.5 Interrupt Priority in the 8051/52 369 11.6 Interrupt Programming in C372 CHAPTER 12 LCD and Keyboard Interfacing 383 12.1 LCD Interfacing 384 12.2 Keyboard Interfacing 395 CHAPTER 13 ADC, DAC, and Sensor Interfacing 405 13.1 Parallel and Serial ADC 406 13.2 DAC Interf 13.3 Sensor Interfacing and Signal Conditioning 436 cing 431 CHAPTER 14 8051 Interfacing to External Memory 445 14.1 8031/51 Interfacing with External ROM 446 14.2 8051 Data Memory Space 451 14,3 Accessing External Data Memory in 8051 C461 CHAPTER 15 Relay, Optoisolator, and Stepper Motor 469 15.1 Relays and Optoisolators 470 15.2 Stepper Motor Interfacing 475 CONTENTS, ix CHAPTER 16 0S12887 RTC Interfacing and Programming 487 16.1 DS12887 RTC Interfacing 488. 16.2 DS12887 RTC Programming inC 496 16.3 Alarm, SQW, und IRQ Features of the DS12887 Chip 499 CHAPTER 17 DC Motor Control and PWM 511 17.1 DC Motor Interfacing and PWM 512 CHAPTER 18 SPI andI2C Protocols 525 18.1 SPI Bus Protocol 526 18.2 12C Bus Protocol 531 Appendix A 8051 Instructions, Timing, and Registers 543 Appendix B_ Basics of Wire Wrapping 583 Appendix CIC Technology and System Design Issues 587 Appendix D Flowcharts and Pseudocode 609 Appendix € 8051 Primer for X86 Programmers 615, Appendix F ASCII Codes 617 Appendix G Assemblers, Development Resources, and Suppliers. 619 Index 623 x CONTENTS, PREFACE ‘The 8051 is a widely used microcontroller. There are many reasons for this, including the existence of multiple producers and its simple architecture This book is intended for use in college-level courses teaching microcontrollers and embedded systems. It not only establishes a foundation of Assembly lan- guage programming, but also provides a comprehensive treatment of 8051 interfacing for engineering students. From this background, the design and interfacing of micro controller-based embedded systems can be explored. This book can be also used by practicing technicians, hardware engineers, comput- er scientists, and hobbyists. It is an ideal source for those building stand-alone projects, or projects in which data is collected and fed into a PC for distribu- tion on a network. PREREQUISITES Readers should have had an introductory digital course. Knowledge of Assembly language would be helpfu! but is not necessary. Although the book is written for those with no background in Assembly language programming, students with prior Assembly language experience will be able to gain a m tery of 8051 architecture very rapidly and start on their projects right away, For the 8051 C programming sections of the book, a basic knowledge of C programming is required. ORGANIZATION OF THE BOOK A systematic, step-by-step approach is used to cover various aspects of 8051 C and Assembly language programming and interfacing. Many examples and sample programs are given to clarify the concepts and provide students With an opportunity to learn by doing. Review questions are provided at the end of each section to reinforce the main points of the section. Chapter 0 covers number systems (binary, decimal, and hex) and pro- vides an introduction to basic logic gates and memory terminology. It also examines the concepts of memory and I/O address decoding. Chapter I discusses the history of the 8051 and features of other 8051 family members such as the 8031, 8751, 89C51. DSS000, and DS89C4x0. It also provides a list of various producers of 8951 chips. Chapter 2 discusses the internal architecture of the 8051 and explains the use of an 8051 assembler to crezie ready-to-run programs. It also explores the stack and the flag register. The concepts of RISC and CISC architectures are also examined. PREFACE xi In Chapter 3, the topics of loop, jump, and call instructions are discussed, with many programming examples Chapter 4 is dedicated to the discussion of I/O ports. This allows students who are working on a project to start experimenting with 8051 /O interfacing and start the project as soon as possible. Chapter 5 covers the 8051 addressing modes and explains how to use the code space of the 8051 to store data, as well as how to access data, Chapter 6 is dedicated to arithmetic, logie instructions, and programs, The C programming of the 8051 is covered in Chapter 7. In Chapter 8, we discuss the hardware connection of the 8051 chip. Chapter 9 describes the 8051 timers and how to use them as event counters. Chapter 10 is dedicated to serial data communication of the 8051 and its interfacing to the RS232. It also shows 8051 communication with COM ports of the x86 PCs. In addition, the second serial port of the DS89C4x0 is, also covered. Chapter 11 provides a detailed discussion of 8051 interrupts with many examples on how to write interrupt handler programs, Chapter 12 shows 8051 interfacing with real-world devices such as LCDs and keyboards, Chapter 13 shows 8051 interfacing with real-world devices such as DAC chips, ADC chips, and sensors. In Chapter 14, we cover 8031/51 interfacing with external memories, both ROM and RAM. Chapter 15 examines the topics of optoisolator, relay, and stepper motor, Chapter 16 shows how to connect and program the D$12887 rea clock chip Chapter 17 shows basic interfacing of DC motor and using PWM. Finally, Chapter 18 shows basic concepts of I2C and SPI protocols. time The appendices have been designed to provide all reference material required for the topics covered in the book. Appendix A describes each 8051 instruction in detail, with examples. It also provides the clock count for instrue- tions, 8051 register diagrams, and RAM memory maps. Appendix B describes basics of wire wrapping, Appendix C covers IC technology and logic fami- lies, as well as 8051 I/O port interfacing and fan-out, Make sure you study this before connecting the 8051 to an external device. In Appendix D, the use of flowcharts and psuedocode is explored. Appendix E is for students familiar with x86 architecture who need to make a rapid transition to 8051 architecture. Appendix F provides the table of ASCII characters. Appendix Gi lists resources, for assembler shareware and electronics parts. xii PREFACE, Assemblers The following websites provide information on assemblers: www. MicroDigitalEd.com www.keil.com for Keil Corporation The authors can be contacted at the following e-mail address if you have any comments or suggestions, or if you find any errors. Please mention 8051 in subject line. mdebooks@yahoo.com SUPPLEMENTS. Instructor Resources * 0135084431 / 9780135084434 Instructor's Resource Manual (Download only) * 0132622807 / 9780132622806 PowerPoint Presentation (Download only) * 0132989891 / 9780132989893 TestGen® Computerized Test Bank (All supplements can be downloaded from the Pearson Instructor's Resource Site: www.pearsonhighered.com/ire ) Student Resources * 0132989646 / 9780132989640 Student Lab Manual to accompany The 8051 Microcontroller: A Systems Approach Datasheet, Sample Programs, Tutorials, and Trainer The following website provides details on data sheet, sample programs, tutorial, and trainer information: www.MicroDigitalEd.com PREFACE xi ABOUT THE AUTHORS Muhammad Ali Mazidi graduated from Tabriz University and holds master’s degrees from both Southern Methodist University and the University of Texas at Dallas, He worked on his Ph.D, in the Electrical Engineering Department of Southern Methodist University, He is the co-author of some widely used textbooks including The x86 IBM PC, PIC Microcontroller and Embedded Systems, HCS12 Microcontroller and Embedded Systems, and AVR Microcontroller and Embedded Systems, available from Prentice Hall, He teaches microcontroller-based system design at DeVry University in Dallas, Texas. He is the founder of the website www. MicroDigitalEd.com. Janice Gillispie Mazidi has a master’s degree in Computer Science from the University of North Texas. She has several years of experience as a software engineer in Dallas. She is the co-author of some widely used textbooks including The x86 PC. available from Prentice Hall. She teaches programming in colleges in the Dallas area. Rolin D. McKinlay has a bachelor’s degree in Electronics Engineering Technology from DeVry University. He works as embedded system engineer and FPGA design consultant in the Dallas area. The authors can be contacted at the following e-mail address if you have any comments or suggestions, or if you find any errors; mdebooks@yahoo.com xiy 1E AUTHORS CHAPTER 0 INTRODUCTION TO COMPUTING OBJECTIVES Upon completion of this chapter, you will be able to: >> >> >> >> >> >> SS >> co 2S >> >> > >> Convert any number from base 2, base 10, or base 16 to any of the other two bases. Describe the logical operations AND, OR, NOT, XOR, NAND, and NOR Use logic gates to diagram simple circuits. Explain the difference between a bit, a nibble, a byte, and a word. Give precise mathematical definitions of the terms kilobyte, megabyte, gigabyte, and terabyte. Describe the purpose of the major components of a computer system. Describe the role of the CPU in computer systems. Contrast and compare various types of semiconductor memories in terms of their capacity, organization, and access time. Describe the relationship between the number of memory locations on a chip, the number of data pins, and the chip’s memory capacity. Contrast and compare PROM, EPROM, UV-EPROM, EEPROM, Flash memory EPROM, and mask ROM memories. Contrast and compare SRAM, NV-RAM, and DRAM memories. List the steps a CPU follows in memory address decoding. List the three types of buses found in computers and describe the purpose of each type of bus. Describe the peripheral and memory-mapped I/O buses. Design decoders for memory and 1/0. List the major components of the CPU and describe the purpose of ea Understand the Harvard architecture. To understand the software and hardware of a microcontroller-based system, one must first master some very basic concepts underlying computer architecture. In this chapter (Which in the tradition of digital computers is called Chapter 0), the fundamentals of numbering and coding systems are presented ia Section 0.1. An overview of logic gates is provided in Sec 0.2. The semiconductor memory is discussed in Section 0,3. In Section 0.4, we examine the bus connection to memory and address decoding. In Section 0.5, we explore the I/O (input/output) address decoding. Finally, in the last sec- tion, Harvard and von Neumann CPU architectures are discussed. Although me readers may have an adequate background in many of the topics of this chapter, it is recommended that the material be reviewed, however briefly. 0.1: NUMBERING AND CODING SYSTEMS Whereas human beings use the base 10 (decimal) arithmetic, computers use the base 2 (hinary) system, In this section, we explain how to convert from the decimal system to the binary system, and vice versa. The convenient representation of binary numbers, called hexadecimal, also is covered. Finally, the binary format of the alphanumeric code, called ASCII, is explored. Decimal and binary number systems Although there has been speculation that the origin of the base 10 system is the fact that human beings have 10 fingers, there is absolutely no speculation about the reason behind the use of the binary system in comput- ers. The binary system is used in computers because | and 0 represent the two voltage levels of on and off. Whereas in base 10 there are 10 distinct symbols or digits. 0, 1, 2, .... 9, in base 2 there are only two, 0 and 1, with which to generate numbers, These two binary digits, 0 and |, are commonly referred to as bits. Converting from decimal to binary One method of converting from decimal to binary is to divide the decimal number by 2 repeatedly. keeping track of the remainders This process continues until the quotient becomes zero. The remainders are then written in reverse order to obtain the binary number. This is demonstrated in Example 0-1 3 © 10° : Converting from binary to decimal Bo 108 a 6 © 10? To convert from binary to decimal, it is important |o < 20° to understand the concept of weight associated with each |4 10% 7 © 10° : digit position. First, as an analogy, recall the weight of numbers in the base 10 system, as shown in the display. a 8 600 0000 40000 790000 740683 2 CHAPTER 0: INTRODUCTION TO COMPUTING

You might also like