Tài liệu PIC Microcontrollers - Vi điều khiển PIC - Pdf 97


About the Authors
Martin P. Bates (Chapters 6, 7, 8, Appendices C, J) is the author of PIC Microcontrollers, 2E.
He is currently lecturing on electronics and electrical engineering at Hastings College, UK.
His interests include microcontroller applications and embedded system design.
Lucio Di Jasio (Chapters 24, 25, 26, 27, 28, 29) is the author of Programming 16-bit
Microcontrollers in C. He joined Microchip Technology in 1995 as a Field Application
Engineer. Since 2005, he has been in charge of the Application Segment Group, a cross-
divisional team of engineers that develops and promotes Microchip’s solutions across a wide
range of application segments, including: utility metering, intelligent power conversion, motor
control and lighting applications.
Chuck Hellebuyck (Chapters 15, 16, 17) is the author of Programming PIC Microcontrollers
using PIC Basic. He is founder and president of Elproducts, Inc., a fi rm specializing in devices
and project kits based on the PIC microcontroller. He writes a monthly column on the PIC
microcontroller for “Nuts and Volts” magazine.
Dogan Ibrahim (Chapters 1, 14) is the author of PICBasic Projects. He works for the
Transport for London in UK. He was formerly a lecturer at South Bank University and Head
of Department of Computer Engineering at Near East University, Cyprus.
John Morton (Chapters 9, 10, Appendices D, E, F, G, H, I) is the author of The PIC
Microcontroller. He is a Junior Research Fellow at St. John’s College, Oxford, investigating
experimental quantum computation using electron spins. He works in the Oxford University
Materials Department and Clarendon Laboratory and in collaboration with the Quantum
Information Processing IRC. His interests include PIC Microcontrollers.
D.W. Smith (Chapters 11, 12, 13) is the author of PIC in Practice. He has 30 years experience
in the Electronics Industry. Before arriving at MMU he worked as an Electronics Design
Engineer for ICL and Marconi. His teaching interests are focused on enabling Design and
Technology students to implement microcontroller designs into their projects.
Jack Smith (Chapters 18, 19, 20, 21, 22, 23) is the author of Programming the PIC
Microcontroller with MBasic. He is currently with Clifton Laboratories in Virginia. He was
www.newnespress.com
Prelims-H8615.indd xiiiPrelims-H8615.indd xiii 7/11/07 10:41:35 AM7/11/07 10:41:35 AM

Martin P. Bates
Jack Smith
D. W. Smith
Chuck Hellebuyck
AMSTERDAM • BOSTON • HEIDELBERG • LONDON
NEW YORK • OXFORD • PARIS • SAN DIEGO
SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO
Newnes is an imprint of Elsevier
Prelims-H8615.indd iiiPrelims-H8615.indd iii 7/11/07 10:41:31 AM7/11/07 10:41:31 AM
Newnes is an imprint of Elsevier
30 Corporate Drive, Suite 400, Burlington, MA 01803, USA
Linacre House, Jordan Hill, Oxford OX2 8DP, UK
Copyright © 2008, Elsevier Inc. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or
transmitted in any form or by any means, electronic, mechanical, photocopying,
recording, or otherwise, without the prior written permission of the publisher.
Permissions may be sought directly from Elsevier’s Science & Technology Rights
Department in Oxford, UK: phone: (ϩ44) 1865 843830, fax: (ϩ44) 1865 853333,
E-mail: You may also complete your request online via
the Elsevier homepage (), by selecting “Support & Contact” then
“Copyright and Permission” and then “Obtaining Permissions.”
Recognizing the importance of preserving what has been written, Elsevier prints
its books on acid-free paper whenever possible.
Library of Congress Cataloging-in-Publication Data
PIC microcontrollers : know it all / Lucio Di Jasio … [et al.].
p. cm. – (The Newnes know it all series)
ISBN-13: 978-0-7506-8615-0
1. Programmable controllers. 2. Microcomputers. 3. Microprocessors.
I. Di Jasio, Lucio.
TJ223. P76P52 2007

References 59
Chapter 3. Parallel Ports, Power Supply and the Clock Oscillator 61
3.1 The Main Idea—Parallel Input/Output 62
3.2 The Technical Challenge of Parallel Input/Output 62
3.3 Connecting to the Parallel Port 68
3.4 The PIC 16F84A Parallel Ports 71
3.5 The Clock Oscillator 74
3.6 Power Supply 78
3.7 The Hardware Design of the Electronic Ping-Pong 80
3.8 Summary 82
References 82
Prelims-H8615.indd vPrelims-H8615.indd v 7/11/07 10:41:32 AM7/11/07 10:41:32 AM
Section II. Programming PIC Microcontrollers Using Assembly Language 83
Chapter 4. Starting to Program—An Introduction to Assembler 85
4.1 The Main Idea—What Programs Do and How We Develop Them 86
4.2 The PIC 16 Series Instruction Set, with a Little More on the ALU 89
4.3 Assemblers and Assembler Format 92
4.4 Creating Simple Programs 94
4.5 Adopting a Development Environment 97
4.6 An Introductory MPLAB Tutorial 99
4.7 An Introduction to Simulation 103
4.8 Downloading the Program to a Microcontroller 106
4.9 What Others Do—A Brief Comparison of CISC and RISC Instruction Sets 108
4.10 Taking Things Further—The 16 Series Instruction Set Format 109
4.11 Summary 110
References 110
Chapter 5. Building Assembler Programs 111
5.1 The Main Idea—Building Structured Programs 111
5.2 Flow Control—Branching and Subroutines 114
5.3 Generating Time Delays and Intervals 118

7.5 Summary 198
Chapter 8. More PIC Applications and Devices 199
8.1 16F877 Application 199
8.2 16F818 Application 219
8.3 12F675 Application 220
8.4 18F452 Application 221
8.5 Summary 226
Chapter 9. The PIC12F50x Series (8-pin PIC Microcontrollers) 227
9.1 Differences from the PIC16F54 227
9.2 Example Project: PIC Dice 231
Chapter 10. Intermediate Operations Using the PIC12F675 237
10.1 The Inner Differences 238
10.2 Interrupts 242
10.3 EEPROM 252
10.4 Analog to Digital Conversion 259
10.5 Comparator Module 264
10.6 Final Project: Intelligent Garden Lights 270
Chapter 11. Using Inputs 275
11.1 Switch Flowchart 277
11.2 Program Development 278
11.3 Scanning (Using Multiple Inputs) 283
11.4 Switch Scanning 283
11.5 Control Application—A Hot Air Blower 287
Chapter 12. Keypad Scanning 291
12.1 Programming Example for the Keypad 291
Chapter 13. Program Examples 307
13.1 Counting Events 307
13.2 Look-Up Table 311
13.3 7-Segment Display 311
13.4 Numbers Larger than 255 321

18.3 Development Boards 470
18.4 Programming Style 473
18.5 Building the Circuits and Standard Assumptions 475
18.6 Pins, Ports and Input/Output 476
18.7 Pseudo-Code and Planning the Program 485
18.8 Inside the Compiler 487
References 491
Chapter 19. The Basics—Output 493
19.1 Pin Architectures 494
19.2 LED Indicators 498
19.3 Switching Inductive Loads 503
viii Contents
Prelims-H8615.indd viiiPrelims-H8615.indd viii 7/11/07 10:41:33 AM7/11/07 10:41:33 AM
www.newnespress.com
19.4 Low Side Switching 506
19.5 Isolated Switching 524
19.6 Fast Switching—Sound from a PIC 533
References 536
Chapter 20. The Basics—Digital Input 539
20.1 Introduction 539
20.2 Switch Bounce and Sealing Current 548
20.3 Hardware Debouncing 549
20.4 Software Debouncing 551
20.5 Isolated Switching 555
20.6 Reading a Keypad 557
Reference 562
Chapter 21. Introductory Stepper Motors 563
21.1 Stepper Motor Basics 563
21.2 Programs 586
References 613

25.5 Review 720
Books 723
Links 723
Chapter 26. More Pattern Work, More Loops 725
26.1 The Plan 725
26.2 Checklist 725
26.3 Coding 725
26.4 Testing with the Logic Analyzer 732
26.5 Using the Explorer16 Demonstration Board 734
26.6 Review 734
Books 736
Links 736
Chapter 27. NUMB3RS 737
27.1 The Plan 737
27.2 Checklist 737
27.3 Coding 737
27.4 Notes for C Experts 742
27.5 Measuring Performance 743
27.6 Review 746
Links 749
Chapter 28. Interrupts 751
28.1 The Plan 751
28.2 Checklist 751
28.3 Coding 751
28.4 Managing Multiple Interrupts 764
28.5 Review 765
Books 768
Links 768
Chapter 29. Taking a Look Under the Hood 769
29.1 The Plan 769

led a development team building original systems for research applications—for example in
measurement of bullet speed, wind tunnel control, simulated earthquakes, or seeking a cure to
snoring. Now he is Head of Electronic Systems at the University of Derby, where he aims to
share his love of engineering design with his students.
xiv About the Authors
www.newnespress.com
Prelims-H8615.indd xivPrelims-H8615.indd xiv 7/11/07 10:41:36 AM7/11/07 10:41:36 AM
An Introduction
to PIC
Microcontrollers
SECTION I
Ch01-H8615.indd 1Ch01-H8615.indd 1 6/29/07 4:58:05 PM6/29/07 4:58:05 PM
This page intentionally left blank
The PIC Microcontroller Family
The PIC microcontroller family is manufactured by Microchip Technology Inc. Currently
they are one of the most popular microcontrollers, used in many commercial and industrial
applications. Over 120 million devices are sold each year.
The PIC microcontroller architecture is based on a modifi ed Harvard RISC (Reduced Instruction
Set Computer) instruction set with dual-bus architecture, providing fast and fl exible design
with an easy migration path from only 6 pins to 80 pins, and from 384 bytes to 128 kbytes of
program memory.
PIC microcontrollers are available with many different specifi cations depending on:

Memory Type
– Flash
– OTP (One-time-programmable)
– ROM (Read-only-memory)
– ROMless

Input–Output (I/O) Pin Count


RISC instruction set with only a handful of instructions to learn

Digital I/O ports

On-chip timer with 8-bit prescaler

Power-on reset

Watchdog timer

Power-saving SLEEP mode

High source and sink current

Direct, indirect, and relative addressing modes

External clock interface

RAM data memory

EPROM or Flash program memory
Some devices offer the following additional features:

Analog input channels

Analog comparators

Additional timer circuits


Number of I/O pins required

Required peripherals (e.g., USART, USB)

The minimum size of program memory

The minimum size of RAM

Whether or not EEPROM nonvolatile data memory is required

Speed

Physical size

Cost
The important point to remember is that there could be many models that satisfy all of
these requirements. You should always try to fi nd the model that satisfi es your minimum
requirements and the one that does not offer more than you may need. For example, if you
require a microcontroller with only 8 I/O pins and if there are two identical microcontrollers,
one with 8 and the other one with 16 I/O pins, you should select the one with 8 I/O pins.
Although there are several hundred models of PIC microcontrollers, the family can be broken
down into three main groups, which are:

12-bit instruction word (e.g., 12C5XX, 16C5X) (also referred to in this book as the 12
Series and the 16C5X Series)
The PIC Microcontroller Family 5
www.newnespress.com
Ch01-H8615.indd 5Ch01-H8615.indd 5 6/29/07 4:58:06 PM6/29/07 4:58:06 PM

14-bit instruction word (e.g., 16F8X, 16F87X) (also referred to in this book as the

can be erased and reprogrammed using the standard PIC programmer devices. Similarly,
the “CE” version of the family (e.g., PIC12CE518) offers an additional 16-byte nonvolatile
EEPROM data memory.
Figure 1.1 shows the pin confi guration of the PIC12F508 microcontroller.
6 Chapter 1
www.newnespress.com
Ch01-H8615.indd 6Ch01-H8615.indd 6 6/29/07 4:58:07 PM6/29/07 4:58:07 PM
PIC16C5X: This is one of the earliest PIC microcontrollers. The device is 18-pin with a
384 ϫ 12 EPROM program memory, 25 bytes of RAM data memory, 12 I/O ports, a timer,
and a watchdog. Some other members in the family, such as PIC16C56, have the same
architecture but more program memory (1024 ϫ 12). PIC16C58A has more program memory
(2048 ϫ 12) and also more data memory (73 bytes of RAM). Figure 1.2 shows the pin
confi guration of the PIC16C56 microcontroller.
PIC 12F508/509
1
2
3
4
8VDD VSS
7
6
5
GP5/OSC1/CLKIN GP0/ICSPDAT
GP4/OSC2 GP1/ICSPCLK
GP3/MCLR/VPP GP2/T0CKI
Figure 1.1: PIC12F508 Microcontroller
PIC16C54
PIC16CR54
PIC16C56
PIC16CR56

5
6
7
8
16
15
14
13
9
10
12
11
1
Figure 1.2: PIC16C56 Microcontroller
1.2 14-bit Instruction Word
This is a big family that includes many models of PIC microcontrollers. Most of the devices
in this family can operate at up to a 20-MHz clock rate. The instruction set consists of 35
instructions. These devices offer advanced features such as internal and external interrupt
sources. Table 1.2 lists some of the microcontrollers in this group.
PIC16C554: This microcontroller has similar architecture to the PIC16C54 but the instructions
are 14 bits wide. The program memory is 512 ϫ 14 and the data memory is 80 bytes of RAM.
There are 13 I/O pins and each pin can source or sink 25 mA of current. Additionally, the
device contains a timer and a watchdog.
The PIC Microcontroller Family 7
www.newnespress.com
Ch01-H8615.indd 7Ch01-H8615.indd 7 6/29/07 4:58:07 PM6/29/07 4:58:07 PM
PIC16F84: This has been one of the most popular PIC microcontrollers for a very long time.
This is an 18-pin device and it offers 1024 ϫ 14 fl ash program memory, 36 bytes of data
RAM, 64 bytes of nonvolatile EEPROM data memory, 13 I/O pins, a timer, a watchdog, and
internal and external interrupt sources. The timer is 8 bits wide but can be programmed to

16
15
5
6
7
8
14
13
12
11
910
RB4
1
Figure 1.3: PIC16F84 Microcontroller Pin Confi guration
Microcontroller Program Data Max Speed I/O A/D
Memory RAM (MHz) Ports Converter
16C554 512 ϫ 14 80 20 13 –
16C64 2048 ϫ 14 128 20 33 –
16F84 1024 ϫ 14 36 10 13 –
16F627 1024 ϫ 14 224 20 16 –
16F628 2048 ϫ 14 224 20 16 –
16F676 1024 ϫ 14 64 20 12 8
16F73 4096 ϫ 14 192 20 22 5
16F876 8192 ϫ 14 368 20 22 5
16F877 8192 ϫ 14 368 20 33 8
Table 1.2: Some 14-bit Microcontrollers
8 Chapter 1
www.newnespress.com
Ch01-H8615.indd 8Ch01-H8615.indd 8 6/29/07 4:58:08 PM6/29/07 4:58:08 PM
368 bytes of RAM, 256 bytes of nonvolatile EEPROM memory, 33 I/O pins, 8 multiplexed

RE1/WR/AN6
RE2/CS/AN7
VDD
VSS
OSC1/CLKI
OSC2/CLKO
RC0/T1OSO/T1CKI
RC1/T1OSI/CCP2
RC3/SCK/SCL
RD0/PSP0
RD1/PSP1
RC2/CCP1
RB7/PGD
RB6/PGC
RB5
RB4
RB3/PGM
RB2
RB1
RB0/INT
V
DD
VSS
RD7/PSP7
RD6/PSP6
RD5/PSP5
RD4/PSP4
RC7/RX/DT
RC6/TX/CK
RC5/SDO

25
24
18 23
19 22
20 21
Ch01-H8615.indd 9Ch01-H8615.indd 9 6/29/07 4:58:08 PM6/29/07 4:58:08 PM
10 Chapter 1
www.newnespress.com
PIC16F627: This is an 18-pin microcontroller with 1024 ϫ 14 fl ash program memory. The
device offers 224 bytes of RAM, 128 bytes of nonvolatile EEPROM memory, 16 I/O pins, two
8-bit timers, one 16-bit timer, a watchdog, and comparator circuits. This microcontroller is
similar to PIC16F84, but offers more I/O pins, more program memory, and a lot more RAM.
In addition, PIC16F627 is more suited to applications that require more than one timer.
Figure 1.5 shows the pin confi guration of this microcontroller.
1
2
3
4
18
RA2/AN2/V
REF
RA3/AN3/CMP1
RA4/TOCKI/CMP2
RA5/MCLR/V
PP
VSS
RB0/INT
RB1/RX/DT
RB2/TX/CK
RB3/CCP1

2
3
4
14
V
DD
RA5/T1CKI/OSC1/CLKIN
RA4/T1G/OSC2/AN3/CLKOUT
RA3/MCLR/V
PP
RC5
RC4
RC3/AN7
V
SS
RA0/AN0/CINϩ/ICSPDAT
RA1/AN1/CINϪ/V
REF/ICSPCLK
RA2/AN2/COUT/T0CK/INT
RC0/AN4
RC1/AN5
RC2/AN6
13
12
11
5
6
7
10
9


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status