`
`The
`Microcontroller Idea Book
`
`Circuits, Programs, & Applications
`
`featuring the 8052-BASIC Microcontroller
`
`Jan Axelson
`"
`
`Lakeview Research
`2209 Winnebago St.
`Madison, WI 53704
`
`COMPASS EXH. 1007 - Page 2 of 20
`
`
`
`
`
`
`
`Chapter 11 Control Circuits
`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
`Infrared Links 199
`Increasing the Distance 212
`Radio Links 216
`
`Chapter 13 Calling Assembly-language Routines
`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
`Reasons 239
`Copying BASIC-52 240
`System Requirements 241
`Storing BASIC-52 Programs 243
`
`Chapter 15 Related Products
`Enhanced BASIC-52 245
`BASIC Compilers 246
`Programming Environments 247
`Pc Boards 248
`BASIC-52 Source Code 250
`
`Appendix A
`
`SOll11I"CeS
`Books 251
`BBS's 254
`Product Vendors 255
`
`185
`
`199
`
`217
`
`239
`
`245
`
`251
`
`v
`
`COMPASS EXH. 1007 - Page 5 of 20
`
`
`
`
`
`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 S052-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
`
`COMPASS EXH. 1007 - Page 7 of 20
`
`
`
`
`
`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(cid:173)
`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 ComputerCrajt magazine
`and its successor, The MicroComputer Journal. l' 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
`
`COMPASS EXH. 1007 - Page 9 of 20
`
`
`
`
`
`Microcontroller Basics
`
`1
`
`Microcontroller Basics
`
`This chapter introduces you to the world of microcontrollers, including definitions, some
`history, and a summary of what's involved in designing and building a microcontroller
`project.
`
`What's a Microcontroller?
`
`A microcontroller is a computer-on-a-chip, or, if you prefer, a single-chip computer. Micro
`suggests that the device is small, and controller tells you that the device might be used to
`control objects, processes, or events. Another term to describe a microcontroileris embedded
`controller; because the microcontroller and its support circuits are often built into, or
`embedded in, the devices they contro!'
`
`You can find microcontrollers in all kinds of things these days. Any device that measures,
`stores, controls, calculates, or displays information is a candidate for putting a microcon(cid:173)
`troller inside. The largest single use for microcontrollers is in automobiles-just about every
`car manufactured today includes at least one microcontroller for engine control, and often
`more to control additional systems in the car. In desktop computers, you can find microcon(cid:173)
`trollers inside keyboards, modems, printers, and other peripherals. In test equipment,
`microcontrollers make it easy to add features such as the ability to store measurements, to
`create and store user routines, and to display messages and waveforms. Consumer products
`that use microcontrollers include cameras, video recorders, compact-disk players, and
`ovens. And these are just a few examples.
`
`The Microcontroller Idea Book
`
`1
`
`COMPASS EXH. 1007 - Page 11 of 20
`
`
`
`
`
`Microcontroller Basics
`
`But along with cheap, powerful, and versatile personal computers has developed a new
`interest in small, customized computers for specific uses. Each of these small computers is
`dedicated to one task, or a set of closely related tasks. Adding computer power to a device
`can enable it to do more, or do it faster, better, or more cheaply. For example, automobile
`engine controllers have helped to reduce harmful exhaust emissions. And microcontrollers
`inside computer modems have made it easy to add features and abilities beyond the basic
`computer -to-phone-line interface.
`
`In addition to their use in mass-produced products like these, it's also become feasible to
`design computer power into one-of-a-kind projects, such as an environmental controller for
`a scientific study or an intelligent test fixture that ensures that a product meets its specifica(cid:173)
`tions before it's shipped to a customer.
`
`At the core of many of these specialized computers is a microcontroller. The computer's
`program is typically stored permanently in semiconductor memory such as ROM or
`EPROM. The interfaces between the microcontroller and the outside world vary with the
`application, and may include a small display, a keypad or switches, sensors, relays, motors,
`and so on.
`
`These small, special-purpose computers are sometimes called single-board computers, or
`SBCs. The term can be misleading, however, since the computer doesn't have to be on a
`single circuit board, and many types of computer systems, such as laptop and notebook
`computers, are now manufactured on a single board.
`
`New Tools
`
`To design and build a computer-controlled device, you need skills in both ciT""it design and
`software programming. The good news is that a couple of recent advances have simplified
`the tasks involved.
`
`One is the introduction of microcontrollers themselves, since they contain all of the elements
`of a computer on a single chip. Using a microcontroller can reduce the number of
`components and thus the amount of design work and wiring required for a project. The
`8052-BASIC microcontroller even includes its own programming language, called BASIC-
`52.
`
`The other development is personal computers themselves. A desktop computer can help
`tremendously by serving as a host system for writing and testing programs. As you are
`developing a project, you can use a serial link to connect the host system to a target system,
`which contains the microcontroller circuits you are testing. You can then use the personal
`computer's keyboard, video display, disk drives, and other resources for writing and testing
`programs and transferring files between the two systems.
`
`The Microcontroller Idea Book
`
`3
`
`COMPASS EXH. 1007 - Page 13 of 20
`
`
`
`
`
`Microcontroller Basics
`
`makes sense. For simpler designs, a microcontroller with perhaps a keypad, small display,
`and solid-state memory (no disk drives) can often do the job, with less expense and smaller
`sIZe.
`
`In fact, recently the two extremes have been meeting. Some 32-bit microcontrollers are as
`capable as desktop systems, and notebook-size computers are available with solid-state,
`diskless storage. Also, expansion cards, other hardware, and software are now available for
`those who want to use desktop computers for monitoring and control tasks. So there's
`something for everyone.
`
`The 8052-BASIC chip described in this book is perfect for many simpler applications,
`especially control and monitoring tasks. Because the chip is easy to use, it's a good way to
`learn about microcontrollers and computers in general. Although you can't do the most
`complex projects with it, you can do a lot, at low cost and without a lot of hassle.
`
`Designing and Building
`
`When you're ready to design and build the circuits for a project, there are several ways to
`proceed. You can design your circuits from scratch, using manufacturers' data books as
`guides; you can follow a tested design (a kit or project presented in a magazine for example);
`or you can buy an assembled single-board computer, adding only the interfaces and
`programming your application requires. This book presents designs that you can build
`yourself, but you can also use a kit or assembled board as a base if you wish.
`
`Choosing a chip. Does it matter which microcontroller chip you use? All microcontrollers
`contain a CPU, and chances are that you can use any of several devices for a specific project.
`
`Within each device family, you'll usnally find a selection of family members, each with
`different combinations of options. For example, the 8052-BASIC is a member of the 8051
`family of micro controllers, which includes chips with program memory in ROM or EPROM,
`and with varying amounts of RAM and other features. You select the version that best suits
`your system's requirements.
`
`Microcontrollers are also characterized by how many bits of data they process at once, with
`a higher number of bits generally indicating a faster or more powerful chip. Eight-bit chips
`are popular for simpler designs, but 4-bit, 16-bit, and 32-bit architectures are also available.
`The 8052-BASIC is an 8-bit chip.
`
`Power consumption is another consideration, especially for battery-powered systems. Chips
`manufactured with CMOS processes usually have lower power consumption than those
`manufactured with NMOS processes. Many CMOS devices have special standby or "sleep"
`modes that limit current consumption to as low as a few microamperes when the circuits are
`
`The Microcontroiler Idea Book
`
`5
`
`COMPASS EXH. 1007 - Page 15 of 20
`
`
`
`;1"
`ji
`I
`
`~II
`
`A
`-I , I
`~ i , ' qi
`
`, I
`, 1
`
`C >,
`" l
`
`,Ii : ,
`C
`
`1 I
`'I
`.J
`>,
`"
`
`, '
`
`l'
`
`, ,
`
`Chapter 1
`
`inactive. Using these modes, a data logger can reduce its power consumption between
`samples, and power up only when it's time to take data.
`
`The 8052-BASIC chip is available in both NMOS and CMOS versions. The original
`8052-BASIC was an NMOS chip, offered directly from InteL (Intel's tenn for its NMOS
`process is HMOS.) Although Intel never offered a CMOS version directly, Micromint
`became a source by ordering a batch of CMOS 8052's with the BASIC-52 programming
`language in ROM. The CMOS version, the 80C52-BASIC, has maximum power consump(cid:173)
`tion of 30 milliamperes, compared to 175 milliamperes for the NMOS 8052-BASIC.
`
`All microcontrollers have a defined instruction set, which consists of the binary words that
`cause the CPU to carry out specific operations. For example, the instruction 0010 all a tells
`an 8052 to add the values in two locations. The binary instructions are also known as
`operation codes, or opcodes for short. The opcodes perfonn basic functions like adding,
`subtracting, logic operations, moving and copying data, and controlling program branching.
`
`Control circuits often require reading or changing single bits of input or output, rather than
`reading and writing a byte at a time. For example, a microcontroller might use the eight bits
`of an output port to switch power to eight sockets. If each socket must operate independently
`of the others, a way is needed to change each bit without affecting the others. Many
`microcontrollers include bit-manipulation (also called Boolean) opcodes that easily allow
`programs to set, clear, compare, copy, or perfonn other logic operations on single bits of
`data, rather than a byte at a time.
`
`Options for storing programs. Another consideration in circuit design is how to store
`programs. Instead of using disk storage, most microcontroller circuits store their programs
`on-chip. For one-of-kind projects or small-volume production, EPROM has long been the
`most popular method of program storage. Besides EPROMs, other options include
`EEPROM, ROM, nonvolatile (NV), or battery-backed, RAM, and Flash EPROM. The
`program memory may be in the microcontroller chip, or a separate component.
`
`To save a program in EPROM, you must set the EPROM's data and address pins to the
`appropriate logic levels for each address and apply special programming voltages and
`control signals to store the data at the selected address. The programming process is
`sometimes called burning the EPROM. You erase the contents by exposing the chip's quartz
`window, and the circuits beneath it, to ultraviolet energy.
`
`Some microcontrollers contain a one-time-programmable, or field-programmable, EPROM.
`This type has no window, so you can't erase its contents, but because it's cheaper than a
`windowed IC, it's a good choice when a program is finished and the device is ready for
`quantity production.
`
`6
`
`The Microcontroller Idea Book
`
`COMPASS EXH. 1007 - Page 16 of 20
`
`
`
`Microcontroller Basics
`
`Several techniques are available for programming EPROMs and other memory chips. With
`a manual programmer, you flip switches to toggle each bit and program the EPROM byte
`by byte. This is acceptable for short programs, but quickly becomes tedious with a program
`of any length. Computer control simplifies the job greatly. With an EPROM programmer
`that connects to a personal computer, you can write a program at your keyboard, save it to
`disk if you wish, and store the program in EPROM in a few easy steps. Data sheets for
`EPROMs rarely specify the number of erase and reprogramming cycles a device is
`guaranteed for, but a typical EPROM should endure 100 erase/program cycles, and usually
`many more.
`
`EEPROMs are much lilce EPROMs except that they are electrically erasable-no ultraviolet
`source is required. Limitations of EEPROMs include slow speed, high cost, and a limited
`number of times that they can be reprogrammed (typically 10,000 to 100,000).
`
`ROMs are cost-effective when you need thousands of copies of a single program. ROMs
`must be factory-programmed and once programmed, can't be changed.
`
`NVRAM typically includes a lithium cell, control circuits, and RAM encapsulated in a.
`single IC package. When power is removed from the circuit, the lithium cell takes over and
`preserves the information in RAM, for 10 years or more. You can reprogram an NVRAM
`n infinite number of times, with the only limitation being battery life.
`
`Flash EPROM is electrically erasable, like EEPROM, but most Flash devices erase all at
`once, or in a few large blocks, rather than byte-by-byte like EEPROM. Some Flash EPROMs
`require special programming voltages. As with EPROMs, the number of erase/program
`cycles is limited.
`
`The 8052-BASIC uses two types of program memory. An 8-kilobyte, or 8K, on-chip ROM
`stores the BASIC-52 interpreter. For storing the BASIC-52 programs that you write, the
`BASIC-52 language has programming commands that enable you to save programs in
`external EPROM, EEPROM, or NVRAM.
`
`Other memory. Most systems also require a way to store data for temporary use. Usually,
`this is RAM, whose contents you can change as often as you wish. Unlike EPROM, ROM,
`EEPROM, and NVRAM, the contents of the RAM disappear when you remove power the
`chip (unless it has battery back-up).
`
`Mostmicrocontrollers include some RAM, typically a few hundred bytes. The 8052-BASIC
`has 256 bytes of internal RAM. Acomplete 8052-BASIC system requires atleast 1024 bytes
`of external RAM as welL
`
`]fO options. Finally, input/output (I/O) requires design decisions. Most systems require
`interfaces to things like sensors, keypads, switches, relays, and displays. Most microcon-
`
`The Microcontroller Idea Book
`
`7
`
`COMPASS EXH. 1007 - Page 17 of 20
`
`
`
`
`
`,
`Interpreters and compilers are two fonus of higher-level languages. An interpreter trans(cid:173)
`lates a program into machine code each time the program runs, while a compiler translates
`only once, creating a new, executable fIle that the computer runs directly, without re-trans(cid:173)
`lating.
`
`Microcontroller Basics
`
`As a rule, interpreters are very convenient for shorter programs where execution speed isn't
`critical. With an interpreted language, you can run your program code immediately after you
`write it, without a separate compile or assembly step. A compiler is a good choice when a
`program is long or has to execute quickly. A single language like BASIC may be available
`in both interpreted and compiled versions.
`
`Each device family requires its own interpreter or compiler to translate the higher-level code
`into the machine code for that device. In other words, you can't use QuickBASIC for IBM
`PCs to program an 8052 microcontroller-you need a compiler that generates program code
`for the 8052.
`
`Compared to an equivalent program written in assembly language, a compiled program
`usually is larger and slower, so assembly language is the way to go if a program must be as .
`fast or as small as possible. A higher-level language also may not offer all of the abilities of
`assembly code, though you can get around this by calling subroutines in assembly language
`when necessary.
`
`BASIC-52 is an interpreted language, but BASIC compilers for the 8052 are also available.
`In fact, you can have the best of both worlds by testing your programs with the BASIC-52
`interpreter, and compiling the finished product for faster execution and other benefits of the
`compiled version.
`
`Testing and Debugging
`
`After you've written a program, or a section of one, it's time to test it and as necessary, find
`and correct mistakes to get it working properly. The process of ferreting out and correcting
`mistakes is called debugging. Easy debugging and troubleshooting can make a big difference
`in how long it takes to get a system up and running. As with programmiug, you have several
`options here as well.
`
`Testing in EPROM. One way is to bum your program into EPROM, install the EPROM
`in your system, run the program, and observe the results. If problems occur (as they usually
`will) you modify the program, erase and rebum the EPROM, and try again, repeating as
`many times as necessary until the system is operating properly.
`
`Development systems. Another option is to use a development system. A typical develop(cid:173)
`ment system consists of a monitor program, which is a program stored in EPROM or other
`memory in the microcontroller system, and a serial link to a personal computer. Using the
`
`The Microcontroller Idea Book
`
`9
`
`COMPASS EXH. 1007 - Page 19 of 20
`
`
`
`