8051 Microcontrollers an Applications Based Introduction.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
//INTEGRAS/ELS/PAGINATION/ELSEVIER UK/MAB/3B2/FINALS_03-11-03/PRELIM.3D – 1 – [1–8/8] 28.11.2003 4:49PM 8051 Microcontrollers An Applications-Based Introduction David Calcutt Fred Cowan Hassan Parchizadeh AMSTERDAM • BOSTON • HEIDELBERG • LONDON NEW YORK • OXFORD • PARIS • SAN DIEGO SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO Newnes is an imprint of Elsevier //INTEGRAS/ELS/PAGINATION/ELSEVIER UK/MAB/3B2/FINALS_03-11-03/PRELIM.3D – 2 – [1–8/8] 28.11.2003 4:49PM Newnes An imprint of Elsevier Linacre House, Jordan Hill, Oxford OX2 8DP 200 Wheeler Road, Burlington, MA 01803 First published 2004 Copyright Ó 2004, David Calcutt, Fred Cowan and Hassan Parchizadeh. All rights reserved The right of David Calcutt, Fred Cowan and Hassan Parchizadeh to be identified as the authors of this work has been asserted in accordance with the Copyright, Designs and Patents Act 1988 No part of this publication may be reproduced in any material form (including photocopying or storing in any medium by electronic means and whether or not transiently or incidentally to some other use of this publication) without the written permission of the copyright holder except in accordance with the provisions of the Copyright, Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licensing Agency Ltd, 90Tottenham Court Road, London, England W1T 4LP. Applications for the copyright holder’s written permission to reproduce any part of this publication should be addressed to the publisher Permissions may be sought directly from Elsevier’s Science and Technology Rights Department in Oxford, UK. Phone: (þ 44) (0) 1865 843830; fax: (þ 44) (0) 1865 853333; e-mail: [email protected]. You may also complete your request on-line via the Elsevier homepage (http://www.elsevier.com), by selecting ‘Customer Support’ and then ‘Obtaining Permissions’ British Library Cataloguing in Publication Data Calcutt, D. 8051 microcontrollers : an applications based introduction 1. INTEL 8051 (Computer) 2. Digital control systems I. Title II. Cowan, Frederick J. III. Parchizadeh, G. Hassan 004.1065 Library of Congress Cataloguing in Publication Data Calcutt, D. M. 8051 microcontrollers : an applications-based introduction / David Calcutt, Fred Cowan, Hassan Parchizadeh. p. cm. 1. Intel 8051 (Computer) 2. Digital control systems. I. Cowan, Frederick J. II. Parchizadeh, G. Hassan. III. Title. QA76.8.I27C35 2003 004.165—dc22 2003066606 ISBN 075065759 6 (alk. paper) For information on all Newnes publications visit our website at www.newnespress.com Typeset by Integra Software Services Pvt. Ltd, Pondicherry, India www.integra-india.com Printed and bound in Meppel, The Netherlands by Krips bv. //INTEGRAS/ELS/PAGINATION/ELSEVIER UK/MAB/3B2/FINALS_03-11-03/PRELIM.3D – 3 – [1–8/8] 28.11.2003 4:49PM Contents Preface v Acknowledgements vii 1 Introduction to Microcontrollers 1 1.1 Introduction 1 1.2 Microcontroller types 2 1.3 P89C66x microcontroller 4 1.4 Bits, nibbles, bytes and number conversions 7 1.5 Inside microcontrollers 10 1.6 Microcontroller programming 11 1.7 Commonly used instructions of the 8051 microcontroller 22 1.8 Microcontroller clock 22 1.9 Time delays 24 Summary 27 2 Flash Microcontroller Board 28 2.1 Introduction 28 2.2 P89C66x microcontroller 29 2.3 Programming the device 31 2.4 Flash magic 35 2.5 XAG49 microcontroller 35 Summary 37 3 Simulation Software 38 3.1 Introduction 38 3.2 Keil m Vision2 39 3.3 Raisonance IDE (RIDE) 50 Summary 64 4 P89C66x Microcontroller 66 4.1 Introduction 66 4.2 Timers 0and 1 67 4.3 Timer 2 79 4.4 External interrupt 82 //INTEGRAS/ELS/PAGINATION/ELSEVIER UK/MAB/3B2/FINALS_03-11-03/PRELIM.3D – 4 – [1–8/8] 28.11.2003 4:49PM iv Contents 4.5 Interrupt priority 84 4.6 Programmable counter array (PCA) 86 4.7 Pulse width modulation (PWM) 88 4.8 Watchdog timer 92 4.9 Universal asynchronous receive transmit (UART) 94 4.10Inter integrated circuit (IIC or I 2C) 103 Summary 111 5 Low Pin Count (LPC) Devices 113 5.1 Introduction 113 5.2 P87LPC769 114 5.3 Analog functions 115 5.4 Analog comparators 125 5.5 P89LPC932 128 5.6 Serial peripheral interface (SPI) 129 5.7 EEPROM memory 136 Summary 141 6 The XA 16-bit Microcontroller 142 6.1 Introduction 142 6.2 XA registers 146 6.3 Watchdog timer 148 6.4 UART 152 6.5 8051 compatibility 155 6.6 Interrupts 156 Summary 168 7 Project Applications 169 7.1 Introduction 169 7.2 Project 1: speed control of a small DC motor 169 7.3 Project 2: speed control of a stepper motor 175 7.4 Project 3: single wire multiprocessor system 185 7.5 Project 4: function generator 192 Solutions to Exercises 201 Appendix A 8051 Instruction Set 226 B Philips XA Microcontroller – XA and 8051 Instruction Set Differences 232 C 8051 Microcontroller Structure 246 D P89C66x Microcontroller 285 E P89LPC932 Microcontroller 327 F XAG49 Microcontroller 360 G P89C66x and XAG49 Microcontroller PCB Board Layouts 401 Index 407 //INTEGRAS/ELS/PAGINATION/ELSEVIER UK/MAB/3B2/FINALS_03-11-03/PRELIM.3D – 5 – [1–8/8] 28.11.2003 4:49PM Preface A potential reader of this text may be forgiven for initially viewing this book as yet another text on the ubiquitous 80C51 microcontroller, a topic on which many books have already been published. However, the authors believe their application-based coverage using only Flash memory devices will bring home to the reader a depth of coverage and an understanding of the versatility of the various members of the 80C51 family, including the 16-bit devices, that have not been seen before. Three devices in particular are described in the text with their own chapters and relevant appendices. The devices are those available from Philips Semiconductors although the applications, both hardware and software, have a broader scope and could apply to other manufacturer’s devices. The text includes a chapter on simulation, using evaluation software that can be downloaded on to a computer. Such software allows the user to compile their program and simply run it to achieve an objective or single step through their program to establish how the program affects registers, timers, ports, etc., as the program develops. It is hoped that the reader will wish to go beyond simulation and interface with external inputs/outputs via an actual microcon- troller. Artwork is included, in an appendix, for a single-sided pcb that could be used for the construction of a development board. Two different boards are described; one board is designed for an 8-bit device while the other is for a 16-bit device, both devices being covered in the text. Information relating to the microcontroller boards can be found in Chapter 2. The use of a Flash micro- controller board and in-system programming techniques allows the user to simulate and debug his/her program and refine it before downloading it to the microcontroller. Source code could then be removed, if required, and replaced with a new program to serve a different purpose. The use of a micro- controller board allows an interface to the outside world and the effect of the program stored in the microcontroller can be observed in real time i.e. to light LEDs, cause a motor shaft to rotate, etc. For those not wishing to have their own microprocessor board the text still offers the opportunity to simulate programs and much can be learnt about the devices by its use. Three members of the Philip’s 80C51 family have been utilised in the text to explain circuit action and used as the basis for specific applications. Several //INTEGRAS/ELS/PAGINATION/ELSEVIER UK/MAB/3B2/FINALS_03-11-03/PRELIM.3D – 6 – [1–8/8] 28.11.2003 4:49PM vi Preface appendices complete the story with details on the 8-bit and 16-bit microcon- troller instruction set and manufacturers’ data on the devices. The book is intended to be read on a chapter by chapter basis for those new to the subject and in this format would be suitable for those on degree, including postgraduate, courses. The text would also be suitable for any reader familiar with the devices but requiring information that takes them somewhat deeper into the detail and applications. For such readers some of the chapters could be omitted and particular chapters studied in more depth. Practising engineers could find the text helpful as an aid to the development of prototype systems prior to full-scale commercial application. Chapters dealing with spe- cific devices have numerous examples to help reinforce key points, and there are also numerous exercises for the reader to attempt if they so wish; answers to these exercises can be found at the end of the book. Relevant appendices can be used for reference where necessary. The text assumes that readers have some experience of programming although some information on assembly language programming can be found in Chapter 1. Programming examples have been implemented using assembly language and C. The authors have attempted to show throughout the text programming applications where relevant, and the final chapter is devoted to practical appli- cations that the authors have found to work. Notwithstanding this, the authors can accept no responsibility for any program that a reader might attempt and find unsatisfactory. It should perhaps be mentioned at this point that the Department of Electro- nic and Computer Engineering at the University of Portsmouth is a Philips Accredited Product Expert Centre, one of only five in England. Short courses relevant to industry are run at the Centre on a regular basis and the Centre is kept up to date on new developments relating to Philips Semiconductors Ltd devices.