PIC Microcontroller L1-L6
PIC Microcontroller L1-L6
General-purpose microprocessor
• CPU for Computers
• No RAM, ROM, I/O on CPU chip itself
• Example:Intel’s x86, Motorola’s 680x0
*source: https://robu.in/pic16f-vs-pic18fanalysis-pic-mid-range-8-
bit-family-advanced-8-bits-family/
PIC 18F
*Source PIC Microcontroller and Embedded Systems Using Assembly and C for PIC18 MA Mazidi,
Rolin D Mckinlay, Danny Causey. Pearson International Edition.
PIC18FXXX High End Microcontroller
• 16-bit instructions
• a deeper stack, and an interrupt system that can
handle internal interrupts as well as several
inputs for external interrupts.
• Some of these PICs have an open architecture.
This allows the increase of both program memory
• and data memory.
• The number of available devices for high-end
microcontrollers is also larger.
PIC18Fxxx Features
• Microcontrollers that belong to the PIC18 family have flash memory and
an instruction set of 77 instructions of 16 bits each.
• Their program memory can have a size of up to 2 MB, and their data
memory can reach 4096 registers of 8 bits in each one.
• Some members of the PIC18 family allow external memory to store
program memory.
• Their stack is 31 levels deep;
PIC18Fxxx Architecture*
PIC Architecture
• PIC 18F has Harvard Architecture and is a RISC
type of processor.
• It uses two separate address buses to access code
and data memory unlike von-Neumann
architecture where both code and data memories
are accessed through same buses.
• This increases speed of processing. Apart from
this the Reduced Instruction Set reduces the
number of transistors required to decode the
instructions.
PIC Architecture
• Almost all the instructions in the PIC18 are 2-byte instructions
leaving few instructions which are 4 byte.
• PIC being a RISC processor has a large number of registers.
• RISC processors have a small instruction set. RISC processors
have only the basic instructions such as ADD, SUB, MUL,
LOAD, STORE, AND, OR, EXOR, CALL, JUMP, and so on.
• The program may get lengthy if written in
assembly language, but the most important
characteristic of the RISC processors is that
more than 95% of instructions are executed
with only one clock cycle
• The speed of execution further increases with
code scheduling and pipelining features.
• Program Counter (PC) keeps track of the program execution by
holding the address of the current instruction. It is automatically
incremented to the next instruction during the current instruction
execution.
• The stack is used to store the address of the next instruction
immediately below the CALL instruction.
• The CALL instruction is used to jump to a subroutine, which must be
terminated with the RETURN instruction. CALL has the address of
the first instruction in the subroutine as its operand.
• When the CALL instruction is executed, the destination address is
copied to the PC. The PC is PUSHed onto the stack when a CALL
instruction is executed, or an interrupt causes a branch. The stack is
POP’ed in the event of a RETURN, RETLW or a RETFIE instruction
execution.
Main CPU Registers
The CPU registers are
• Working Register (Wreg)
• Status – Register; psw
• FSR – File Select Register
• INDF
• Program Counter
• Stack
Working (Wreg) Register
• The working register in PIC has features similar
to Accumulator of Intel 8051 microcontroller.
• It is an eight bit register.
• The Wreg as destination register is not fixed in
arithmetic and logical instructions unlike
accumulator in 8051.
Examples showing use of Wreg
• The MOVLW instruction format is
• MOVLW K; K is a literal 8 bit value to be
moved into working register W.
• The instruction stands for: Move the literal
value specified by K into register W.
• Ex MOVLW 20H will move 20 H into register
W.
• MOVLW 40H ; MOVE THE LITERAL VALUE IN
WORKING REGISTER
• MOVWF 30H ; WREG INTO FILE LOCATION
WHOSE ADDRESS IS 30H
Arithmetic and logic unit (ALU)
The ALU executes the arithmetic and logic operations available in the instruction set.
There is one register associated with the ALU that temporarily stores at least one
operand involved in the operation, as well as the result of that operation.
The ALU also has bits to indicate specific characteristics of the resulting value, such as
if the result is zero, the sign of the resulting value, or the existence of carry over. These
bits are normally part of the STATUS register.
In most microprocessors and microcontrollers the register associated with the ALU is
called the accumulator (ACC). In PIC microcontrollers the register associated with the
ALU is called the W register. The W register carries out tasks similar to the ACC, it is
positioned in a different place. Therefore, the ACC and the W register do not operate
in the same way.
In traditional architectures, the ACC is placed at the output of the ALU, so it always
stores the result of an arithmetic or logic operation. In PIC microcontrollers, however,
the result of an operation can either be placed in the W register or in any register in
the data memory
ALU OPERATIONS
ADDLW 0AFH
Instruction Execution and Machine cycle
There are three phases in the execution of a program
instruction: fetch, decode, and execution
Pipe Lining
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’).
To maintain alignment with instruction boundaries, the PC increments in steps of 2.
The CALL and GOTO instructions have an absolute program memory address embedded
into the instruction.
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
shows how the instruction “GOTO 000006h’ is encoded in the program memory. The
offset value stored in a branch instruction represents the number of single word
instructions that the PC will be offset by.
• The PIC18FXX2 devices have four two-word
instructions: MOVFF, CALL, GOTO and LFSR.
Return Address Stack
• The return address stack allows any combination of up to 31 program calls and interrupts to
occur. The PC (Program Counter) is pushed onto the stack when a CALL or RCALL
instruction is executed, or an interrupt is acknowledged. The PC value is pulled off the stack
on a RETURN, RETLW or a RETFIE instruction.
• PCLATU and PCLATH are not affected by any of the RETURN or CALL instructions.
• The stack operates as a 31-word by 21-bit RAM and a 5-bit stack pointer, with the stack
pointer initialized to 00000b after all RESETS.
• There is no RAM associated with stack pointer 00000b. This is only a RESET value.
• During a CALL type instruction, causing a push onto the stack, the stack pointer is first
incremented and the RAM location pointed to by the stack pointer is written with the contents
of the PC.
• During a RETURN type instruction, causing a pop from the stack, the contents of the RAM
location pointed to by the STKPTR are transferred to the PC and then the stack pointer is
decremented.
• The stack space is not part of either program or data space. The stack pointer is readable and
writable, and the address on the top of the stack is readable and writable through SFR
registers. Data can also be pushed to, or popped from, the stack using the top-of-stack SFRs.
TOP-OF-STACK ACCESS
• The top of the stack is readable and writable.
• Three register locations TOSU, TOSH and TOSL hold the
contents of the stack location pointed to by the STKPTR
register.
• This allows users to implement a software stack if
necessary.
• After a CALL, RCALL or interrupt, the software can read the
pushed value by reading the TOSU, TOSH and TOSL
registers.
• These values can be placed on a user defined software
stack.
• At return time, the software can replace the TOSU, TOSH
and TOSL and do a return.
RETURN STACK POINTER (STKPTR)
RETURN STACK POINTER (STKPTR)
RETURN STACK POINTER (STKPTR)
• The STKPTR register contains the stack pointer value, the STKFUL (stack full) status bit, and the
STKUNF
• (stack underflow) status bits.
• The value of the stack pointer can be 0 through 31. The stack pointer increments when values are pushed
onto the stack and decrements when values are popped off the stack.
• At RESET, the stack pointer value will be 0. The user may read and write the stack pointer value.
• After the PC is pushed onto the stack 31 times (without popping any values off the stack), the STKFUL bit
is set. The STKFUL bit can only be cleared in software or by a POR.
• The action that takes place when the stack becomes full depends on the state of the STVREN (Stack
Overflow
• Reset Enable) configuration bit. If STVREN is set (default), the 31st push will push the (PC + 2) value onto
the stack, set the STKFUL bit, and reset the device. The STKFUL bit will remain set and the stack pointer
will be set to ‘0’.
• If STVREN is cleared, the STKFUL bit will be set on the 31st push and the stack pointer will increment to
31.
• Any additional pushes will not overwrite the 31st push, and STKPTR will remain at 31.
Data Memory Organization Map
Data Memory Organization
• The data memory is implemented as static RAM. Each register in the data memory has a 12-bit address,
allowing up to 4096 bytes of data memory.
• The data memory map is divided into as many as 16 banks that contain 256 bytes each.
• The lower 4 bits of the Bank Select Register (BSR<3:0>) select which bank will be accessed.
• The upper 4 bits for the BSR are not implemented.
• The data memory contains Special Function Registers (SFR) and General Purpose Registers (GPR).
• The SFRs are used for control and status of the controller and peripheral functions, while GPRs are used for data
• storage and scratch pad operations in the user’s application.
• The SFRs start at the last location of Bank 15 (0xFFF) and extend downwards.
• Any remaining space beyond the SFRs in the Bank may be implemented as GPRs. GPRs start at the first
location of Bank 0 and grow upwards.
• Any read of an unimplemented location will read as ’0’s.
• The entire data memory may be accessed directly or indirectly.
• Direct addressing may require the use of the BSR register.
• Indirect addressing requires the use of a File Select Register (FSRn) and a corresponding Indirect File Operand
(INDFn).
• Each FSR holds a 12-bit address value that can be used to access any location in the Data Memory map without
banking.
• The instruction set and architecture allow operations across all banks.
• This may be accomplished by indirect addressing or by the use of the MOVFF instruction. The MOVFF
instruction is a two-word/two-cycle instruction that moves a value from one register to another.
• 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.
• A segment of Bank 0 and a segment of Bank 15 comprise the Access RAM.
GENERAL PURPOSE REGISTER FILE
• The register file can be accessed either directly or
indirectly.
• Indirect addressing operates using a File Select
Register and corresponding Indirect File Operand.
• Data RAM is available for use as GPR registers by
all instructions.
• The top half of Bank 15 (0xF80 to 0xFFF) contains
SFRs.
• All other banks of data memory contain GPR
registers, starting with Bank 0.
SPECIAL FUNCTION REGISTERS
• The Special Function Registers (SFRs) are registers 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..
• The SFRs are typically distributed among the
peripherals whose functions they control.
• The unused SFR locations will be unimplemented and
read as '0's.
SPECIAL FUNCTION REGISTERS MAP
SPECIAL FUNCTION REGISTERS SUMMARY
SPECIAL FUNCTION REGISTERS SUMMARY
Access Bank
• While the use of the BSR, with an embedded 8-bit address, allows users to address the entire
range of data memory, it also means that the user must always ensure that the correct bank
is selected. Otherwise, data may be read from or written to the wrong location.
• To streamline access for the most commonly used data memory locations, the data memory
is configured with an Access Bank, which allows users to access a mapped block of memory
without specifying a BSR.
• The Access Bank is comprised of the upper 128 bytes in Bank 15 (SFRs) and the lower 128
bytes in Bank 0.
• These two sections will be referred to as Access RAM High and Access RAM Low, respectively.
• A bit in the instruction word specifies if the operation is to occur in the bank specified by the
BSR register or in the Access Bank.
• This bit is denoted by the ’a’ bit (for access bit).
• When forced in the Access Bank (a = 0), the last address in Access RAM Low is followed by
the first address in Access RAM High.
• Access RAM High maps the Special Function registers, so that these registers can be
accessed without any software overhead. This is useful for testing status flags and modifying
control bits.
Access Bank
The Access Bank is an architectural enhancement THAT ALLOWS data memory region can be used for:
• Intermediate computational values
• Local variables of subroutines
• Faster context saving/switching of variables
• Common variables
• Faster evaluation/control of SFRs (no banking)
Direct addressing (BSR role)
• The data memory is partitioned into sixteen
banks. When using direct addressing, the BSR
should be configured for the desired bank.
• BSR<3:0> holds the upper 4 bits of the 12-bit
RAM address. The BSR<7:4> bits will always
read ’0’s, and writes will have no effect.
• BSR (address 0xFE) refer page 48 of datasheet
• A MOVLB instruction has been provided in the
• instruction set to assist in selecting banks.
DIRECT ADDRESSING
Indirect Addressing, INDF and FSR Registers