Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Microcontrollers in Practice (Mitescu M., 2005)

.pdf
Скачиваний:
808
Добавлен:
12.08.2013
Размер:
1.25 Mб
Скачать

Springer Series in

18

advanced microelectronics

Springer Series in

advanced microelectronics

Series Editors: K. Itoh T. Lee T. Sakurai W. M. C. Sansen D. Schmitt-Landsiedel

The Springer Series in Advanced Microelectronics provides systematic information on all the topics relevant for the design, processing, and manufacturing of microelectronic devices. The books, each prepared by leading researchers or engineers in their fields, cover the basic and advanced aspects of topics such as wafer processing, materials, device design, device technologies, circuit design, VLSI implementation, and subsystem technology. The series forms a bridge between physics and engineering and the volumes will appeal to practicing engineers as well as research scientists.

18Microcontrollers in Practice

By I. Susnea and M. Mitescu

19Gettering Defects in Semiconductors

By V.A. Perevoshchikov and V.D. Skoupov

20Low Power VCO Design in CMOS

By M. Tiebout

Volumes 1–17 are listed at the end of the book.

M. Mitescu I. Susnea

Microcontrollers

in Practice

With 117 Figures, 34 Tables and CD-Rom

123

Marian Mitescu

Ioan Susnea

Razoare Street 2

Brailei Street 179

6200 Galati

800578 Galati

Romania

Romania

mitescum@yahoo.com

isusnea@yahoo.com

Series Editors:

 

Dr. Kiyoo Itoh

 

Hitachi Ltd., Central Research Laboratory, 1-280 Higashi-Koigakubo

Kokubunji-shi, Tokyo 185-8601, Japan

Professor Thomas Lee

Stanford University, Department of Electrical Engineering, 420 Via Palou Mall, CIS-205 Stanford, CA 94305-4070, USA

Professor Takayasu Sakurai

Center for Collaborative Research, University of Tokyo, 7-22-1 Roppongi

Minato-ku, Tokyo 106-8558, Japan

Professor Willy M. C. Sansen

Katholieke Universiteit Leuven, ESAT-MICAS, Kasteelpark Arenberg 10

3001 Leuven, Belgium

Professor Doris Schmitt-Landsiedel

Technische Universität München, Lehrstuhl für Technische Elektronik

Theresienstrasse 90, Gebäude N3, 80290 München, Germany

ISSN 1437-0387

ISBN-10 3-540-25301-7 Springer Berlin Heidelberg New York

ISBN-13 978-3-540-25301-3 Springer Berlin Heidelberg New York

Library of Congress Control Number: 2005927903

This work is subject to copyright. All rights are reserved, whether the whole or part of the material is concerned, specif ically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microf ilm or in any other way, and storage in data banks. Duplication of this publication or parts thereof is permitted only under the provisions of the German Copyright Law of September 9, 1965, in its current version, and permission for use must always be obtained from Springer-Verlag. Violations are liable to prosecution under the German Copyright Law.

Springer is a part of Springer Science+Business Media.

springeronline.com

© Springer Berlin Heidelberg 2005

Printed in Germany

The use of general descriptive names, registered names, trademarks, etc. in this publication does not imply, even in the absence of a specif ic statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.

Typesetting and production:

LE-TEX Jelonek, Schmidt & Vöckler GbR, Leipzig, Germany

Cover design: design & production GmbH, Heidelberg

Printed on acid-free paper

SPIN: 11327721

57/3141/YL - 5 4 3 2 1 0

This book is dedicated to the great inventor John W. Halpern, who taught me that life can be re-invented, and to Simona, who taught me that it’s worth doing so.

Ioan Susnea

To my sons George Dan and Cosmin

Marian Mitescu

Preface

The Aim of this Book

The manufacturers of microcontrollers report annual sales of hundreds of millions of units. To support this massive market demand, they offer tens of thousands of pages of free, good quality technical documentation: data sheets, application notes, articles, etc.

The problem is that the more information is available on this subject, the harder it is to find the information useful for you. Therefore, the need for synthesis seems to be growing.

While the vast majority of the literature available is monographic, dedicated to a specific circuit, this book tries to emphasize that various microcontrollers have many common structural characteristics; in fact they are all implementations of the same concept. When starting with the big picture, it’s easier to focus on details from time to time, than to build the big picture, starting from details.

Throughout this book, we present three different families of microcontrollers: HC11, AVR, and 8051 and we aim to make reading of this book more rewarding for the reader than reading three monographs dedicated to each of the above families. If you have ever studied one microcontroller, by reading this book you will discover that you already know a lot of things about all microcontrollers.

Another important aim of this book is to help the reader to make the small, yet decisive step between theory and practice. The book presents the design of three development boards, one for each microcontroller discussed, which can serve as platforms for a large number of experimental projects. The design examples presented demonstrate that, regardless of the microcontroller selected, and the complexity of the project, the software applications can be built according to the same general structure.

VIII Preface

What’s in this Book

The book is structured into three sections. Chapters 1–8 aim to create a detailed overview of microcontrollers, by presenting their subsystems starting from a general functional block diagram, valid for most microcontrollers on the market.

In each case, we describe the distinctive features of that specific subsystem for HC11, 8051 and AVR. This whole section has a more theoretical approach, but, even here, many practical examples are presented, mainly regarding the initializations required by each subsystem, or the particular use of the associated interrupts. The purpose of this section is to create a perspective that views the microcontroller as a set of resources, easy to identify and use.

Chapters 9–16 contain eight complete projects, described from the initial idea, to the printed circuit board and detailed software implementation. Here too, we permanently focus on the similarities between the microcontrollers discussed, from the hardware and software perspectives.

All chapters contain exercises, suggesting modifications or improvements of the examples in the book. Most exercises have solutions in the book; for the others the solutions can be found on the accompanying CD.

Finally, the appendices contain additional information intended to help the reader to fully understand all the aspects of the projects described in the previous sections. We chose to present these details separately in these appendices, in order to avoid fragmentation of the flow of the main text.

Who Should Read this Book

Most of the available books on microcontrollers are either “guides for idiots”, assuming that the reader knows nothing on the subject, or “rocket science books” for a limited academic audience. Little is offered to the majority of readers that are in between.

This book is primarily aimed at students of technical universities, but can be rewarding reading for anyone having a reasonable dose of “technical common sense”, like service technicians, hobbyists, inventors, etc. We assume that the reader knows the fundamentals about binary representation of numbers, Boolean algebra, logic functions, and logic circuits, and knows how to locate and read data sheets and other technical literature supplied by the manufacturers.

We also assume that he has the basic skills required to use a personal computer, and knows how to install a software application, how to view and create text files, etc. Thus, we can spare the space needed for explaining these notions for more substantial projects.

However, eliminating puerile explanations doesn’t mean that we give up all explanations.

Don’t let yourself be intimidated by projects that seem very complex, like the “fuzzy logic temperature controller”. Even the most complex projects presented in this book are built according to the same structure as the simplest, LED flashing type projects.

Preface IX

How to Read this Book

The chapters are intended to be self-contained, so that advanced users can, in principle, read the chapters individually as they choose.

However, the density of information requires permanent contact with practice in order to consolidate the knowledge acquired. We strongly recommend downloading and installing the software tools needed to test the examples and exercises presented in the book. For your convenience, all the source files for the examples and exercises presented in the book have been included on the accompanying CD.

This CD also contains the schematic files and the PCB layout drawings for the main projects described in the book. In our opinion, PCB design skills are equally important as good knowledge of the software, and therefore we recommend that you download and install the freeware version of the Eaglelayout editor, from CadSoft, so that you can view and edit the projects included.

The information in this book is not intended to replace the manufacturers’ data sheets; therefore it is a good idea to keep at hand the data sheets for the microcontrollers discussed here: 68HC11F1, AT90S8535, and AT89C51.

If you encounter new terms, try using the glossary of terms included in the appendices, which contains short definitions for the most common terms used in the book.

No special conventions have been used in writing this book. The only notation worth mentioning here is that the names of active LOW signals are written using a backslash as terminator, e. g. RD\, WR\, etc.

Disclaimer

The information in this book is for educational purposes only. Although we have made every effort to provide accurate information, the examples in this book should not be interpreted as a promise or a guarantee that you will be able to reach a certain level of knowledge or skills by reading this material.

The author and publisher shall in no event be held liable to any party for any direct, indirect, punitive, special, incidental or other consequential damages arising directly or indirectly from any use of this material, which is provided “as is”, and without warranties.

All internet links indicated are for information purposes only and are not warranted for content, accuracy or any other implied or explicit purpose.

Contents

1 Resources of Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1

1.1

In this Chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1

1.2

Microcontroller Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1

1.3

The Memory Map . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

1.4

CPU Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

 

1.4.1 The CPU Registers of HC11 . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

 

1.4.2 The CPU Registers of AVR . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

 

1.4.3 The CPU Registers of 8051 . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7

1.5

The Peripheral Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

8

1.6

The Interrupt System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9

 

1.6.1 General Description of the Interrupt System . . . . . . . . . . . . . .

9

 

1.6.2 Distinctive Features of the Interrupt System of HC11 . . . . . .

11

 

1.6.3 Distinctive Features of the Interrupt System of AVR . . . . . . .

12

 

1.6.4 Distinctive Features of the Interrupt System of 8051 . . . . . . .

12

1.7

Expanding the Resaurces of Microcontrollers . . . . . . . . . . . . . . . . . . .

12

 

1.7.1 HC11 Operating with External Bus . . . . . . . . . . . . . . . . . . . . .

13

 

1.7.2 AT90S8515 Operating with External Bus . . . . . . . . . . . . . . . .

14

 

1.7.3 8051 Operating with External Bus . . . . . . . . . . . . . . . . . . . . . .

14

1.8

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

15

2 Using the Digital I/O Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

19

2.1

In this Chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

19

2.2

Overview of the Parallel I/O System . . . . . . . . . . . . . . . . . . . . . . . . . . .

19

2.3

Electrical Characteristics of the I/O Lines . . . . . . . . . . . . . . . . . . . . . .

21

2.4

Controlling the I/O Lines by Software . . . . . . . . . . . . . . . . . . . . . . . . .

23

2.5

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

24

3 Using the Asynchronous Serial Interface . . . . . . . . . . . . . . . . . . . . . . . . . .

27

3.1

In this Chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

27

3.2

Synchronous vs. Asynchronous Communication . . . . . . . . . . . . . . . . .

27

XII Contents

3.3 Error Detection in Asynchronous Communication . . . . . . . . . . . . . . . 29 3.4 The General Structure of the Asynchronous Serial Communication

Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 3.5 The Serial Communication Interface of 68HC11F1 . . . . . . . . . . . . . . 30 3.6 The Asynchronous Serial Communication Interface of AVR

Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 3.7 The Asynchronous Serial Interface of 8051 . . . . . . . . . . . . . . . . . . . . . 36 3.8 Programming the Asynchronous Serial Interface . . . . . . . . . . . . . . . . 37 3.8.1 Programming the SCI of HC11 . . . . . . . . . . . . . . . . . . . . . . . . . 38 3.8.2 Programming the UART of AT90S8535 . . . . . . . . . . . . . . . . . 40 3.8.3 Programming the UART of 8051 . . . . . . . . . . . . . . . . . . . . . . . 41 3.9 Hardware Interfaces for Serial Communication . . . . . . . . . . . . . . . . . . 42 3.9.1 The RS232 Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

3.9.2Differential Communication. The Interfaces RS422 and

RS485 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3.9.3 The Current Loop Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

3.10 Basic Principles of Networking with Microcontrollers, Using the Asynchronous Serial Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

3.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

4 Using the Synchronous Serial Interface SPI . . . . . . . . . . . . . . . . . . . . . . .

49

4.1

In this Chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

49

4.2

General Description of the SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

49

4.3

The SPI of HC11 Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . .

50

4.4

The SPI of the AVR Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . .

53

4.5

Examples of Using The SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

55

 

4.5.1 Using the SPI To Connect Two Microcontrollers . . . . . . . . . .

55

 

4.5.2 Expanding the I/O Space Using the SPI . . . . . . . . . . . . . . . . . .

56

4.6

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

59

5 Using The I2C Bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

61

5.1

In this Chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

61

5.2

The Principles of Implementation of the I2C Bus . . . . . . . . . . . . . . . .

61

 

5.2.1 The Start Transfer Condition . . . . . . . . . . . . . . . . . . . . . . . . . . .

62

 

5.2.2 The Data Transfer on the I2C BuS . . . . . . . . . . . . . . . . . . . . . .

62

 

5.2.3

The ACK Bit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

62

 

5.2.4

The STOP Condition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

63

5.3

A Software Implementation of the I2C Protocol . . . . . . . . . . . . . . . . .

63

5.4

Accessing 24C256 Memory Devices . . . . . . . . . . . . . . . . . . . . . . . . . .

64

5.5

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

66

6 Using the MCU Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

67

6.1

In this Chapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

67

6.2

The General Structure and Functions of the Timer System . . . . . . . .

67