Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 8482

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Microcontroller

Programming

The Microchip PIC®

Microcontroller

Programming

The Microchip PIC®

Julio Sanchez

Minnesota State University, Mankato

Maria P. Canton

South Central College, North Mankato, Minnesota

Boca Raton London New York

CRC Press is an imprint of the

Taylor & Francis Group, an informa business

CRC Press

Taylor & Francis Group

6000 Broken Sound Parkway NW, Suite 300

Boca Raton, FL 33487 2742

© 2007 by Taylor & Francis Group, LLC

CRC Press is an imprint of Taylor & Francis Group, an Informa business

No claim to original U.S. Government works

Printed in the United States of America on acid free paper 10 9 8 7 6 5 4 3 2 1

International Standard Book Number 10: 0 8493 7189 9 (Hardcover)

International Standard Book Number 13: 978 0 8493 7189 9 (Hardcover)

This book contains information obtained from authentic and highly regarded sources. Reprinted material is quoted with permission, and sources are indicated. A wide variety of references are listed. Reasonable efforts have been made to publish reliable data and information, but the author and the publisher cannot assume responsibility for the validity of all materials or for the consequences of their use.

No part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any informa tion storage or retrieval system, without written permission from the publishers.

For permission to photocopy or use material electronically from this work, please access www.copyright.com (http:// www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC) 222 Rosewood Drive, Danvers, MA 01923, 978 750 8400. CCC is a not for profit organization that provides licenses and registration for a variety of users. For orga nizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged.

Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe.

Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com

and the CRC Press Web site at http://www.crcpress.com

Table of Contents

Preface xv

Chapter 1 - Basic Electronics

1

1.0

The Atom

1

1.1

Isotopes and Ions

2

1.2

Static Electricity

3

1.3

Electrical Charge

4

1.3.1

Voltage

4

1.3.2

Current

4

1.3.3

Power

5

1.3.4

Ohm's Law

5

1.4

Electrical Circuits

6

1.4.1

Types of Circuits

6

1.5

Circuit Elements

8

1.5.1

Resistors

9

1.5.2

Revisiting Ohm's Law

9

1.5.3

Resistors in Series and Parallel

10

1.5.4

Capacitors

12

1.5.5

Capacitors in Series and in Parallel

13

1.5.6

Inductors

14

1.5.7

Transformers

15

1.6

Semiconductors

15

1.6.1

Integrated Circuits

16

1.6.2

Semiconductor Electronics

16

1.6.3

P-Type and N-Type Silicon

17

1.6.4

The Diode

17

Chapter 2 - Number Systems

19

2.0

Counting

19

2.0.1

The Tally System

19

2.0.2 Roman Numerals

20

2.1

The Origins of the Decimal System

20

2.1.1

Number Systems for Digital-Electronics

22

2.1.2

Positional Characteristics

22

2.1.3

Radix or Base of a Number System

23

v


vi

Microcontroller Programming

2.2

Types of Numbers

23

2.2.1 Whole Numbers

24

2.2.2

Signed Numbers

24

2.2.3

Rational, Irrational, and Imaginary Numbers

24

2.3

Radix Representations

25

2.3.1

Decimal versus Binary Numbers

25

2.3.2

Hexadecimal and Octal

26

2.4

Number System Conversions

27

2.4.1 Binary-to-ASCII-Decimal

28

2.4.2

Binary-to-Hexadecimal Conversion

29

2.4.3

Decimal-to-Binary Conversion

29

Chapter 3 - Data Types and Data Storage

33

3.0

Electronic-Digital Machines

33

3.1

Character Representations

33

3.1.1

ASCII

34

3.1.2 EBCDIC and IBM

36

3.1.3

Unicode

36

3.2

Storage and Encoding of Integers

37

3.2.1

Signed and Unsigned Representations

37

3.2.2

Word Size

38

3.2.3

Byte Ordering

39

3.2.4

Sign-Magnitude Representation

40

3.2.5

Radix Complement Representation

41

3.3

Encoding of Fractional Numbers

44

3.3.1

Fixed-Point Representations

45

3.3.2

Floating-Point Representations

46

3.3.3

Standardized Floating-Point Representations

47

3.3.4

IEEE 754 Single Format

48

3.3.5

Encoding and Decoding Floating-Point Numbers

50

3.4

Binary-Coded Decimals (BCD)

51

3.4.1

Floating-Point BCD

52

Chapter 4 - Digital Logic, Arithmetic, and Conversions

55

4.0

Microcontroller Logic and Arithmetic

55

4.0.1

CPU Flags

55

4.0.2

Word Size

56

4.1

Logical Instructions

56

4.1.1

Logical AND

57

4.1.2

Logical OR

57

4.1.3

Logical XOR

57

4.1.4

Logical NOT

58

4.2

Microcontroller Arithmetic

58

4.2.1

Unsigned and Two’s Complement Arithmetic

58

4.2.2

Operations on Decimal Numbers

60

4.3

Bit Manipulations and Auxiliary Operations

62

4.3.1

Bit Shift and Rotate

62

4.3.2

Comparison Operations

63

4.3.3

Other Support Operations

63


Contents

vii

4.4

Unsigned Binary Arithmetic

64

4.4.1

Multi-byte Unsigned Addition

64

4.4.2

Unsigned Multiplication

65

4.4.3

Unsigned Division

67

4.5

Signed Binary Arithmetic

67

4.5.1

Overflow Detection in Signed Arithmetic

69

4.5.2

Sign Extension Operations

70

4.5.3

Multi-byte Signed Operations

71

4.6

Data Format Conversions

72

4.6.1

BCD Digits to ASCII Decimal

72

4.6.2

Unsigned Binary to ASCII Decimal Digits

73

4.6.3

ASCII Decimal String to Unsigned Binary

73

4.6.4

Unsigned Binary to ASCII Hexadecimal Digits

75

4.6.6

Signed Numerical Conversions

76

Chapter 5 - Circuits and Logic Gates

77

5.0

Digital Circuits

77

5.1

The Diode Revisited

78

5.1.1

The Light-Emitting Diode (LED)

79

5.2

The Transistor

81

5.2.1

Bipolar Transistor

81

5.2.2

MOS Transistor

83

5.3

Logic Gates

84

5.4

Transistor-Transistor Logic

85

5.4.1

Inverter Gates

86

5.4.2 The AND Gate

87

5.4.3 The NAND Gate

87

5.4.4 The OR Gate

88

5.4.5 The NOR Gate

88

5.4.6

Positive and Negative Logic

89

5.4.7 The XOR Gate

90

5.4.8

Schmitt Trigger Inverter

91

5.5

Other TTL Logic Families

93

5.6

CMOS Logic Gates

93

Chapter 6 - Circuit Components

95

6.0

Power Supplies

95

6.1

Clocked Logic and Flip-flops

96

6.1.1 The RS Flip-flop

96

6.1.2

Clocked RS Flip-flop

98

6.1.3

The D Flip-flop

99

6.1.4

The Edge-triggered D Flip-flop

100

6.1.5

Preset and Clear Signals

101

6.1.6

D Flip-flop Waveform Action

102

6.1.7

Flip-flop Applications

103

6.2

Clocks

103

6.2.1

Clock Waveforms

104

6.2.2

The TTL Clock

105

6.2.3

The 555 Timer

106


viii

Microcontroller Programming

6.2.4

Microcontroller Clocks

106

6.3

Frequency Dividers and Counters

107

6.3.1

Frequency Dividers

107

6.3.2

The JK Flip-flop Counter

107

6.3.3

Ripple Counters

108

6.3.4

Decoding Gates

110

6.3.5

Synchronous Counters

110

6.3.6

Counter ICs

112

6.3.7

Shift Registers

113

6.4

Multiplexers and Demultiplexers

115

6.4.1

Multiplexers

115

6.4.2

Demultiplexers

118

6.4.3

Multiplexer and Demultiplexer ICs

118

6.5

Input Devices

118

6.5.1

Switches

118

6.5.2

Switch Contact Bounce

120

6.5.3

Keypads

121

6.6 Output Devices

122

6.6.1

Seven-segment LED

122

6.6.2

Liquid Crystal Displays

124

6.6.3

LCD Technologies

125

Chapter 7 - The Microchip PIC

129

7.0

The PICMicro Microcontroller

129

7.0.1 Programming the PIC

130

PIC Programmers

131

Development Boards

131

7.0.2

Prototyping the PIC Circuit

132

7.1

PIC Architecture

134

7.1.1

Baseline PIC Family

134

PIC10 Devices

135

PIC12 Devices

135

PIC14 Devices

138

7.1.2

Mid-range PIC Family

138

PIC16 Devices

139

7.1.3

High-Performance PIC Family

139

PIC18 Devices

139

Chapter 8 - Mid-range PIC Architecture

141

8.0

Processor Architecture and Design

142

8.0.1

Harvard Architecture

142

8.0.2 RISC CPU Design

143

8.0.3

Single-word Instructions

143

8.0.4

Instruction Format

144

8.0.5

Mid-Range Device Versions

145

8.1

The Mid-range Core Features

145

8.1.1

Oscillator

145

8.1.2

System Reset

147

8.1.3

Interrupts

148

8.2 Mid-Range CPU and Instruction Set

149


Contents

ix

8.2.1

Mid-Range Instruction Set

149

8.2.2 STATUS and OPTION Registers

151

8.3 EEPROM Data Storage

153

8.3.1 EEPROM in Mid-Range PICs

153

8.4

Data Memory Organization

154

8.4.1

The w Register

154

8.4.2

The Data Registers

154

Memory Banks

154

The SFRs

155

The GPRs

157

8.4.3

Indirect Addressing

158

8.5

Mid-range I/O and Peripheral Modules

158

8.5.1

I/O Ports

159

8.5.2

Timer Modules

160

8.5.3

Capture-and-Compare Module

160

8.5.4

Master Synchronous Serial Port (MSSP) Module

161

8.5.5 USART Module

161

8.5.6

A/D Module

161

Chapter 9 - PIC Programming: Tools and Techniques

163

9.0

Microchip’s MPLAB

163

9.0.1 Embedded Systems

164

9.1

Integrated Development Environment

165

9.1.1

Installing MPLAB

165

9.1.2

Creating the Project

167

9.1.3

Project Build Options

169

9.1.4

Building the Project

169

9.2

Simulators and Debuggers

170

9.2.1 MPLAB SIM

171

9.2.2 MPLAB Hardware Debuggers

172

9.2.3

A “Quick-and-Dirty” Debugger

174

9.3 Programmers

174

9.4

Engineering PIC Software

175

9.4.1 Using Program Comments

176

Program Header

176

Commented Banners

177

Commented Bitmaps

178

9.4.2

Defining Data Elements

179

The cblock Directive

179

9.4.3

Banking Techniques

180

The banksel Directive

180

Bank Selection Macros

180

Deprecated Banking Instructions

181

9.4.4

Processor and Configuration Controls

182

Configuration Bits

182

9.4.5

Naming Conventions

184

9.4.6

Errorlevel Directive

186

9.5 Pseudo Instructions

186