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

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

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

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

Добавлен: 14.06.2025

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

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

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

x Microcontroller Programming

Chapter 10 - Programming Essentials: Input and Output

189

10.0

16F84A Programming Template

189

10.1

Introducing the 16F84A

191

10.1.1

Template Circuit for 16F84A

191

10.1.2

Power Supplies

191

Voltage Regulator

192

10.1.3

Comparisons in PIC Programming

193

The Infamous PIC Carry Flag

194

10.2

Simple Circuits and Programs

194

10.2.1

A Single LED Circuit

194

LED Flasher Program

196

10.2.2

LED/Pushbutton Circuit

199

10.2.3

Multiple LED Circuit

202

10.3

Programming the Seven-segment LED

204

10.4

A Demonstration Board

206

10.4.1 PCB Images for Demo Board

206

10.4.2

TestDemo1 Program

208

Chapter 11 - Interrupts

211

11.0

Interrupts on the 16F84

211

11.0.1

The Interrupt Control Register

211

11.0.2

The OPTION Register

212

11.1

Interrupt Sources

213

11.1.1

Port-B External Interrupt

214

11.1.2

Timer0 Interrupt

214

11.1.3

Port-B Line Change Interrupt

215

Multiple External Interrupts

217

11.1.4

EEPROM Data Write Interrupt

217

11.2

Interrupt Handlers

217

11.2.1

Context Saving Operations

218

Saving w and STATUS Registers

218

11.3

Interrupt Programming

218

11.3.1

Programming the External Interrupt

219

RB0 Interrupt Initialization

220

RB0 Interrupt Service Routine

221

11.3.2

Wakeup from SLEEP Using the RB0 Interrupt

222

The SleepDemo Program

223

11.3.3

Port-B Bits 4-7 Status Change Interrupt

224

RB4-7 Interrupt Initialization

225

RB4-7 Change Interrupt Service Routine

227

11.4 Sample Programs

229

11.4.1

The RB0Int Program

229

11.4.2 The SleepDemo Program

232

11.4.3

The RB4to7Int Program

235

Chapter 12 - Timers and Counters

241

12.0 The 16F84 Timer0 Module

241

12.0.1 Timer0 Operation

241


Contents

xi

Timer0 Interrupt

242

Timer0 Prescaler

242

12.1

Delays Using Timer0

243

12.1.1

Long Delay Loops

244

How Accurate the Delay?

245

The Black-Ammerman Method

245

12.2

Timer0 as a Counter

246

12.3

Timer0 Programming

247

12.3.1

Programming a Counter

247

A Timer/Counter Test Circuit

248

The Tmr0Counter Program

248

12.3.2

Timer0 as a Simple Delay Timer

250

12.3.3

Measured Time Lapse

252

Interrupt-driven Timer

255

12.4 The Watchdog Timer

259

12.4.1

Watchdog Timer Programming

260

12.5 Sample Programs

260

12.5.1

The Tmr0Counter program

260

12.5.2

The Timer0 Program

263

12.5.3 The LapseTimer Program

265

12.5.4 The LapseTmrInt Program

269

Chapter 13 - LCD Interfacing and Programming

275

13.0

LCD Features and Architecture

275

13.0.1

LCD Functions and Components

276

Internal Registers

276

Busy Flag

276

Address Counter

276

Display Data RAM (DDRAM)

276

Character Generator ROM (CGROM)

276

Character Generator RAM (CGRAM)

277

Timing Generation Circuit

277

Liquid Crystal Display Driver Circuit

278

Cursor/Blink Control Circuit

278

13.0.2

Connectivity and Pin-Out

278

13.1

Interfacing with the HD44780

279

13.1.1

Busy Flag or Timed Delay Options

280

13.1.2

Contrast Control

281

13.1.3

Display Backlight

281

13.1.4

Display Memory Mapping

281

13.2

HD44780 Instruction Set

283

13.2.1

Instruction Set Overview

283

Clearing the Display

283

Return home

284

Entry mode set

284

Display and Cursor ON/OFF

284

Cursor/display shift

284

Function set

285

Set CGRAM address

285

Set DDRAM address

285

Read busy flag and Address register

285


xii

Microcontroller Programming

Write data

285

Read data

286

13.2.2

A 16F84 8-bit Data Mode Circuit

286

13.3 LCD Programming

287

13.3.1

Defining Constants and Variables

287

Using MPLAB Data Directives

289

13.3.2

LCD Initialization

290

Function Set Command

290

Display Off

291

Display and Cursor On

291

Set Entry Mode

292

Cursor and Display Shift

292

Clear Display

293

13.3.3

Auxiliary Operations

293

Time Delay Routine

293

Pulsing the E Line

295

Reading the Busy Flag

295

Bit Merging Operations

296

13.3.4

Text Data Storage and Display

298

Generating and Storing a Text String

299

Displaying the Text String

301

13.3.5

Data Compression Techniques

302

4-bit Data Transfer Mode

302

Master/Slave Systems

304

13.4 Sample Programs

306

13.4.1

LCDTest1

306

13.4.2

LCDTest2 Program

316

13.4.3

LCDTest3 Program

327

Chapter 14 - Communications

339

14.0

PIC Communications Overview

339

14.1

Serial Data Transmission

340

14.1.1

Asynchronous Serial Transmission

340

14.1.2

Synchronous Serial Transmission

342

14.1.3

PIC Serial Communications

342

14.1.4

The RS-232-C Standard

343

Essential Concepts

344

The Serial Bit Stream

344

Parity Testing

345

Connectors and Wiring

345

The Null Modem

346

The Null Modem Cable

347

14.1.5

The EIA-485 Standard

349

EIA-485 in PIC-based Systems

350

14.2

Parallel Data Transmission

350

14.2.1

PIC Parallel Slave Port (PSP)

351

14.3

PIC “Free-style” Serial Programming

351

14.3.1

PIC-to-PIC Serial Communications

352

PIC-to-PIC Serial Communications Circuits

352

PIC-to-PIC Serial Communications Programs

354

14.3.2

Program Using Shift Register ICs

360


Contents

xiii

The 74HC165 Parallel-to-Serial Shift Register

361

74HC164 Serial-to-Parallel Shift Register

364

14.4

PIC Protocol-based Serial Programming

366

14.4.1

RS-232-C Communications on the 16F84

366

The RS-232-C Transceiver IC

367

PIC to PC Communications

368

An RS-232-C TTY Board

368

A 16F84A UART Emulation

369

An LCD Scrolling Routine

371

14.4.2

RS-232-C Communications on the 16F87x

375

The 16F87x USART Module

376

The USART Baud Rate Generator

376

16F87x USART Asynchronous Transmitter

379

16F87x USART Asynchronous Receiver

380

PIC-to-PC RS-232-C Communications Circuit

381

16F877 PIC Initialization Code

381

USART Receive and Transmit Routines

384

The USART Receive Interrupt

386

14.5 Sample Programs

389

14.5.1

SerialSnd Program

389

14.5.2

SerialRcv Program

394

14.5.3

Serial6465 Program

400

14.5.4

TTYUsart Program

404

14.5.5 SerComLCD Program

420

14.5.6

SerIntLCD Program

438

Chapter 15 - Data EEPROM Programming

459

15.0 PIC Internal EEPROM Memory

460

15.0.1 EEPROM Programming on the 16F84

460

Reading EEPROM Data Memory on the 16F84

460

16F84 EEPROM Data Memory Write

461

16F84 EEPROM Demonstration Program

462

15.0.2 EEPROM Programming on the 16F87x

465

Reading EEPROM Data Memory on the 16F87x

467

Writing to EEPROM Data Memory in the 16F87x

467

GFR Access Issue in the 16F87x

469

15.0.3 16F87x EEPROM Circuit and Program

469

15.1

EEPROM Devices and Interfaces

475

15.1.1

The I2C Serial Interface

476

15.1.2

I2C Communications

476

15.1.3 EEPROM Communications Conditions

477

15.1.4 EEPROM Write Operation

478

15.1.5 EEPROM Read Operation

478

15.1.6 I2C EEPROM Devices

479

15.1.7

PIC Master Synchronous Serial Port (MSSP)

480

MSSP in Master Mode

482

15.1.8 I2C Serial EEPROM Programming on the 16F877

486

IC2 Initialization Procedure

486

I2C Write Byte Procedure

488

I2C Read Byte Procedure

490

15.2 Sample Programs

492


xiv

Microcontroller Programming

15.2.1

EECounter Program

492

15.2.2 Ser2EEP Program

504

15.2.3 I2CEEP Program

521

Chapter 16 - Analog to Digital and Realtime Clocks

543

16.0

A/D

Converters

544

16.0.1

Converter Resolution

544

16.0.2

ADC Implementation

545

16.1

A/D Integrated Circuits

546

16.1.1

ADC0331 Sample Circuit and Program

547

16.2 PIC On-Board A/D Hardware

549

16.2.1

A/D Module on the 16F87x

549

The ADCON0 Register

550

The ADCON1 Register

552

SLEEP Mode Operation

554

16.2.2

A/D Module Sample Circuit and Program

554

16.3

Realtime Clocks

558

16.3.1

The NJU6355 Realtime Clock

558

16.3.2

RTC Demonstration Circuit and Program

560

BCD Conversion Procedures

565

16.4 Sample Programs

568

16.4.1 ADF84 Program

568

16.4.2 A2DinLCD Program

580

16.4.3 RTC2LCD Program

595

Appendix A - Resistor Color Codes

613

Appendix B - Building Your Own Circuit Boards

615

Appendix C - Mid-range Instruction Set

621

Appendix D - Supplementary Programs

659

Index

795