PIC MICROCONTROLLERS
Contents
1 PIC HARDWARE
1.1 Processor System
1.2 PIC 16F877 Architecture
1.3 PIC Instruction Set
1.4 Special Function Registers
2 PIC SOFTWARE
2.1 Assembly Language
2.2 Software Design
2.3 ‘C’ Programming
3 CIRCUIT SIMULATION
3.1 Basic Circuit
3.2 Software Debugging
3.3 Hardware Testing
3.4 Hardware Implementation
4 INPUT & OUTPUT
4.1 Switch Input
4.2 Switch Debouncing
4.3 Timer and Interrupts
4.4 Keypad Input
4.5 7-Segment LED Display
4.6 Liquid Crystal Display
5 DATA PROCESSING
5.1 Number Systems
5.2 Conversion
5.3 Variable Types
5.4 Arithmetic
6 CALCULATE, COMPARE & CAPTURE
6.1 Calculator
6.2 Pulse Output
6.3 Period Measurement
7 ANALOGUE INTERFACING
7.1 8-bit Conversion
7.2 10-bit Conversion
7.3 Amplifier Interfaces
7.4 Transient & Frequency Response
7.5 Instrumentation Amplifier
7.7 Comparators
7.8 Op-amp Selection
7.9 Analogue Output
8 POWER OUTPUTS
8.1 Current Drivers
8.2 Relays & Motors
8.3 Power Output Interfacing
8.4 Motor Interfacing
9 SERIAL COMMUNICATION
9.1 USART
9.2 SPI
9.3 I2C
10 SENSOR INTERFACING
10.1 Sensors
10.2 Sensor types
10.3 Amplifier Design
10.4 Weather Station
11 SYSTEM DESIGN
11.1 Base system
11.2 Memory System
11.3 Other PIC Chips
11.4 System Design
11.5 Other MCU families
I
have my students to thank for this book – the good ones and the bad ones! The
good ones have always helped, through our project work, to develop new ideas
and solutions. Some have displayed outstanding engineering talent, and others
have been so eager to learn as to make teaching a pleasure. But even those who
struggle have had a positive impact because there is rarely enough time give
individuals the help they need. So, one is forced to write it down, so that
they would have no more excuses if they get it wrong!
Another
spur to writing this book has been the development of the interactive design
software which has made the job of learning and teaching electronics that much
more enjoyable. The Proteus software used in this book has been developed by a
talented team at Labcenter Electronics in the UK, led by John Jameson and Iain
Cliffe. They have a world beating product, and I wanted to make a small
contribution to encouraging students and engineers to use it. It allows us to
bring electronic circuits to life on the computer screen instantly.
It
has always been a problem in electronics that you cannot see a circuit working
in the same way that a mechanical engineer can see a steam engine pumping up
and down. Sure, we can see the screen flickering on a television, or an
electric motor spinning, but you can’t see electrons or volts. As a result, it
has always been that much more difficult to teach electronics. Proteus is a big
step towards bringing electronics alive, as a such, helps us to participate
more effectively in the communications and information revolution which
continues to have a major impact on all our lives.
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
March
2006
This
book is a sequel to my first effort ‘PIC Microcontrollers, an Introduction to
Microelectronics’. This attempted to provide a comprehensive introduction to
the subject via single type of microcontroller, which is essentially a complete
computer on a chip. The PIC was the first widely available device to use flash
memory, which makes them ideal for experimental work. Flash memory allows the
program to be replaced quickly and easily with a new version. It is now
commonplace, not least in our USB memory sticks, but also in a wide range of
electronic systems where user data needs to be retained during power down.
Cheap flash memory microcontrollers have transformed the teaching of
microelectronics – they are re-usable and the internal architecture is fixed,
making them easier to explain. On the other hand, beginners can ignore the
innards and treat them as a black box, and get on with the programming! The
small instruction set of the PIC is also a major advantage – only 35
instructions to learn. Compare that with a complex processor such as the
Pentium, which is quite terrifying compared with the PIC! The quality of the
PIC technical documentation is also a major factor.
For
these reasons, I set out to introduce the PIC into my teaching as widely as
possible. At the same time, schools, universities and hobbyists were starting
to using it , so continuity of learning was possible. Since there is never
enough time to teach all the detail, I decided to set out a full description of
the basic PIC device, the 16F84, and some representative applications. Although
this particular chip is now redundant in terms of new products, the basic architecture
is unchanged in current chips, so it is still a useful starting point.
My
students and I soon graduated to the more powerful PIC 16F877. This is now used
widely as a more advanced teaching device, because it has a full complement of
interfaces: analogue input, serial ports, ICD, slave port and so on, plus a
good range of hardware timers. A full description of this chip covers most of
the features that higher level students need for project work with
microcontrollers.
When
interactive simulation of microcontrollers became available, a new dimension
was added. We could now see them in action without having to spend a lot of
time building and debugging hardware! These design tools allow even the
inexperienced designer to create a working system relatively quickly. As a
result, my next step was to the document the 16F877 and its applications,
through the medium of interactive simulation.
Proteus©
from Labcenter Electronics © consists of two main parts, ISIS and ARES. ARES is
a layout package, which is used to create a PCB when the circuit has been
designed. ISIS is the schematic capture and interactive simulation software
used to create the circuit drawing and to test the circuit prior to building
the real hardware. SPICE is a mathematical circuit modelling system which has
been developed over many years – these models can now be used to bring the
drawing to life. On screen buttons and virtual signal sources, for example,
provide inputs to the circuit Output can be displayed on a voltage probe or on
a virtual oscilloscope. Now that we have microcontroller simulation as well, we
really are in business. The MCU can be dropped on the screen, a program
attached and debugged instantly. Electronic design has never been so easy!
It
is assumed that reader is familiar with the basics of microcontroller systems,
as covered in the first book. This one follows on, and is divided into three
main parts. In the first, the 16F877 hardware and programming, and the
simulation system are introduced. In the second part, a range of interfacing
techniques are covered; switches, keypads, displays, digital and analogue
interfacing, data conversion and so on. In the third, power outputs, serial
interfaces, sensors, and system design examples culminate in a design for a
general purpose board which provides a platform for further developments.
Each
topic is illustrated by designs based on the 16F877, so that the reader can
concentrate on the interfacing, a not have to deal with different
microcontrollers. All the circuits are available on the associated website (see
links below). All schematics were produced using ISIS – and you can produce
them to the same standard in your own reports. The designs can be downloaded
and run along side the book. ISIS Lite, the introductory design package, can be
downloaded free, with extra features available for a small registration fee.
The 16F877 will simulate fully, and the software changed, but the hardware
cannot be modified unless a licence is purchase for this device. The
microcontroller models can be purchased for institution or professional use in
packages – see the Labcenter website.
Get
PICing!
LINKS,
REFERENCES AND ACKNOWLEDGEMENTS
Manufacturer
and supplier of Proteus VSM electronic design system
Microchip
Technology Inc.
Manufacturer
of the PIC microcontroller range and MPLAB IDE
Custom
Computer Services Inc.
Manufacturer
and supplier of PIC CCS ‘C’ Compilers