Interfacing PIC Microcontrollers: Embedded Design by Interactive Simulation

Interfacing PIC Microcontrollers: Embedded Design by Interactive Simulation

by Martin P. Bates, J. F. Manzoni
•Explore in detail microcontroller interfacing techniques using the popular PIC 16F877
•Work through step-by-step examples interactively using circuit simulation software, supplied as assembly source code
•Gain the knowledge of a wide range of peripheral devices such as keyboards, displays, sensors and drives and serial communication with other


•Explore in detail microcontroller interfacing techniques using the popular PIC 16F877
•Work through step-by-step examples interactively using circuit simulation software, supplied as assembly source code
•Gain the knowledge of a wide range of peripheral devices such as keyboards, displays, sensors and drives and serial communication with other processors, memory and more
•Use interactive simulation software to design and test circuits

Interfacing PIC Microcontrollers provides a thorough introduction to interfacing techniques for students, hobbyists and engineers looking to take their knowledge of PIC application development to the next level. Each chapter ends with suggestions for further applications, based on the examples given, and numerous line drawings illustrate application of the hardware.

Step-by-step examples in assembly language are used to illustrate a comprehensive set of interfaces, and these can be run interactively on circuit simulation software, used to aid understanding without the need to build real hardware.

A companion website includes all examples in the text which can be downloaded together with a free version of Proteus’s ISIS Lite:

*Comprehensive coverage of a topic not widely explored in the wealth of PIC books on the market, concentrating on the popular PIC16F877 device
*Circuit simulation software allows step-by-step examples, supplied as assembly source code, to be run interactively - aiding student, technician and hobbyist learning.
*A companion website includes all examples in the text which can be downloaded together with a free version of Proteus's ISIS Lite:

Editorial Reviews

From the Publisher
"…He revises, updates, and expands his tutorial on using the microcontroller; it can support learning at college, at work, or at home…It is a sequel to his PIC Microcontrollers: An Introduction to Microelectronics. He explores the basic techniques for connecting the PIC to simple input and output devices such as switches, sensors, display, and motors. He also demonstrates methods that allow the PIC to communicate data with other devices, including intelligent sensors."—, February 2014

Product Details

Elsevier Science
Publication date:
Sold by:
Barnes & Noble
File size:
7 MB

Read an Excerpt

Interfacing PIC Microcontrollers

Embedded Design by Interactive Simulation
By Martin Bates


Copyright © 2006 Martin Bates
All right reserved.

ISBN: 978-0-08-046387-2

Chapter One

PIC Hardware

The microcontroller is simply a computer on a chip. It is one of the most important developments in electronics since the invention of the microprocessor itself. It is essential for the operation of devices such as mobile phones, DVD players, video cameras, and most self-contained electronic systems. The small LCD screen is a good clue to the presence of an MCU (Microcontroller Unit) – it needs a programmed device to control it. Working sometimes with other chips, but often on its own, the MCU provides the key element in the vast range of small, programmed devices which are now commonplace.

Although small, microcontrollers are complex, and we have to look carefully at the way the hardware and software (control program) work together to understand the processes at work. This book will show how to connect the popular PIC range of microcontrollers to the outside world, and put them to work. To keep things simple, we will concentrate on just one device, the PIC 16F877, which has a good range of features and allows most of the essential techniques to be explained. It has a set of serial ports built in, which are used to transfer data to and from other devices, as well as analogue inputs, which allow measurement of inputs such as temperature. All standard types of microcontrollers work in a similar way, so analysis of one will make it possible to understand all the others.

The PIC 16F877 is also a good choice for learning about micro-controllers, because the programming language is relatively simple, as compared with a microprocessor such as the Intel Pentium™, which is used in the PC. This has a powerful, but complex, instruction set to support advanced multimedia applications. The supporting documentation for the PIC MCU is well designed, and a development system, for writing and testing programs, can be downloaded free from the Microchip website (

Processor System

The microcontroller contains the same main elements as any computer system:

• Processor

• Memory

• Input/Output

In a PC, these are provided as separate chips, linked together via bus connections on a printed circuit board, but under the control of the microprocessor (CPU). A bus is a set of lines which carry data in parallel form which are shared by the peripheral devices. The system can be designed to suit a particular application, with the type of CPU, size of memory and selection of input/output (I/O) devices tailored to the system requirements.

In the microcontroller, all these elements are on one chip. This means that the MCU for a particular application must be chosen from the available range to suit the requirements. In any given circuit, the microcontroller also tends to have a single dedicated function (in contrast to the PC); this type of system is described as an embedded application (Figure 1.1).


In a microprocessor system or a microcontroller, a single processor block is in charge of all input, output, calculations and control. This cannot operate without a program, which is a list of instructions that is held in memory. The program consists of a sequence of binary codes that are fetched from memory by the CPU in sequence, and executed (Figure 1.2).

The instructions are stored in numbered memory locations, and copied to an instruction register in the CPU via the data bus. Here, the instruction controls the selection of the required operation within the control unit of the processor. The program codes are located in memory by outputting the address of the instruction on an address bus. The address is generated in the program counter, a register that starts at zero and is incremented or modified during each instruction cycle. The busses are parallel connections which transfer the address or data word in one operation. A set of control lines from the CPU are also needed to assist with this process; these control lines are set up according to the requirements of the current instruction.

Decoding the instruction is a hardware process, using a block of logic gates to set up the control lines of the processor unit, and fetching the instruction operands. The operands are data to be operated on (or information about where to find it) which follows most instructions. Typically, a calculation or logical operation is carried out on the operands, and a result stored back in memory, or an I/O action set up. Each complete instruction may be 1, 2 or more bytes long, which includes the operation (instruction) code (op-code) itself and the operand/s (1 byte = 8 bits).

Thus, a list of instructions in memory is executed in turn to carry out the required process. In a word processor, for example, keystrokes are read in via the keyboard port, stored as character codes, and sent to a screen output for display. In a game, input from the switches on the control pad are processed and used to modify the screen. In this case, speed of the system is a critical factor.


There are two types of memory: volatile and non-volatile. Volatile memory loses its data when switched off, but can be written by the CPU to store current data; this is RAM (Random Access Memory). ROM (Read Only Memory) is non-volatile, and retains its data when switched off.

In a PC, a small ROM is used to get the system started when it is switched on; it contains the BIOS (Basic Input Output System) program. However, the main Operating System (OS), for example, Windows™ and application program (e.g. Word) have to be loaded into RAM from Hard Disk Drive (HDD), which takes some time, as you may have noticed!

So why not put the OS in ROM, where it would be instantly available? Well, RAM is faster, cheaper and more compact, and the OS can be changed or upgraded if required. In addition, an OS such as Windows is very large, and some elements are only loaded into RAM as needed. In addition, numerous applications can be stored on disk, and loaded only as required.

The ideal memory is non-volatile, read and write, fast, large and cheap. Unfortunately, it does not exist! Therefore, we have a range of memory technologies as shown in Table 1.1, which provide different advantages, which may all be used with a standard PC. The main trade-off is cost, size and speed of access. Flash ROM, as used in memory sticks and MP3 players, is closest to the ideal, having the advantages of being non-volatile and rewritable. This is why it is used as program memory in microcontrollers which need to be reprogrammed, such as the PIC 16F877.

Input and Output

Without some means of getting information and signals in and out, a data processing or digital control system would not be very useful. Ports are based on a data register, and set of control registers, which pass the data in and out in a controlled manner, often according to a standard protocol (method of communication).

There are two main types of port: parallel and serial. In a parallel port, the data is usually transferred in and out 8 bits at a time, while in the serial port it is transmitted 1 bit at a time on a single line. Potentially, the parallel port is faster, but needs more pins; on the other hand, the port hardware and driver software are simpler, because the serial port must organise the data in groups of bits, usually 1 byte at a time, or in packets, as in a network (Figure 1.3).

Taking printers as an example, the old standard is a parallel port (Centronics), which provides data to the printer 1 byte (8 bits) at a time via a multipin connector. The new standard, USB (Universal Serial Bus) is a serial data system, sending only 1 bit at a time. Potentially, the parallel connection is 8 times faster, but USB operates at up to 480 megabits (Mb) per second, and the printer is slow anyway, so there is no problem. One advantage of using USB is that it provides a simple, robust connector and this outweighs the fact that the interface protocol (driver software) is relatively complex, because this is hidden from the user. USB also provides power to the peripheral, if required, and the printer can be daisy-chained with other devices. USB also automatically configures itself for different peripherals, such as scanners and cameras.

In the parallel port operating in output mode, the data byte is loaded from the internal data bus under the control of a read/write signal from the CPU. The data can then be seen on the output pins by the peripheral; for testing, a logic probe, logic analyser or just a simple LED indicator can be used. In input mode, data presented at the input pins from a set of switches or other data source are latched into the register when the port is read, and is then available on the data bus for collection by the CPU. One of the functions of the port is to separate the internal data bus from the external hardware, and the other is to temporarily store the data. The data can then be transferred to memory, or otherwise processed, as determined by the CPU program.

The serial port register also loads data from the internal bus in parallel, but then sends it out 1 bit at a time, operating as a shift register. If an asynchronous serial format is used, such as RS232 (COM ports on old PCs), start and stop bits are added so that bytes can be separated at the receiving end. An error check bit is also available, to allow the receiver to detect corrupt data. In receive mode, the register waits for a start bit, and then shifts in the data at the same speed as it is sent. This means the clock rate for the send and receive port must be the same. The USART (Universal Synchronous/Asynchronous Receive/Transmit) protocol will be described in more detail later.

A USB or network port is more sophisticated, and arranges the data bytes in packets of, say, 1k bytes, which are sent in a form which is self-clocking; that is, there is a transition within each bit (1 or 0), so each can be picked up individually. An error-correction code follows the data, which allows mistakes to be corrected, rather than just be detected. This reduces the need for retransmission of incorrectly received data, as required by simple error detection. Addressing information preceding the data allows multiple receivers to be used.

The PIC 16F877, in common with most current MCUs, does not have USB or network interfaces built in, so we can avoid detailed consideration of these complex protocols. It does, nevertheless, have a good selection of other interfaces, which will be discussed in detail and sample programs provided.

PIC 16F877 Architecture

Microcontrollers contain all the components required for a processor system in one chip: a CPU, memory and I/O. A complete system can therefore be built using one MCU chip and a few I/O devices such as a keypad, display and other interfacing circuits. We will now see how this is done in practice in our typical microcontroller.

PIC 16F877 Pin Out

Let us first consider the pins that are seen on the IC package, and we can then discover how they relate the internal architecture. The chip can be obtained in different packages, such as conventional 40-pin DIP (Dual In-Line Package), square surface mount or socket format. The DIP version is recommended for prototyping, and is shown in Figure 1.4.

Most of the pins are for input and output, and arranged as 5 ports: A(5), B(8), C(8), D(8) and E(3), giving a total of 32 I/O pins. These can all operate as simple digital I/O pins, but most have more than one function, and the mode of operation of each is selected by initialising various control registers within the chip. Note, in particular, that Ports A and E become ANALOGUE INPUTS by default (on power up or reset), so they have to set up for digital I/O if required.

Port B is used for downloading the program to the chip flash ROM (RB6 and RB7), and RB0 and RB4–RB7 can generate an interrupt. Port C gives access to timers and serial ports, while Port D can be used as a slave port, with Port E providing the control pins for this function. All these options will be explained in detail later.

The chip has two pairs of power pins (VDD = 5 V nominal and Vss = 0 V), and either pair can be used. The chip can actually work down to about 2 V supply, for battery and power-saving operation. A low-frequency clock circuit using only a capacitor and resistor to set the frequency can be connected to CLKIN, or a crystal oscillator circuit can be connected across CLKIN and CLKOUT. MCLR is the reset input; when cleared to 0, the MCU stops, and restarts when MCLR = 1. This input must be tied high allowing the chip to run if an external reset circuit is not connected, but it is usually a good idea to incorporate a manual reset button in all but the most trivial applications.


Excerpted from Interfacing PIC Microcontrollers by Martin Bates Copyright © 2006 by Martin Bates. Excerpted by permission of Newnes. All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.

Meet the Author

Martin Bates is one of the leading authors specializing in introductory level texts on PIC microcontrollers for the academic, professional and hobby markets, with 20 years’ experience of teaching microprocessor systems.

Customer Reviews

Average Review:

Write a Review

and post it to your social network


Most Helpful Customer Reviews

See all customer reviews >