You are on page 1of 43

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 0 and 1 67
4.3 Timer 2 79
4.4 External interrupt 82
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.10 Inter integrated circuit (IIC or I2 C) 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
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

Chapter 1: Introduction To Embedded Systems


Introduction ............................................................................................................. 2

Chapter 2: 8031 Microcontrollers


2.1 Intel’s 8031 Architecture ....................................................................................... 4
2.2 Central Processing Unit ........................................................................................ 6
2.3 Input / Output Ports ................................................................................................ 7
2.4 Timers / Counters .................................................................................................. 8
2.5 Serial Port ............................................................................................................... 8
2.6 Memory Organization ............................................................................................ 9
2.7 Common Memory Space ..................................................................................... 12
2.8 Interrupts ...............................................................................................................12
2.9 Addressing Modes
2.9.1 Register Addressing .................................................................................13
2.9.2 Direct Addressing ..................................................................................... 13
2.9.3 Register Indirect Addressing ...................................................................14
2.9.4 Immediate Addressing .............................................................................. 14
2.9.5 Index Addressing ......................................................................................14
2.10 Instruction Set
2.10.1 Data Transfer Instructions .................................................................... 15
2.10.2 Data Transfer In External RAM ............................................................16
2.10.3 Lookup Tables ........................................................................................17
2.10.4 Arithmetic Instructions ........................................................................... 18
2.10.5 Logical Instructions ................................................................................ 19
2.10.6 Program Control - Jumps, Calls, and Returns ...................................20
2.10.7 Jump Instructions ...................................................................................20
2.10.8 Conditional Jump Instructions ..............................................................22
2.10.9 Operate and Branch Instructions .........................................................23
2.10.10 Boolean Instructions .............................................................................. 24

57

Embedded System Design Using 8031 Microcontrollers


Chapter 3: 8031 Derivatives
3.1 8031 Derivatives ..................................................................................................27
3.2 Why Atmel Devices? ...........................................................................................29

Chapter 4: Real Life Projects


4.1 16KHz Monitor for the Public Call Office ..........................................................31
4.2 Telephone Line Interface ....................................................................................32
4.3 16KHz Metering Pulse Detection .......................................................................33
4.4 Stepper Motor Controller ....................................................................................36
4.5 Programmable Timer ...........................................................................................39
4.6 8 Channel Data Acquisition System .................................................................. 43
4.7 8 Channel Sequential Controller ........................................................................46
4.8 Frequency Counter ..............................................................................................50

Chapter 5: Project Tools


5.1 Software Options ................................................................................................. 53
5.2 Stand Alone Device Assemblers ........................................................................54
5.3 Stand Alone Remote Debugger ..........................................................................54
5.4 Standalone Simulators .........................................................................................55
5.5 In Circuit Emulators .............................................................................................56

Chapter 6: Topview Simulator


6.1 Introduction ...........................................................................................................58
6.2 Device Selection ..................................................................................................60
6.3 Program Editing ....................................................................................................62
6.4 Clearview ..............................................................................................................63
6.5 Program Execution ..............................................................................................65
6.6 Simulation Facilities .............................................................................................67
6.6.1 LED Modules .............................................................................................69
6.6.1.1 Plain Point LEDs ........................................................................69
6.6.1.2 Seven Segment Displays ..........................................................70
6.6.2 LCD Module ...............................................................................................71

58

Embedded System Design Using 8031 Microcontrollers


6.6.3 Keyboard Module ......................................................................................72
6.6.4 I 2C Module ..................................................................................................74
6.6.5 SPI Modules ...............................................................................................75
6.7 Code Generation Facilities .................................................................................75
6.7.1 Internal Peripheral Functions .................................................................. 76
6.7.1.1 Serial Port ...................................................................................76
6.7.2 External Peripheral Modules ...................................................................76
6.7.2.1 LED Display Functions ..............................................................76
6.7.2.2 LCD Module Selection ............................................................... 77
6.7.2.3 Keyboard Interfacing .................................................................78
6.7.2.4 I 2 C / SPI Buses ........................................................................... 80

Chapter 7: Topview Debugger


7.1 Introduction ...........................................................................................................82

Chapter 8: Topview Programmer


8.1 Introduction ...........................................................................................................88
8.2 GUI Features ........................................................................................................ 90

Chapter 9: Contact Frontline Electronics


9.1 Contact Frontline Electronics .............................................................................93

59

Embedded System Design Using 8031 Microcontrollers


Contents

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

6.6 Application 6: Counting Cars 110


6.7 Application 7: Anonymous Voting 114
6.8 Energy Efficient Lighting Using Microcontroller 119

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

1 Estructura básica de un sistema microprocesador


1.1 Introducción .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. 15
1.2 Estructura básica de un sistema microprocesador ...... .. ... . ... .. .. .. .. ... .. .. .. .. .. . 15
1.2.1 Unidad central de proceso (CPU) ...... .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. 16
1.2.2 Módulo de entradas/salidas (E/S) .. .. .. ... .. .. .. .. .. ... ... . .. .. .. ... .. .. .. .. .. ... .. . 18
1.2.3 Buses del sistema .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. 18
1.3 Estructura general de un sistema basado en microprocesador .... ... .. .. .. .. .. ... .. .. .. 21
1.4 Estructura general de un sistema basado en microcontrolador ...... .. .. .. .. ... .. .. .. .. 23

2 Las familias de microcontroladores de Intel y de otros fabricantes


2.1 La familia MCS-48 .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. 25
2.2 La familia MCS-51 .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. 25
2.3 La familia MCS-151 ... .. ... . ... .. .. .. .. .. ... .. .. .. .. ... ... . .. .. .. ... .. .. .. .. .. ... 26
2.4 La familia MCS-251 ... .. ... . ... .. .. .. .. .. ... .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... 27
2.5 Microcontroladores de otros fabricantes .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. 28
2.5.1 Microcontroladores de Philips ... ... .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. 28
2.5.2 Microcontroladores de Siemens .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... 30
2.5.3 Microcontroladores de Atmel .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. 31
2.5.4 Microcontroladores de Dallas Semiconductor .. .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. .. 31

3 Arquitectura de las familias MCS-51 y MCS-251


3.1 Arquitectura interna de la MCS-51 .. .. .. ... .. .. .. .. .. .... . .. .. .. .. ... .. .. .. .. .. ... . 33
3.1.1 Relación de terminales .. .... . .. .. .. .. ... .. .. .. .. .. ... ... . .. .. .. ... .. .. .. .. .. ... .. . 34
3.1.2 Puertos de entrada/salida .... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. 36
3.2 Organización de la memoria y de los registros internos de la MCS-51 .. . .. ... .. .. .. .. .. 38
3.2.1 Área de memoria de código de programa y memoria de datos .... .. .. .. .. ... .. .. .. .. .. 38
3.2.2 Área de memoria interna y de registros de propósito general .. .. ... .. .. .. .. .. ... .. .. .. 40
3.2.3 Área de registros especiales (SFR) .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. ... . ... .. .. .. 41
3.3 Arquitectura interna de la MCS-251 ... .. .. .. .. ... .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. 44
3.3.1 Relación de terminales .. .... . .. .. .. .. ... .. .. .. .. .. ... ... . .. .. .. ... .. .. .. .. .. ... .. . 45
3.3.2 Estructura interna de los puertos de entrada/salida ... . ... ... . .. .. .. ... .. .. .. .. .. ... .. . 48
3.4 Organización de los espacios de memoria de la MCS-251 ... .. .. .. ... .. .. .. .. .. ... .. .. 48
3.4.1 Área de memoria ... .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. .... . .. .. .. .. ... .. .. .. .. .. ... . 49
3.4.2 Área de registros de propósito genérico ... ... .. .. .. .. .. ... .. .. .. .. ... .. .. .. .. .. ... .. 51
3.4.3 Área de registros de función específica SFR . .. .. .. .. ... ... . .. .. .. ... .. .. .. .. .. ... .. . 54

© Los autores, 2002; © Edicions UPC, 2002.


10 Microcontroladores MCS-51 y MCS-251

3.4.4 Compatibilidad con la arquitectura de la familia MCS-51 ... . .. .. ... .. .. .. .. .. ... .. .. . 57


3.5 Configuración de la serie 8XC251Sx . .. .. .. ... .. .. .. .. ... .. .. .. .. .. .. ... .. .. .. .. .. . 58
3.5.1 Configuración del acceso a la memoria externa .. .. .. .. .... . .. .. .. ... .. .. .. .. .. ... .. . 58

4 Programación de las familias MCS-51 y MCS-251


4.1 Introducción .. .. .. .. .. ... ... . .. .. .. ... .. .. .. .. .. ... .. ... . .. .. ... .. .. .. .. .. ... .. . 63
4.2 Tipos de direccionamiento .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. . 64
4.2.1 Direccionamiento inmediato . ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. ... . ... .. .. .. .. .. 65
4.2.2 Direccionamiento directo .... . ... .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. . 66
4.2.3 Direccionamiento por registro .. ... .. .. .. .. .. ... .. .. .. .. ... .. .. .. .. .. .. ... .. .. .. .. . 67
4.2.4 Direccionamientoindirecto ... .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. 68
4.2.5 Direccionamiento por desplazamiento o indexado .... .. .. .. ... .. .. .. .. .. ... .. .. .. .. 69
4.2.6 Direccionamiento de bit ... .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. 70
4.2.7 Direccionamiento relativo .... . .. .. .. ... .. .. .. .. .. .. ... ... . .. .. .. ... .. .. .. .. ... .. . 73
4.3 Conjunto de instrucciones de la familia MCS-51 y MCS-251 .. .. .. .. ... .. .. .. .. .. ... 73
4.3.1 Formato de una instrucción .. . ... .. .. .. .. .. ... .. .. .. .. ... .. .. .. .. .. .. ... .. .. .. .. . 74
4.3.2 Directivas de ensamblador ..... .. .. .. .. .. ... .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... 75
4.3.3 Los registros de estado PSW y PSW1 ..... .. .. .. .. .. ... ... . .. .. .. ... .. .. .. .. .. ... . 77
4.3.4 Instrucciones aritméticas .. ... . .. ... .. .. .. .. .. ... .. .. .. ... . ... .. .. .. .. .. ... .. .. .. . 79
4.3.5 Instrucciones lógicas . .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. 83
4.3.6 Instrucciones de transferencia de datos ... ... .. .. .. .. .. .... . .. .. .. .. ... .. .. .. .. .. .. 86
4.3.7 Instrucciones booleanas ... ... . .. .. .. ... .. .. .. .. .. ... .. ... . .. .. ... .. .. .. .. .. ... .. . 90
4.3.8 Instrucciones de control .. ... . .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. 92

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

© Los autores, 2002; © Edicions UPC, 2002.


Índice 11

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

7 Temporizadores/contadores internos y watchdog


7.1 Introducción . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
7.2 Temporizadores/contadores para la MCS-51 .... .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... 174
7.2.1 Timer 0 y Timer 1 .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. 174
7.2.2 Timer 2 .. .. .. ... .. .. .. .. ... .. .. .. .. .. .. ... .. .. .. .. .. ... ... . .. .. ... .. .. .. .. .. ... 181
7.2.3 Timer 0, 1 y 2 como contador ... .. .. ... .. .. .. .. .. ... .. ... . .. .. ... .. .. .. .. .. ... .. . 186
7.3 Temporizadores para la MCS-251 ..... .. ... .. .. .. .. .. .... . .. .. .. .. ... .. .. .. .. .. .. 191
7.4 Funcionamiento de los Timers .. .. ... .. .. .. .. .. ... ... . .. .. .. ... .. .. .. .. .. ... .. .. .. 192
7.4.1 Funcionamiento como temporizador .... ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... 193
7.4.2 Funcionamiento como contador .... .. .. .. .. ... .. .. .. .. ... .. .. .. .. .. .. ... .. .. .. .. . 193
7.5 Timer 0 y Timer 1 .. .. ... .. .. .. .. .. ... .. .. ... . .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. 194
7.5.1 Habilitación de los Timers 0 y 1 ... .. .. .. ... .. .. .. .. .. .... . .. .. .. .. ... .. .. .. .. .. .. 194
7.5.2 Desbordamiento de los Timers 0 y 1 ... .. .. ... .. .. .. .. .... . .. .. .. .. ... .. .. .. .. .. .. 198
7.5.3 Modos de funcionamiento de los Timers 0 y 1 ... .. .. .. .... . .. .. .. ... .. .. .. .. .. ... . 199
7.6 Timer 2 .. ... .. .. .. .. .. ... .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. 213
7.6.1 Modo captura ..... .. ... .. ... . .. .. ... .. .. .. .. .. ... .. .. ... . .. ... .. .. .. .. .. ... .. .. . 215
7.6.2 Modo autorrecarga ..... .. ... . ... .. .. .. .. .. ... .. .. .. .. ... .. .. .. .. .. .. ... .. .. .. .. . 215
7.6.3 Modo de generador de baudios (Baud Rate Generator Mode) .... .. ... .. .. .. .. .. ... .. 217
7.6.4 Modo Clock-out ..... .. .. ... . ... .. .. .. .. .. ... .. .. .. .. ... .. .. .. .. .. .. ... .. .. .. .. . 217
7.7 Timer watchdog ..... .. .. ... . ... .. .. .. .. .. ... .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. . 219
7.7.1 Descripción de funcionamiento . .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. 219
7.7.2 Utilización del timer WDT ..... .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. 220
7.7.3 Timer WDT durante el modo Idle ..... .. .. .. .. .. ... .. .. ... . .. ... .. .. .. .. .. ... .. .. . 220
7.7.4 Timer WDT durante Power Down .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... 220

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

© Los autores, 2002; © Edicions UPC, 2002.


12 Microcontroladores MCS-51 y MCS-251

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

9 Puerto de comunicación serie


9.1 Introducción .. .. .. .. .. ... ... . .. .. .. ... .. .. .. .. .. ... .. ... . .. .. ... .. .. .. .. .. ... .. . 247
9.2 La comunicación serie en la MCS-51 .. .. .. ... .. .. .. .. .. ... .. .. .. .. ... .. .. .. .. .. .. 248
9.3 Modos de funcionamiento del puerto serie .. ... .. .. .. .. .. ... .. .. .. .. ... .. .. .. .. .. .. 250
9.3.1 Modo 0. Modo síncrono ... .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. 250
9.3.2 Modos 1, 2 y 3. Modos asíncronos .. .. .. .. .. .. ... .. .. .. ... . ... .. .. .. .. .. ... .. .. .. . 250
9.3.3 El Timer 2 como base para el puerto serie ..... .. .. .. ... ... . .. .. .. ... .. .. .. .. .. ... . 252
9.4 Detección de errores ..... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. 253
9.5 La comunicación serie en la MCS-251 ... .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. 262
9.6 Modos de operación ..... .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. ... . 264
9.6.1 Modo 0 o síncrono .... .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. 264
9.6.2 Modos 1, 2 y 3. Modos asíncronos .. .. .. .. .. .. ... .. .. .. ... . ... .. .. .. .. .. ... .. .. .. . 267
9.7 Detección de errores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
9.8 Comunicación multiprocesador ..... .. .. .. .. ... .. .. .. .. ... .. .. .. .. .. .. ... .. .. .. .. . 272
9.9 Reconocimiento automático de direcciones ... .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. 273
9.9.1 Direcciones given ... .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. .... . .. .... . . ... .. .. .. .. .. .. 273
9.9.2 Direcciones broadcast ... .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. 274

10 El array de contadores programables (PCA)


10.1 Introducción .. .. .. .. .. ... ... .. . .. .. ... .. .. .. .. .. ... .. ... . .. .. ... .. .. .. .. .. ... .. . 277
10.2 Temporizador/contador del PCA ... .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. 279
10.3 Módulos de comparación/captura del PCA . .. ... .. .. .. .. ... .. .. .. .. .. .. ... .. .. .. .. . 280
10.3.1 Modo captura ..... .. ... .. ... . .. .. ... .. .. .. .. .. ... .. .. ... . .. ... .. .. .. .. .. ... .. .. . 280
10.3.2 Modos de comparación ..... . .. .. .. .. ... .. .. .. .. .. ... ... . .. .. ... .. .. .. .. .. ... .. . 287

11 Entradas y salidas analógicas


11.1 Introducción .. .. .. .. .. ... ... . .. .. .. ... .. .. .. ... . ... .. ... . .. .. ... .. .. .. .. .. ... .. . 297
11.2 Conexión de un convertidor D/A . ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. 298
11.3 Convertidor A/D de bajo coste mediante aproximaciones sucesivas .. . .. .. ... .. .. .. .. 302
11.4 Conexión de un convertidor A/D . ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. 305
11.5 Conversión A/D utilizando los temporizadores del microcontrolador ..... .. .. .. .. ... . 317

© Los autores, 2002; © Edicions UPC, 2002.


Índice 13

12 Modos especiales de funcionamiento


12.1 Introducción .. .. .. .. .. ... ... . .. .. .. ... .. .. .. .. .. ... .. ... ... . . ... .. .. .. .. .. ... .. . 327
12.2 Registro de control de potencia (PCON) ... .. ... .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. . 327
12.2.1 Bits de control del puerto serie ..... ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. 327
12.2.2 Bit de Power Off (POF) ...... . .. .. ... .. ... . .. .. ... .. .. ... . .. ... .. .. .. .. .. ... .. .. . 327
12.3 Modo Idle ..... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... 328
12.4 Modo Power Down ..... .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. 329
12.5 Modo ONCE (On-Circuit Emulation) ...... .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. 330

Apéndice Juego de instrucciones de la familia MCS-51 y MCS-251 .. . .. .. ... .. .. .. .. 331

Bibliografía .... .. .. ... ... . .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. ... .. .. .. .. .. .. 337

© Los autores, 2002; © Edicions UPC, 2002.


Table of Contents
Chapter 1 Microcontroller Basics 1
What’s a Microcontroller? 1
A Little History 2
New Tools 3
Project Steps 4
Chapter 2 Inside the 8052-BASIC 11
Possibilities 11
Limits 13
What You Need 14
The 8051 Family 16
Elements of the 8052 and 8052-BASIC 17
Chapter 3 Powering Up 23
About the Circuit 23
Circuit Construction 30
Powering Up 35
Basic Tests 38
Simple Programs to Try 40
Exiting Programs 44

Chapter 4 Saving Programs 47


Nonvolatile Memory Options 47
Adding NVRAM or EEPROM 50
Using the Programming Commands 53
Adding Bootup Options 54

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

You might also like