Labcenter Electronics
www
.labcenter.co.uk
Manufacturer and supplier of Proteus VSM electronic design system
Microchip Technology Inc.
www
.microchip.com
Manufacturer of the PIC microcontroller range and MPLAB IDE
Custom Computer Services, Inc.
www
.ccsinfo.com
Manufacturer and supplier of PIC CCS ‘C’ Compilers
Data References and Trademark Acknowledgements
Microchip Technology Inc., RS Components, Fairchild, Intel,
Freescale (Motorola), National Semiconductor, Sensor Technics,
Densitron, Honeywell, SGS Thomson, Maxim, ST Microelectronics,
HBM, ARM, AVR Atmel, Texas, Vishay.
I would also like to thank the dedicated teachers of engineering that I have
worked with, especially Melvyn Ball at Hastings College and Chris Garrett at the
University of Brighton, and, of course, Julia Bates.
Martin Bates
Hastings, UK
Links, References and Acknowledgements
x
Else_IPM-BATES_fm.qxd 7/20/2006 11:52 AM Page x
Contents
Part 1 Microcontroller 1
1 PIC Hardware 3
Processor System 4
PIC 16F877 Architecture 8
PIC Instruction Set 18
Pulse Output 128
Period Measurement 130
7 Analogue Interfacing 141
8-bit Conversion 141
10-bit Conversion 145
Amplifier Interfaces 149
Else_IPM-BATES_fm.qxd 7/20/2006 11:52 AM Page xii
Contents
xiii
Transient & Frequency Response 160
Instrumentation Amplifier 161
Current Loop 163
Comparators 165
Op-amp Selection 168
Analogue Output 168
Part 3 Systems 177
8 Power Outputs 179
Current Drivers 179
Relays & Motors 183
Power Output Interfacing 185
Motor Interfacing 189
9 Serial Communication 201
USART 201
SPI 205
I
2
C 210
10 Sensor Interfacing 223
Sensors 223
Sensor Types 228
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 pop-
ular 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,
Else_IPM-BATES_ch001.qxd 6/27/2006 10:04 PM Page 3
3
and a development system, for writing and testing programs, can be down-
loaded free from the Microchip website (www.microchip.com).
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 connec-
tions 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 partic-
ular application, with the type of CPU, size of memory and selection of
input/output (I/O) devices tailored to the system requirements.
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).
PIC Hardware
5
Program Memory
Address Instruction
0000 10010011
0001 01010001
0002 10000100
0003 00011001
0004 01011100
0005 xxxxxxxx
0006 xxxxxxxx
etc etc
CPU
Instruction Register
Decoder Logic
Execution Logic
Control lines to system
Data bus
Address bus
Program
Counter
Figure 1.2 Processor program execution