Microcontroller Theory and Applications with the PIC18F - M. Rafiquzzaman - E-Book

Microcontroller Theory and Applications with the PIC18F E-Book

M. Rafiquzzaman

0,0
108,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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.

Sie lesen das E-Book in den Legimi-Apps auf:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 796

Veröffentlichungsjahr: 2025

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

Cover

Table of Contents

Title Page

Copyright Page

Dedication Page

Preface

About the Companion Website

1 Introduction to Microcontrollers

1.1 Explanation of Terms

1.2 Microcontroller Data Types

1.3 Evolution of the Microcontroller

1.4 Embedded Controllers

2 Microcontroller Basics

2.1 Basic Blocks of a Microcomputer

2.2 Microcontroller Architectures

2.3 Central Processing Unit (CPU)

2.4 Basic Concept of Pipelining

2.5 RISC Versus CISC

2.6 Functional Representation of a Typical Microcontroller – The PIC18F4321

Questions and Problems

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

3.1 Introduction to Microcontroller Memory

3.2 Microcontroller Input/Output (I/O)

Questions and Problems

4 Programming Languages

4.1 Computer Programming Languages

4.2 Machine Language

4.3 Assembly Language

4.4 High‐Level Language

4.5 Introduction to C Language

4.6 Choosing a Programming Language

4.7 Flowcharts

Questions and Problems

5 PIC18F Architecture and Addressing Modes

5.1 Basic Features of the PIC18F Family

5.2 PIC18F Register Architecture

5.3 PIC18F Memory Organization

5.4 PIC18F Addressing Modes

Questions and Problems

6 Assembly Language Programming with the PIC18F: Part 1

6.1 Introduction to the PIC18F MPLAB Assembler

6.2 PIC18F Instruction Format

6.3 PIC18F Instruction Set

Questions and Problems

7 Assembly Language Programming with the PIC18F: Part 2

7.1 PIC18F Jump/Branch Instructions

7.2 PIC18F Table Read/Write Instructions

7.3 PIC18F Subroutine Instructions

7.4 PIC18F System Control Instructions

7.5 PIC18F Hardware Versus Software Stack

7.6 Multiplication and Division Algorithms

7.7 Advanced Programming Examples

7.8 PIC18F Delay Routine

Questions and Problems

8 PIC18F Programmed I/O Using Assembly & C

8.1 PIC18F Pins and Signals

8.2 PIC18F4321 Programmed I/O

Questions and Problems

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

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

9.2 PIC18F Interrupts

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

9.4 Interfacing PIC18F4321 to a Hexadecimal Keyboard and a Seven‐Segment Display

Questions and Problems

10 PIC18F Timers and Analog Interface

10.1 PIC18F Timers

10.2 Analog Interface

Questions and Problems

11 PIC18F CCP and Serial I/O

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

11.2 DC Motor Control

11.3 Serial Interface

11.4 PIC18F Serial I/O

Questions and Problems

Appendix A: Answers to Selected Problems

Appendix B: Glossary

Appendix C: PIC18F Instruction Set (Alphabetical Order)

Appendix D: PIC18F Instruction Set – Details

D.0 Instruction Set Summary

D.1 Standard Instruction Set

Appendix E: PIC18F4321 Special Function Registers

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

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

Appendix H: Interfacing the PIC18F4321 to a Personal Computer or Laptop Using PICkit

TM

4

H.1 Initial Hardware Setup for the PIC18F4321

H.2 Connecting the Personal Computer (PC) or the Laptop to the PIC18F4321 via PICkit4

H.3 Programming the PIC18F4321 from a Personal Computer or a Laptop Using the PICkit4

Bibliography

Index

End User License Agreement

List of Tables

Chapter 1

Table 1.1 Comparison of Typical Features of Basic Microcontrollers.

Chapter 3

Table 3.1 Truth Table for Controlling SRAM Unit.

Table 3.2 Address Map of the Memory Organization of Figure 3.8.

Table 3.3 Decoding Guide.

Table 3.4 Address Map of the Memory Organization of Figure 3.9.

Table 3.5 Current and Voltage Requirements of LEDs.

Table P3.12 Table for Problem 3.12.

Table P3.13 Table for Problem 3.13.

Chapter 4

Table 4.1 Conversion of PIC18F SLEEP Instruction into its Binary Op‐Code.

Table 4.2 Typical Logic/Arithmetic and Shift/ Rotate Operations.

Table 4.3 Bit Manipulation Operations in C.

Table 4.4 Flowchart Symbols.

Chapter 5

Table 5.1 Basic Differences of Two Popular Members of the PIC18F Family (F ...

Table 5.2 Selected Special Function Registers (SFRs).

Chapter 6

Table 6.1 Conversion of SLEEP Instruction into its Binary Op‐Code.

Table 6.2 General Format for Instructions.

Table 6.3 PIC18F Instructions and the Status Flags.

Table 6.4 PIC18F Data Movement Instructions.

Table 6.5 Results for Example 6.1 (All Numbers in Hex).

Table 6.6 PIC18F Arithmetic Instructions.

Table 6.7 PIC18F Logic Instructions.

Table 6.8 PIC18F Rotate Instructions.

Table 6.9 Bit Manipulation Instructions.

Table 6.10 PIC18F Test, Compare, and Skip Instructions.

Chapter 7

Table 7.1 PIC18F Jump/Branch Instructions.

Table 7.2 PIC18F Table Read/Write Instructions.

Table 7.3 PIC18F Subroutine Instructions.

Table 7.4 PIC18F System Control Instructions.

Chapter 8

Table 8.1 PIC18F4321 Pinout Description.

Table 8.2 PIC18F4321 I/O PORTS, TRISx Registers, Along with Addresses (Upon ...

Chapter 9

Table 9.1 PIC18F4321 External Interrupts Along with Interrupt Priority (IP)...

Table 9.2 Typical LCD Commands Along with 8‐Bit Codes in Hex.

Chapter 10

Table 10.1 PIC18F4321 Timers with Basic Features.

Chapter 11

Table 11.1 Assignment of Timers and Flags for the PIC18F4321 CCP1 Module.

Appendix D

Table D.1 OPCODE Field Descriptions.

Table D.2 PIC18FXXXX Instruction Set.

Appendix E

Table E.1 Special Function Register Map for PIC18F4321 Family Devices.

List of Illustrations

Chapter 1

Figure 1.1 Furnace Temperature Control.

Chapter 2

Figure 2.1 Basic blocks of a microcomputer.

Figure 2.2 Simplified version of a typical microcomputer.

Figure 2.3 Typical clock signal.

Figure 2.4 von Neumann architecture.

Figure 2.5 Harvard architecture.

Figure 2.6 CPU with the main functional elements.

Figure 2.7 Addition program with initial register and memory contents.

Figure 2.8 Addition program (modified during execution).

Figure 2.9 Addition program (modified during execution).

Figure 2.10 Addition program (modified during execution).

Figure 2.11 Addition program (modified during execution).

Figure 2.12 PUSH operation when accessing a stack from the bottom.

Figure 2.13 POP operation when accessing a stack from the bottom.

Figure 2.14 PUSH operation when accessing a stack from the top.

Figure 2.15 POP operation when accessing a stack from the top.

Figure 2.16 An example of PIC18F4321 hardware stack with arbitrary data.

Figure 2.17 An example of PIC18F4321 hardware stack before PUSH operation.

Figure 2.18 PIC18F4321 hardware stack after PUSH operation.

Figure 2.19 Four‐segment pipeline.

Figure 2.20 Overlapped execution of four tasks using a pipeline.

Figure 2.21 PIC18F4321 block diagram.

Chapter 3

Figure 3.1 PIC18F data memory.

Figure 3.2 PIC18F4321 data memory divided into 25,6

10

banks with low 4 bits ...

Figure 3.3 Summary of available semiconductor memories for microcontroller s...

Figure 3.4 Typical instruction fetch timing diagram for an 8‐bit microcontro...

Figure 3.5 Typical memory READ timing diagram.

Figure 3.6 Typical memory WRITE timing diagram.

Figure 3.7 Pertinent signals of a typical CPU required for main memory inter...

Figure 3.8 Typical 1k × 8 SRAM unit.

Figure 3.9 CPU connected to 4k SRAM using the linear select decoding techniq...

Figure 3.10 Interconnecting a CPU with a 4k RAM using full decoded memory ad...

Figure 3.11 Two open‐collector outputs A and B tied together.

Figure 3.12 TTL totem‐pole output.

Figure 3.13 Typical switch for a microcontroller’s input.

Figure 3.14 Interfacing LED to PIC18F.

Figure 3.15 A seven‐segment display.

Figure 3.16 I/O port with the corresponding data‐direction register

.

Figure 3.17 Example illustrating conditional or polled I/O.

Figure 3.18 Example illustrating interrupt I/O.

Figure P3.10 Figure for Problem 3.10.

Figure P3.11 Figure for Problem 3.11.

Chapter 4

Figure 4.1 Translating assembly or high‐level language into binary machine l...

Figure 4.2 Logical right‐shift operation.

Figure 4.3 True arithmetic right‐shift operation.

Figure 4.4 The

if‐else

construct.

Figure 4.5 The

while

construct.

Figure 4.6 Flowchart for the

for

loop.

Figure 4.7 Figure for the

do‐while

construct.

Figure 4.8 Example of space saving with union.

Chapter 5

Figure 5.1 Clock/instruction cycle.

Figure 5.2 Instruction pipeline flow.

Figure 5.3 PIC18F registers.

Figure 5.4 SP (Stack Pointer).

Figure 5.5 SR (Status Register).

Figure 5.6 PIC18F4321 program memory map.

Figure 5.7 PIC18F4321 data memory map.

Figure 5.8 Illustration of the indirect addressing mode.

Figure 5.9 Illustration of indirect with postincrement mode.

Figure 5.10 Instruction sequence for illustrating the postincrement mode.

Figure 5.11 Illustration of postdecrement mode.

Figure 5.12 Instruction sequence for illustrating postdecrement mode.

Figure 5.13 Illustration of Indirect with 8‐bit indexed mode.

Chapter 6

Figure P6.13 Figure for Problem 6.13.

Chapter 7

Figure 7.1 Table read operation (instruction TBLRD*).

Figure 7.2 Table write operation (instruction TBLWT*).

Figure 7.3(a) PIC18F hardware stack with Arbitrary Data Before Execution of ...

Figure 7.3(b) PIC18F Hardware Stack with Arbitrary Data After Execution of t...

Figure 7.4(a) PIC18F Hardware Stack with Arbitrary Data Before the Execution...

Figure 7.4(b) PIC18F Hardware Stack with Arbitrary Data After the Execution o...

Figure 7.5 (a) PIC18F Hardware Stack with Arbitrary Data Before Execution of...

Figure 7.5 (b) PIC18F Hardware Stack with Arbitrary Data After the Execution...

Figure 7.6(a) PIC18F Software Stack with Arbitrary Data Before Accessing Sta...

Figure 7.6(b) PIC18F Software Stack with Arbitrary Data After Accessing Stac...

Figure 7.7(a) PIC18F Software Stack with Arbitrary Data Growing from HIGH to...

Figure 7.7(b) PIC18F Software Stack with Arbitrary Data Growing from HIGH to...

Chapter 8

Figure 8.1 PIC18F4321 pins and signals.

Figure 8.2 OSCCON (Oscillator Control) register.

Figure 8.3 Typical crystal oscillator circuit.

Figure 8.4 RC oscillator.

Figure 8.5 PIC18F manual reset circuit.

Figure 8.6 RCON (RESET CONTROL) register.

Figure 8.7 Simplified PIC18F4321 setup.

Figure 8.8 I/O port with the corresponding data direction register.

Figure 8.9 Generic I/O port operation (simplified).

Figure 8.10 PORT D Along with TRISD.

Figure 8.11 ADCON1 register for digital I/O.

Figure 8.12 Interfacing LED to PI18F.

Figure 8.13 A seven‐segment display.

Figure 8.14 Seven‐segment display configurations.

Figure 8.15 PIC18F4321 interface to a common cathode seven‐segment display v...

Figure 8.16 Figure for Example 8.1.

Figure 8.17 Figure for Problem 8.2.

Figure 8.18 Figure for Example 8.3.

Figure 8.19 Figure for Example 8.6.

Figure P8.10 Figure for Problem 8.10.

Figure P8.11 Figure for Problem 8.11.

Figure P8.12 Figure for Problem 8.12.

Figure P8.17 Figure for Problem 8.17.

Chapter 9

Figure 9.1 Example illustrating conditional or polled I/O.

Figure 9.2 Example illustrating interrupt I/O.

Figure 9.3 Figure for Example 9.2.

Figure 9.4 PIC18F interrupts.

Figure 9.5 INTCON (interrupt control) and INTCON3 (interrupt control 3) regi...

Figure 9.6 Simplified schematic for the PIC18F external interrupts for power...

Figure 9.7 INTCON2 register.

Figure 9.8 Figure for Example 9.2.

Figure 9.9 Figure for Example 9.3.

Figure 9.10 Interrupt‐driven on‐chip peripheral device such as the ADC.

Figure 9.11 Hitachi HD44780 LCD and the pinout.

Figure 9.12 Figure for Example 9.4.

Figure 9.13 A 2 × 2 keyboard interfaced to the PIC18F4321.

Figure 9.14 A row of four displays.

Figure 9.15 Nonmultiplexed hexadecimal displays.

Figure 9.16 Multiplexed hexadecimal displays.

Figure 9.17 PIC18F4321 interface to keyboard and display for the assembly pr...

Figure 9.18 74LS47 display for hex digits 0–F.

Figure 9.19 PIC18F4321 interface to keyboard and display for the C‐program....

Figure P9.9 Figure for Problem 9.9.

Figure P9.11 Figure for Problem 9.11.

Figure P9.15 Figure for Problem 9.15.

Figure P9.16 Figure for Problem 9.16.

Figure P9.17 Figure for Problem 9.17.

Chapter 10

Figure 10.1 Timer0 interrupt.

Figure 10.2 T0CON (TIMER0 Control) Register.

Figure 10.3 TIMER0 block diagram (8‐bit mode).

Figure 10.4 TIMER0 block diagram (16‐bit mode).

Figure 10.5 INTCON register with the TMR0IE and TMR0IF bits.

Figure 10.6 T0CON Register with Binary Data 11010100.

Figure 10.7 T1CON (Timer1 Control) Register.

Figure 10.8 PIR1 (peripheral interrupt request) register 1.

Figure 10.9 PIE1 (peripheral interrupt enable) register 1.

Figure 10.10 T2CON (Timer2 control) register.

Figure 10.11 T3CON (Timer3 control) register.

Figure 10.12 PIR2 (peripheral interrupt request) register 2.

Figure 10.13 PIE2 (peripheral interrupt enable) register 2.

Figure 10.14 Figure for Example 10.10.

Figure 10.15 Figure for Example 10.11.

Figure 10.16 ADCON0 (ADC control register 0).

Figure 10.17 ADCON1 (ADC control register 1).

Figure 10.18 ADCON2 (ADC control register 2).

Figure 10.19 Block diagram of the PIC18F4321 A/D.

Figure 10.20 Interrupt‐driven ADC.

Figure 10.21 Figure for Example 10.12.

Figure 10.22 Figure for Example 10.13.

Figure P10.9 Figure for Problem 10.9.

Figure P10.10 Figure for Problem 10.10.

Figure P10.11 Figure for Problem 10.11.

Chapter 11

Figure 11.1 CCPxCON register.

Figure 11.2 CCP1 waveform in PWM mode.

Figure 11.3

(a)

Figure for Example 11.5.

Figure 11.3

(b)

Circuit to be used instead of the Optocoupler.

Figure 11.4 PIC18F Master interface to a single slave PIC18F.

Figure 11.5 SPI Master/Slave interface between two PIC18F4321’s along with r...

Figure 11.6 SSPCON1 (MSSP CONTROL) register 1 in SPI mode.

Figure 11.7 SSPSTAT (MSSP status register) in SPI mode.

Figure 11.8 Figure for Example 11.6 using SPI mode.

Figure 11.9 Master–slave interface in I

2

C mode.

Figure 11.10 I

2

C write transmission sequence.

Figure 11.11 MSSP block diagram (I

2

C).

Figure 11.12 SSPSTAT: MSSP Status Register (I

2

C™ mode).

Figure 11.13 SSPCON1: MSSP Control Register 1 (I2C™ mode).

Figure 11.14 SSPCON2: MSSP Control Register 2 (I2C™ mode).

Figure 11.15 SSPADD: MSSP address register.

Figure 11.16 Figure for Example 11.7.

Figure P11.8 Figure for Problem 11.8.

Figure P11.9

Appendix D

Figure D.1 General Format for Instructions.

Appendix H

Figure H.1 Initial set up for the PIC18F4321.

Figure H.2 PIC18F4321 computer interface using the PICkit™ 4.

Figure H.3 Pictorial view of the breadboard implementation.

Figure H.4 Pictorial view of connecting the PICkit™ 4 to the USB port.

Figure H.5 Connecting the PICkit™ 4 to the breadboard.

Guide

Cover Page

Table of Contents

Title Page

Copyright Page

Dedication Page

Preface

About the Companion Website

Begin Reading

Appendix A Answers to Selected Problems

Appendix B Glossary

Appendix C PIC18F Instruction Set (Alphabetical Order)

Appendix D DPIC18F Instruction Set – Details

Appendix E PIC18F4321 Special Function Registers

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

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

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

Bibliography

Index

WILEY END USER LICENSE AGREEMENT

Pages

iii

iv

v

xiii

xiv

xv

xvi

xvii

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

193

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

262

263

264

265

266

267

268

269

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

314

315

316

317

318

319

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

Microcontroller Theory and Applications with the PIC18F

Third Edition

M. Rafiquzzaman, Ph.D., PE

Professor EmeritusElectrical and Computer EngineeringCalifornia State Polytechnic UniversityPomona, California, USA

and

PresidentRafi Systems, Inc.Corona, California, USA

Copyright © 2025 by John Wiley & Sons, Inc. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies.

Published by John Wiley & Sons, Inc., Hoboken, New Jersey.Published simultaneously in Canada.

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, scanning, or otherwise, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per‐copy fee to the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750‐8400, fax (978) 750‐4470, or on the web at www.copyright.com. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748‐6011, fax (201) 748‐6008, or online at http://www.wiley.com/go/permission.

Trademarks: Wiley and the Wiley logo are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates in the United States and other countries and may not be used without written permission. All other trademarks are the property of their respective owners. John Wiley & Sons, Inc. is not associated with any product or vendor mentioned in this book.

Limit of Liability/Disclaimer of Warranty: While the publisher and author have used their best efforts in preparing this book, they make no representations or warranties with respect to the accuracy or completeness of the contents of this book and specifically disclaim any implied warranties of merchantability or fitness for a particular purpose. No warranty may be created or extended by sales representatives or written sales materials. The advice and strategies contained herein may not be suitable for your situation. You should consult with a professional where appropriate. Further, readers should be aware that websites listed in this work may have changed or disappeared between when this work was written and when it is read. Neither the publisher nor authors shall be liable for any loss of profit or any other commercial damages, including but not limited to special, incidental, consequential, or other damages.

For general information on our other products and services or for technical support, please contact our Customer Care Department within the United States at (800) 762‐2974, outside the United States at (317) 572‐3993 or fax (317) 572‐4002.

Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic formats. For more information about Wiley products, visit our web site at www.wiley.com.

Library of Congress Cataloging‐in‐Publication DataNames: Rafiquzzaman, Mohamed, author.Title: Microcontroller theory and applications with the PIC18F / M. Rafiquzzaman.Description: Third edition. | Hoboken, New Jersey : Wiley, [2025] | Includes bibliographical references and index.Identifiers: LCCN 2024050344 (print) | LCCN 2024050345 (ebook) | ISBN 9781394318230 (hardback) | ISBN 9781394318254 (adobe pdf) | ISBN 9781394318247 (epub)Subjects: LCSH: Microcontrollers. | PIC microcontrollers. | C++ (Computer program language) | Assembly languages (Electronic computers)Classification: LCC TK7895.E42 R34 2025 (print) | LCC TK7895.E42 (ebook) | DDC 006.2/2–dc23/eng/20241214LC record available at https://lccn.loc.gov/2024050344LC ebook record available at https://lccn.loc.gov/2024050345

Cover Design: WileyCover Image: © KKulikov/Shutterstock

To my wife, Kusum; and our grandsons (triplets), Ayaaz, Adyan, and Safir

Preface

Microcontrollers play a vital role in the design of digital systems. Microcontrollers evolved from single‐chip microcomputers during the 70s. The microcomputer contains CPU (central processing unit), memory, and I/O (input/output) on a single chip. Microcontrollers, on the other hand, typically contain CPU, memory, I/O, ADC (analog‐to‐digital converter), DAC (digital‐to‐analog Converter), hardware timers, and serial I/O on a single chip.

Intel Corporation is generally acknowledged as the company that introduced the first microprocessor (CPU on a chip) in the marketplace in 1971. Single‐chip microcomputers such as the Intel 8048 evolved during the late 70s. Soon afterward, based on the concept of single‐chip microcomputers, Intel introduced the first 8‐bit microcontroller: the Intel 8051. The microcontrollers became popular during the 80s. Microcontrollers are extensively used in embedded systems and are also called embedded controllers.

As an example of an embedded controller, consider a personal computer interfaced to a printer as the host. The microcontroller hidden inside the printer is the “embedded controller.” The purpose of the microcontroller, in this case, is to input data from the host and print it. Thus, an embedded controller typically performs only one task (printing in this case). Typical microcontroller applications include automobile engine control systems, implantable medical devices, remote controls, office machines, appliances, power tools, toys, and other embedded systems. Although Intel developed the first microcontroller, Intel is not that active in manufacturing contemporary microcontrollers. Typical microcontroller manufacturers include Microchip Technology, Texas Instruments, Arduino, and ARM.

Microchip Technology manufactures PIC18F 8‐bit microcontroller chips. The PIC18F is a very basic microcontroller chip. The PIC18F family is popular with both industrial developers and hobbyists due to their low cost, wide availability, and availability of low cost or free development tools. PIC18F microcontrollers are typically used for industrial automation, home automation, embedded systems, robotics, instrumentation, and control systems, education, and training.

Arduino (an Italian‐based company) offers licensing for microcontroller manufacturers to provide single boards with their microcontrollers. Arduino‐based microcontrollers are very popular in industries because they are easy to use and have strong features and tools that make them attractive for many applications. An example of a simple Arduino board consists of a Microchip/Atmel 8‐bit microcontroller, large amount of flash memory, serial I/O, and other features. Arduino is extensively used in home automation systems. Arduino boards, combined with sensors, actuators, and wireless connectivity, allow homeowners to control various aspects including lighting and temperature control, and automated security systems.

ARM (Advanced RISC Machine) is a UK‐based company that makes Reduced Instruction Set Computer (RISC)‐based designs and licenses to chip manufacturers. ARM does not actually manufacture any chip themselves. Instead, they design the architecture and cores of a processor and then partner companies like Apple, ST Microelectronics, and NXP who license those designs to build their own chips. ARM offers 32‐bit and 64‐bit CPU core. Due to their low costs, low power consumption, and low heat generation, ARM processors are useful for low‐powered devices such as smartphones, laptops, tablet computers, and embedded systems.

The PIC18F family continues to be popular. It provides a basic understanding of a typical and a simple microcontroller from chip level. Based upon over 50 years of industrial and academic experiences, the author feels that the PIC18F family is an excellent and powerful educational tool for a first course in microcontrollers from chip level. Arduino and ARM‐based microcontrollers can be covered in follow‐up courses or in senior projects.

The third edition of this book is written to enhance the coverage of the second edition and will present the fundamental concepts of assembly and C language programming and interfacing techniques associated with the Microchip’s PIC18F4321 microcontroller. The author believes in the basics. Hence, simple I/O devices such as switches and LEDs are used to illustrate I/O techniques. This is very important for the average students and beginners. Design of PIC18F‐based Digital DC voltmeter and then interfacing the PIC18F to DC motor using both assembly and C languages are provided. Several chapters of the second edition are re‐written, and new end‐of‐chapter problems will be included.

In the third edition, the first part of the book will contain theory of microcontrollers as in the second edition. However, several topics including microcontroller basics such as stack, memory banks, access bank, and programmed and interrupt I/O are clarified and enhanced. These concepts are then related to the PIC18F4321 in the second part of the book.

The PIC18F uses Harvard architecture with a RISC‐based CPU. Conventional CPUs complete fetch, decode, and execute cycles of an instruction in sequence. However, the PIC18F uses pipelining in which instruction fetch and execute cycles are overlapped. This speeds up instruction execution time of the PIC18F. Furthermore, coverage of CPU architectures, RISC vs. CISC, pipelining, assembly and C language programming, and I/O techniques associated with typical microcontrollers is presented in a more simplified manner in the first part of the book. These topics are then related to the PIC18F4321 in the second part of the book.

Several assembly and C language programs along with I/O examples are included using Microchip’s newer versions of MPLAB X assembler, XC8 C‐compiler, and PICKit4 module (for interfacing the PIC18F4321to a Personal Computer). These newer versions of the PIC18F assembler, C‐compiler, and PICKit4 interface module are used in developing the hardware and software in the third edition. One can build a PIC18F‐based system on a breadboard using one of the PIC18F devices such as the PIC18F4321. The designer can download the assembled or compiled programs using PICKit4 from the personal computer to the PIC18F4321 on the breadboard, and then perform meaningful experiments. This is the most inexpensive way for the students or beginners to implement experiments using a typical microcontroller chip such as the PIC18F4321.

Like the second edition, the third edition is also self‐contained and includes several basic topics that are updated and more clarified from the previous edition. 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 to more advanced devices such as LCD displays., ADC (analog to digital converter), and DAC (digital to analog converter). Topics such as PWM (pulse width modulation), and Serial I/O are also included. These topics are presented in a more simplified manner in the third edition.

The book has evolved from the author’s 50+ years of industrial experience with microprocessors and microcontrollers, and from classroom notes for the microcontroller course taught at the Electrical and Computer Engineering Department, California State Polytechnic University, Pomona: ECE 3301 (Microcontroller Applications).

The text is divided into 11 chapters in the third edition. Most of these chapters are enhanced with more explanations along with examples and more end‐of‐chapter problems.

Chapter 1 is provided with a review of terminologies, number systems, and evolution of microcontrollers. Finally, a comparison of the basic features of a few popular microcontrollers comparable to the PIC18F along with typical microcontroller applications are included. Some of these topics such as state‐of‐the‐art in microcontroller applications and comparison of basic features of typical microcontroller chips are re‐written in this edition.

Chapters 2 through 9 form the nucleus of the book. Chapter 2 covers typical microcontroller architectures. The concepts of CPU architecture, program and data memory units, pipelining, and RISC vs. CISC are included. Certain topics such as the stack and bank memory are simplified and explained with more examples.

Chapter 3 is focused on the memory organization and I/O (input/output) techniques associated with typical microcontrollers. Topics such as main memory array design, including memory maps, are also covered. Typical microcontroller input/output techniques including programmed I/O and interrupt I/O are also included. In the third edition, microcontroller I/O techniques are explained with more examples in a simplified manner.

Chapter 4 contains programming concepts associated with typical microcontrollers. Topics include machine, assembly and C language programming, typical addressing modes, and instruction sets. More explanations are provided in the third edition relating them to the PIC18F MPLAB X assembler/debugger), and XC8 C‐compiler (newer versions than MPLAB and C18 in the second edition).

The theory of assembly and C programming languages and I/O techniques covered in Chapter 4 is demonstrated in Chapters 5 through 7 by means of a typical 8‐bit microcontroller. A specific device from the PIC18F family such as the PIC18F4321 as in the second edition is used to illustrate the concepts.

The I/O techniques covered in Chapter 3 are demonstrated in Chapters 8 and 9 using the PIC18F4321. Several I/O examples of PIC18F assembly and C using the PIC18F4321 are also included. These chapters also demonstrate how the software and hardware work together by interfacing simple I/O devices such as switches, LEDs, seven‐segment displays, and LCD’s (liquid crystal displays). Both PIC18F programmed L/O and interrupt I/O using assembly and C are used for this purpose. Interfacing techniques associated with these simple I/O devices are rewritten in the third edition to clarify the concepts to average students and beginners.

Chapter 10 covers PIC18F on‐chip hardware timers along with the analog module that includes on‐chip ADC and external DAC. The purpose of pre‐scalers and post‐scalers of the on‐chip hardware timers are clarified in a very simplified manner with examples. Furthermore, examples are re‐written in the third edition to clarify certain concepts associated with timing parameters of ADC acquisition time and ADC clock.

Chapter 11 contains PIC18F on‐chip hardware CCP (Compare/Capture/PWM) module along with Serial I/O. The concepts associated with waveform generation with a predefined duty cycle, along with DC motor interface with the PIC18F using the CCP module are rewritten and presented more clearly in this edition.

The book can easily be adopted as a text for one‐semester or one‐quarter first course in microcontroller taught at the undergraduate level in electrical/computer engineering and computer science departments. The students are expected to have a background in digital logic (both combinational and sequential logic design).

The book will also be useful for practicing microcontroller system designers. Practitioners of microcontroller‐based applications will find more simplified explanations, together with examples and comparison considerations, than are found in manufacturers’ manuals.

The author expresses his sincere appreciation to his student and Teaching Assistant, Bianca Chavez of California State Polytechnic University, for making some constructive suggestions. The author is grateful to CJ Media of California for preparing the manuscript. The author is also indebted to Mary Vang of Rafi System Inc. for editing and correcting several sections of the manuscript. Finally, the author wishes to express his sincere appreciation to his editor, Brett Kurzman of Wiley, USA, for his personal commitment, dedication, and excellent job in bringing the book to publication.

M. RafiquzzamanPomona, California

About the Companion Website

This book is accompanied by a companion website:

www.wiley.com/go/Microcontroller_Theory_and_Applications_with_the_PIC18F/3e

The Instructor Companion Site includes:

Lecture Slides