This book is a collection of projects based around various microcontrollers from the PIC family. The reader is carefully guided through the book, from very simple to more complex projects in order to gradually build their knowledge about PIC microcontrollers and digital electronics in general. On completion of this book, the reader should be able to design and build their own projects and solve other practical problems in digital electronics.
Many books in this area are theory based and can tend toward being overly explanatory in their approach to the subject. Courses are moving towards being more practically oriented and this book provides the ideal companion to students completing projects with PIC microcontrollers.
"synopsis" may belong to another edition of this title.
Hassan Parchizadeh is a principal lecturer at the University of Portsmouth. He has been lecturing for over 20 years and currently teaches a number of subjects including microcontrollers, power electronics and signal processing. He as also published two books on microcontrollers.
Dr. Branislav Vukanovic is a senior lecturer at the University of Portsmouth, previously he was a lecturer and the University of Derby and a research fellow at the University of Sheffield. He teaches in the areas of digital electronics, microprocessors and digital signal processing. He has published a significant number of papers in his fields.
1.1 Introduction 1.2 Overview of PIC Microcontroller 1.3 Basics of PIC Assembly Language 1.4 Introduction to C Programming for PIC Microcontroller 1.5 MPLAB Integrated Development Environment (IDE) 1.6 Advanced Debugger Features - Stimulus
1.1 Introduction
The aim of this chapter is to consider a number of issues that need to be taken into account before doing almost any microcontroller-based project. First, the reader will be introduced to a PIC (programmable interface controller) microcontroller by a brief discussion of one of the models from the PIC microcontroller family - the PIC16F627A. This model is now a common choice for low-cost PIC projects and has practically replaced the very popular PIC16F84 model. The PIC16F627A is therefore a choice for a large number of projects from this book although some other simpler and more complex models are also being used. The rest of the PIC family will be considered briefly, introducing some other models used for the projects in this book. We will then discuss the basics of two programming languages commonly used to develop PIC programs in practice and throughout this book - assembly and C. This will by no means be a detailed discussion of those two languages; a separate book would be needed for that. The aim instead is to provide a short overview of the basic features of both languages and to enable readers to learn the rest of it while doing projects from the other chapters of this book. Material covered in this chapter should therefore be sufficient to allow the reader to start with the first programs and projects from Chapter 2 and gradually build knowledge to domore complex projects fromthe rest of the book. Finally, we will demonstrate how to develop and test a simple PIC program using the MPLAB(r) -Integrated Development Environment (IDE).
1.2 Overview of PIC Microcontroller
The name PIC denotes several families of microcontrollers manufactured by Microchip Technology. This range is huge and very versatile so discussing even a small number of microcontrollers would be a difficult and time-consuming task. Instead, in this section, we will concentrate on the basic features and layout of one of the most popular members of the mid-range PIC16 family: PIC16F627A. Once the basic features of this device are explained it will be easier to introduce and understand the operations of more complex PICs used in later chapters of this book.
1.2.1 PIC16F627 Building Blocks
Every computer system, however complicated or simple, consists of a number of common building blocks. Those are: the CPU (central processing unit or microprocessor) block, the memory block (RAM and ROM) and the input/output (I/O) block (interface circuitry). The CPU performs all the logic and arithmetic functions; memory is used to store programs and data while the interface provides means of communication and data exchange between the microcomputer system and the external world.
A microcontroller is a stripped-down version of the computer system architecture with one important difference - all of the system blocks are placed on one chip. The microcontroller-based system therefore requires very little additional circuitry for its proper operation. All that is needed in most cases is a clock input to provide timing for the system operation.
The PIC16F27A microcontroller contains all of the previously mentioned blocks. Components of this microcontroller, described in slightly more detail, are:
The CPU. The 'brain' of a microcontroller. It is responsible for finding and fetching the right instruction to be executed, for decoding that instruction, and finally for its execution.
Memory. Split into two physically separate blocks - program and data memory. This so-called Harvard architecture is used to speed up the operation of the microcontroller as both data and instructions can be fetched from separate memories using separate buses simultaneously.
Program memory. Used to store a program to be executed in the central processing unit of the microcontroller. It is of flash type so the microcontroller can be programmed many times before a system developer is happy with its performance and the programmed PIC is finally installed into some bigger system. If the power to the microcontroller is switched off, the content of the flash-type memory is not lost. The size of the program memory on the PIC16F27A is 1024 words (1 kwords), where one word holds 14 bits.
Data memory. Used to store microcontroller data. It is further divided into EEPROM and RAM memory: EEPROM memory holds important data that need to be saved when there is no power supply to the microcontroller; RAM is used by a program to store inter-results or temporary data during the program execution. EEPROM contains 128 bytes of data whereas RAM holds 224 bytes (1 byte contains 8 bits so the widths of program and data memories are different).
PORTA and PORTB. Physical connections between the microcontroller and the outside world. Both of those ports have eight pins and those pins are bidirectional - they can be used for input or output of data provided they are properly configured as input or output pins in the program. The exception is pin 5 of port A (RA5), which is an input-only type pin. Two special function registers within PIC, TRISA and TRISB, control the direction of the port pins. Writing '1' in the particular bit of the TRISA register configures the corresponding pin of port A as an input pin; '0' in TRISA makes it an output pin. The same is true for the port B pins and TRISB register. Some of these port pins are multiple-purpose pins and can be used for other peripheral functions of the processor. This will be explained in more detail in Section 1.2.3 where the layout (pin out) of the PIC16F27A chip is discussed.
The PIC blocks mentioned above communicate through a complex system of communication lines called buses. The data buses are used for the transfer of data through the system and address buses communicate addresses of data and program instructions to be accessed during program execution. Various other communication lines exist in the PIC and those are usually referred to as control bus lines. Note that since two separate memories exist in the PIC, both data and address bus systems are doubled, i.e. PIC16F27A (Figure 1.1) has a data memory (DM) address bus as well as program memory (PM) address bus. Similarly, this processor also has a DM data bus and PM data bus.
1.2.2 EEPROM and RAM Memories on PIC16F27A
The EEPROM memory on the PIC16F27A holds 128 bytes of non-volatile information. This memory is electronically programmable so it is not a fast RAM-type memory and it can be awkward to access it within the program. It is normally used to store data that is not frequently changed.
The RAM memory on the PIC16F27A is actually split into four memory banks where each bank holds access to 80 memory locations. This, however, does not mean that the total capacity of the RAM memory on the PIC16F27A is 320 bytes. It is more complicated than that. Here is the explanation.
This memory can be considered to consist of two different types of registers - special function registers (SFRs) and general purpose registers (GPRs). The first 32 bytes of each memory bank (00h-1Fh) belong to SFRs. Those registers are used by the CPU to control the desired operation of the device and to record the operating states of the PIC, the I/O port conditions and the other conditions. Not all of those registers are implemented on the PIC16F27A model. There are 21 SFR bytes in bank 0, 18 in bank 1 and seven SFRs in banks 2 and 3. Some of these registers will be used and described in more detail in the remaining chapters of this book.
The GPRs are placed in the first three memory banks - 80 bytes of GPR in banks 0 and 1 and 48 in bank 2. Those registers can be used to store results and conditions temporarily while the program is running. The content of the GPR is lost when the power is switched off. The rest of the available memory space is not used - if it gets accessed, it reads as 0. This memory arrangement is shown schematically in Figure 1.2.
1.2.3 PIC16F27A Pins
We will complete this section with a short description of all 18 pins of the PIC16F27A microcontroller shown in Figure 1.3.
RA0 to RA7 are the eight pins of port A. Port A is a bidirectional port, which means it can be configured as an input or an output. The number following RA is the bit number (0 to 7). So, we have one 8-bit directional port where each bit (with the exception of bit 5) can be configured as input or output. As shown in Figure 1.3, all of the port-A pins have alternative functions.
Pins 1, 2, 17 and 18 can be configured and used as analog inputs (AN0 to AN3). Those pins are therefore attached to internal comparators of the PIC. Voltage in the range 0-5V on those pins is converted into digital form and further processed by the microcontroller.
Pin 4 can be used as a master clear - reset pin (MCLR). Reset is used for putting the microcontroller into a 'known' (default) condition. Upon setting this pin to 0 V, all of its registers will be placed in a starting position. Here we use internal reset circuitry activated when the processor is powered up (power-up reset). This option is normally used when the microcontroller does not behave in an appropriate way due to some undesirable condition.
Pins 16 and 17 (OSC1/CLKIN and OSC2/CLKOUT) can be used when an external oscillator or crystal/RC timing elements are used to provide timing for the microcontroller.
Pin 3 (TOCK1) can be used as an input for the Timer 1 module. It operates independently from the main clock.
RB0 to RB7 are the eight I/O pins of port B. Port B is a second bidirectional port and it behaves in almost the same way as port A. Alternative functions of port pins are different for port A and port B.
Pin 6 can be configured and used as an external interrupt pin to detect external events. An event is detected when the interrupt pin changes state from '1' to '0' or from '0' to '1' (programmable).
Pins 7 and 8 can be used for serial communications: TX is the asynchronous serial transmit pin - data is sent from the chip on this pin; RX is the serial receive pin and data is sent to the chip on this pin.
Pin 9 can be used as a capture/compare pin (CCP1) in order to measure duration of external events (the length of the PWM - pulse width modulated pulse).
Pin 10 is used for a low-voltage programming of the PIC. Pins 12 and 13 can be used as an oscillator and as timer inputs for timer 1.
VSS and VDD are the power supply pins. VDD is the positive supply and VSS is the low supply or 0 V. All PIC family members operate off a 5 V supply and some can use supplies down to 2 V.
1.2.4 More on PIC Architecture
Trying to explain the architecture of PIC microcontrollers in more detail would probably take another book or at least another long and not-very-interesting chapter of this book. Instead, we will provide a very brief insight into some of the PIC features useful for understanding and doing some interesting PIC projects explained in the other chapters.
Timing. An oscillator (internal or external) is generally used to drive the PIC by clocking data and instructions into the processor. The actions of the CPU are caused by every fourth oscillator pulse, which makes the instruction times easy to calculate. Most instructions take one clock cycle (four oscillator pulses), so with a 4 MHz oscillator it will take 1s to execute each of those one-cycle instructions.
Program execution control. The program counter is an internal 13-bit CPU register used to store the current program position. After each loaded instruction the program counter is incremented automatically so that it points to the location of the next instruction in the program memory.
CPU status. Bits of the status register contain the information about the status of the arithmetic and logic unit from the CPU. Those bits are updated after certain instructions that modify the main working register content of the CPU. The PIC16F27A has an 8-bit status register, which contains information about the memory bank currently being accessed, carry, power state of the PIC, borrow or zero results of the executed instruction.
Timers. To provide accurate timing for the microcontroller actions a special function register called a timer can be used. This register is connected to the internal clock and increments at the clock frequency divided by four. When it rolls over from its maximum count (255 for an 8-bit timer, 65535 for a 16-bit timer) to zero, a flag is set to signal that event. It would not take long to count from 0 to the maximum count at 1 MHz, so a programmable prescaler can be used in combination with the timer module. The prescaler can be set to give out pulses at ratios of 1:2, 1:4 and so forth, up to 1:256, extending the timeout up to tens of milliseconds range for a 4 MHz oscillator used in the system.
Interrupts. An improved solution to exact timing described above is to set up a timer to generate an interrupt. A routine can be set by the programmer to be executed automatically every time the timer over-flows (rolls over at the maximum count). This requires some programming skill and will be explained in more detail in Chapter 5. There might be other situations that can cause an interrupt routine to be executed, such as a change of state on the port B pins or some external event sensed by the interrupt pin RB0.
Watchdog timer. To provide a means of recovery from some system problems a watchdog timer is implemented on all PIC microcontrollers. During its execution, the program needs to reset the watchdog timer at predetermined intervals, but if it fails to do so, due to a problem in the execution, the watchdog timer will initiate the reset itself. This can be a useful option in case the program goes into an endless loop or some hardware problem occurs to prevent the program operating correctly.
1.2.5 Brief Overview of the PIC Family
The whole PIC family can be divided in a number of distinct groups:
Baseline core devices, represented by the PIC10 series, as well as some PIC12 and PIC16 devices. Baseline devices are available in six-pin to 40-pin packages.
Midrange core devices, labelled PIC12 and PIC16.
PIC17 and PIC18 high-end core devices, represented by a not-very-popular PIC17 series, produced in packages from 40 to 68 pins and later superseded by the PIC18 architecture.
PIC24 and dsPIC microcontrollers are 16-bit microcontrollers. The PIC24 devices are designed as general-purpose microcontrollers and dsPICs include digital signal processing capabilities.
PIC32MX - these 32-bit microcontrollers are the latest addition to the PIC family introduced in November 2007.
1.3 Basics of PIC Assembly Language
Assembly languages are closer than high-level languages to possessing a one-to-one correspondence between symbolic instructions and executable machine codes. They are also usually more difficult to use. The aim of this section is to give a brief introduction to some aspects of PIC programming using assembler language. Most of the microcontroller programs spend a lot of time on the actual movement of data through the device. This is certainly the case for assembler language-type programs so we will start our introduction to PIC assembler language by looking into some instructions that are commonly used to move data through the microcontroller.
1.3.1 Data Movement, Arithmetic and Logical Operations
The PIC16F27A has a very small set of instructions - there are only 37 of them. Since the width of the program memory is 14 bits and the address of each register can take up to eight bits, two registers cannot be used in the same instruction. Remember that we also need to specify what action we actually want to accomplish with each particular instruction - too much information to fit into just 14 bits!
(Continues...)
Excerpted from PIC Projects - A Practical Approachby Hassan Parchizadeh Branislay Vuksanovic Copyright © 2009 by John Wiley & Sons, Ltd. Excerpted by permission.
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.
"About this title" may belong to another edition of this title.
Seller: Shakespeare Book House, Rockford, IL, U.S.A.
Condition: new. Seller Inventory # EBBV.0470694610.N
Seller: WeBuyBooks, Rossendale, LANCS, United Kingdom
paperback. Condition: Like New. Most items will be dispatched the same or the next working day. An apparently unread copy in perfect condition. Dust cover is intact with no nicks or tears. Spine has no signs of creasing. Pages are clean and not marred by notes or folds of any kind. Seller Inventory # rev3328550642
Quantity: 1 available
Seller: PBShop.store UK, Fairford, GLOS, United Kingdom
PAP. Condition: New. New Book. Shipped from UK. Established seller since 2000. Seller Inventory # FW-9780470694619
Quantity: 15 available
Seller: GreatBookPrices, Columbia, MD, U.S.A.
Condition: New. Seller Inventory # 5509684-n
Seller: Brook Bookstore On Demand, Napoli, NA, Italy
Condition: new. Seller Inventory # af12f33fb0422e50d96f245938eab970
Quantity: Over 20 available
Seller: THE SAINT BOOKSTORE, Southport, United Kingdom
Paperback / softback. Condition: New. This item is printed on demand. New copy - Usually dispatched within 5-9 working days. Seller Inventory # C9780470694619
Quantity: Over 20 available
Seller: GreatBookPrices, Columbia, MD, U.S.A.
Condition: As New. Unread book in perfect condition. Seller Inventory # 5509684
Seller: GreatBookPricesUK, Woodford Green, United Kingdom
Condition: New. Seller Inventory # 5509684-n
Quantity: Over 20 available
Seller: Ria Christie Collections, Uxbridge, United Kingdom
Condition: New. In English. Seller Inventory # ria9780470694619_new
Quantity: Over 20 available
Seller: Chiron Media, Wallingford, United Kingdom
Paperback. Condition: New. Seller Inventory # 6666-IUK-9780470694619
Quantity: 10 available