Note the following details of the code protection feature on Microchip devices:
YSTEM
CERTIFIED BY DNV
== ISO/TS 16949==
•Microchip products meet the specification contained in their particular Microchip Data Sheet.
•Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
•There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
•Microchip is willing to work with the customer who is concerned about the integrity of their code.
•Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as “unbreakable.”
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.
Information contained in this publication regarding device
applications and the like is provided only for your convenience
and may be superseded by updates. It is your responsibility to
ensure that your application meets with your specifications.
MICROCHIP MAKES NO REPRESENTATIONS OR
WARRANTIES OF ANY KIND WHETHER EXPRESS OR
IMPLIED, WRITTEN OR ORAL, STATUTORY OR
OTHERWISE, RELATED TO THE INFORMATION,
INCLUDING BUT NOT LIMITED TO ITS CONDITION,
QUALITY, PERFORMANCE, MERCHANTABILITY OR
FITNESS FOR PURPOSE. Microchip disclaims all liability
arising from this information and its use. Use of Microchip
devices in life support and/or safety applications is entirely at
the buyer’s risk, and the buyer agrees to defend, indemnify and
hold harmless Microchip from any and all damages, claims,
suits, or expenses resulting from such use. No licenses are
conveyed, implicitly or otherwise, under any Microchip
intellectual property rights.
Trademarks
The Microchip name and logo, the Microchip logo, dsPIC,
FlashFlex, K
PICSTART, PIC
and UNI/O are registered trademarks of Microchip Technology
Incorporated in the U.S.A. and other countries.
FilterLab, Hampshire, HI-TECH C, Linear Active Thermistor,
MTP, SEEVAL and The Embedded Control Solutions
Company are registered trademarks of Microchip Technology
Incorporated in the U.S.A.
Silicon Storage Technology is a registered trademark of
Microchip Technology Inc. in other countries.
Analog-for-the-Digital Age, Application Maestro, BodyCom,
chipKIT, chipKIT logo, CodeGuard, dsPICDEM,
dsPICDEM.net, dsPICworks, dsSPEAK, ECAN,
ECONOMONITOR, FanSense, HI-TIDE, In-Circuit Serial
Programming, ICSP, Mindi, MiWi, MPASM, MPF, MPLAB
Certified logo, MPLIB, MPLINK, mTouch, Omniscient Code
Generation, PICC, PICC-18, PICDEM, PICDEM.net, PICkit,
PICtail, REAL ICE, rfLAB, Select Mode, SQI, Serial Quad I/O,
Total Endurance, TSHARC, UniWinDriver, WiperLock, ZENA
and Z-Scale are trademarks of Microchip Technology
Incorporated in the U.S.A. and other countries.
SQTP is a service mark of Microchip Technology Incorporated
in the U.S.A.
GestIC and ULPP are registered trademarks of Microchip
Technology Germany II GmbH & Co. & KG, a subsidiary of
Microchip Technology Inc., in other countries.
All other trademarks mentioned herein are property of their
respective companies.
Microchip received ISO/TS-16949:2009 certification for its worldwide
headquarters, design and wafer fabrication facilities in Chandler and
Tempe, Arizona; Gresham, Oregon and design centers in California
and India. The Company’s quality system processes and procedures
are for its PIC
devices, Serial EEPROMs, microperipherals, nonvolatile memory and
analog products. In addition, Microchip’s quality system for the design
and manufacture of development systems is ISO 9001:2000 certified.
All documentation becomes dated, and this manual is no exception. Microchip tools and
documentation are constantly evolving to meet customer needs, so some actual dialogs
and/or tool descriptions may differ from those in this document. Please refer to our web site
(www.microchip.com) to obtain the latest documentation available.
Documents are identified with a “DS” number. This number is located on the bottom of each
page, in front of the page number. The numbering convention for the DS number is
“DSXXXXXA”, where “XXXXX” is the document number and “A” is the revision level of the
document.
For the most up-to-date information on development tools, see the MPLAB
Select the Help menu, and then Topics to open a list of available online help files.
INTRODUCTION
This chapter contains general information that will be useful to know before using the
PICkit™ 3 Starter Kit User’s Guide. Items discussed in this chapter include:
• Document Layout
• Conventions Used in this Guide
• Warranty Registration
• Recommended Reading
• The Microchip Web Site
• Development Systems Customer Change Notification Service
• Customer Support
• Document Revision History
®
IDE online help.
DOCUMENT LAYOUT
This document describes how to use the PICkit™ 3 Starter Kit User’s Guide as a development tool to emulate and debug firmware on a target board. The manual layout is as
follows:
• Section Chapter 1. “Overview”
• Section Chapter 2. “PIC® MCU Architecture”
• Section Chapter 3. “Lessons”
• Appendix A. “Block Diagram and MPLAB® X Shortcuts”
2012 Microchip Technology Inc.DS41628B-page 7
PICkit™ 3 Starter Kit User’s Guide
CONVENTIONS USED IN THIS GUIDE
This manual uses the following documentation conventions:
DOCUMENTATION CONVENTIONS
DescriptionRepresentsExamples
Arial font:
Italic charactersReferenced booksMPLAB
Emphasized text...is the only compiler...
Initial capsA windowthe Output window
A dialogthe Settings dialog
A menu selectionselect Enable Programmer
QuotesA field name in a window or
dialog
Underlined, italic text with
right angle bracket
Bold charactersA dialog buttonClick OK
N‘RnnnnA number in verilog format,
Text in angle brackets < >A key on the keyboardPress <Enter>, <F1>
Courier New font:
Plain Courier NewSample source code#define START
Italic Courier NewA variable argumentfile.o, where file can be
Choice of mutually exclusive
arguments; an OR selection
Represents code supplied by
user
®
IDE User’s Guide
“Save project before build”
4‘b0010, 2‘hF1
any valid filename
[options]
errorlevel {0|1}
var_name...]
void main (void)
{ ...
}
DS41628B-page 8 2012 Microchip Technology Inc.
WARRANTY REGISTRATION
Please complete the enclosed Warranty Registration Card and mail it promptly.
Sending in the Warranty Registration Card entitles users to receive new product
updates. Interim software releases are available at the Microchip web site.
RECOMMENDED READING
This user’s guide describes how to use the PICkit™ 3 Starter Kit User’s Guide. Other
useful documents are listed below. The following Microchip documents are available
and recommended as supplemental reference resources.
Readme for PICkit™ 3 Starter Kit User’s Guide
For the latest information on using PICkit™ 3 Starter Kit User’s Guide, read the
“Readme for PICkit™ 3 Starter Kit Board User’s Guide.txt” file (an
ASCII text file) in the Readmes subdirectory of the MPLAB IDE installation directory.
The Readme file contains update information and known issues that may not be
included in this user’s guide.
PIC16(L)F1825/29 Data Sheet (DS41440)
This data sheet summarizes the features of the PIC16F1829.
PIC18(L)F1XK22 Data Sheet (DS41365)
This data sheet summarizes the features of the PIC18F14K22.
Readme Files
For the latest information on using other tools, read the tool-specific Readme files in
the Readmes subdirectory of the MPLAB IDE installation directory. The Readme files
contain update information and known issues that may not be included in this user’s
guide.
Preface
2012 Microchip Technology Inc.DS41628B-page 9
PICkit™ 3 Starter Kit User’s Guide
THE MICROCHIP WEB SITE
Microchip provides online support via our web site at www.microchip.com. This web
site is used as a means to make files and information easily available to customers.
Accessible by using your favorite Internet browser, the web site contains the following
information:
• Product Support – Data sheets and errata, application notes and sample
programs, design resources, user’s guides and hardware support documents,
latest software releases and archived software
• General Technical Support – Frequently Asked Questions (FAQs), technical
support requests, online discussion groups, Microchip consultant program
member listing
• Business of Microchip – Product selector and ordering guides, latest Microchip
press releases, listing of seminars and events, listings of Microchip sales offices,
distributors and factory representatives
DEVELOPMENT SYSTEMS CUSTOMER CHANGE NOTIFICATION SERVICE
Microchip’s customer notification service helps keep customers current on Microchip
products. Subscribers will receive e-mail notification whenever there are changes,
updates, revisions or errata related to a specified product family or development tool of
interest.
To register, access the Microchip web site at www.microchip.com, click on Customer
Change Notification and follow the registration instructions.
The Development Systems product group categories are:
• Compilers – The latest information on Microchip C compilers and other language
tools. These include the HI-TECH C
pilers; MPASM™ and MPLAB ASM30 assemblers; MPLINK™ and MPLAB
LINK30 object linkers; and MPLIB™ and MPLAB LIB30 object librarians.
• In-Circuit Debuggers – The latest information on the Microchip in-circuit
debugger, MPLAB ICD 2, MPLAB ICD 3, PICkit™ 3.
• MPLAB
Integrated Development Environment for development systems tools. This list is
focused on the MPLAB IDE, MPLAB SIM simulator, MPLAB IDE Project Manager
and general editing and debugging features.
• Programmers – The latest information on Microchip programmers. These include
the MPLAB PM3 device programmers and PICkit™ 3 development programmers.
®
IDE – The latest information on Microchip MPLAB IDE, the Windows®
®
C16, MPLAB C18 and MPLAB C30 C com-
DS41628B-page 10 2012 Microchip Technology Inc.
CUSTOMER SUPPORT
Users of Microchip products can receive assistance through several channels:
• Distributor or Representative
• Local Sales Office
• Field Application Engineer (FAE)
• Technical Support
Customers should contact their distributor, representative or field application engineer
(FAE) for support. Local sales offices are also available to help customers. A listing of
sales offices and locations is included in the back of this document.
Technical support is available through the web site at: http://support.microchip.com
This chapter introduces the hardware that is included in the kit, as well as a quick start
to downloading and installing the accompanying software.
1.2HIGHLIGHTS
This chapter discusses:
-What’s New
- Included Items
- The Low Pin Count (LPC) Board Hardware
- Software Overview
- Running the Demonstrations
1.3WHAT’S NEW
This kit is an update to the PICkit™ 2 Starter Kit. Modifications to the previous LPC
board (DM164120-1) were made so that the full functionality of the code can be
debugged without the need of a debug header. The software has also been rewritten
to accommodate new technologies. The following is a list of new features:
1. Software is in both the ‘C’ and assembler language
2. Extension of the number of lessons and modules covered
3. MPLAB
4. New PIC16 enhanced mid-range and PIC18 routines
5. Uses the universal XC8 compiler
The following is a list of hardware changes to the LPC board:
1. Potentiometer connected to RA4 (formerly to RA0)
2. Switch connected to RA2 (formerly to RA3)
This new LPC board is still backwards compatible. Bridging the old pins to the new pins
will restore functionality.
PICkit™ 3 STARTER KIT USER’S GUIDE
Chapter 1. Overview
®
X support as well as the older MPLAB® 8
1.4INCLUDED ITEMS
1. 1x PICkit 3 Programmer
2. 1x Micro USB cable
3. 1x LPC Board (Part Number : DM164130-9)
4. 1x PIC16F1829-I/P
5. 1x PIC18F14K22 -I/P
The 13 lessons can be downloaded from the web.
The PIC16F1829 is a new enhanced mid-range device, which supports more features
than the older mid-range PIC16 parts.
2012 Microchip Technology Inc.DS41628B-page 13
PICkit™ 3 Starter Kit User’s Guide
20-pin DIP
Socket
External Power
Push Button
ICSP™ Programming Header
14-pin Expansion Header
Generous Prototyping
Area
Potentiometer
LEDs
The software associated with the kit supports the PIC16F1829 and PIC18F14K22. The
software is intended to run on these two devices, although the software can be easily
ported to other devices.
1.5THE LOW PIN COUNT BOARD
Support for 18-pin devices requires some board modifications. 14- and 20-pin PIC
devices will have full access to all of the human interface devices. If an 8-pin part is
used, then the LEDs will have to be bridged to the necessary pins on the PIC MCU.
The switch and potentiometer are already connected to pins that are supported by an
8-pin device. The board provides holes next to the LEDs that can be easily soldered to
in order to create any desired hardware changes.
The board is programmable by an In-Circuit Serial Programmer™ (ICSP™), such as a
PICkit™ programmer. The board should be supplied with 5V. Figure 1-1 shows the LPC
Demo Board.
FIGURE 1-1:DEMO BOARD HARDWARE LAYOUT
Ta bl e 1 -1 lists the components that are connected to the two PIC devices that come
This guide will assume that the reader has a basic knowledge of electronics. The
reader does not need to have any programming experience with a PIC MCU before
reading, although a basic knowledge of programming and what the difference between
a bit and byte will help.
The software is written in both assembly and ‘C’ in the MPLAB X and MPLAB 8 integrated design environment (IDE). The assembly version is more complex and requires
more lines of code, however it is closely tied to the PIC device’s hardware and the
reader will gain a much better understanding by doing these lessons in parallel with the
‘C’ routines. The ‘C’ programming language is a higher level language assembly, hence
it provides the reader with an easier to read flow of the program. Each lesson has both
versions and are functionally equivalent.
It is recommended that the lessons be followed sequentially, as presented, since most
of the lessons build up on one another. Each new program will introduce a new peripheral or concept. This guide is not intended to be read without following along in the
code.
The PIC18 and enhanced PIC16 programs will be presented side-by-side and their
differences and similarities explained.
1.7RUNNING THE DEMONSTRATIONS
Overview
The board comes preprogrammed with a lesson. To use this program, either apply 5V
to the power header (P2), or connect a programmer to the programmer header (P1)
and apply 5V through the programmer in the IDE. The demo program will blink the four
red LEDs in succession. Press the push button (SW1), and the sequence will reverse.
Rotate the potentiometer (RP1), and the light sequence will blink at a different rate. This
demo program is developed through the first seven lessons in this guide.
2012 Microchip Technology Inc.DS41628B-page 15
PICkit™ 3 Starter Kit User’s Guide
NOTES:
DS41628B-page 16 2012 Microchip Technology Inc.
Chapter 2. PIC® MCU Architecture
Data Bus
8
14
Program
Bus
Instruction reg
Program Counter
8 LevelStack
(13-bit)
Direct Addr
7
12
Addr MUX
FSR reg
STATUS reg
MUX
ALU
Power-up
Timer
Oscillator
Start-up Timer
Power-on
Reset
Watchdog
Timer
Instruction
Decode &
Control
Timing
Generation
OSC1/CLKIN
OSC2/CLKOUT
V
DD
8
8
Brown-out
Reset
12
3
V
SS
Internal
Oscillator
Block
Configuration
Data Bus
8
14
Program
Bus
Instruction reg
Program Counter
8 LevelStack
(13-bit)
7
Addr MUX
FSR reg
STATUS reg
MUX
ALU
W Reg
Instruction
Decode &
Control
Timing
Generation
V
DD
8
8
3
V
SS
Internal
Oscillator
Block
Configuration
15
Data Bus
8
14
Program
Bus
Instruction Reg
Program Counter
16-Level Stack
(15-bit)
7
RAM Addr
Addr MUX
Indirect
Addr
FSR0 Reg
STATUS Reg
MUX
ALU
Instruction
Decode and
Control
Timi ng
Generation
V
DD
8
8
3
V
SS
Internal
Oscillator
Block
Configuration
Flash
Program
Memory
RAM
FSR regFSR reg
FSR1 Reg
15
15
MUX
15
Program Memory
Read (PMR)
12
FSR regFSR reg
BSR Reg
5
2.1INTRODUCTION
This chapter describes the architecture of the enhanced mid-range PIC16F1829
(DS41440), as well as the PIC18 (DS41365).
2.2CORE BASICS
Enhanced PIC16 and PIC18 devices use a modified Harvard architecture, meaning the
code memory and data memory are independent. This allows faster execution because
code instructions and data can be accessed simultaneously. The subsequent instruction is fetched while decoding and executing the current instruction. In Figure 2-1 and
Figure 2-3, the reader should notice the separate lines for data bus and program bus.
This guide will cover nearly all of the registers and modules as seen in the following
figures. The following block diagrams should be referenced while each lesson is being
performed in order to understand the interactions.
PICkit™ 3 STARTER KIT USER’S GUIDE
FIGURE 2-1:SIMPLIFIED ENHANCED MID-RANGE PIC
®
MCU BLOCK DIAGRAM
2012 Microchip Technology Inc.DS41628B-page 17
PICkit™ 3 Starter Kit User’s Guide
PORTA
EUSART
Comparators
MSSP
Timer2Timer1Timer4Timer0
ECCP1
ADC
10-Bit
ECCP2ECCP3CCP4CCP5
Timer6
PORTB
PORTC
PORTD
PORTE
LCD
SR
Latch
Note 1:See applicable chapters for more information on peripherals.
CPU
Program
Flash Memory
EEPROM
RAM
Timing
Generation
INTRC
Oscillator
MCLR
OSC1/CLKIN
OSC2/CLKOUT
Figure 2-1
FIGURE 2-2:SIMPLIFIED ENHANCED MID-RANGE PIC® MCU DATA BLOCK DIAGRAM
DS41628B-page 18 2012 Microchip Technology Inc.
FIGURE 2-3:SIMPLIFIED PIC18 BLOCK DIAGRAM
Instruction
Decode and
Control
PORTA
PORTB
PORTC
RA1
RA0
Data Latch
Data Memory
Address Latch
Data Address<12>
12
Access
BSR
FSR0
FSR1
FSR2
inc/dec
logic
Address
4
12
4
PCH PCL
PCLATH
8
31-Level Stack
Program Counter
PRODLPRODH
8 x 8 Multiply
8
BITOP
8
8
ALU<8>
20
8
8
Table Pointer<21>
inc/dec logic
21
8
Data Bus<8>
Table Latch
8
IR
12
3
ROM Latch
PCLATU
PCU
Note 1:RA3 is only available when MCLR functionality is disabled.
2:OSC1/CLKIN and OSC2/CLKOUT are only available in select oscillator modes and when these pins are
not being used as digital I/O.
EUSARTComparator
MSSP
10-bit
ADC
Timer2Timer1Timer3Time r0
ECCP1
BOR
Data
EEPROM
W
Instruction Bus <16>
STKPTR
Bank
8
State machine
control signals
Decode
8
8
Power-up
Timer
Oscillator
Start-up Timer
Power-on
Reset
Watchdog
Timer
OSC1
(2)
OSC2
(2)
VDD,
Internal
Oscillator
Fail-Safe
Clock Monitor
Precision
Reference
Band Gap
V
SS
MCLR
(1)
Block
LFINTOSC
Oscillator
16 MHz
Oscillator
Single-Supply
Programming
FVR
FVR
FVR
CV
REF
Address Latch
Program Memory
Data Latch
CV
REF
RA3
RA4
RA5
RB4
RB5
RB6
RB7
RC0
RC1
RC2
RC3
RC4
RC5
RC6
RC7
(512/768 bytes)
RA1
PIC® MCU Architecture
2012 Microchip Technology Inc.DS41628B-page 19
PICkit™ 3 Starter Kit User’s Guide
T c lock cycle
1
F
OSC
-------------=
4 * T
4
FOSC
-------------
4
4 MHz
------------- ----
1 µs== =
BTFSS PORTA, RA0
2.3DATA/PROGRAM BUS
The data bus is connected to the outside world via port pins, as well as all of the peripheral registers (timers, ADC, PWM). The program bus connects to the Flash memory
where the program is stored. This is where assembled code is programmed to.
2.4ACCUMULATOR
There is only one accumulator – the working register (WREG). The accumulator handles all data bus related tasks, such as mathematical operations. The ALU only deals
with 8-bit sized data – hence the categorical names of 8/16/32-bit micros.
2.5INSTRUCTIONS
Instructions tell what the PIC device should do, whether it is shifting a few bits or jumping to a new line in code. They form the very essence of each program in program
memory. All enhanced mid-range PIC devices have only 49 instructions. The PIC18
has 75 available instructions. Since there are very few instructions needed to learn, the
PIC device can be referred to as a “reduced instruction set computing”, or RISC,
processor.
Each instruction will be explained in detail as they are introduced in each lesson. For
now, the basis of what makes up each instruction will be explained.
One instruction cycle consists of four clock cycles. This means that if the PIC MCU is
running at 4 MHz, each instruction will take one microsecond, as seen in Equation 2-1.
EQUATION 2-1:INSTRUCTION TIME
All instructions are executed in a single instruction cycle, unless a conditional test is
true, or the program counter (PC) is changed. In these cases, the execution takes two
instruction cycles, with the additional instruction cycle executed as a NOP (do nothing),
see Example 2-1.
EXAMPLE 2-1:
This takes two instruction cycles only if pin RA0 is set (active-high), since the skip operation affects the PC.
The PIC18 has a larger word size than the enhanced PIC16 architecture. The PIC18
has a 16-bit wide word containing the operation code (opcode) and all required operands. The enhanced PIC16 has a 14-bit wide word. An opcode is interpreted by the
processor and is unique to each instruction.
The opcodes are broken into four formats:
1. Byte oriented
2. Bit oriented
3. Literal
4. Control
DS41628B-page 20 2012 Microchip Technology Inc.
2.6BYTE
ADDWF data,f
BSF PORTA,RA0
MOVLW ‘A’
PIC® MCU Architecture
All byte instructions on the enhanced PIC16 contain a 6-bit opcode, 7-bit file address,
and a destination bit. All PIC18 byte instructions contain a 6-bit opcode, 8-bit file
address, a destination bit, and a RAM access bit.The sum of all the bit field sizes confirms that the PIC16 enhanced core does indeed have a 14-bit wide word size for
instructions. Likewise, the same can be seen for the PIC18 for its 16-bit wide word
length.
The RAM access bit (a) on the PIC18 is set when the user wishes to use the Bank
Select Register (BSR) for manually selecting the bank. The PIC16 user will always
need to make sure that they are in the correct bank by using the ‘banksel’ directive.
This is explained in the first few lessons.
The destination bit (d) specifies whether the result will be stored in WREG or back in
the original file register. When ‘d’ is zero, the result is placed in the WREG resister.
Otherwise, the result is placed in the file register.
The file register (f) specifies which register to use. This can be a Special Function
Register (SFR) or General Purpose Register (GPR).
EXAMPLE 2-2:
2.7BIT
2.8LITERAL
This adds the contents of WREG and data, with the result being saved back to the file
register data.
The PIC18 can move data from one file register directly to another file register, circumventing the WREG. All file moves in the enhanced PIC16 architecture must go through
the WREG.
Bit instructions operate on a specific bit within a file register. These instructions may set
or clear a specific bit within a file register. They may also be used to test a specific bit
within a file register. All bit instructions on the enhanced PIC16 contain a 4-bit opcode,
7-bit file address, and a 3-bit bit address. All PIC18 byte instructions contain a 4-bit
opcode, 8-bit file address, 3-bit bit address and a RAM access bit.
EXAMPLE 2-3:
This sets pin RA0 in the PORTA register.
Literal operations contain the data operand within the instruction. Both architectures
use an 8-bit intermediate value. The rest of the bits are reserved for the opcode.
EXAMPLE 2-4:
This moves the ASCII value of ‘A’ (0x41) into WREG.
2012 Microchip Technology Inc.DS41628B-page 21
PICkit™ 3 Starter Kit User’s Guide
2.9CONTROL
Instructions that dictate what address the PC will select in program memory are called
control instructions. This would include call, goto, and branch. Each has a unique
word length. Please refer to the “Instruction Set Summary” chapter in any PIC
data sheet for more information.
device
DS41628B-page 22 2012 Microchip Technology Inc.
PIC® MCU Architecture
Byte-oriented file register operations
13 8 7 6 0
d = 0 for destination W
OPCODE d f (FILE #)
d = 1 for destination f
f = 7-bit file register address
Bit-oriented file register operations
13 10 9 7 6 0
OPCODE b (BIT #) f (FILE #)
b = 3-bit bit address
f = 7-bit file register address
Literal and control operations
13 8 7 0
OPCODE k (literal)
k = 8-bit immediate value
13 11 10 0
OPCODE k (literal)
k = 11-bit immediate value
General
CALL and GOTO instructions only
MOVLP instruction only
13 5 4 0
OPCODE k (literal)
k = 5-bit immediate value
MOVLB instruction only
13 9 8 0
OPCODE k (literal)
k = 9-bit immediate value
BRA instruction only
FSR Offset instructions
13 7 6 5 0
OPCODE n k (literal)
n = appropriate FSR
FSR Increment instructions
13 7 6 0
OPCODE k (literal)
k = 7-bit immediate value
13 3 2 1 0
OPCODE n m (mode)
n = appropriate FSR
m = 2-bit mode value
k = 6-bit immediate value
13 0
OPCODE
OPCODE only
ADDWF MYREG, W
BSF MYREG, BIT
MOVLW 0x45
CALL LABEL
MOVLP 15
MOVLB 3
BRA LABEL
ADDFSR FSR1, 3
MOVIW ++FSR0
Example Instruction
FIGURE 2-4:ENHANCED PIC16 GENERAL FORMAT FOR INSTRUCTIONS
2012 Microchip Technology Inc.DS41628B-page 23
PICkit™ 3 Starter Kit User’s Guide
Byte-oriented file register operations
15 10 9 8 7 0
d = 0 for result destination to be WREG register
OPCODE d a f (FILE #)
d = 1 for result destination to be file register (f)
a = 0 to force Access Bank
Bit-oriented file register operations
15 12 11 9 8 7 0
OPCODE b (BIT #) a f (FILE #)
b = 3-bit position of bit in file register (f)
Literal operations
15 8 7 0
OPCODE k (literal)
k = 8-bit immediate value
Byte to Byte move operations (2-word)
15 12 11 0
OPCODE f (Source FILE #)
CALL, GOTO and Branch operations
15 8 7 0
OPCODE n<7:0> (literal)
n = 20-bit immediate value
a = 1 for BSR to select bank
f = 8-bit file register address
a = 0 to force Access Bank
a = 1 for BSR to select bank
f = 8-bit file register address
15 12 11 0
1111 n<19:8> (literal)
15 12 11 0
1111 f (Destination FILE #)
f = 12-bit file register address
Control operations
Example Instruction
ADDWF MYREG, W, B
MOVFF MYREG1, MYREG2
BSF MYREG, bit, B
MOVLW 7Fh
GOTO Label
15 8 7 0
OPCODE n<7:0> (literal)
15 12 11 0
1111 n<19:8> (literal)
CALL MYFUNC
15 11 10 0
OPCODE n<10:0> (literal)
S = Fast bit
BRA MYFUNC
15 8 7 0
OPCODE n<7:0> (literal)
BC MYFUNC
S
FIGURE 2-5:PIC18 GENERAL FORMAT FOR INSTRUCTIONS
There are some subtle differences between the block diagrams in Figure 2-1 and
Figure 2-3. This document will point out a few of the important ones.
DS41628B-page 24 2012 Microchip Technology Inc.
2.10STACK LEVEL
The PIC18 has a deeper stack level of 31, whereas the enhanced core has 16. A
deeper stack allows the PIC device to make more calls in the software before returning
to the original address where the first call was made.
A call or goto modifies the program counter to point to a different place in code. Without these, the code would execute from the top to the bottom. The lessons will show
the significance of this.
The call stack is used to save the return address before going to a new position in
program memory.
As a frame of reference, some of the baseline parts (PIC10/12) devices have a call
stack that is only two levels deep. It is quite a challenge to create modular code with a
limited stack depth.
2.11MEMORY ORGANIZATION
There are three sections of memory in the PIC16 enhanced mid-range and PIC18
devices:
1. Program Memory
2. Data RAM
3. Data EEPROM
PIC® MCU Architecture
2.12PROGRAM MEMORY
There are five sections of program memory:
1. Flash Program Memory
2. Configuration Words
3. Device ID
4. Revision ID
5. User ID
2.12.1Flash Program Memory
All enhanced mid-range and PIC18 devices use Flash memory for programming. Flash
allows the PIC device to be erased and written to hundreds of thousands of times.
2.12.2Configuration Words
There are several Configuration Word bits, or fuses, that allow different configurations
at run-time. Oscillator selections, memory protection, low-voltage detection, etc., are
some examples of configuration options. Each device has different configuration
options. Enhanced mid-range Configuration bits are read-only during code execution.
PIC18 can read all and write most Configuration bits during code execution. The
Configuration bits are programmed in a special way, as seen in the lesson source files.
2.12.3Device ID
The Device ID contains the read-only manufacture’s ID for the PIC MCU. The
PIC16F1829 ID is stored in DEVICEID and the PIC18F14K22 is stored in DEVID1 and
DEVID2.
2012 Microchip Technology Inc.DS41628B-page 25
PICkit™ 3 Starter Kit User’s Guide
PC<14:0>
15
0000h
0004H
Stack Level 0
Stack Level 15
Reset Vecto r
Interrupt Vector
Stack Level 1
0005h
On-chip
Program
Memory
Page 0
07FFh
Rollover to Page 0
0800h
0FFFh
1000h
7FFFh
Page 1
Rollover to Page 3
Page 2
Page 3
17FFh
1800h
1FFFh
2000h
CALL
, CALLW
RETURN, RETLW
Interrupt, RETFIE
2.12.4Revision ID
There are five bits in each PIC MCU that indicate the silicon revision of the PIC device.
These bits are read-only and found in the DEVID register. There are usually multiple
revisions of silicon for each PIC device. The errata document, which points out any
errors and their temporary work-arounds, should be read alongside the data sheet.
The PIC18 has a program bus that is 21 bits wide, whereas the enhanced core is only
15 bits wide. A larger program bus infers that the program memory is larger, since it
allows the core to locate a higher address value. The enhanced core program counter
is capable of addressing 32K x 14 program memory space as seen in Figure 2-6.
FIGURE 2-6:ENHANCED MID-RANGE PROGRAM MEMORY MAP AND
CALL STACK
PIC18 devices are capable of addressing a 2-Mbyte program memory space, as seen
in Figure 2-7. PIC18 devices also have two interrupt vectors, whereas the enhanced
PIC devices only have one. A stark difference is that the PIC18 has no concept of
pages, whereas the enhanced core has its program memory split into different pages.
DS41628B-page 26 2012 Microchip Technology Inc.
PIC® MCU Architecture
PC<20:0>
Stack Level 1
Stack Level 31
Reset Vector
Low Priority Interrupt Vector
CALL,RCALL,RETURN
RETFIE,RETLW
21
0000h
0018h
High Priority Interrupt Vector
0008h
User Memory Space
1FFFFFh
4000h
3FFFh
200000h
On-Chip
Program Memory
Read ‘0’
1FFFh
2000h
On-Chip
Program Memory
Read ‘0’
PIC18(L)F14K22
PIC18(L)F13K22
Changing pages is necessary in the enhanced core when changing execution from one
page to another. None of the lessons for the enhanced PIC16 occupy more than one
page and, therefore, page changes are not necessary. If the code does overflow into
another page, the assembler will give a warning, indicating that a pagesel may be
required.
FIGURE 2-7:PIC18 PROGRAM MEMORY MAP AND CALL STACK
2.13DATA MEMORY
2.12.5User ID
These four memory locations are designated as ID locations where the programmer
can store checksum or other code identification numbers. These are readable and
writable during normal execution.
The data memory layout of the two device families is perhaps the most significant. Data
memory on both families can be split into four types:
1. Core Registers
2. Special Function Registers
3. General Purpose RAM
4. Common RAM
2012 Microchip Technology Inc.DS41628B-page 27
PICkit™ 3 Starter Kit User’s Guide
2.13.1Core Registers
The core registers contain the registers that directly affect the basic operation of the
PIC device, repeated at the top of every data memory bank. Here are three examples
of the 12 core registers:
1. STATUS
2. WREG
3. INTCON
The STATUS register contains the arithmetic status of the ALU. The WREG register is
used to move bits in and out of registers. The INTCON register contains the various
enable and flag bits that would cause the PIC MCU to jump to the Interrupt Vector.
2.13.2Special Function Registers
The Special Function Registers provide access to the peripheral functions in the
device. The Special Function Registers occupy 20 bytes immediately after the core registers of every data memory bank (addresses x0Ch/x8Ch through x1Fh/x9Fh) on the
enhanced mid-range core. The PIC18 enhanced core has all of its SFRs in Access
RAM, which is discussed in Section 2.14 “Banks”.
2.13.3General Purpose RAM
2.14BANKS
GPRs are used for data storage and scratchpad operations in the user’s application.
Think of this as RAM that can be used for your program, but the correct bank must be
selected before using. For the enhanced mid-range PIC devices, there are up to 80
bytes of GPR that follow immediately after the SFR space in each data memory bank.
2.13.4Common RAM
There are 16 bytes of common RAM accessible from all banks in the enhanced core.
The PIC18 architecture has something similar called Access RAM, which contains up
to 96 bytes.
The PIC18F14K22 data memory is divided into 16 banks that contain 256 bytes each.
The PIC16F1829 data memory is partitioned in 32 memory banks with 128 bytes in
each bank. For the PIC16 enhanced mid-range, each bank consists of:
1. 12 core registers
2. 20 Special Function Registers (SFR)
3. Up to 80 bytes of General Purpose RAM (GPR)
4. 16 bytes of shared RAM (accessible by any bank)
Figure 2-8 shows the above information on the enhanced PIC16.
Addresses 70h-7Fh are shared by all of the banks. This is useful for storing a few bytes
of RAM without the need to switch banks each time the byte is used. Figure 2-9 shows
the first eight banks on the PIC16F1829. Notice how the top 12 core registers are
accessible from every bank, as are the 16 bytes of common RAM.
2012 Microchip Technology Inc.DS41628B-page 29
DS41628B-page 30 2012 Microchip Technology Inc.
FIGURE 2-9:PIC16F1829 MEMORY MAP – THE CORRECT BANK MUST BE SELECTED BEFORE WRITING/READING FROM A