1. Objects and Components.
2. Object-Oriented Programming in C.
Classes, Objects, and Encapsulation. Inheritance. Polymorphism and Virtual Functions. Toward Better Object Interface. Improved Interface with Inheritance and Virtual Functions. Summary.
3. Some Useful Classes.
Semaphore. Linked List. Look-Up Table. Summary.
4. Asynchronous Serial Port.
Error Handler. Development Environment. Serial Driver.
Output Compare Subsystem. Timer Class. Delay. Clock. Clock1. Clock2. Pulse-Width Modulator. Summary.
6. Additional Timer Applications.
Input Capture and Internal PWM. Input Capture Subsystem. Timer1 Test Program. Internal Pulse-Width Modulators. Summary.
7. Data Manual.
Delay. Error Handler. Link. Link List. Look-Up Table. Object. Pulse-Width Modulator 1. Pulse-Width Modulator 2. Semaphore. Serial Input/Output: Basic. Serial Input/Output: Keyboard-Specific. Serial Input/Output: Special Baud-Rate Handling. Output Compare Timer Subsystem. Input Capture Timer Subsystem.
Appendix A The C Compiler.
Appendix B M68HC16 Header Files.
MC68HC16Y1 and MC68HC16Z1 Header Files. General-Purpose Timer Header File. Multichannel Communications Interface Header File. Queued Serial Module Header File. System Integration Module Header File. Single-Chip Integration Module Header File. Static RAM Module Header File. Timer Processor Unit RAM Header File. Index.
This book introduces you to new and useful techniques of programming microcontrollers with the high-level language C. The concept of reusable components evolved from research aimed at object-oriented programming for microcontrollers in C. It was found that classes written in C can be packaged as components for easy reuse and modification needed to extend their capability.
In this book you will learn how to create classes in C. Instances of these classes are objects, and within the limits of the microcontroller and other peripherals, as many instances of these objects as are needed can be created. We call these classes components. Such software components have advantages over the corresponding hardware components. In the construction of components, we can apply the concepts of inheritance and polymorphism. Inheritance allows extension of the basic component to meet new requirements without modification of the basic class code. Polymorphism, or late binding, permits the program to select one of several alternative functions or methods. The choice of which to execute cannot be determined until run time. It will be found that careful application of polymorphism can allow a saving of program code.
One of the features of any high-level language is to implement an abstraction in which the underlying computer seemingly disappears. The total programmer interface with the computer is through the language. Often, the basic nature of the computer being programmed is of little concern to the programmer. True for the computer, but untrue for the peripherals. Most of the skill needed to program microcontrollers with a high-level language involves the efficient coding of the interface between the main computer and its program with the peripherals of the system. The code in this book is for an M68HC16 family part. This code is also usable on the M68300 series of components. The main difference will be that the initialization of the core computers is slightly different, and since most peripherals are common between the chips, the only header file change needed will be the basic core part header. The microcontroller code contained in this book, where possible, has been compiled and tested in an M68300 component.
Chapter 1 contains a very brief summary of object-oriented programming (OOP). The main purpose of the chapter is to introduce some of the language of OOP, how classes and objects are used, what to expect from inheritance and virtual functions, and the many limitations of C as OOP language.
Objects written in C are explored in Chapter 2. Here we start with the simplest concept of an object and develop it into a class. We make a class that can support multiple instantiations of itself in the form of different objects. Inheritance is examined and implemented. We then see how virtual functions can be implemented and used. Finally, several mechanisms to improve the appearance, maintainability, and size of the code written with these techniques are shown. All the code in Chapter 2 is written for a DOS-based machine. The code in the remainder of the book is written for the MC68HC16Y1 microcontroller.
The contents of Chapter 3 show the development of several useful objects for microcontroller programs. Here the classes cover some typical data structure classes that can be used in your programs. These data structures have been developed to meet a programming need for some embedded system.
In Chapter 4 we develop a class that creates instances of an asynchronous serial port. This serial port driver is interrupt-controlled and block-oriented. Its implementation is object-oriented. Instances of serial port objects can be created, and with proper care by the basic program, these instances can be run simultaneously. The microcontroller programmed here contains a dual-channel asynchronous serial port that is comprised of two distinct universal asynchronous receiver-transmitters (UARTs). Although this application might seem to be a perfect example where it is possible to merge the software drivers for these two on-board devices into a single class, it is a case where the merging of the two programs will essentially double the total code, and it will increase the complexity of the software driver considerably. It was decided that the natural integration of multiple devices into a single object-oriented interface would best be demonstrated with the on-board general-purpose timer described in Chapters 5 and 6.
Several timer functions are developed in Chapters 5 and 6. These timers are based on the timers in the general-purpose timer (GPT). The GPT contains both output compare operations and input capture functions. You will find development of components to make use of the output compare systems in Chapter 5. There are four output compare systems on board the chip. Object-oriented interfaces to these output compare systems are created in a single class. Mechanizations are provided to attach a delay object to any timer. When needed, multiple operations can be controlled by each timer through the use of a linked list of object-oriented drivers that are executed with each output compare interrupt. Input capture operations are used to measure time between events. An object-oriented interface to the several on-board input capture systems is completed, and applications to make precision time measurements are shown in Chapter 6.
Chapter 7 is a data manual. The test is on components, and most components that we encounter in hardware have an associated data sheet. Data sheets for all the components derived in this book have been collected together and placed in Chapter 7. Here you can find how to use each software component, its parameters, and the essential items needed to use the class as a super class for the derivation of another class. I suspect that this chapter will be a well-used chapter of the book.
In Appendix A you will find a very brief description of the Cosmic C compiler used to develop code for the M68HC16 series of components. Appendix B includes several important header files needed to compile code for this chip family.
A CD-ROM is included with this book for your convenience. The contents of the CD-ROM should be installed on your hard drive. A command like:
x: > install y:
where x: is the volume of your CD-ROM drive and y: is the volume of the destination drive. This install command will create three directories on your hard drive: programs, html, and Cosmic.
The directory Programs contains all the executable code contained in the book. It is organized by chapter to help you find what you need. The directory Cosmic contains a demonstration C compiler from Cosmic software. This fine product will compile all the code contained in this book. The directory HTML contains a complete copy of the book. You are about to use one of the fine developments that has come from the World Wide Web. The language used to generate Web pages is called html for Hypertext Markup Language. This language allows you to create data pages that are formatted by the language rather than by the author. Look into any of the *.htm files in this directory to see what HTML files look like.
To access an HTML file you must use a Web Browser such as Netscape or any of the other popular programs. Once you get the program running, you can bring up the text with a command like:
where is the path of your html files. From that point on, it is merely point and click to move through the book.
I would like to take this opportunity to extend my thanks to the Semiconductor Products Sector of Motorola, Inc. for their support in the production of this book. Neil Krohn, my manager, is always there with encouragement to try new business ideas. This, my second book, has been written with his support and blessing. I could not have completed it without his help. My associates Lou Pagnuccon and A. J. Pohlmeyer have provided me with many interesting discussions that have shaped the contents of this book. Finally, I would also like to thank Brian Wilkie, Corporate Vice President and General Manager of the Advanced Microcontroller Division.
Ted Van Sickle