How this book came to be written

From the first days of the 8008, I have planned and carried out dedicated microprocessor projects, and for 25 years I have been teaching in that area. Some 15 years ago, I began a senior-level Electrical Engineering Technology course at Purdue covering real-time multitasking. I began it with RMX 86, and later shifted to DCX and BITBUS. For the first 13 years of the 8051’s existence, I kept waiting for a book to come out. Finally, I took class material and assembled the first edition of this book, published by Prentice Hall in 1992. That book provided most examples in three languages—PL/M, C and assembly—and gave extensive coverage to real-time operating systems for the 8051. A 1998/9 revision of the first edition led to a two-volume second edition, also published by Prentice Hall, that expanded on input and output hardware details as well as algorithms that were useful for common applications.

When Prentice Hall chose to stop printing those books, I began revising that material to produce this mostly-new third edition. In addition to re-worked material from the earlier editions I have added material from a never-published third volume on networking. In searching for a publisher, I discovered a whole new world of publish-on-demand options. I have enjoyed formatting and laying out the text and figures using word processing software (Adobe Framemaker in this case with Visio for the drawings). My wife, Jill, acts as my copy editor and chief non-technical critic. By choosing self-publishing on demand, I can afford to make this book available to you at a much lower cost. Best of all, the book will not go out of print at the point where sales numbers drop too low, corrections and revisions can be added at any time without leaving a warehouse full of obsolete books, and the material is mine if I want to develop future companion books.

While revising and updating the previous material, I was amazed to discover how much the 8051 field has changed while my attention was directed elsewhere. The software development process has been greatly streamlined, the speed of both timers and instruction execution has increased, and most of the devices I previously attached to the basic processor as separate circuits are now integrated on the chip. In particular, I am impressed with Silicon Labs’s (formerly Cygnal’s) line of 8051 relatives. Because of its wide range of internal "devices" and because through Silicon Labs you can now purchase a complete development system for a low price for, I chose to feature their development boards in all the examples. Realize, however, that this book is useful for any 8051 member.

Highlights of this book

Embedded Controller Applications

This book is a good starting place if you are developing embedded microcontroller applications of any sort. In it you can

  1. learn how to program for embedded applications,

  2. learn the hardware of the 8051 family of microcontrollers,

  3. develop the mindset of multitasking, and

  4. develop an appreciation for efficiency.

Program examples illustrate the use of the common internal peripherals of the 8051 family of microcontrollers. While the examples are set to run on a specific microcontroller—the "target" board— most will work with any 8051 processor.

I focus on embedded applications; the attached hardware is likely to be switches and solenoids rather than disk drives and keyboards. The examples encompass multitasking (based on a real-time interrupt) where a microcontroller can handle almost any number of inputs and outputs at the "same" time.

While small microcontrollers run more slowly, access much less memory, and seldom have built in math capability, they continue to be much less expensive than the faster, bigger processors. Instead of costing hundreds of dollars (newest Pentium-class processor without its support electronics), some embedded microcontroller chips cost under a dollar in large quantities. High-performance devices have their place, but I focus on ideas and techniques that set the embedded controller field apart from large-computer systems.

Programming

The C Language

While I cover the assembly language instructions in the architecture chapter, the focus in the third edition is on the C language. High-level languages such as C have increased the efficiency of writing large programs and have helped reduce human error, but program development has also been helped by new software tools. Even so, C remains a relatively detail-oriented language compared to many of the other languages now used in the PC field. The specific understanding and control possible with C nearly matches that of assembly and allows very efficient solutions to the problems of embedded applications.

Multitasking

Few textbooks talk at any length about applying multitasking to control projects. Most introductory programming books see multitasking as an advanced topic and never cover it in any detail. Sadly, PC operating systems obscure their event-driven features, so real-time interaction with hardware never gets the attention that I give it in this book. To illustrate how simply interrupt-based programming can be formalized, I include the planning and development of two real-time operating systems. Besides providing the code, those chapters discuss the design issues that were involved in these operating systems and introduce several commercial systems.

Networking

Both simple node-to-node communication and multi-drop networks are discussed. While high-powered networking protocols are quite complicated, the concepts can be seen in simple, microcontroller-based protocols. SPI, I2C (SMBus) and CAN bus protocols are demonstrated, and USB is described. Most of Silicon Labs’s processors support the first two protocols, and they also have family members that support CAN and USB.

Algorithms

In several chapters I develop algorithms for problems that commonly arise in embedded projects. The conversion of a number to printable form and back to numbers has always plagued my students. Math-oriented engineers can benefit from using tables and interpolation to replace complex math libraries and improve in efficiency and speed.

Both Silicon Labs and Keil have web sites with extensive application notes about the 8051 family. If my tentative plans work out, I will have a web site with the source code for most program examples in this book.

Development Software

The compiler, assembler, linker and integrated development environment (IDE) are a part of any of the 8051-family development kits available from Silicon Labs. The compiler and assembler are licensed from Keil and are among the best in the field. To encourage commercial users to purchase the more extensive "professional" package from Keil, the included linker is limited to 4KB of final program code, which is plenty of space to try out almost anything and enough for most student projects. The full Keil tools allow unlimited final program size and the addition of a librarian feature, and can include versions of their real-time operating system. Their full development software packages (and most competing 8051 development products) sell for $1000US or more. There are less expensive full-size compilers from other sources that work with Silicon Labs’ IDE, but they lack features or produce less efficient code when adapted for the (rather unusual) architecture of the 8051.

Hardware

Target Hardware

All of the examples in the book are tailored to one of the low-cost Silicon Labs development kits— the C8051F020 kit (sold for $129US in 2004) that includes both hardware and all necessary software. The target board has a JTAG interface, which avoids tying up processor resources for the running and debugging of programs under development. It is a true emulator, not just a simulator, so you can watch the program run on actual target hardware with actual timing.

If you do not buy one of the Silicon Labs kits, you may choose instead to buy other, more traditional development boards with built-in monitor programs. While the development process will differ slightly from that described in the book, the software examples should run the same on any 8051-family processor.

Chip Changes

The more I study the newer 8051 family members (the Silicon Labs devices in particular), the more convinced I become that the 8-bit microcontroller is far from dead. As I have long expected, the use of assembly language is declining, and the size of programs is increasing. But I had not anticipated how the basic 8051 has been enhanced with a host of on-chip peripherals and a faster, leaner-running processor. The basic execution time of an instruction has dropped by about a factor of 12, independent of the faster clock speed. With a 25MHz clock, the Silicon Labs processor’s speed has gone up by a factor of 25 compared to the first 8051!

Seldom now do you need to add peripheral devices, ports, or memory. Some of the Silicon Labs processors have 8 parallel ports, 2 UARTS, an SPI interface, an I2C interface, two A-Ds with multiplexed inputs, multiple D-As, analog comparators, and a large number of timers and counter arrays. Many of the family have 32K of flash memory and at least 4K of data memory.

The target boards come with a JTAG interface which keeps all of the processor’s resources available; you do not lose serial ports and some of the code space to fit a monitor. And you do not have to run a simulator. You have a full emulator, so you can go to break points on the actual hardware rather than trying to simulate things like interrupts and UARTs.

Peripheral Hardware Ideas

Several chapters discuss more complex I/O devices that are commonly attached to microcontrollers—keypads, displays, motors, speech devices, and other special–purpose systems. The sections give circuit design ideas and advice as well as sample programs.