+353-1-416-8900REST OF WORLD
+44-20-3973-8888REST OF WORLD
1-917-300-0470EAST COAST U.S
1-800-526-8630U.S. (TOLL FREE)

Microcontroller Theory and Applications with the PIC18F. Edition No. 3

  • Book

  • 496 Pages
  • December 2024
  • John Wiley and Sons Ltd
  • ID: 6006522

Straightforward and comprehensive textbook on programming and interfacing techniques for the PIC18F4321 microcontroller, supported by hundreds of illustrations throughout

Microcontroller Theory and Applications with the PIC18F presents core information on the theory of microcontrollers and the fundamental concepts of assembly and C language programming and interfacing techniques associated with the Microchip's PIC18F4321 microcontroller. Characteristics and principles common to typical microcontrollers are emphasized, and basic microcontroller interfacing techniques are demonstrated via examples using the simplest possible devices such as switches, LEDs, Seven-Segment Displays, and the hexadecimal keyboard. In addition, interfacing the PIC18F with other devices such as LCD displays, ADC, DAC is also included. Furthermore, topics such as CCP (Capture, Compare, PWM) and Serial I/O using assembly and C languages along with simple examples are also provided.

Information on the design of the PIC18F-based digital DC voltmeter and interfacing the PIC18F with PWM (Pulse Width Modulation) mode to a DC motor using both assembly and C languages is provided. Finally, PIC18F Serial I/O examples using both SPI and I2C modes are also included. All these examples are illustrated by means of successful implementations in the laboratory.

Building on the success of previous editions, this Third Edition has been extensively revised to include enhanced clarity in each chapter and additional illustrations, end-of-chapter problems, and examples. Certain concepts such as stack, bank-memory, programmed I/O, interrupt I/O, and CCP have been rewritten to better relate them to the PIC18F. Details on the MPLABX assembler/debugger and XC8 C-Compiler are now included as well.

Microcontroller Theory and Applications with the PIC18F includes information on:

  • Microcontroller data types, unsigned and signed binary numbers and ASCII code, unpacked and packed binary-coded-decimal numbers, and the evolution of the microcontroller
  • Provides guidelines on how to choose the right language (Assembly or C) for specific applications
  • PIC18F architecture and addressing modes, covering register architecture, memory organization, and program and data memories
  • Programming PIC18F programmed I/O, interrupt I/O, and interfacing PIC18F4321 to a hexadecimal keyboard and a seven-segment display
  • ADC, DAC, CCP, and Serial I/O interfacing techniques
Microcontroller Theory and Applications with the PIC18F is an essential learning resource for students in related programs of study seeking information on basic concepts relating to a specific and simple microcontroller such as the PIC18F in an organized and simplified manner.

Table of Contents

Preface xiii

About the Companion Website xvii

1 Introduction to Microcontrollers 1

1.1 Explanation of Terms 3

1.2 Microcontroller Data Types 7

1.2.1 Unsigned and Signed Binary Numbers 7

1.2.2 ASCII and EBCDIC Codes 9

1.2.3 Unpacked and Packed Binary-Coded-Decimal Numbers 10

1.3 Evolution of the Microcontroller 10

1.4 Embedded Controllers 14

2 Microcontroller Basics 15

2.1 Basic Blocks of a Microcomputer 15

2.1.1 System Bus 16

2.1.2 Clock Signals 17

2.2 Microcontroller Architectures 18

2.3 Central Processing Unit (CPU) 19

2.3.1 Register Section 19

2.3.2 Control Unit 28

2.3.3 Arithmetic and Logic Unit (ALU) 29

2.3.4 Simplified Explanation of Control Unit Design 29

2.4 Basic Concept of Pipelining 30

2.5 RISC Versus CISC 32

2.6 Functional Representation of a Typical Microcontroller - The PIC18F 4321 33

Questions and Problems 34

3 Microcontroller Memory and Input/Output (I/O) 37

3.1 Introduction to Microcontroller Memory 37

3.1.1 Main Memory 38

3.1.2 READ and WRITE Timing Diagrams 40

3.1.3 Main Memory Organization 42

3.2 Microcontroller Input/Output (I/O) 45

3.2.1 Overview of Digital Output Circuits 47

3.2.2 Simple I/O Devices 49

3.2.3 Programmed I/O 51

3.2.4 Unconditional and Conditional Programmed I/O 52

3.2.5 Interrupt I/O 53

Questions and Problems 56

4 Programming Languages 61

4.1 Computer Programming Languages 61

4.2 Machine Language 62

4.3 Assembly Language 63

4.3.1 Types of Assemblers 64

4.3.2 Assembler Delimiters 65

4.3.3 Specifying Numbers by Typical Assemblers 65

4.3.4 Assembler Directives or Pseudoinstructions 65

4.3.5 Assembly Language Instruction Formats 67

4.3.6 Typical Instruction Set 69

4.3.7 Typical Addressing Modes 75

4.3.8 Subroutine Calls in Assembly Language 76

4.4 High-Level Language 77

4.5 Introduction to C Language 78

4.5.1 Data Types 81

4.5.2 Bit Manipulation Operators 81

4.5.3 Control Structures 83

4.5.4 The if-else Construct 83

4.5.5 The switch Construct 84

4.5.6 The while Construct 85

4.5.7 The for Construct 86

4.5.8 The do-while Construct 87

4.5.9 Structures and Unions 87

4.5.10 Functions in c 88

4.5.11 Arrays 89

4.5.12 Macros 89

4.6 Choosing a Programming Language 90

4.7 Flowcharts 90

Questions and Problems 91

5 PIC18F Architecture and Addressing Modes 93

5.1 Basic Features of the PIC18F Family 93

5.2 PIC18F Register Architecture 96

5.3 PIC18F Memory Organization 99

5.3.1 PIC18F Program Memory 100

5.3.2 PIC18F Data Memory 100

5.4 PIC18F Addressing Modes 104

5.4.1 Literal or Immediate Addressing Mode 104

5.4.2 Inherent or Implied Addressing Mode 104

5.4.3 Direct or Absolute Addressing Mode 105

5.4.4 Indirect Addressing Mode 105

5.4.5 Relative Addressing Mode 111

5.4.6 Bit Addressing Mode 113

Questions and Problems 113

6 Assembly Language Programming with the PIC18F: Part 1 115

6.1 Introduction to the PIC18F MPLAB Assembler 115

6.2 PIC18F Instruction Format 119

6.3 PIC18F Instruction Set 121

6.3.1 Data Movement Instructions 123

6.3.2 Arithmetic Instructions 128

6.3.3 Logic Instructions 137

6.3.4 Rotate Instructions 140

6.3.5 Bit Manipulation Instructions 144

6.3.6 PIC18F Test, Compare, and Skip instructions 147

Questions and Problems 153

7 Assembly Language Programming with the PIC18F: Part 2 157

7.1 PIC18F Jump/Branch Instructions 157

7.2 PIC18F Table Read/Write Instructions 159

7.3 PIC18F Subroutine Instructions 163

7.4 PIC18F System Control Instructions 165

7.5 PIC18F Hardware Versus Software Stack 165

7.6 Multiplication and Division Algorithms 173

7.6.1 Signed Multiplication Algorithm 173

7.6.2 Unsigned Division Algorithm 175

7.6.3 Signed Division Algorithm 177

7.7 Advanced Programming Examples 179

7.8 PIC18F Delay Routine 183

Questions and Problems 185

8 PIC18F Programmed I/O Using Assembly & C 189

8.1 PIC18F Pins and Signals 189

8.1.1 Clock 194

8.1.2 PIC18F Reset 198

8.1.3 High-Voltage and Low-Voltage Programming (HVP and LVP) 200

8.1.4 “pragma config” and “config” Directives 200

8.1.5 A Simplified Setup for the PIC18F 4321 201

8.1.6 Downloading Programs into the PIC18F4321 Using the PICKit3 Interface 202

8.2 PIC18F4321 Programmed I/O 202

8.2.1 I/O Instructions in PIC18F Assembly 206

8.2.2 Configuring PIC18F4321 I/O Ports Using PIC18F Assembly 206

8.2.3 configuring PIc18F4321 I/O Ports Using c 208

8.2.4 Interfacing LEDs (Light-Emitting Diodes) and Seven-Segment Displays 210

8.2.5 Programmed I/O Examples Using PIC18F Assembly 211

8.2.6 Programmed I/O Examples Using C Language 215

Questions and Problems 222

9 PIC18F Interrupt I/O, LCD, and Keyboard Interfacing 227

9.1 Basics of Polled I/O Versus Interrupt I/O 227

9.1.1 Interrupt I/O 228

9.2 PIC18F Interrupts 233

9.2.1 Interrupt Procedure 233

9.2.2 PIC18F Interrupt Types 233

9.2.3 Programming the PIC18F External Interrupts 234

9.2.4 Programming PIC18F External Interrupts Using Assembly 239

9.2.5 Programming PIc18F External Interrupts Using c 240

9.2.6 Accessing PIC18F On-Chip Peripheral Devices Using Polled I/O Versus Interrupt I/O 249

9.3 PIC18F Interface to a Typical LCD (Liquid Crystal Display) 250

9.4 Interfacing PIC18F4321 to a Hexadecimal Keyboard and a Seven-Segment Display 257

9.4.1 Basics of Keyboard and Display Interface to a Microcontroller 257

9.4.2 PIC18F4321 Interface to a Hexadecimal Keyboard and a Seven-Segment Display 258

Questions and Problems 266

10 PIC18F Timers and Analog Interface 271

10.1 PIC18F Timers 271

10.1.1 Timer 0 273

10.1.2 Timer 1 282

10.1.3 Timer 2 288

10.1.4 Timer 3 292

10.2 Analog Interface 302

10.2.1 PIC18F On-Chip ADC (A/D Converter) 302

10.2.2 Interfacing an External D/A converter Using c 315

Questions and Problems 316

11 PIC18F CCP and Serial I/O 321

11.1 PIC18F CCP (Capture/Compare/PWM (Pulse Width Modulation)) Module 321

11.1.1 CCP Registers 322

11.1.2 CCP Modules and Associated Timers 322

11.1.3 PIC18F4321 Capture Mode 322

11.1.4 PIC18F4321 Compare Mode 326

11.1.5 PIC18F4321 PWM (Pulse Width Modulation) Mode 329

11.2 dc Motor Control 332

11.3 Serial Interface 336

11.3.1 Synchronous Serial Data Transmission 337

11.3.2 Asynchronous Serial Data Transmission 337

11.3.3 Basics of SPI and I2c 337

11.4 PIC18F Serial I/O 338

11.4.1 PIC18F SPI Mode 338

11.4.2 PIC18F I2C (Inter-integrated Circuit) Mode 348

Questions and Problems 358

Appendix A Answers to Selected Problems 361

Appendix B Glossary 369

Appendix C PIC18F Instruction Set (Alphabetical Order) 379

Appendix D PIC18F Instruction Set - Details 383

Appendix E PIC18F4321 Special Function Registers 429

Appendix F Tutorial for Assembling and Debugging a PIC18F Assembly Language Program Using the MPLAB X 431

Appendix G Tutorial for Compiling and Debugging a C-Program Using the Microchip’s XC8 Compiler 447

Appendix H Interfacing the PIC18F4321 to a Personal Computer or Laptop Using PICkitTM 4 463

Bibliography 469

Index 471

Authors

M. Rafiquzzaman California State Polytechnic University.