Advanced PIC Microcontroller Projects in C
From USB to RTOS with the PIC18F Series
A microcontroller is a microprocessor system which contains data and program memory, serial and parallel I/O, timers, and external and internal interrupts — all integrated into a single chip that can be purchased for as little as two dollars. About 40 percent of all microcontroller applications are found in office equipment, such as PCs, laser printers, fax machines, and intelligent telephones. About one third of all microcontrollers are found in consumer electronic goods. Products like CD players, hi-fi equipment, video games, washing machines, and cookers fall into this category. The communications market, the automotive market, and the military share the rest of the applications.
This book is written for advanced students, for practicing engineers, and for hobbyists who want to learn more about the programming and applications of PIC18F-series microcontrollers. The book assumes the reader has taken a course on digital logic design and been exposed to writing programs using at least one high-level programming language. Knowledge of the C programming language will be useful, and familiarity with at least one member of the PIC16F series of microcontrollers will be an advantage. Knowledge of assembly language programming is not required since all the projects in the book are based on the C language.
Chapter 1 presents the basic features of microcontrollers, discusses the important topic of numbering systems, and describes how to convert between number bases.
Chapter 2 reviews the PIC18F series of microcontrollers and describes various features of these microcontrollers in detail.
Chapter 3 provides a short tutorial on the C language and then examines the features of the mikroC compiler.
Chapter 4 covers advanced features of the mikroC language. Topics such as built-in functions and libraries are discussed in this chapter with examples.
Chapter 5 explores the various software and hardware development tools for the PIC18F series of microcontrollers. Various commercially available development kits as well as development tools such as simulators, emulators, and in-circuit debuggers are described with examples.
Chapter 6 provides some simple projects using the PIC18F series of microcontrollers and the mikroC compiler. All the projects are based on the PIC18F452 microcontroller, and all of them have been tested. This chapter should be useful for those who are new to PIC microcontrollers as well as for those who want to extend their knowledge of programming PIC18F microcontrollers using the mikroC language.
Chapter 7 covers the use of SD memory cards in PIC18F microcontroller projects. The theory of these cards is given with real working examples.
Chapter 8 reviews the popular USB bus, discussing the basic theory of this bus system with real working projects that illustrate how to design PIC18F-based projects communicating with a PC over the USB bus.
The CAN bus is currently used in many automotive applications. Chapter 9 presents a brief theory of this bus and also discusses the design of PIC18F microcontroller-based projects with CAN bus interface.
Chapter 10 is about real-time operating systems (RTOS) and multi-tasking. The basic theory of RTOS systems is described and simple multi-tasking applications are given.
The CD-ROM that accompanies this book contains all the program source files and HEX files for the projects described in the book. In addition, a 2K size limited version of the mikroC compiler is included on the CD-ROM.
The following material is reproduced in this book with the kind permission of the respective copyright holders and may not be reprinted, or reproduced in any other way, without their prior consent.
Figures 2.1–2.10, 2.22–2.36, 2.37, 2.38, 2.41–2.55, 5.2–5.4, 5.17, 5.20, 8.8, and 9.13, and Table 2.2 are taken from Microchip Technology Inc. data sheets PIC18FXX2 (DS39564C) and PIC18F2455/2550/4455/4550 (DS39632D).
Figure 5.5 is taken from the web site of BAJI Labs.
Figures 5.6–5.8 are taken from the web site of Shuan Shizu Ent. Co., Ltd. Figures 5.9, 5.13, 5.18 are taken from the web site of Custom Computer Services Inc. Figures 5.10, 5.19, and 6.43 are taken from the web site of mikroElektronika Ltd. Figure 5.11 is taken from the web site of Futurlec.
Figure 5.21 is taken from the web site of Smart Communications Ltd. Figure 5.22 is taken from the web site of RF Solutions.
Figure 5.23 is taken from the web site of Phyton.
Figures 5.1 and 5.14 are taken from the web site of microEngineering Labs Inc. Figure 5.16 is taken from the web site of Kanda Systems.
Thanks is due to mikroElektronika Ltd. for their technical support and for permission to include a limited size mikroC compiler on the CD-ROM that accompanies this book. PIC®, PICSTART®, and MPLAB® are all registered trademarks of Microchip Technology.
The term microcomputer is used to describe a system that includes at minimum a microprocessor, program memory, data memory, and an input-output (I/O) device. Some microcomputer systems include additional components such as timers, counters, and analog-to-digital converters. Thus, a microcomputer system can be anything from a large computer having hard disks, floppy disks, and printers to a single-chip embedded controller.
In this book we are going to consider only the type of microcomputers that consist of a single silicon chip. Such microcomputer systems are also called microcontrollers, and they are used in many household goods such as microwave ovens, TV remote control units, cookers, hi-fi equipment, CD players, personal computers, and refrigerators. Many different microcontrollers are available on the market. In this book we shall be looking at programming and system design for the PIC (programmable interface controller) series of microcontrollers manufactured by Microchip Technology Inc.
1.2 Microcontroller Systems
A microcontroller is a single-chip computer. Micro suggests that the device is small, and controller suggests that it is used in control applications. Another term for microcontroller is embedded controller, since most of the microcontrollers are built into (or embedded in) the devices they control.
A microprocessor differs from a microcontroller in a number of ways. The main distinction is that a microprocessor requires several other components for its operation, such as program memory and data memory, input-output devices, and an external clock circuit. A microcontroller, on the other hand, has all the support chips incorporated inside its single chip. All microcontrollers operate on a set of instructions (or the user program) stored in their memory. A microcontroller fetches the instructions from its program memory one by one, decodes these instructions, and then carries out the required operations.
Microcontrollers have traditionally been programmed using the assembly language of the target device. Although the assembly language is fast, it has several disadvantages. An assembly program consists of mnemonics, which makes learning and maintaining a program written using the assembly language difficult. Also, microcontrollers manufactured by different firms have different assembly languages, so the user must learn a new language with every new microcontroller he or she uses.