Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

PIC microcontroller: an introduction to software and hardware interfacing
Nội dung xem thử
Mô tả chi tiết
7'
THOMSON
rm
DELMAR LEARNING
PIC Microcontroller: An Introduction to Software and Hardware Interfacing
Han-Way Huang
Vice President, Technology
and Trades SBU:
Alar Elken
Editorial Director:
Sandy Clark
Senior Acquisitions Editor:
Steve Helba
Senior Development Editor:
Michelle Ruelos Cannistraci
COPYRIGHT 2005 by Delmar Learning, a
division of Thomson Learning, Inc.
Thomson Learning' is a trademark used
herein under license.
Printed in the United States of America
2 3 4 5 XX 08 07 06 05
For more information contact
Delmar Learning
Executive Woods
5 Maxwell Drive, PO Box 8007,
Clifton Park, NY 12065-8007
Or find us on the World Wide Web at
http://www.delmar.com
Notice To The Reader
Publisher does not warrant or guarantee
any of the products described herein or
perform any independent analysis in
connection with any of the product
information contained herein. Publisher
does not assume, and expressly disclaims, any obligation to obtain and
include information other than that provided to it by the manufacturer.
The reader is expressly warned to consider and adopt all safety precautions
Senior Editorial Assistant:
Dawn Daugherty
Marketing Director:
Dave Garza
Channel Manager:
Fair Huntoon
Marketing Coordinator:
Casey Bruno
Production Director:
Mary Ellen Black
ALL RIGHTS RESERVED. No part of this
work covered by the copyright hereon
may be reproduced in any form or by
any means-graphic, electronic, or
mechanical, including photocopying,
recording, taping, Web distribution,
or information storage and retrieval
systems-without the written permission of the publisher.
For permission tq use material from the
text or product, contact us by
Tel. (800) 730-2214
Fax (800) 730-221 5
www.thomsonrights.com
that might be indicated by the activities
herein and to avoid all potential hazards. By following the instructions contained herein, the reader willingly
assumes all risks in connection with such
instructions.
The publisher makes no representation
or warranties of any kind, including but
not limited to, the warranties of fitness
for particular purpose or merchantability, nor are any such representations
Production Manager:
Andrew Crouth
Production Editor:
Stacy Masucci
Technology Project Manager:
Kevin Smith
Technology Project Specialist:
Linda Verde
Library of Congress
Cataloging-in-Publication Data:
ISBN 1-4018-3967-3
implied with respect to the material set
forth herein, and the publisher takes no
responsibility with respect to such material. The publisher shall not be liable for
any special, consequential, or exemplary
damages resulting, in whole or part,
from the readers' use of, or reliance
upon, this material.
Contents
Preface xxj
1.1 Objectives 1
1.2 What Is a Computer? 2
1.2.1 The Processor 2
1.2.2 The Microprocessor 3
1.2.3 Microcontrollers 4
1.2.4 Memory 4
1.3 The Computer Software 6
1.3.1 Assembly Language 6
1.3.2 High-Level Languages 7
1.4 Overview of the PIC18 MCU 8
1.5 The PIC18 Memory Organization 13
1.5.1 Separation of Data Memory and Program Memory 13
1.5.2 PIC18 Data Memory 14
1.5.3 EEPROM Data Memory 15
1.5.4 Program Memory Organization 15
1.6 The PIC18 CPU Registers 17
1.7 The PIC18 Pipelining 19
1.8 PIC18 Instruction Format 20
1.9 Addressing Modes 22
1.9.1 Register Direct 22
1.9.2 Immediate Mode 23
1.9.3 Inherent Mode 23
1.9.4 Indirect Mode 23
1.9.5 Bit-Direct Addressing Mode 25
1.10 A Sample of PIC18 Instructions 26
1.10.1 Data Movement Instructions 26
1.10.2 ADD Instructions 27
1.10.3 SUB Instructions 29
Contents
1.11 Overview of the 8-Bit MCU Market 29
1.1 1.1 CISC versus RISC 30
1.1 1.2 Major 8-Bit MCUs 31
1.11.3 Motorola 8-Bit MCUs 32
1.11.4 Intel 805 118052 Variants 32
1.1 1.5 The Atmel AVR 33
1.12 Summary 33
1.13 Exercises 35
Chapter 2 PIC18 Assembly Language Programming 37
Objectives 37
Introduction 38
Assembly Language Program Structure 38
2.3.1 The Label Fields 38
2.3.2 The Mnemonic Field 39
2.3.3 The Operand Field 39
2.3.4 The Comment Field 39
Assembler Directives 40
2.4.1 Control Directives 40
2.4.2 Data Directives 44
2.4.3 Macro Directives 47
2.4.4 Listing Directives 48
2.4.5 Object File Directives 50
Representing the Program Logic 53
A Template for Writing Assembly Programs 55
Case Issue 56
Writing Programs to Perform Arithmetic Computations 56
2.8.1 Perform Addition Operations 56
2.8.2 Perform Subtraction Operations 58
2.8.3 Binary Coded Decimal Addition 60
2.8.4 Multiplication 63
Program Loops 66
2.9.1 Program Loop Constructs 66
2.9.2 Changing the Program Counter 68
Reading and Writing Data in Program Memory 75
Logic lnstructions 78
Using Program Loop to Create Time Delays 80
Rotate lnstructions 82
Using Rotate lnstructions to Perform Multiplications and Divisions 85
Summary 86
Exercises 87
Contents vii
Chapter 3 PIC18 Development Tools 89
3.1 Objectives 89
3.2 Development Tools 90
3.3 Software Tools 90
3.3.1 Text Editors 90
3.3.2 Cross Assemblers and Cross Compilers 90
3.3.3 Simulator 90
3.3.4 Source-Level Debugger 91
3.3.5 Integrated Development Environment 9 1
3.4 Hardware Tools 91
3.4.1 The Nature of Debugging Activities 91
3.4.2 ICE 92
3.4.3 Device Programmer 93
3.4.4 In-Circuit-Debugger I1 94
3.4.5 Demo Boards 95
3.5 Using MPLAB IDE 95
3.5.1 Getting Started with MPLAB IDE 96
3.5.2 Creating a Simple Project 97
3.5.3 Entering Source Code 100
3.5.4 Adding Source Files to the Project 101
3.5.5 Building the Project 102
3.5.6 Debugging the Project 102
3.6 Using the MPLAB SIM in Debugging PIC18 Applications 102
3.6.1 Setting Up the Simulator 103
3.6.2 Running Code under MPLAB SIM 103
3.6.3 Viewing Variables 105
3.6.4 Using Watch Window to View Variables 105
3.6.5 Setting Breakpoints 106
3.6.6 Tracing Code 108
3.6.7 Advanced Simulator Options 1 10
3.7 Using the MPLAB ICD 2 111
3.7.1 ICD2 Settings 112
3.8 Demo Boards from ShuanShizu Enterprise 116
3.8.1 SSE452 Demo Board 116
3.8.2 SSE8720DemoBoard 118
3.8.3 SSE8680 Demo Board 119
3.8.4 Debug Monitor 120
3.9 Summary 120
3.10 Lab Exercises and Assignments 121
viii Contents
Chapter 4 Advanced Assembly Programming 125
Objectives 125
lntroduction 126
Signed Arithmetic 126
4.3.1 Signed 8-Bit Multiplication 126
4.3.2 Signed 16-Bit Multiplication 128
Unsigned Divide Operation 131
Signed Divide Operation 133
The Stack 137
4.6.1 Staclz Operations 138
Subroutines 139
4.7.1 Instructions for Supporting Subroutine Calls 140
4.7.2 Return Address Stack 141
4.7.3 Top-of-Stack Access 141
4.7.4 Return Address Stack Pointer (STKPTR) 141
4.7.5 Fast Register Stack 141
4.7.6 Table Lookup Instruction 142
Issues Related to Subroutine Calls 142
4.8.1 The Stack Frame 144
4.8.2 Accessing Locations in the Staclz Frame 144
String Processing 153
More Examples on Subroutine Calls 166
4.10.1 Square Root Computation 171
4.10.2 Finding Prime Numbers 174
Summary 177
Exercises 177
Lab Exercises and Assignments 179
Chapter 5 A Tutorial to the C Language and the Use
of the C Compiler 183
5.1 Objectives 183
5.2 Introduction to C lS4
5.3 Types, Operators, and Expressions 185
5.3.1 Data Types 185
5.3.2 Variable Declarations 185
5.3.3 Constants 185
5.3.4 Arithmetic Operators 186
5.3.5 Bitwise Operators 186
5.3.6 Relational and Logical Operators 187
5.3.7 Precedence of Operators 188
5.4 Control Flow 188
5.4.1 If Statement 188
Contents
5.4.2 If-Else Statement 189
5.4.3 Multiway Conditional Statement 189
5.4.4 Switch Statement 190
5.4.5 For-Loop Statement 190
5.4.6 While Statement 191
5.4.7 Do-While Statement 191
5.4.8 GOT0 Statement 191
Input and Output 192
Functions and Program Structure 193
Pointers, Arrays, Structures, and Unions 196
5.7.1 Pointers and Addresses 196
5.7.2 Arrays 197
5.7.3 Pointers and Arrays 197
5.7.4 Passing Arrays to a Function 198
5.7.5 Initializing Arrays 199
5.7.6 Structures 199
5.7.7 Unions 200
Miscellaneous Items 201
5.8.1 Automatic/External/Static/Volatile 201
5.8.2 Scope Rules 202
Introduction to Project Build Process for the Microchip PIC18 Compiler 203
The MPLINK Linker 204
A Tutorial on Using the MCC18 Compiler 209
5.11.1 CreatingaNewProject 210
5.1 1.2 Setting Build Options 212
5.1 1.3 Add Source Files to Project 212
5.1 1.4 Building the Project 214
5.11.5 Choosing Debug Tool 214
5.11.6 Buildtheproject 214
5.11.7 Checking the ICD2 Settings 214
5.11.8 Program the Target Device 215
5.1 1.9 Running the Program 215
C Language Implementation in the MCC18 Compiler 216
5.12.1 Data Types and Limits 216
5.12.2 Storage Classes 217
5.12.3 Storage Qualifiers 218
5.12.4 Overlay 218
ANSI/ISO Divergences 219
5.13.1 Integer Promotions 219
5.13.2 Numeric Constants 220
5.13.3 String Constants 220
5.13.4 Anonymous Structures 221
lnline Assembly 221
Bit Field Manipulation in C Language 222
Contents
5.15.1 Processor-Specific Header Files 223
5.15.2 Processor-Specific Register Definition Files 225
5.16 The #pragma Statement 225
5.16.1 #pragma Section Type 225
5.17 Mixing C and Assembly 226
5.17.1 Calling Conventions 226
5.17.2 Return Values 228
5.17.3 Calling C Routines from an Assembly Program 228
5.17.4 Calling Assembly Functions from a C Program 229
5.17.5 Referencing C Variables in an Assembly Function 230
5.17.6 Referencing Assembly Variables in a C Function 23 1
5.17.7 Startup Code 231
5.18 MPLAB C18 Library Functions 232
5.18.1 Processor-Specific Libraries 232
5.18.2 Processor-Independent Library 232
5.18.3 Character Classification Functions 232
5.18.4 Data Conversion Library Functions 233
5.18.5 Memory and String Manipulation Functions 234
5.18.6 Delay Functions 235
5.19 Using the Hi-Tech C18 Compiler 237
5.20 Summary 238
5.21 Exercises 239
5.22 Lab Exercises and Assignments 240
Chapter 6 Interrupts, Resets, and Configuration 243
6.1 Objectives 243
6.2 Basics of lnterrupts 244
6.2.1 What Is an Interrupt? 244
6.2.2 Why Interrupts? 245
6.2.3 Interrupt Maskability 245
6.2.4 Interrupt Priority 246
6.2.5 Interrupt Service 246
6.2.6 Interrupt Vector 246
6.2.7 Interrupt Programming 247
6.2.8 Interrupt Overhead 248
6.3 Resets 248
6.4 The PIC18 lnterrupts 249
6.4.1 PIC18 Interrupt Priority 249
6.4.2 Registers Related to Interrupts 249
6.4.3 RCON Register 249
6.4.4 Interrupt Control Registers (INTCON, INTCON2, INTCON3) 250
6.4.5 PIR1 . . . PIR3 Registers 253
Contents
6.4.6 PIE1 . . . PIE3 Registers 255
6.4.7 Interrupt Priority Registers (IPR1 . . . IPR3) 258
6.5 PIC18 Interrupt Operation 261
6.5.1 PIC18 Interrupt without Setting Priority 262
6.5.2 PIC18 Interrupt with Priority Enabled 262
6.5.3 INT Pin Interrupts 263
6.5.4 Port B Pins Input Change 263
6.5.5 TMRO Overflow Interrupt 263
6.5.6 Peripheral Interrupts 263
6.6 PIC18 Interrupt Programming 264
6.6.1 Interrupt Programming in Assembly Language 264
6.6.2 Interrupt Programming in C Language 267
6.6.3 Context Saving during Interrupts 269
6.7 The PIC18 Resets 270
6.7.1 Power-On Reset 271
6.7.2 Power-Up Timer 271
6.7.3 Oscillator Start-Up Timer 271
6.7.4 Brown-Out Reset 271
6.7.5 The Reset Instruction 271
6.8 Summary 271
6.9 Exercises 273
6.10 Lab Exercises and Assignments 273
Chapter 7 Parallel Ports 275
7.1 Objectives 275
7.2 Introduction 276
7.3 1/O Addressing 277
7.4 1/O Synchronization 277
7.4.1 Synchronizing the CPU with the Interface Chip 278
7.4.2 Synchronizing the Interface Chip with 110 Devices 279
7.5 Overview of the PIC18 Parallel Ports 281
7.5.1 Port A 283
7.5.2 Port B 284
7.5.3 Port C 285
7.5.4 Port D 285
7.5.5 Port E and Port F 286
7.5.6 Port G 286
7.5.7 Port H and Port J 286
7.5.8 Port K 287
7.6 Interfacing with Simple Output Devices 287
7.6.1 Interfacing with LEDs 287
7.6.2 Interfacing with Seven-Segment Displays 288
7.6.3 Liquid Crystal Display 293
7.7 The HD44780 LCD Controller 294
7.7.1 Display Data RAM 296
7.7.2 Character Generator ROM 297
7.7.3 Character Generator RAM 297
7.7.4 Registers 297
7.7.5 Instruction Description 298
7.7.6 Interfacing the HD44780 to the PIC18 Microcontroller 299
7.8 Interfacing with DIP Switches 312
7.9 lnterfacing with a Keypad 313
7.9.1 Keypad Scanning 3 13
7.9.2 Keypad Debouncing 3 14
7.10 lnterfacing with a D/A Converter 322
7.10.1 The MAX5 102 DAC 322
7.10.2 Interfacing the MAX5102 with the PIC18 Microcontroller 324
7.11 Parallel Slave Port 326
7.12 Summary 330
7.13 Exercises 330
7.14 Lab Exercises and Assignments 332
Chapter 8 Timers and CCP Modules 333
8.1 Objectives 333
8.2 Overview of PIC18 Timer Functions 334
8.3 Timers 334
8.3.1 Timer0 335
8.3.2 Timer1 338
8.3.3 Timer2 343
8.3.4 Timer3 345
8.3.5 Timer4 346
8.3.6 C Library Functions for Timers 347
8.4 Capture/Compare/PWM Modules 349
8.4.1 CCP Module Configuration 350
8.5 CCP in Capture Mode 351
8.5.1 Capture Operation 352
8.5.2 Microchip C Library Functions for CCP in Capture Mode 352
8.5.3 Applications of Capture Mode 353
8.6 CCP in Compare Mode 361
8.6.1 Compare Mode Operation 361
8.6.2 Applications of CCP in Compare Mode 362
8.7 CCP in PWM Mode 373
8.7.1 PWM C Library Functions 376
8.7.2 PWM Applications 377
8.7.3 DC Motor Control 380
Contents
8.8 Enhanced CCP Module 385
8.8.1 ECCPPins 385
8.8.2 ECCP Registers 385
8.8.3 Enhanced PWM Mode 387
8.9 Summary 401
8.10 Exercises 403
8.11 Lab Exercises and Assignments 404
Chapter 9 Addressable Uniyersal Synchronous Asynchronous Receiver Transceiver 407
Objectives 407
Overview of Serial Communication 408
The EIA232 Standard 408
9.3.1 Electrical Specifications 409
9.3.2 EIA232E Functional Specifications 409
9.3.3 EIA232E Mechanical Specifications 412
9.3.4 EIA232E Procedural Specifications 413
9.3.5 Data Format 416
9.3.6 Data Transmission Errors 41 7
9.3.7 Null-Modem Connection 41 7
The PIC18 Serial Communication Interface 418
9.4.1 USART-Related Registers 418
9.4.2 USART Asynchronous Mode 421
9.4.3 Flow Control of USART in Asynchronous Mode 426
9.4.4 C Library Functions for USART 427
9.4.5 Interface Asynchronous Mode USART with EIA232 430
9.4.6 USART Synchronous Master Mode 432
9.4.7 USART Synchronous Slave Mode 434
9.4.8 Applications of USART Synchronous Mode 434
Enhanced USART 440
Summary 441
Exercises 442
Lab Exercises and Assignments 443
Chapter 10 Serial Peripheral Interface 445
10.1 Objectives 445
10.2 Introduction 446
10.3 SPl Mode 446
10.3.1 MSSP Registers 447
10.3.2 SPI Operation 449
Contents
10.3.3 Configuration of MSSP for SPI Mode 449
10.3.4 SPI Data Transfer 453
10.3.5 SPI Circuit Connection 453
SPI C Library Functions 455
Applications of SPI 456
Interfacing with the 74HC595 456
10.6.1 Operation of the 74HC595 457
MAX7221 Seven-Segment Display Driver 462
10.7.1 Signal Pins 462
10.7.2 MAX7221 Functioning 463
10.7.3 Choosing the Value for RsET 468
Digital Temperature Sensor TC72 474
10.8.1 Functioning of TC72 475
10.8.2 Temperature Data Format 475
10.8.3 Serial Bus Interface 476
10.8.4 Internal Register Structure 478
The DS1306 Alarm Real-Time Clock 481
10.9.1 Signal Descriptions 482
10.9.2 RTC Registers 483
10.9.3 Serial Interface of DS1306 487
10.9.4 Power Supply Configurations 488
10.9.5 Applications 488
Summary 498
Exercises 499
Lab Exercises and Assignments 502
Chapter 11 Interintegrated Circuit Interface 503
11.1 Objectives 503
11.2 The I2C Protocol 504
11.2.1 Characteristics of 12C Protocol 504
1 1.2.2 12C Signal Levels 504
11.2.3 12C Data Transfer Signal Components 505
11.2.4 Synchronization 507
11.2.5 Arbitration 508
11.2.6 Data Transfer Format 509
11.2.7 7-Bit Addressing 5 10
11.2.8 10-Bit Addressing 5 12
11.3 PIC18 MSSP Module in I2C Mode 515
11.4 Registers for I2C Operation 515
11.4.1 The SSPCONl Register 515
11.4.2 The SSPCON2 Register 516
11.4.3 The SSPSTAT Register (12C Mode) 5 18
1 1.4.4 MSSP Address Register 5 19