Professional Documents
Culture Documents
Preface Acknowledgements: V VII 1
Preface Acknowledgements: V VII 1
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
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 0 and 1 67
4.3 Timer 2 79
4.4 External interrupt 82
iv Contents
Index 407
Contents
1 Supported Microcontrollers ............................................................. 9
1.1 8051 Family .............................................................................. 9
1.2 AVR Family............................................................................. 11
2 BASCOM....................................................................................... 23
2.1 BASCOM Demos.................................................................... 23
2.2 BASCOM Commercial Versions............................................. 25
2.3 Update of BASCOM Commercial Versions ............................ 25
2.4 BASCOM Projects .................................................................. 27
2.4.1 Working on Projects ........................................................ 27
2.4.2 BASCOM Options ............................................................ 28
2.5 BASCOM Tools ...................................................................... 37
2.5.1 Simulation ........................................................................ 37
2.5.2 Terminal Emulator ........................................................... 40
2.5.3 LCD Designer .................................................................. 42
2.5.4 Library Manager ............................................................... 46
2.5.5 Programming Devices ..................................................... 50
2.6 Hardware for AVR RISC Microcontroller ................................ 55
2.6.1 DT006 AVR Development Board ..................................... 55
2.6.2 AVR-ALPHA with AT90S2313 ......................................... 56
2.7 Instead of "Hello World".......................................................... 57
2.7.1 AVR.................................................................................. 57
2.7.2 8051 ................................................................................. 58
2.7.3 Things in Common........................................................... 59
2.7.4 Simulation ........................................................................ 64
2.8 BASCOM Help System........................................................... 67
3 Some BASCOM Internals ............................................................. 69
3.1 Building new instructions ........................................................ 69
5
3.2 Parameters for Subroutines in BASCOM-AVR....................... 71
3.3 BASIC & Assembler................................................................ 73
3.3.1 AVR .................................................................................. 74
3.3.2 8051 ................................................................................. 75
4 Applications ................................................................................... 77
4.1 Programmable Logic............................................................... 77
4.2 Timer and Counter.................................................................. 81
4.2.1 AVR .................................................................................. 81
4.2.2 8051 ............................................................................... 104
4.3 LED Control .......................................................................... 107
4.3.1 Single LED ..................................................................... 107
4.3.2 Seven-Segment Displays ............................................... 108
4.3.3 Dot-Matrix Displays ........................................................ 114
4.4 LCD Control .......................................................................... 119
4.4.1 Direct Control ................................................................. 119
4.4.2 LCD with Serial Interface ............................................... 122
4.5 Connecting Keys and Keyboards.......................................... 128
4.5.1 Single Keys .................................................................... 129
4.5.2 Matrix Keypad ................................................................ 132
4.5.3 PC-AT Keyboard ............................................................ 136
4.6 Data Input by IR Remote Control.......................................... 140
4.7 Asynchronous Serial Communication................................... 143
4.8 1-WIRE Interface .................................................................. 151
4.9 SPI Interface ......................................................................... 161
2
4.10 I C Bus................................................................................ 167
4.11 Scalable Network Protocol S.N.A.P .................................... 173
4.11.1 S.N.A.P. Features ........................................................ 174
4.11.2 Description of S.N.A.P. Protocol .................................. 175
4.11.3 S.N.A.P. Monitor........................................................... 179
4.11.4 Digital I/O...................................................................... 183
6
4.12 CANDIP - Interface to CAN ................................................ 197
4.13 Random Numbers .............................................................. 209
4.14 Moving Average.................................................................. 214
5 Appendix ..................................................................................... 219
5.1 Decimal-Hex-ASCII Converter.............................................. 219
5.2 DT006 Circuit Diagram ......................................................... 220
5.3 Characters in Seven-Segment Display................................. 222
5.4 BASIC Stamp II .................................................................... 223
5.5 Literature .............................................................................. 224
5.6 Links ..................................................................................... 225
6 Index ........................................................................................... 231
7
Contents
57
58
59
Foreword ix
Preface xiii
Acknowledgments xvii
1
Microcontrollers: Yesterday, Today, and Tomorrow 1
1.1 Defining Microcontrollers 1
1.2 Eagle’s View: Microcontrollers and Other Competing Devices 2
1.3 Vignettes: Microcontrollers 3
1.4 Microcontroller Applications 5
1.5 Growth Economics 7
1.6 The Major Players in the Microcontroller Chip Market 8
1.7 Architectural Trends 10
1.8 Jump Starting Your First Embedded System Project 14
1.9 Execution of Embedded System Project: Is it a
Gordian’s Knot? 16
2
Integrated Development Environment 19
2.1 Getting Familiar with the IDE 19
2.2 Working with Keil IDE 20
2.3 Development Flow for the Keil IDE 21
3
Art of C Programming for Microcontrollers 29
3.1 Familiarizing with Your Compiler Capabilities 29
3.2 Whether to Use Headers or Not? 30
3.3 Basic C Program Structure 34
3.4 Differences from ANSI C 34
v
vi Contents
4
Exploring the Capabilities of On-Chip Resources Programming 37
for I/O Ports, Interrupts and Timer/Counter
4.1 Importance of Ports 37
4.2 Simple Ideas for Port Expansion 46
4.3 LED Interfacing 46
4.4 Relevance of LEDs in Today’s Lightening Industry 48
4.5 Different Programs for LED Interfacing 48
4.6 More Projects on LED Interfacing to Microcontrollers 51
4.6.1 Running LEDs 51
4.6.2 Running Bicolor LED 51
4.6.3 Interfacing 6 LEDs
Using 3 Microcontroller Pins by 51
4.6.4 The LED Dimmer Demoboard 51
4.6.5 Fading RGB LED 52
4.6.6 LED Moving Font 52
4.7 DIP Switch Interfacing: Getting Input to Your
Embedded System 52
4.8 LCD Interfacing 54
4.9 Interrupts in Microcontrollers 57
4.9.1 Writing ISRs in C 58
4.9.2 A Word about Interrupt Latency 63
5
Hyperterminal-Based Control 69
5.1 Hyperterminal 69
5.2 Packet-based Control Scheme 69
5.3 Mechanism and Lots of Possibilities 70
5.4 Application 1: Packet-based Interface for LEDs 71
5.5 Application 2: Packet-based Interface for Stepper
Motor Control 74
5.6 Application 3: Home Automation from PC HyperTerminal 78
6
Embedding Microcontroller in Routine Applications 85
6.1 Application 1: Podium Timer 85
6.2 Application 2: Front Desk Notifier 90
6.3 Application 3: Cafeteria Food Alert/Microcontroller-based
Menu Card 94
6.4 Application 4: Chimney Sentinel 100
6.5 Application 5: Who’s First Timer 104
Contents vii
7
Microcontroller-based Measurement and Control Applications 123
7.1 Application 1: Reading a PWM Waveform Using
Microcontroller 123
7.2 Single Set-point On/Off Controller 127
7.3 Application 3: I2 C Interface with Serial EPROM 133
8
Securing Your Embedded System Application 139
8.1 Security Challenges in Embedded Systems 139
8.2 Application 1: Authentication for Your Embedded
System Application 140
8.3 Application 2: Timeout Waiting for Input Data 144
References 151
Index 155
Índice 9
Índice
5 El modelo de programación
5.1 Creación y consulta a tablas .. .. .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. ... . . 97
5.2 Transferencia de bloques de datos .... .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. . 98
5.3 Funciones booleanas .. ... ... . .. .. .. ... .. .. .. .. .. ... .. ... . .. .. ... .. .. .. .. .. ... .. . 99
5.4 Retardos de tiempo ... ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .... . . ... .. .. 100
5.5 Suma y resta de datos .... .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. 101
5.6 Contador en BCD .. .. .. .. ... . ... .. .. .. .. .. ... .. .. .. .. ... .. .. .. .. .. .. ... .. .. .. .. . 103
5.7 Multiplicación y división de datos de 16 bits .... .. .. .. ... .. .. .. ... .. .. .. .. .. .. ... .. 105
5.8 Suma y resta de datos con signo . .. .. .. ... .. .. .. .. .. ... ... . .. .. .. ... .. .. .. .. .. ... . 109
5.9 Multiplicación y división de 16 bits con signo . .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. 109
5.10 Ejemplos de aplicación ... ... . .. .. .. ... .. .. .. .. ... .. .. ... . .. .. ... .. .. .. .. .. ... .. . 113
5.10.1 Generación de una señal cuadrada ... .. .. ... .. .. .. .. .. .... . .. .. .. .. ... .. .. .. .. .. .. 113
5.10.2 Conexión de teclas al microcontrolador ..... .. .. .. ... .. ... . .. .. ... .. .. .. .. .. ... .. . 115
5.10.3 Conexión de un dígito de siete segmentos .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. 117
5.10.4 Conexión de un teclado matricial de 4 x 4 teclas .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. 118
5.10.5 Conexión de varios dígitos de siete segmentos, aplicación de “Su turno” .. .. .. .. ... .. . 121
5.10.6 Contador de piezas .... .. .. ... .. .. .. .. .. ... .. .. .. .. .. .... . .. .. .. .. ... .. .. .. .. .. .. 125
5.10.7 Control de un ascensor ... ... .. .. .. .. .. .. ... .. .. .. .. .. .... . .. .. .. .. ... .. .. .. .. .. .. 127
5.10.8 Control de un calefactor ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. 129
5.10.9 Control de una cinta elevadora .... .. ... .. .. .. .. .. ... .. ... . .. .. ... .. .. .. .. .. ... .. . 131
5.10.10 Control de la temperatura de un horno de cocción ... .. .. ... .. .. .. .. .. .... . .. .. .... . 134
6 Las interrupciones
6.1 Introducción .. .. .. .. .. ... ... . .. .. .. ... .. .. .. .. .. ... .. ... . .. .. ... .. .. .. .. .. ... .. . 137
6.2 Las interrupciones en la familia MCS-51 . .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. 139
6.2.1 Vectorización de interrupciones en la MCS-51 .. .. ... . .. ... .. .. .. .. .. .. ... .. .. .. .. . 142
6.2.2 Habilitación de interrupciones y establecimiento de prioridades en la MCS-51 .. . .. .. .. 143
6.2.3 Tiempos de respuesta del proceso de interrupción ... .. .. ... .. .. .. .. .. ... .. .. .. .. .. . 145
6.3 El sistema de interrupciones en la familia MCS-251 .. .. .. .. .. .. ... .. .. .. .. .. ... .. .. 152
6.3.1 Habilitación de las interrupciones . .. .. .. .. .. .. ... .. .. .. ... . ... .. .. .. .. .. ... .. .. .. . 155
6.3.2 Niveles de prioridad de las interrupciones .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. 156
6.3.3 Interrupciones externas /INT0 e /INT1 ..... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... 158
6.3.4 Interrupción de los Timers ... . .. .. ... .. .. .. .. .. ... .. .. ... . .. ... .. .. .. .. .. ... .. .. . 163
6.3.5 Interrupción del array de contadores programables (PCA) .. .. ... .. .. .. .. .. ... .. .. .. 164
6.3.6 Interrupción del puerto serie ... .. .. .. .. .. ... .. .. .. .. .. .... . .. .. .. .. ... .. .. .. .. .. .. 165
8 Memoria externa
8.1 Introducción .. .. .. .. .. ... ... . .. .. .. ... .. .. .. .. .. ... .. ... . .. .. ... .. .. .. .. .. ... .. . 221
8.2 Memorias semiconductoras .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. 221
8.3 Estructura externa de las memorias .. .. .. .. ... .. .. .. .. .. .... . .. .. .. .. ... .. .. .. .. .. 223
8.4 Ciclos de fetch, de lectura y de escritura .. .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. 224
8.5 Conexión entre la MCS-51 y la memoria externa . .. ... .. ... . .. .. ... .. .. .. .. .. ... .. . 225
8.5.1 Diagramas de tiempo para la MCS-51 . ... .. .. ... . .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. 225
8.6 Ejemplos de conexión para la MCS-51 .. .. .. .. .. ... .. .. ... . .. ... .. .. .. .. .. ... .. .. . 227
8.7 Conexión con la memoria externa para la familia MCS-251 .. .. .. .. .. .. ... .. .. .. .. .. 231
8.8 Configuraciones de acceso a la memoria externa . ... .. .. ... . .. ... .. .. .. .. .. ... .. .. . 233
8.8.1 18 bits de bus de direcciones (RD1, RD0 = 00) ..... .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. 233
8.8.2 17 bits de bus de direcciones (RD1, RD0 = 01) ..... .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. 239
8.8.3 16 bits de bus de direcciones (RD1, RD0 = 10) ..... .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. 240
8.8.4 16 bits de bus de direcciones (RD1, RD0 = 11) ..... .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. 240
iii
Erasing NV Memory 55
Adding more NVRAM or EEPROM 56
Adding EPROM 56
EPROM-programming Circuits 57
Power Supplies for Programming 61
Storing Programs on Disk 63
Chapter 5 Programming 65
Programming Basics 65
BASIC-52 Bugs and Things to Watch Out For 69
Finding Program Errors 70
BASIC-52 Keywords by Function 72
Quick Reference to BASIC-52 74
Chapter 6 Inputs and Outputs 87
The Memory Map 87
Uses for I/O Ports 89
Adding Ports 89
The 8255 Programmable Peripheral Interface 98
Chapter 7 Switches and Keypads 109
Simple Switches 109
Adding a Keypad 116
Chapter 8 Displays 125
Using LEDs 125
7-segment Displays 129
Displaying Messages 138
Inside the Display Controller 140
Mounting Displays in an Enclosure 152
Chapter 9 Using Sensors to Detect and Measure 153
Sensor Basics 153
Choosing Sensors 154
On/off Sensors 155
Analog Sensors 156
Sensor Examples 163
Level Translating 167
Choosing a Converter 169
Chapter 10 Clocks and Calendars 171
BASIC-52’s Real-time Clock 171
A Watchdog Timekeeper 174
iv
Chapter 11 Control Circuits 185
Switching Power to a Load 185
Controlling a Switch Matrix 187
Op Amp with Programmable Gain 189
Controlling a Stepper Motor 191
Speed Control of a Continuous DC Motor 195
Chapter 12 Wireless Links 199
Infrared Links 199
Increasing the Distance 212
Radio Links 216
Chapter 13 Calling Assembly-language Routines 217
Assembly-language Basics 218
What You Need 218
Loading a Routine 221
File Formats for Assembly-language Routines 222
Assembling a Program 224
Uploading a Program 225
Example: Creating a Sine Wave 227
Avoiding Program Crashes 231
Interrupts 232
Adding Custom Commands and Instructions 233
A General-purpose EPROM Programmer 237
Chapter 14 Running BASIC-52 from External Memory 239
Reasons 239
Copying BASIC-52 240
System Requirements 241
Storing BASIC-52 Programs 243
Chapter 15 Related Products 245
Enhanced BASIC-52 245
BASIC Compilers 246
Programming Environments 247
Pc Boards 248
BASIC-52 Source Code 250
Appendix A Sources 251
Books 251
BBS’s 254
Product Vendors 255
v
Appendix B Programs for Loading Files 263
Appendix C Number Systems 271
About Number Systems 271
Kilobytes and Megabytes 273
vi
THE FINAL WORD ON THE 8051
- Introduction
This is a book about the Intel 8051 microcontroller and its large family of descendants. It is intended to
give you, the reader, some new techniques for optimizing your 8051 projects and the development
process you use for those projects. It is not the purpose of this book to provide various recipes for
different types of embedded projects.
Wherever possible, I have included code examples to make the discussion clearer. There are points in
the book where projects are discussed as a means of illustrating the point of the given chapter. Much of
this code is available on the companion disk, to use it you will need to be familiar with C and 8051
assembler since this book is not intended to be a tutorial in C or 8051 assembler. There are many fine
books you can buy to learn about ANSI C. As for 8051 assembler, the best source is the Intel data book
which is free from your 8051 vendor or the manual that comes with your particular assembler.
The code on the companion diskette contains the code I wrote and compiled for the book you hold in
your hands. It is fully functional and has been tested. This is not to say that that the code on the diskette
is ready to go into your system and be delivered as part of your projects. Some of it will require change
before it can be integrated into your system.
This book will help you learn how to make the best out of the tools you have. If you only have an 8051
assembler, you can still learn from this book and use the examples, but you will have to decide for
yourself how to implement the C language examples in assembler. This is not a difficult task for anyone
who understands the basics of C and the 8051 assembler set.
If you have a C compiler for the 8051, then I congratulate you. You have made an excellent decision in
your use of C. You will find that your project development time using C is lower and that your
maintenance time using C is also lower. If you have the Keil C51 package, then you have made an
excellent decision in 8051 development tools. I have found that the Keil package for the 8051 provides
the best support. The code in this book directly supports the Keil C extensions. If you have one of the
other development packages such as Archimedes or Avocet, you will find that this book is still of great
service to you. The main thing to be aware of is that you may have to change some of the Keil specific
directives to the appropriate ones for your development tools.
In many places in this book are diagrams of the hardware on which the example code runs. These are
not intended to be full schematics, but are merely block diagrams that have enough information to allow
you to understand how the software must interface to the hardware.
You should look upon this book as a learning tool rather than a source of various system designs. This is
not an 8051 cookbook, but rather an exploration of the capabilities of the 8051 given proper hardware
and software design. I prefer to think that you will use this book as a source of ideas from which your
designs springboard and grow in a marvelous world of sunshine and roses! Seriously, though, I think you
will gain useful knowledge from this book that will help you greatly improve your designs and make you
look like your company’s 8051 guru.
Page 1