PICmicro®MCU Part Number Feature to Part Number Table 3 Low-End Instruction Set 22 Mid-Range Instruction Set 31 PIC17Cxx Instruction Set 40 PIC18Cxx Instruction Set 63 Microchip Special
Trang 2PICmicro ®
Microcontroller
Pocket Reference
Trang 4PICmicro ® Microcontroller
Pocket Reference Myke Predko
Trang 5All trademarks are trademarks of their respective owners Rather than put a trademark symbolthe benefit of the trademark owner, with no intention of infringement of the trademark Wheresuch designations appear in this book, they have been printed with initial caps
McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales Hoare, Special Sales, at george_hoare@mcgraw-hill.com or (212) 904-4069
pro-TERMS OF USE
This is a copyrighted work and The McGraw-Hill Companies, Inc (“McGraw-Hill”) and its mitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work,works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or anycial and personal use; any other use of the work is strictly prohibited Your right to use the workmay be terminated if you fail to comply with these terms
licen-THE WORK IS PROVIDED “AS IS” McGRAW-HILL AND ITS LICENSORS MAKE NOPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUD-HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY,MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE McGraw-Hill and itsrequirements or that its operation will be uninterrupted or error free Neither McGraw-Hill nor itscause, in the work or for any damages resulting therefrom McGraw-Hill has no responsibility forMcGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, conse-them has been advised of the possibility of such damages This limitation of liability shall apply
Trang 7Chapter 1 Conventions Used in This Book 1
Chapter 2 PICmicro®MCU Part Number
Feature to Part Number Table 3
Low-End Instruction Set 22
Mid-Range Instruction Set 31
PIC17Cxx Instruction Set 40
PIC18Cxx Instruction Set 63
Microchip Special Instruction Mnemonics 104
Parallax PICmicro®MCU Instruction Set 111
v
Trang 8Chapter 5 PICmicro®MCU Processor
The PICmicro®MCU’s Arithmetic Logic Unit 123
Low-End PICmicro®MCUs 125
Interrupt handler skeleton 150
Chapter 6 PICmicro®MCU Register Mappings 151 Low-End PICmicro®MCUs 151
Mid-Range PICmicro®MCUs 154
Trang 9Chapter 7 Built-In Hardware Features 207 Configuration Registers 207
Built-In EEPROM Data Memory Access 263
EPROM Program Memory Access 269
Flash Program Memory Access 271
External Parallel Memory 273
Chapter 8 PICmicro®MCU
Trang 10Parallel Bus Device Interfacing 282
Switch Matrix Keypad/Keyboard Interfacing 287
Combining Input and Output 291
Simulated “Open Collector’’/“Open Drain’’ I/O 292
Asynchronous Serial I/O Software Routines 347
Dallas Semiconductor One-Wire Interface 357
Reading Potentiometer Using Parallel I/O Pins 363
Sony Infrared TV Remote Control 374
Chapter 9 PICmicro®MCU Programming 379
Low-End PICmicro®MCU Programming 381
Mid-Range Serial Programming 385
PIC17Cxx ICSP Programming 398
Microchip ICSP Programming Connector 405
Third Party/Downloadable Programmers 406
Trang 11Chapter 10 PC Interfaces 407
Interrupt Function by Number 422
Keyboard operation with timing diagrams 431
Keyboard controller commands 436
Trang 12Time Delays 466
Negating the Contents of a Register 467
Incrementing/Decrementing “w’’ 468
Rotating a Byte in Place 469
Copy Bits from One Register to Another 470
Converting a Nybble to ASCII 470
Converting an ASCII Byte to a Hex Nybble 471
Using T0CKI as an Interrupt Source Pin 471
Sixteen-Bit Pulse Measurement with
Detect a Change in a Register 474
Test a Byte within a Range 474
Convert ASCII to Upper Case 475
Swap the Contents of “w’’ with a Register 475
Swap the Contents of Two Registers 475
Compare and Swap if Y X 476
Counting the Number of “1’’s in a Byte 476
Generating Parity for a Byte 477
Keeping a Variable within a Range 477
Constant Multiplication 479
Defining 16 Bit Numbers 483
Increments and Decrements 484
Bitwise Operations on Constants and Variables 488
Trang 13Comparisons with 16-Bit Variables 489
Chapter 13 PICmicro®MCU
I/O Pin Current Capabilities 500
RC Oscillator Component Values 500
LP Oscillator Operating Characteristics 501
XT Oscillator Operating Characteristics 502
HS Oscillator Operating Characteristics 504
Chapter 14 PICmicro®MCU Application
Chapter 15 PICmicro®MCU Application
Microsoft Compatible Editor “Ctrl’’ Key
Stimulus (.STI) files 525
MPLAB assembler directives 526
Standard Declaration and Include (“.inc’’) Files 536
Linking–Linked Applications 536
Application Code Template 539
Microsoft BASIC Enhancements 542
Trang 14PICmicro®MCU enhancement functions 603
Chapter 16 Constants and Data Tables 605 Mathematical and Physical Constants 605
ASCII control characters 607
ANSI display control sequences 609
IBM PC extended ASCII characters 612
Windows ASCII characters 612
“Touch-Tone’’ Telephone Frequencies 616
Chapter 17 Miscellaneous Electronics 629
Electromagnetic Spectrum 630
Radar bands
Trang 15PICList Internet List Server 648
Recommended PICmicro®MCU Web Sites 656
Trang 17Conventions Used in
This Book
kHz Kilohertz (Thousands of Cycles per Second) MHz Megahertz (Millions of Cycles per Second) GHz Gigahertz (Billions of Cycles per Second)
mbps Millions of Bits per Second
Trang 18AND and & Bitwise “AND”
_Label Negative Active Pin In some manufacturer’s
data sheets this is represented with a leading
“!” character or with a bar over the entire label
[parameter] The parameter is optional.
parameter |
parameter One or another parameter can be used.
Trang 19Part Number Feature Comparison
Feature to Part Number Table
The following table lists the different PICmicro®MCU families with the features that are specific to them.
2
3
Trang 20ADC/Internal Voltage Reference/On Chip Temperature Sensor.
PIC16C5x 18- to 28-Pin PICmicro®MCU 12-Bit (Low-End) Processor.
PIC16C505 14-Pin PICmicro®MCU 12-Bit (Low-End) Processor Internal Reset & Oscillator.
PIC16HV540 18-Pin PICmicro®MCU 12-Bit (Low-End) Processor Extended Vdd
Capabilities with Built-In Regulator.
PIC16C55x 18-Pin PICmicro®MCU 14-Bit (Mid-Range) Processor.
PIC16C6x 18- to 40-Pin PICmicro®MCU 14-Bit (Mid-Range) Processor Optional TMR1
& TMR2/Optional SPI/Optional USART/Optional PSP.
PIC16C62x 18-Pin PICmicro®MCU 14-Bit (Mid-Range) Processor Voltage Comparators
Built-In with Voltage Reference/Optional EEPROM Data Memory.
PIC16F62x 18-Pin PICmicro®MCU 14-Bit (Mid-Range) Processor Flash Program
Memory/Voltage Comparators Built-In with Voltage Reference/Internal Reset
& Oscillator.
PIC16C642 28-Pin PICmicro®MCU 14-Bit (Mid-Range) Processor Voltage Comparators
Trang 21PIC16C77x 28- to 40-Pin PICmicro®MCU 14-Bit (Mid-Range) Processor 12-Bit
ADC/TMR1 & TMR2/USART/I2C/SPI/Optional PSP.
PIC16F8x 18-Pin PICmicro®MCU 14-Bit (Mid-Range) Processor Flash Data and Program
Memory.
PIC16F87x 28- to 40-Pin PICmicro®MCU 14-Bit (Mid-Range) Processor 10-Bit
ADC/TMR1 & TMR2/USART/I2C/SPI/Optional PSP.
PIC16C92x 64-Pin PICmicro®MCU 14-Bit (Mid-Range) Processor Optional 8-Bit
ADC/TMR1 & TMR2/LCD Controller.
PIC17C4x 40-Pin PICmicro®MCU 16-Bit (High-End) Processor USART/Multiply.
PIC17C5x 68-Pin PICmicro®MCU 16-Bit (High-End) Processor
Trang 2216Cx2 28-Pin PICmicro®MCU 2K Program Memory SPI/TMR1 & TMR2.
16Cx3 28-Pin PICmicro®MCU 4K Program Memory USART/SPI/TMR1 & TMR2.
16Cx4 40-Pin PICmicro® MCU 4K Program Memory USART/SPI/PSP/TMR1 &
Trang 23Device Pinouts
As a rule of thumb, Pin-Through-Hole (“PTH”) parts (“P” and “JW”) are standard 0.300 and 0.600 widths with pins 0.100" apart in dual in-line packages The height of the device is dependent on the package used Surface Mount Technology (“SMT”) parts are either in dual in- line packages (“SO”) or in quad plastic chip carriers (“PT”, “PQ”, and “L”).
For actual device dimensions, check the datasheets (on the CD-ROM or from the Microchip Web site) for the PICmicro® MCU that you are planning on using Dif- ferent packages for different PICmicro® MCUs have different via pad and clearance specifications.
3
7
Trang 25Figure 3.3 “PIC16C54”/“PIC16C56” Pinout
Trang 26Figure 3.5 “PIC12C67x” Pinout
Mid-Range
The mid-range devices have the widest range of pinouts
of any of the PICmicro®MCU families (Figs 3.5 – 3.10) For many of the devices, the pinout is similar, but the pin functions may be different In these cases, the pins marked with “*” show that these pins have other, op- tional purposes Actual part number functions can be confirmed with Microchip Datasheets.
The PIC14000, which is designed for “Mixed Signals” uses the 28-pin packaging of the standard devices, but the pinouts are different as shown in Fig 3.11.
The PIC16C92x LCD Driver microcontrollers are fairly high pin count devices Figure 3.12 shows the 64-pin
“DIP” (“Dual In-line Package”) part There is also a
“PLCC” and “TQFP” package for the parts as well.
Trang 27Figure 3.7 Mid-Range PICmicro®MCU 28-Pin Device Pinout
Figure 3.6 Mid-Range 18-Pin PICmicro®MCU Pinout
Trang 28Figure 3.8 Mid-Range PICmicro®MCU 40-Pin Device Pinout
Trang 29Figure 3.10 Mid-Range PICmicro®MCU 44-Pin “QFP” Pinout
Trang 30Figure 3.12 PIC16C92x 64-Pin Device Pinout
Trang 3115Figure 3.13PIC17C4x 40-Pin Device Pinout
Figure 3.14PIC17C75x 64-Pin Device Pinout
Trang 32Figure 3.15PIC17C4x 44-Pin “PLCC” Pinout
Figure 3.16 PIC17C4x 44-Pin “QFP” Pinout
Trang 34Figure 3.18 PIC18C4X2 40-Pin Device Pinout
18 Chapter 3
Trang 35Figure 3.20 PIC18C4X2 44-Pin “QFP’’ Pinout
Trang 38Low-End Instruction Set
Register Banks are 32 bytes in size in the low-end vices This makes “Reg” in the range of 0x00 to 0x01F.
de-Op code Parameter Symbol letter Value range
Don’t Care N/A x
Byte Constant k k 0 to 0x0FF
Register Address Reg f PICmicro® MCU
ArchitectureSpecificDestination d d 0 or 1
Selection Bit Bit b 0 to 7
Destination Address a 0 to 0x07FF
Address
Destination Port Port p PORTA (5) to
PORTC(7)
Trang 39Z (w & k) 0AND Register Contents with “w” and andwf Reg, d if (d 1) 0001 01df ffff
Optionally store result in “w” Reg Reg & w
else
w Reg & wendif
Z (Reg & w) 0Clear the Specified Bit bcf Reg, bit Reg Reg & 0100 bbbf ffff
(1 Bit))
Trang 40Clear One Instruction Cycle if Skip not (1 Bit))) 0)
Executed, two if it is PC PC 1
endifSkip if the Specified Bit In the Register is btfsc Reg, bit if ((Reg & 0111 bbbf ffff
Set One Instruction Cycle if Skip not (1 Bit))
PC PC 1endifSave the Stack Pointer and jump to the call Address [SP] PC 1001 aaaa aaaa
Specified Address (two Instruction SP SP 1
& 0x0E0) 4)
AddressClear the Specified Register clrf Reg Reg 0 0000 011f ffff
Z 1Clear the “w” Register clrw w 0 0000 0100 0000
Z 1Clear the Watchdog Timer’s Counter clrwdt WDT 0 0000 0000 0100
_TO 1_PD 1
Trang 41Decrement the Contents of the Register decf Reg, d if (d 1) 0011 11df ffff
and Optionally store the results in “w” Reg Reg 1
else
w Reg 1endif
Z (Reg 1) 0Decrement the Contents of the Register decfsz Reg, d if (d 1) 0010 11df ffff
and Optionally store the results in “w” Reg Reg 1
and Skip the next instruction else
if the results are equal to Zero w Reg 1
Two Instruction Cycles taken endif
if ((Reg 1) 0)
PC PC 1endifJump to the Specified Address (two goto Address PC 101a aaaa aaaa
Instruction cycles) ((STATUS & 0x0E0)
Trang 42
Z (Reg 1) 0Increment the Contents of the incfsz Reg, d if (d 1) 0011 11df ffff
Register and Optionally store the Reg Reg 1
results in “w” and Skip the next else
instruction if the results are equal to w Reg 1
Zero Two Instruction Cycles endif
taken if Skip Executed PC PC 1
if ((Reg 1) 0)
PC PC 1endif
OR Immediate with “w” iorlw k w w | k 1101 kkkk kkkk
Z (w | k) 0
OR Register Contents with “w” and iorwf Reg, d if (d 1) 0001 00df ffff
Optionally store result in “w” Reg Reg | w
else
w Reg | wendif
Z (Reg | w) 0
Trang 43Store the value in “w” movwf Reg Reg w 0000 001f ffff
Move the contents of “w” into the option TRIS(Port) w 0000 0000 0010
OPTION Register
Resume Execution after Subroutine retlw k w k 1000 kkkk kkkk
and Place a constant value in SP SP 1
Resume Execution after Subroutine return w 0 1000 0000 0000
and Place Zero in “w” (Two Cycles SP SP 1
used) This is actually a “retlw 0” PC [SP]
instruction that MPLAB provides
Trang 44and optionally Save the Result in “w” C (Reg 7) & 1
if (d 1)Reg (Reg 1)
Tempelse
w (Reg 1)
TempendifRotate the Register right through carry rrf Reg, d Temp C 0011 00df ffff
and optionally Save the Result in “w” C Reg & 1
if (d 1)Reg (Reg 1)
(Temp 7)else
w (Reg 1)
(Temp 7)endif
Go into “Standby” Mode (Indeterminate sleep _TO 1 0000 0000 0011
Trang 45
(w ^ 0x0FF) 1)
0x0FF
Z ((Reg (w ^ 0x0FF) 1)
of a Register and Optionally store Reg
4) ((Reg & 0x00F)
4)else
w ((Reg & 0x0F0)
4) ((Reg & 0x00F)
4)
Trang 46Description Instruction Operation Op code
Move the contents of “w” into the tris Port TRIS(Port) w 0000 0000 0ppp
Tri-state control Register of the Port
XOR Immediate with “w” xorlw k w w ^ k 1111 kkkk kkkk
Z (w ^ k) 0XOR Register Contents with xorwf Reg, d if (d 1) 0001 10df ffff
“w” and Optionally store result in “w” Reg Reg ^ w
else
w Reg ^ wendif
Z (Reg ^ w) 0
Trang 47Add Immediate to “w” addlw k w w k 11 111x kkkk kkkk
C (w k) 0x0FF
Z ((w k) & 0x0FF)
0
DC ((w & 0x0F) (k & 0x0F)) 0x0FAdd Register addwf Reg, d if (d 1) 00 0111 dfff ffff
store result in “w” w Reg w
Trang 48AND Register andwf Reg, d if (d 1) 00 0101 dfff ffff
Contents with Reg Reg & w
store result in “w” w Reg & w
endif
Z (Reg & w) 0Clear the Specified bcf Reg, bit Reg Reg & 01 00bb bfff ffff
(1 Bit))
Set the Specified bcf Reg, bit Reg Reg | 01 01bb bfff ffff
Bit in the Register (1 Bit)
Skip if the Specified btfsc Reg, bit if ((Reg & 01 10bb bfff ffff
Register is Clear PC PC 1
One Instruction Cycle endif
if Skip not Executed,
Trang 49Address (two PC ((PCLATH 8)
Instruction cycles) & 0x01800)
AddressClear the Specified Register clrf Reg Reg 0 00 0001 1fff ffff
Z 1Clear the “w” Register clrw w 0 00 0001 0xxx xxxx
Z 1Clear the Watchdog clrwdt WDT 0 00 0000 0110 0100
_PD 1
Complement the Contents comf Reg, d if (d 1) 00 1001 dfff ffff
of the Specified Register Reg Reg ^ 0x0FF
the results in “w” w Reg ^ 0x0FF
endif
Z (Reg ^ 0x0FF)
Trang 50
Z (Reg 1) 0Decrement the Contents decfsz Reg, d if (d 1) 00 1011 dfff ffff
of the Register and Reg Reg 1
results in “w” and Skip w Reg 1
the next instruction if the endif
results are equal to Zero
Two Instruction Cycles if ((Reg 1) 0)
taken if skip executed PC PC 1
endifJump to the Specified goto Address PC 10 1aaa aaaa aaaa
Address (two Instruction ((PCLATH 8)
AddressIncrement the Contents incf Reg, d if (d 1) 00 1010 dfff ffff
of the Register and Reg Reg 1
endif
Z (Reg 1) 0
Trang 51taken if Skip Executed PC PC 1
endif
OR Immediate with “w” iorlw k w w | k 11 1000 kkkk kkkk
Z (w | k) 0
OR Register Contents iorwf Reg, d if (d 1) 00 0100 dfff ffff
with “w” and Optionally Reg Reg | w
w Reg | wendif
Z (Reg | w) 0Check Register Contents movf Reg, d if (d 0) 00 1000 dfff ffff
Optionally store Register endif
Trang 52Load “w” with an immediate movlw k w k 11 00xx kkkk kkkk
value
Store the value In “w” movwf Reg Reg w 00 0000 1fff ffff
Move the contents of option TRIS(Port) w 00 0000 0110 0010
“w” into the OPTION
Register Use of this instruction
is not recommended
Resume Execution after retfie GIE 1 00 0000 0000 1001
Interrupt (Two Cycles used) SP SP 1
PC [SP]
Resume Execution after retlw k w k 11 01xx kkkk kkkk
Subroutine and Place a SP SP 1
constant Value in “w” PC [SP]