The Microcontroller Idea Book (Jan Axelson, 1994)
.pdfThe
Microcontroller Idea Book
Circuits, Programs, & Applications featuring the 8052-BASIC Microcontroller
Jan Axelson
Lakeview Research
Madison, WI
copyright 1994, 1997 by Jan Axelson Published by Lakeview Research
14 13 12 11 10 9 8 7 6 5 4
Lakeview Research
5310 Chinook Ln.
Madison, WI 53704 USA
Phone: 608-241-5824
Fax: 608-241-5848 Email: jaxelson@lvr.com
WWW: http://www/lvr.com
No part of this book, except the programs and program listings, may be reproduced in any form, or stored in a database or retrieval system, or transmitted or distributed in any form, by any means, electronic, mechanical photocopying, recording, or otherwise, without the prior written permission of Lakeview Research or the author, except as permitted by the Copyright Act of 1976. The programs and program listings, or any portion of these, may be stored and executed in a computer system and may be incorporated into computer programs developed by the reader.
Trademarks
Macintosh is a registered trademark of Apple Computer. Procomm Plus and Datastorm are registered trademarks of Datastorm Technologies, Inc. VT100 is a registered trademark of Digital Equipment Corporation. IBM is a registered trademark of International Business Machines Corporation. MCS-BASIC-52 and Intel are registered trademarks of Intel Corporation. Microsoft, MS-DOS, GW-BASIC, and Microsoft Windows are registered trademarks of Microsoft Corporation. Philips is a registered trademark of Philips International BV.
The author and publisher have used their best efforts in preparing this book and the materials in it. The author built and tested the electronic circuits described, ran and tested the computer programs presented, and reviewed all materials for completeness and accuracy. The author and publisher make no warranty with regard to the circuit schematics, program listings, and other materials in this book. The author and publisher take no responsibility for any damages resulting from any use of the material in this book.
ISBN 0-9650819-4-X
Apart from any fair dealing for the purposes of research or private study, or criticism or review, as permitted under the relevant copyright, designs and patents acts, this publication may only be reproduced, stored or transmitted, in any form or by any means, with the prior permission in writing of the publisher. eBooks Corporation
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
Introduction
Introduction
This book is a hands-on guide to designing, building, and testing microcontroller-based devices. Microcontrollers, or single-chip computers, are ideal for projects that require computer intelligence, but don’t need the overhead of a complete personal computer with disk drives, keyboard, and full-screen display.
Why the 8052-BASIC?
This book focuses on the 8052-BASIC microcontroller, which is easy to use, full featured, and inexpensive to work with. The on-chip BASIC-52 programming language enables you to write, run, and test your programs quickly. With over 100 commands, instructions, and operators, BASIC-52 is more capable than other microcontroller BASICs. And, as a member of the 8051 microcontroller family, the 8052-BASIC has a standard, popular architecture.
The ideas and applications presented here are not limited to the 8052-BASIC, however. If your favorite chip is a different one, you can adapt the circuits and programs to it. The schematics and program listings in this book include comments and explanations to help you apply the ideas, whether or not you are using the 8052-BASIC.
vii
Introduction
What’s Inside?
This is not just a textbook that presents information but glosses over the details of how to apply it. Inside, you’ll find practical information, including the following:
•Complete circuit schematics and parts lists—so you can easily build the circuits yourself.
•Design theory—for example, why use this particular component? or how can I expand or modify the circuit shown?
•Example program listings—for easy testing and use of the circuits.
•Construction and debugging tips—to help you get the circuits up and running without problems.
The appendices include a complete list of sources to help you find the components and additional information you need for your projects.
Because microcontroller projects involve both circuit design and programming, I cover both the hardware and software aspects. The book begins with an introduction to microcontrollers, and to the 8052-BASIC chip in particular. Next are basic circuits to get you started programming and interfacing to the chip, along with the reasons behind the component and design choices, and construction details for prototyping. To the basic circuits, I show how to add switches, keypads, displays, and other input/output interfaces.
A programming reference describes each of BASIC-52’s keywords, with specific tips for trouble-free programming in BASIC-52.
You’ll also find out how to add these to your system:
•Sensors, for detecting detect and measuring physical properties.
•Clock/calendar functions, for keeping track of seconds, minutes, hours, days, months, and years, and to trigger alarms at particular times.
•Control of AC power, switch matrices, stepper and continuous motors, and gain of an op amp.
•Programmable wireless links, for situations where stringing wires isn’t practical or convenient.
viii
Introduction
A chapter on assembly-language interfacing shows how to add assembly-language programs for faster program execution, how to add your own commands the BASIC-52 programming language, and how to use the 8052-BASIC as a development system for an all-assembly- language project.
The final chapters cover other options for 8052-BASIC systems, including how to store BASIC-52 in external memory rather than in the 8052’s internal ROM, and a review of related products, including BASIC compilers, 8052-BASIC circuit boards, and development software for more convenient and possibly cheaper project development.
Your Feedback Is Welcome
This book is the result of requests from readers of my articles in ComputerCraft magazine and its successor, The MicroComputer Journal. I’ve expanded the coverage of several topics, including programming of EPROMs and other devices, display options, sensors, and programming tips.
Thanks to everyone who responded to my articles with comments, questions, criticisms, and suggestions, and who, in doing so, helped to make this book as useful as it can be. As always, I welcome your comments on this work.
Jan Axelson 10-94
ix
x