Table Of ContentR
i
ElEctrical EnginEEring z
v Microcontroller
i
Microcontroller Programming, An Introduction is a comprehensive and one-stop resource
to the concepts, principles, solution development and techniques of microcontroller-based
PrograMMing
systems, focusing on the elements and features of the popular and powerful 68HC11 M
microcontroller iC as a representative example.
i
c A n I n t r o d u c t I o n
FeAtures
r
• Performance-based objectives at the beginning of each chapter to outline the
o
goals to be achieved.
• Extensive margin annotations include expert tips to help build practical knowledge.
c
• More than 150 examples with step-by-step solutions to clarify problems that are
o
normally stumbling blocks.
• More than 225 detailed illustrations allow readers to keep major insights in focus
n
while studying the details.
• Section review quiz with answers to provide readers self evaluation. t
• Five fully explained projects with step-by-step guidance to gain hands-on experience r
in embedded application development. o
• End-of-chapter summary to provide quick chapter revision.
• End-of-chapter glossary to summarize the essential terminologies introduced in l
the chapter. l
• Nearly 450 end-of-chapter quizzes, questions and problems of varied complexity e
to help reinforce concepts. r
• Extensive appendix to supplement key topics.
• Accompanying CD-ROM contains relevant assembly and C source code to practice P
programming skills. r
o
Unlike other Microcontroller books that take the long, detailed approach to every topic of
concern to microcontroller programmers, this text instead focuses on how microcontroller- g
based applications are architected, developed, and designed, enabling developers to develop
r
solid, effective embedded applications. Packaged with helpful examples, projects, and
illustrations, it gives an in-depth treatment of microcontroller design, programming in both a
assembly and C.
M
Extensive supplements package is available free of charge to instructors using the book
for a course. M
Anyone interested in learning microcontroller-based application development in depth,
i
rather than just scraping the surface, will enjoy and benefit immensely from reading n
this book.
g
Syed R. Rizvi
K12313
K12313_Cover_mech.indd 1 9/29/11 12:40 PM
MICROCONTROLLER
PROGRAMMING
A N I N T R O D U C T I O N
MICROCONTROLLER
PROGRAMMING
A N I N T R O D U C T I O N
SYED R. RIZVI
Boca Raton London New York
CRC Press is an imprint of the
Taylor & Francis Group, an informa business
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2012 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business
No claim to original U.S. Government works
Version Date: 2011912
International Standard Book Number-13: 978-1-4398-9765-2 (eBook - PDF)
This book contains information obtained from authentic and highly regarded sources. Reasonable efforts
have been made to publish reliable data and information, but the author and publisher cannot assume
responsibility for the validity of all materials or the consequences of their use. The authors and publishers
have attempted to trace the copyright holders of all material reproduced in this publication and apologize to
copyright holders if permission to publish in this form has not been obtained. If any copyright material has
not been acknowledged please write and let us know so we may rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmit-
ted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented,
including photocopying, microfilming, and recording, or in any information storage or retrieval system,
without written permission from the publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.
com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood
Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and
registration for a variety of users. For organizations that have been granted a photocopy license by the CCC,
a separate system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used
only for identification and explanation without intent to infringe.
Visit the Taylor & Francis Web site at
http://www.taylorandfrancis.com
and the CRC Press Web site at
http://www.crcpress.com
Dedication
To my beloved wife Susan, for providing encouragement and understanding
throughout the preparation of this text; and to my daughters, Zahra and Sophia.
Contents
Preface......................................................................................................................xv
Acknowledgments.............................................................................................xxiii
The.Author...........................................................................................................xxv
1. Number Systems, Operations, and Codes..................................................1
1.1. Introduction............................................................................................2
1.2. Digital.versus.Analog.Quantities........................................................3
1.3. Digital.Numbering.System.(Base.10)..................................................4
1.4. Binary.Numbering.System.(Base.2).....................................................5
1.5. Octal.Numbering.System.(Base.8).......................................................7
1.6. Hexadecimal.Numbering.System.(Base.16).......................................8
1.7. Binary-Coded-Decimal.System..........................................................10
1.8. Binary.Conversions..............................................................................11
1.9. Binary.Operations................................................................................12
1.9.1. Binary.Addition......................................................................13
1.9.2. Binary.Subtraction..................................................................14
1.9.3. Binary.Multiplication.............................................................15
1.9.4. Binary.Division.......................................................................16
1.10. Octal.Conversions................................................................................17
1.11. Hexadecimal.Conversions..................................................................18
1.12. Hexadecimal.Operations....................................................................20
1.12.1. Hexadecimal.Addition...........................................................20
1.12.2. Hexadecimal.Subtraction......................................................21
1.13. 1’s.and.2’s.Complements.of.Binary.Numbers..................................21
1.13.1. Finding.the.1’s.Complement..................................................21
1.13.2. Finding.the.2’s.Complement.................................................21
1.14. Signed.Numbers...................................................................................23
1.14.1. The.Sign.Bit..............................................................................24
1.14.2. Sign-Magnitude.Form............................................................24
1.14.3. 1’s.Complement.Form.............................................................24
1.14.4. 2’s.Complement.Form.............................................................25
1.15. The.ASCII.Code....................................................................................25
1.16. Summary...............................................................................................28
Glossary............................................................................................................29
Answers.to.Section.Review.Quiz.................................................................30
True/False.Quiz..............................................................................................30
Questions.........................................................................................................31
Problems...........................................................................................................32
vii
viii Contents
2. Semiconductors and Digital Logic.............................................................35
2.1. Introduction..........................................................................................36
2.2. Diode.Logic...........................................................................................36
2.3. The.Inverter...........................................................................................37
2.3.1. Inverter.Truth.Table................................................................38
2.3.2. Inverter.Symbol.......................................................................39
2.3.3. Operation.of.an.Inverter........................................................40
2.3.4. Timing.Diagrams....................................................................40
2.3.5. Logic.Expressions.for.an.Inverter.........................................41
2.4. The.AND.Gate......................................................................................42
2.4.1. AND.Gate.Symbol..................................................................42
2.4.2. Operation.of.an.AND.Gate....................................................43
2.4.3. AND.Gate.Truth.Table...........................................................44
2.4.4. Timing.Diagrams....................................................................46
2.4.5. Logic.Expressions.for.an.AND.Gate....................................47
2.5. The.OR.Gate..........................................................................................49
2.5.1. OR.Gate.Symbol......................................................................49
2.5.2. Operation.of.an.OR.Gate........................................................49
2.5.3. OR.Gate.Truth.Table...............................................................51
2.5.4. Timing.Diagram......................................................................52
2.5.5. Logic.Expressions.for.an.OR.Gate........................................55
2.6. The.NAND.Gate...................................................................................57
2.6.1. NAND.Gate.Symbol...............................................................57
2.6.2. Operation.of.a.NAND.Gate...................................................57
2.6.3. Timing.Diagram......................................................................58
2.6.4. Negative-OR.Equivalent.Operation.of.a.NAND.Gate.......60
2.6.5. Logic.Expressions.for.a.NAND.Gate...................................61
2.7. The.NOR.Gate.......................................................................................62
2.7.1. NOR.Gate.Symbol...................................................................62
2.7.2. Operation.of.a.NOR.Gate.......................................................62
2.7.3. Timing.Diagram......................................................................63
2.7.4. Negative-AND.Equivalent.Operation.of.the.NOR.Gate....64
2.7.5. Logic.Expressions.for.a.NOR.Gate.......................................65
2.8. The.Exclusive-OR.Gate........................................................................67
2.8.1. XOR.Gate.Symbol....................................................................67
2.8.2. Operation.of.XOR.Gate..........................................................67
2.8.3. XOR.Gate.Truth.Table.............................................................69
2.8.4. Timing.Diagram......................................................................69
2.8.5. Parity.........................................................................................71
2.8.5.1. Odd-Parity.Generator.............................................72
2.9. The.Exclusive-NOR.Gate.....................................................................74
2.9.1. XNOR.Gate.Symbol................................................................74
2.9.2. Operation.of.XNOR.Gate.......................................................75
Contents ix
2.9.3. XNOR.Gate.Truth.Table.........................................................75
2.9.4. Timing.Diagram......................................................................76
2.10. Summary...............................................................................................77
Glossary............................................................................................................78
Answers.to.Section.Review.Quiz.................................................................79
True/False.Quiz..............................................................................................80
Questions.........................................................................................................80
Problems...........................................................................................................81
3. Microcontroller Hardware...........................................................................89
3.1. Introduction..........................................................................................90
3.2. A.Transistor.as.a.Switch......................................................................91
3.3. The.TTL.Integrated.Circuit.................................................................93
3.4. The.CMOS.Integrated.Circuit............................................................94
3.5. Using.Integrated-Circuit.Logic.Gates...............................................96
3.6. Seven-Segment.Displays.....................................................................98
3.7. Liquid-Crystal.Displays....................................................................102
3.8. Keypads...............................................................................................105
3.9. The.68HC11/68HC12.Microcontroller.............................................106
3.9.1. HC11.Processor.....................................................................110
3.9.2. HC11.Memory.......................................................................112
3.9.2.1. RAM........................................................................114
3.9.2.2. ROM........................................................................115
3.9.2.3. EEPROM.................................................................116
3.9.2.4. System.Registers....................................................117
3.9.3. HC11.Advanced.On-Chip.Input/Output.(I/O).
Capabilities............................................................................118
3.10. EVBU/BUFFALO................................................................................121
3.11. Summary.............................................................................................124
Glossary..........................................................................................................126
Answers.to.Section.Review.Quiz...............................................................128
True/False.Quiz............................................................................................129
Questions.......................................................................................................130
Problems.........................................................................................................131
4. Microcontroller Software...........................................................................133
4.1. Introduction........................................................................................134
4.2. Programming.Concepts....................................................................135
4.3. System.Software.................................................................................139
4.4. Developing.a.Program......................................................................142
4.4.1. Problem.Analysis..................................................................144
4.4.2. Design.Development............................................................145
4.4.3. Coding....................................................................................145
4.4.4. Program.Testing....................................................................147