Professional Documents
Culture Documents
Memoria de Pic 18f Family
Memoria de Pic 18f Family
Memory Organization
HIGHLIGHTS
This section of the manual contains the following major topics:
7.1 Introduction .................................................................................................................... 7-2
7.2 Program Memory ........................................................................................................... 7-3
7.3 Program Counter (PC) ................................................................................................... 7-6
7.4 Lookup Tables................................................................................................................ 7-9
7.5 Stack ............................................................................................................................ 7-12
7.6 Data Memory Organization.......................................................................................... 7-13
7.7 Return Address Stack .................................................................................................. 7-17
7.8 Initialization .................................................................................................................. 7-23
7.9 Design Tips .................................................................................................................. 7-24
7.10 Related Application Notes............................................................................................ 7-25
7.11 Revision History ........................................................................................................... 7-26
Section 7. Memory
Memory
7
7.2 Program Memory
Enhanced MCU devices have a 21-bit program counter capable of addressing 2 Mbytes
(1Mwords) of program memory space. The program memory contains instructions for execution
and data tables for storing fixed data. Data tables may be written once using table write instructions
and read as required, using the table read instructions.
The program space is implemented as a single contiguous block. The reset vector is at address
000000h, the high priority interrupt vector is at address 000008h, and the low priority interrupt
vector is at address 000018h (Figure 7-1).
CALL and GOTO instructions can address any location in the memory map, while the BRA and
RCALL instructions have a limited program memory reach (+1024, -1023 program memory word
locations). To allow the CALL and GOTO instructions to contain the entire address, it requires that
these instructions use 2 program memory words (2 word instruction).
Instructions are also available to move information between the data memory and the program
memory areas. These are called table operations. Table operations work with byte entities. This
is discussed in detail in the “Table Read/Table Write” section.
Figure 7-1: Program Memory Map and Stack for PIC18CXXX
PC<20:0>
Stack Level 1
Stack Level 31
Reset Vector
Low Priority Interrupt Vector
CALL,BSUB,RETURN
RETFIE,RETLW
21
0000h
0018h
On-chip
High Priority Interrupt Vector 0008h
User Memory Space
External/Unimplemented
1FFFFFh
200000h
Program Memory
(Read as ’0’ in
microcontroller mode)
Program Memory
Section 7. Memory
Memory
7
7.2.4 Instructions in Program Memory
The program memory is addressed in bytes. Instructions are stored as two bytes or four bytes
in program memory. The least significant byte of an instruction word is always stored in a program
memory location with an even address (LSb = ’0’). Figure 7-2 shows an example of how
instruction words are stored in the program memory. To maintain alignment with instruction
boundaries, the PC increments in steps of 2 and the LSb will always read ’0’.
The CALL and GOTO instructions have an absolute program memory address embedded into the
instruction. Since instructions are always stored on word boundaries, the data contained in the
instruction is a word address. The word address is written to PC<20:1>, which accesses the
desired byte address in program memory. Instruction #2 in Figure 7-2 shows how the instruction
"GOTO 000006h’ is encoded in the program memory. Program branch instructions which encode
a relative address offset operate in the same manner. The offset value stored in a branch instruction
represents the number of single word instructions that the PC will be offset by. The “Instruction
Set” section provides further details of the instruction set.
Figure 7-2: Instructions in Program Memory
Word Address
High Byte Low Byte ↓
Program Memory
Byte Locations →
000000h
000002h
000004h
000006h
Instruction 1: MOVLW 055h 0Fh 55h 000008h
Instruction 2: GOTO 000006h EFh 03h 00000Ah
F0h 00h 00000Ch
Instruction 3: MOVFF 123h, 456h C1h 23h 00000Eh
F4h 56h 000010h
000012h
000014h
Section 7. Memory
Memory
7
Figure 7-4: Loading of PC In Different Situations
PC
ALU Result
8
PCL
Situation 1 - Instruction with PCL as destination
Situation 2 - GOTO Instruction
PCLATU PCLATH
20 PCU 16 15 PCH 8 7 0
PCL
20 15 8 7 0
PCH
16
PCU
91
0
K19:K8
(2nd word
K7:K0
(1st word
of instruction) of instruction)
STACK (21-bits x 31)
Top of STACK
STACK (21-bits x 31)
Top of STACK
20 16 15 8 7 1 0
ADDR
Situation 3 - BRA Instruction in Conditional Branch Instruction STACK (21-bits x 31)
Top of STACK
Offset
from
Instruction
0
PCU PCH PCL
Situation 4 - CALL Instruction
20
PCL
20 15 8 7 0
PCH
16
PCU
91
0
K19:K8
(2nd word
K7:K0
(1st word
STACK (21-bits x 31)
Top of STACK
of instruction) of instruction)
Note: PCLATU and PCLATH are not updated with the contents of PCH.
Section 7. Memory
Memory
7
7.3.1 Computed GOTO
A computed GOTO is accomplished by adding an offset to the program counter (ADDWF PCL).
When doing a table read using a computed GOTO method, care should be exercised if the table
location crosses a PCL memory boundary (each 256 byte block) and the PCH memory boundary
(each 64Kbyte block).
A lookup table can be formed with an ADDWF PCL instruction and a group of RETLW 0xnn
instructions. WREG is loaded with an offset into the table before executing a call to that table.
The first instruction of the called routine is the ADDWF PCL instruction. The next instruction executed
will be one of the RETLW 0xnn instructions that returns the value 0xnn to the calling function.
Since the Program Counter is a byte counter (instead of a word counter), adds to the PCL allow
a table size of 128 entries before the PCLATH needs to be modified.
In this method of storing tables in PIC18CXXX devices, only one data value may be stored in
each instruction location, and room on the return address stack is required. A better method of
storing data in program memory is through the use of table reads and writes. Two bytes of data
can now be stored in each instruction location.
7.4 Lookup Tables
Look-up tables instructions are implemented two ways in the PIC18CXXX devices. The computed
goto is compatible with the PIC16CXXX and PIC17CXXX parts. Code written for those
devices will run on the PIC18CXXX devices with minor modifications.
Table read instructions are implemented on the PIC17CXXX and PIC18CXXX devices. However,
table operations on the PIC18CXXX work differently than on the PIC17CXXX.
7.4.1 Table Reads/Table Writes
Lookup table data may be stored 2 bytes per program word. By using TBLPTR and TABLAT, data
may be retrieved from program memory one byte at a time as required.
Table writes to program memory can be executed as many times as desired. Remember that the
technology of the program memory determines the outcome of the table write. Table writes to
EPROM memory allow the program memory cell to go from a ’1’ state to a ’0’ state, but not the
other direction. FLASH memory allows the cell to go from a ’1’ to a ’0’ and a ’0’ to a ’1’ (though
typically a program memory word or block location is always written).
Note: Since the Program Counter is 21-bits, the uppercase PCLATU register may also
need to be modified when doing computed gotos.
Note: Any write to the Program Counter (PCL) will cause the contents of PCLATU and
PCLATH to be loaded into PCU and PCH, respectively.
Section 7. Memory
Memory
7
Example 7-3: PIC16CXXX Table Lookup
CLRF CNTR ;
TABLELP
MOVF CNTR, W ; Place value in
; WREG register
CALL TABLE1
MOVWF INDF
INCF FSR
INCF CNTR
BTFSS CNTR, 3 ; CNTR = 00001000b?
GOTO TABLE_LP
:
:
TABLE1
ADDWF PCL ; Enusure that table does
; not cross 256 byte
; page boundary.
RETLW ’G’
RETLW ’O’
RETLW ’ ’
RETLW ’M’
RETLW ’C’
RETLW ’H’
RETLW ’P’
RETLW ’!’
Section 7. Memory
Memory
7
7.6 Data Memory Organization
Data memory is made up of the Special Function Registers (SFR) area and the General Purpose
Registers (GPR) area. The SFRs are used for control and status of the microcontroller and
peripheral functions, while GPRs are the general area for user data storage and scratch pad
operations.
Each register has a 12-bit address. This allows up to 4096 bytes of data memory. This memory
is partitioned into 16 banks of 256 bytes that contain the General Purpose Registers (GPRs) and
Special Function Registers (SFRs).
The data memory can be banked for both the GPR and SFR areas. Banking requires the use of
BSR Register. Figure 7-6 shows the data memory map organizations, while Table 7-2 shows
which banks will be used depending on the memory size of the devices.
SFRs start at the last location of Bank 15 (0xFFF) and work up. Once the SFR space ends, any
lower locations in that bank may be implemented as GPRs. GPRs start at the first location of
Bank 0 (0h) and work down. Any read of an unimplemented location will read as ’0’s.
The Instruction set and architecture allows operations across all banks. To move values from one
register to another register, the MOVFF instruction can be used. This is a two word / two cycle
instruction.
The entire data memory can be accessed either directly or indirectly. Direct addressing may
require the use of the BSR register. Indirect addressing requires the use of the File Select Registers
(FSRs). Each FSR holds a 12-bit value that can access any location in the Data Memory
map.
To ensure that commonly used registers (SFRs and select GPRs) can be accessed in a single
cycle, regardless of the current BSR values, an Access Bank is implemented. This is explained
in Section 7.6.1.
The GPR area is banked to allow greater than 256 bytes of general purpose RAM to be
addressed. SFRs are for the registers that control the peripheral and core functions.
Figure 7-6: The Data Memory Map and the Access Bank
Bank 0
Bank 1
Bank 14
Bank 15
BSR<3:0> Data Memory Map
= 0000b
= 0001b
= 1110b
= 1111b
00h
FFh
00h
FFh
Access Bank
When a = 0,
the BSR is ignored and this Access
Bank is used.
The first 128 bytes are General Purpose
RAM (from Bank 0).
The second 128 bytes are Special
Function Registers (from Bank 15).
See Section 7.6.1.
When a = 1,
the BSR is used to specify the RAM
location that the instruction uses.
Bank n
Section 7. Memory
Memory
7
7.6.3 Special Function Registers
The SFRs are used by the CPU and peripheral modules for controlling the desired operation of
the device. These registers are implemented as static RAM.
The SFRs can be classified into two sets; those associated with the “core” function and those
related to the peripheral functions. Those registers related to the “core” are described in this section,
while those related to the operation of the peripheral features are described in the section
of that peripheral feature.
The SFRs are typically distributed among the peripherals whose functions they control.
If the SFRs do not use all the available locations on a particular device, the unused locations will
be unimplemented and read as '0's. In devices that have a high integration of features, some of
the SFRs may be in banks other than bank 15. See Figure 7-7 for addresses for the SFRs. As
new devices are introduced with new SFRs, this register map will be updated. Please refer to the