TT1l ... " THE 8051 MICRO CONTROLLER AND EMBEDDED SYSTEMS Using Assembly and C SECOND EDITION Muhammad Ali Mazidi Janice Gillispie Mazidi Rolin D. McKinlay -- PEARSON Prentice Hall pper addle River, ew Jersey Columbus, Ohio ealato ing-in-Publicalion Data tlbnryofCongrtM g andC/Muhammad ~lJ.t~h.Muh.1mmad All. d bedded systems: using Assembly, I luoconlrolltr an em . J Tht: 80S m , . • Mazidi Rolin 0 McKtn ay. AhMUldl, Janke Gillispie 1 I : ~~ 1,l.119402-X(alk.paper) d I" systems.IMcKinlay,RolinD.IL I ~rilmmi!blt conirollers.2.Embedde compu Tl\k IJ22] P76MJ78 2006 .2989 dell 2005054446 Edllor: Kaleunsnar .. Edltorl.l Assistant; LaraDimmick Production Editor: KevinHappell o..lgn Coordlnalo" DianeErnsberger CovarDeslgne" BryanHuber CoverArt; SuperStock Production Managa" MattOllenweller Stnlor Marketing Coordinator: LizFarrell Stnlor Marketing Managar: BenLeonard op)'Tlght 0 2006 byPearson Edncation, Ine., Upper Saddle River, New Jersey 074~8.Pearson Prentice Hall. All rights """rved. Printed intheUnited States ofAmerica. Thispublication isprotected byCopyright and permission should beobtaine d fromthepublisher prior toanyprohibited reproduction, storage inaretrieval system, or tranSmission inany formor by any means, ePlcemctrllcsmsico,l\smDecehpaanritcmale,npt.hotocopying, recording, orlikewise. Forinformation regarding pennission(s), write to: Rights and Pearson Prentice Hall'" isatrademark ofPearson Education, Inc. Pearson- isaregistered trademark of Pearsonpic Pr nUce Hall- isaregistered trademark ofPearson Education, Inc. Peal>Ol1Education Ltd. P arson Education Singapore, Pte., Ltd. Pearson Education Australia PTY, Lintited Pearson Education Canada, Ltd. Pearson Education North AsiaLtd. Petlt!OnEducation-Japan Pearson Educacion de Mexico, SA de C.V. Pearsoll Education MalaYSia,Pte. LId. Pearson Education lnc., Upper Saddle River, New Jersey ISBN: o-l3-119402-X ... man's glory lieth in his knowledge, his upright conduct, his praiseworthy character, his wisdom, and not in his nationality or rank. - Baha'u'llah CONTENTS AT A GLANCE CHAPTERS I IntroductiontoComputing 0: 23 The8051 Microcontrollers I: 37 8051Assembly Language Programming 2: 69 Jump, Loop,andCallInstructions 3: 9 I/O Portprogramming 4: 109 s: 80SIAddressing Modes 1'9 Arithmetic& LogicInstructions and Programs 6: I I 7: 80SI Programming inC 217 80SI HardwareConnection and Intel Hex File 8: 8051Timer Programming inAssembly and C 2 9 9: 10: 80SISerialPortProgramming inAssembly and C 277 Ed Ed II: InterruptsProgramming inAssembly and C 317 PI 12: LCDand KeyboardInterfacing 51 De Co 13: ADC.DAC.andSensorInterfacing 37 Co 14: 8051Interfacingto ExternalMemory 411 PI IS: 8051Interfacing withthe 8255 449 So So 16: DS12887RTCInterfacingand Programming 467 17: MotorControl: Relay,PWM. DC. and Stepper Motors 491 APPENDICES A: 8051 Instructions Timing and R . B: Ba.srcs of WI.reWIrapping, egisters 52 C: IeTechnology adS . 56 FI h n ystem DeSIgnIssues 0: 567 Owe arts and Pseudocode E: 8051 Primer forX86 P 7 F: ASCII Codes rogrammers 592 G: Assemblers. Development R 593 H: DataSheets eSOurces.and Suppliers 594 596 CONTENTS CHAPTER 0: INTRODUCTION TO COMPUTING 1 Section 0.1: Numbering and coding systems 2 Section 0.2: Digital primer 9 Section 0.3: Inside the computer 13 CHAPTER 1:THE 8051 MICROCONTROLLERS 23 Section 1.1: Microcontrollers and embedded processors 24 Section 1.2: Overview of the 8051 family 28 CHAPTER 2: 8051ASSEMBLYLANGUAGE PROGRAMMING 37 Section 2.1: Inside the 8051 38 Section 2.2: Introduction to 8051 Assembly programming 41 Section 2.3: Assembling and running an 8051 program 44 Section 2.4: The program counter and ROM space in the 8051 46 Section 2.5: 8051 data types and directives 49 Section 2.6: 8051 flag bits and the PSW register 52 Section 2.7: 8051 register banks and stack 55 CHAPTER 3: JUMP, LOOP, ANDCALL INSTRUCTIONS 69 Section 3.1: Loop andjump instructions 70 Section 3.2: Call instructions 75 Section 3.3: Time delay for various 8051 chips 80 CHAPTER 4: I/O PORT PROGRAMMING 93 Section 4.1: 8051 I/O programming 94 Section 4.2: I/O bit manipulation programming 100 CHAPTER 5: 8051ADDRESSING MODES 109 Section 5.1: Immediate and register addressing modes 110 Section 5.2: Accessing memory using various addressing modes 112 Section 5.3: Bit addresses for I/O and RAM 122 Section 5.4: Extra 128-byte on-chip RAM in 8052 13\ CHAPTER 6:ARITHMETIC & LOGIC INSTRUCTIONS AND PROGRAMS 139 Section 6.1: Arithmetic instructions 140 Section 6.2: Signed number concepts and arithmetic operations 150 Section 6.3: Logic and compare instructions 155 Section 6.4: Rotate instruction and data serialization 161 Section 6.5: BCD, ASCII, and other application programs :167 - v CHAPTER 7: 8051 PROGRAMMING IN C 181 Section 7.1: Data types and time delay in 805 I C 182 Section 7.2: I/O programming in 8051 C 188 Section 7.3: Logic operations in 8051 C 194 Section 7.4: Data conversion programs in 8051 C 199 Section 7.5: Accessing code ROM space in 8051 C 204 Section 7.6: Data serialization using 8051 C 209 CHAPTER 8: 8051 HARDWARE CONNECTION AND INTEL HEX FILE 217 Section 8.1: Pin description of the 805 I 218 Section 8.2: Design and test of DS89C4xO trainer 224 Section 8.3: Explaining the Intel hex file 232 CHAPTER 9: 8051 TIMER PROGRAMMING IN ASSEMBLY AND C 239 Section 9.1: Programming 8051 timers 240 Section 9.2: Counter programming 255 Section 9.3: Programming timers 0 and I in 8051 C 260 CHAPTER 10: 8051 SERIAL PORT PROGRAMMING IN ASSEMBLY AND C 277 Section 10.1: Basics of serial communication 278 Section 10.2: 8051 connection to RS232 285 Section 10.3: 8051 serial port programming inAssembly 287 Section 10.4: Programming the second serial port 300 Section 10.5: Serial port programming in C 306 CHAPTER II: INTERRUPTS PROGRAMMING IN ASSEMBLY AND C 317 Section I1.1: 8051 interrupts 318 Section 11.2: Programming timer interrupts 322 Section I1.3: Programming external hardware interrupts 326 Section 11.4: Programming the serial communication interrupt 333 Section I1.5: Interrupt priority in the 8051/52 337 Section 11.6: Interrupt programming in C 340 CHAPTER 12: LCD AND KEYBOARD INTERFACING 351 Section 12.1: LCD interfacing 352 Section 12.2: Keyboard interfacing 363 CHAPTER 13: ADC, DAC, AND SENSOR INTERFACING 373 Section 13. I: Parallel and serial ADC 374 Section 13.2: DAC interfacing 398 Section 13.3: Sensor interfacing and signal conditioning 403 VI --~---------~.-. CHAPTER 14: 8051 INTERFACING TO EXTERNAL MEMORY 411 Section 14.1:Semiconductor memory 412 Section 14.2: Memory address decoding 422 Section 14.3:8031/51 interfacing with external ROM 425 Section 14.4: 8051 data memory space 430 Section 14.5:Accessing external data memory in 8051 C 440 CHAPTER 15: 8051INTERFACING WITH THE 8255 449 Section 15.1:Programming the 8255 450 Section 15.2: 8255 interfacing 458 Section J5.3: 8051 C programming for the 8255 462 CHAPTER 16: DS12887 RTC INTERFACING AND PROGRAMMING 467 Section 16.1: DS12887 RTC interfacing 468 Section 16.2: OSI2887 RTC programming inC 476 Section 16.3:Alarm, SQW,and IRQ features of the DS12887chip 479 CHAPTER 17: MOTOR CONTROL: RELAY, PWM, DC, AND STEPPER MOTORS 491 Section 17.1: Relays and optoisolators 492 Section 17.2: Stepper motor interfacing 498 Section 17.3: DC motor interfacing and PWM 507 APPENDIX A: 8051 INSTRUCTIONS, TIMING, AND REGISTERS 523 APPENDIX B:BASICS OF WIRE WRAPPING 563 APPENDIX C: ICTECHNOLOGY AND SYSTEM DESIGN ISSUES 567 APPENDIX D: FLOWCHARTS AND PSEUDOCODE 587 APPENDIX E: 8051 PRlMER FOR X86 PROGRAMMERS 592 APPENDIX F: ASCll CODES 593' APPENDIXG:ASSEMBLERS,DEVELOPMENTRESOURCES, AND SUPPLIERS 594 APPENDIX H: DATASHEETS 596 INDEX 617 vii K..,ad...,. h J In IIIIroduc 1(1) dlgilal 'our e. Knowledge of hIp/iii ut n t neces ry. Ahhough the book is I tt lund in,~ 'mhly language programming, stu- pcncnce w ill be able 10 gain a ma tery of nd I.m on their proje ts right away. For the 8051 ~ k, a ha", knowledge of C programming is ..",.n,·. ICp ppr .h I used to cover various aspects of 1.1II,1I"",£;1e)1' • mrrung and interfacing. Many examples and n 10 I tIf) the concept and provide students with an din' Rev qu lions are provided at the end of each le\\ m n POlOt of lh ecuon. ver num t m (bmary. decimal, and hex), and provides f J • nd mpuier terminology. This is designed h nical engineering students, who have not \ ho need to refresh their memory on these the 0 I and feature of other 8051 fam- n I. ~OOO.and D 9C4xO. Italso pro- hIp • r hue lure 0/ the 0 I and explains the -to-run program. It also explores the