Chapter 6

Chapter 6
Bank Switching,
Table
Processing,
Macros, and
Modules
Addressing Modes
• Addressing Modes  The ways of accessing data.
• The PIC18 provides a total of four distinct addressing
modes.
–
–
–
–
Immediate
Direct
Register indirect
Indexed-ROM
0-2
Immediate Addressing Modes
• The operand is a literal constant.
• The operand comes immediately after the opcode
when the instruction is assembled.
MOVLW 0x25
SUBLW
D’62’
ANDLW B’01000000’
0-3
Direct Addressing Modes
• The operand data is in a RAM memory location whose
address is know, and this address is given as a part the
instruction.
MOVLW 0x56
MOVWF 0x40
MOVFF
0x40, 0x50
• Also called register direct mode to contrast with the
register indirect mode.
0-4
MOVFF Direct Addressing
Opcode
MOVWF Direct Addressing
Opcode
• File register data RAM does not support immediate
addressing mode. It must first move to WREG.
INCF fileReg, W v.s. INCF fileReg, F
SFR Registers and their addresses
Register indirect Addressing
Modes
• A register is used as a pointer to the data RAM
location.
• In the PIC18, three registers are used for this purpose:
FSR0, FSR1, FSR2.
• FSR stands for file selector register.
• The FSR is a 12-bit register allowing access to the
entire 4096 bytes of data RAM space in the PIC18.
• We use LFSR (load FSR) to load the RAM address.
LFSR
0, 0x30
0-10
Register indirect Addressing
Modes
• The FSR registers have the low-byte and high-byte
parts called FSRxL and FSRxH. Note that the FSRxH
is only 4-bit and the upper 4 bits are not used.
• Each of the FSRx has an INDFx (indirect register) to
represent the RAM location pointed to by the FSRx.
LFSR
0, 0x30
MOVWF INDF0
0-11
Advantage of Indirect Mode
• One of the advantage of register indirect addressing
mode is that it makes accessing data dynamic rather
than static.
• Looping is not possible in direct addressing mode.
0-12
Auto-increment option for FSR
• The PIC18 give us the options of auto-increment and
auto-decrement for FSRn.
• INDF0, POSTDEC0, PLUSW0 , etc., registers are
physically implemented memory locations.
0-17
Fixed Data in ROM  DB
• The DB data directive is widely used to allocate ROM
program memory in byte-sized chunks.
0-21
Table Processing
• We need to have a special function register to point to
the data placed in ROM.
• This is called register indirect ROM addressing mode.
• TBLPTR is a 21-bit register that used to point to the
byte to be fetched.
• TBLPTR is divided into three 8-bit parts: TBLPTRL,
TBLPTRH, and TBLPTRU.
• TABLAT (TABle LATch) is the register used for
keeping the byte once it is fetched.
0-23
Simplified view of a PIC
microcontroller
24
Auto-Increment option for TBLPTR
• The PIC18 gives us the instructions to
increment/decrement TBLPTR.
0-25
Look-Up Table and RETLW
• The look-up table allows access to elements of a
frequently used table.
• The RETLW instruction will provide the desired lookup table element in the WREG register.
0-32
Look-Up Table in RAM
• The look-up table can also in RAM instead of ROM.
• We use the FSR as a pointer.
• For example, MOVFF PLUSW2, PORTD. In this
case, WREG is used as an index into the look-up table.
0-37
Writing Table Elements in PIC18
• In PIC18 we also have the TBLWRT instruction to
write data into program ROM space.
• The TBLWRT can be used only with PIC18 chips that
have flash ROM for the program ROM space.
0-41
Bit-Addressability of Data RAM
• The entire 4096 bytes of file register data RAM of the
PIC18 are bit addressable.
• The PIC18 provides two categories of instructions:
bit-oriented and byte-oriented instructions.
• The bit-oriented instructions use only one addressing
mode, the direct addressing mode.
0-42
Status Register Bit-Addressability
Bank Switching
• The instruction MOVWF fileReg is really MOVWF
fileReg, A.
• If A = 0, the access bank is the default bank.
• If A = 1, the instruction will use the BSR (bank
selector register) to select the bank insteead of using
the access bank.
• Only the 4 least-significant bits of the BSR are used in
the PIC18.
0-49
Data RAM Registers
Bank Switching
• MOVLB loads the bank number into the BSR register.
• Although we can use any of the addressing modes to
access the GP register regions, we use direct
addressing mode in accessing SFR registers.
• INCF MTREG, F, 1 vs INCF MTREG, F, 0
0-51
A Bit in the Instruction Field for
INCF
MOVFF and banks
• MOVFF can move data anywhere within the 4K RAM.
• There is no need to worry about bank switching.
0-56
Checksum Byte in ROM
• To ensure the integrity of ROM contents.
• Checksum byte is calculated by:
– Add the bytes together and drop the carriers.
– Take the 2’s complement of the total sum.
• A checksum operation:
– Add all the bytes including the checksum byte. The result
must be zero.
0-60
Program Examples
• Prog 61: Checksum program
• Prog 62: BCD to ASCII conversion program
• Prog 63: Binary to ASCII conversion program
0-62
Macro definition
• Macros allow the programmer to write the task once
only, and to invoke it whenever it is needed.
• name
MACRO dummy1, dummy2,…, dummyN
0x55
ENDM
0-63
LOCAL Directive
• If a macro is expanded more than once in a program,
all labels used in macro body must be declared as
LOCAL.
• The LOCAL directive must be right after the MACRO
directive.
• LOCAL name1, name2, name3
or
LOACL name1
LOACL name2
LOACL name3
0-64
INCLUDE Directive
• The INCLUDE directive allows a programmer to write
macros and save them in a file, and later bring them
into any program file.
0-66
NOEXPAND/EXPAND Directive
• When viewing the .lst file with macros, we see them
fully displayed.
• The expand directive is set by.
• Using the noexpand directive, we can turn off the
display of macros in the list file.
0-67
List File with NOEXPAND Option
for Program 6-4
List File with EXPAND Option for
Program 6-4
List File with EXPAND Option for
Program 6-4 (cont.)
Macros vs. Subroutines
• Macros increase code size every time they are invoked.
• Subroutines use stake space when called.
• The nested call can lead to a stack overflow.
0-71
Modules
• EXTERN directive:
– To notify the assembler and linker that certain names and
variables that are not defined in the present module are
defined externally somewhere else.
• GLOBAL directive:
– To allow assembler and linker to match names and variables
with their EXTERN counterparts.
• Linking modules together.
0-72