Unit 4
Unit 4
This improves bandwidth over traditional Von Neumann architecture where program and
data may be fetched from the same memory using the same bus. Separating program and data
buses further allows instructions to be sized differently than 8-bit wide data words.
Instruction opcodes are 14-bits wide making it possible to have all single word instructions.
A 14-bit wide program memory access bus fetches a 14-bit instruction in a single cycle. A
two-stage pipeline overlaps fetch and execution of instructions. Consequently, all instructions
execute in a single cycle (200 ns@ 20MHz) except for program branches.4
The PIC 16F87X devices have a 13-bit program counter capable of addressing an
8KX14program memory space. The PIC 16FF876/877 devices have 8Kx 14 words of Flash
program memory .The RESET vector is at 0000h and the Interrupt vector is at 0004h.
The general purpose register file is another name for the microcontroller’s RAM .
Data can be written to each 8-bit location updated and retrieved any number of times.
The special function register file consists of input, output ports and control registers
used to configure each 8-bit port either as input or output. It contains registers that provide
the data input and data output to a chip resources like Timers, Serial Ports and Analog to
Digital converter and also the registers that contains control bits for selecting the mode of
operation and also enabling or disabling its operation.
CPU REGISTERS
The CPU registers are used in the execution of the instruction of the PIC microcontroller. The
PIC PIC16F877 Microcontroller has the following registers.
2. Status Register
4. INDF
5. Program Counter
C: Carry/borrow bit
Z: Zero bit
RPO: Register bank Select The bits 7 and 6 of Status Register are unused by 16c6x/7x. The
‘C’ bit is set when two 8-bit operands are added together and a 9-bit result occurs. This 9-bit
is placed in the carry bit.
The DC or Digit carry bit indicates that a carry from the lower 4 bits occurred during an 8-bit
addition. Example: 0011 1000 0011 1000 0111 0000 Here DC=1 as a result of the carry from
the bit 3 to the bit 4 position.8
The Z or zero bits is affected by the execution of arithmetic or logic instructions. The reset
status bits NOT_TO and NOT_PD are used in conjunction with PIC’s sleep mode. The micro
controller can put itself to sleep mode to save power during intervals when it has nothing to
do. It can be reset by any of three kinds. Upon reset the CPU can check these two reset status
bits to determine which kind of event resettled it and then respond accordingly. The Register
bank select bit RPO is used to select either bank or bank. When RPO=0,select Bank 0,
RPO=1, select Bank 1.
It is the pointer used for indirect addressing. In the indirect addressing mode the 8-
bitregister file address is first written into FSR. It is a special purpose register that serves as
an address pointer to any address throughout the entire register file.
[Link] – (Indirect File): It is not a physical register addressing but this INDF will cause
indirect addressing. Any instructions using the INDF register actually access the register
pointed to by the FSR.
5 .PROGRAM COUNTER
PIC PIC16F877A has a 13 bit program counter in which PCL is the lower 8-bits of
the PC and PCLATH is the write buffer for the upper 5 bits of the PC.
PCLATH (program counter Latch can be read or from or written to without affecting the
Program Counter(PC).The upper 3 bits of PCLATH remain zero. It is only when PCL is
written to that PCLATH is automatically written into the PC at the same time.
I/O Ports
Most of the PIC16cx/7x family controllers have 33 I/O lines and five I/O ports They are
PORT A, PORT B, PORT C , PORT D and PORT E.
PORT A: Port A is a 6-bit wide bi-directional port. Its data direction register is TRISA setting
TRISA bit to 1 will make the corresponding PORT A Pin an input. Clearing a TRIS a bit will
make the corresponding pin as an output.
PORT B: Port B is an 8-bit wide, bi-directional port. Four of the PORT B pins RB 7 – RB4
have an interrupt-on- change feature. Only the pins configured as inputs can cause this
interrupt to occur.
PORT C: Port C is an 8-bit wide, bidirectional port. Bits of the TRISC Register determine the
function of its pins. Similar to other ports, a logic one 1 in the TRISC Register configures the
appropriate port pin as an input.
PORT D: Port D is an 8-bit wide bi-directional port. In addition to I/O port, Port D also
works as8-bit parallel slave port or microprocessor port. When control bit PSPMODE
(TRISE:4) is set.
PORT E: It is a 3-bit bi-directional port. Port E bits are multiplexed with analog inputs of
ADCand they serve as control signals (RD , WR, CS) for parallel slave port mode of
operation.
TIMER MODULES:
There are three completely independent Timers available in PIC 16F8XX Microcontrollers.
The CCP module(s) can operate in one of three modes 16-bit capture, 16-bit
compare,or up to 10-bit Pulse Width Modulation (PWM)
Capture mode captures the 16-bit value of TMR1 into the CCPRxH:CCPRxL register
pair. The capture event can be programmed for either the falling edge, rising edge, fourth
rising edge, or sixteenth rising edge of the CCPx pin.
INTERRUPTS:
• Watchdog Timer (WDT) can be helpful to automatically reset the system whenever a
timeout occurs.
• System reset is required for preventing failure of the system in a situation of a
hardware fault or program error.
To execute an instruction, the processor copies the instruction code from the program
memory into the instruction register (IR). It can then be decoded (interpreted) by
the instruction decoder, which is a combinational logic block which sets up the processor
control lines as required.
These control lines are not shown explicitly in the block diagram, as they go to all
parts of the chip, and would make it too complicated.
In the PIC, the instruction code includes the operand (working data), which may be a
literal value or register address. For example, if a literal (a number) given in the instruction is
to be loaded into the working register (W), it is placed on an internal data bus and the
W register latch enable lines are activated by the timing and control logic.
POWER UP TIMER:
The Power-up Timer (PWRT) provides a nominal 72-millisecond delay after a Power
On Reset (POR), Brown Out Reset (BOR), or after the Master Clear Pin External Reset
(MCLR) pin reset is initiated. The PWRT operates on a dedicated internal RC oscillator. The
device is kept in reset as long as the PWRT has not timed out. The PWRT allows the Vdd
voltage of the PIC®MCU to rise to an acceptable level before operating.
POWER ON RESET:
When a PIC® MCU is first powered up, it will run through some defined hardware
functions internally to prepare the MCU for proper operation. This can include the oscillator
start-up and power stabilization. The Power On Reset will hold the device in reset while these
operations happen. It prevents the device from running any software until a minimum level
Vdd voltage threshold is met and the oscillator is stable.
BOR will hold a PIC® MCU in reset when the Vdd drops below a brown out
threshold voltage. Not all devices have BOR, but most do, and some have multiple voltage
thresholds to select from. Between a BOR and Power On Reset the whole range of startup
voltages can be covered to protect for proper operation after a power drop at the Vdd line. It
is also recommended that the Power-Up Timer (PWRT) be enabled to increase the delay in
returning from a BOR event.
MEMORY ORGANIZATION OF PIC 16F877A
Memory of the PIC16F877 divided into 3 types of memories:
• Program Memory - A memory that contains the program (which we had written), after
we've burned it. As a reminder, Program Counter executes commands stored in the
program memory, one after the other.
• Data Memory – This is RAM memory type, which contains a special registers like SFR
(Special Faction Register) and GPR (General Purpose Register). The variables that we
store in the Data Memory during the program are deleted after we turn of the micro.
These two memories have separated data buses, which makes the access to each one of
them very easy.
Each one of them has a different role. Program Memory and Data Memory two memories
that are needed to build a program, and Data EEPROM is used to save data after the
microcontroller is turn off.
Program Memory and Data EEPROM they are non-volatile memories, which store the
information even after the power is turn off. These memories called Flash Or EEPROM. In
contrast, Data Memory does not save the information because it needs power in order to
maintain the information stored in the chip.
The PIC16F87XA devices have a 13-bit program counter capable of addressing an 8K word x
14 bit program memory space. This memory is used to store the program after we burn it to
the microcontroller. The PIC16F876A/877A devices have 8K words x 14 bits of Flash
program memory that can be electrically erased and reprogrammed. Each time we burn
program into the micro, we erase an old program and write a new one.
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 PIC16F87XA family has an 8-level deep x 13-bit wide hardware stack. The stack space
is not part of either program or data space and the stack pointer is not readable or writable. In
the PIC microcontrollers, this is a special block of RAM memory used only for this purpose.
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.
The stack operates as a circular buffer. This means that after the stack has been PUSHed
eight times, the ninth push overwrites the value that was stored from the first push. The tenth
push overwrites the second push (and so on).
Each time the main program execution starts at address 0000 - Reset Vector. The address
0004 is “reserved” for the “interrupt service routine” (ISR).
If we plan to use an interrupt, our program will begin after the Interrupt Vector; and if not we
can start to write from the beginning of the Reset Vector.
main
Some of the memory is divided into the pages that are designed for write/burn the program
into them; the remaining memory (Stack, Interrupt Vector, and Reset Vector) is hardware
registers.
Attention!
Program Memory is divided into the pages, where the program is stored. Data Memory is
divided into the banks. The banks are located inside the RAM, where the special registers and
the data located.
The data memory is partitioned into multiple banks which contain the General Purpose
Registers and the Special Function Registers. Number of banks may vary depending on the
microcontroller; for example, micro PIC16F84 has only two banks.
Each bank extends up to 7Fh (128 bytes). The lower locations of each bank are reserved for
the Special Function Registers. Above the Special Function Registers are General Purpose
Registers, implemented as static RAM. While program is being executed, it is working with
the particular bank. The default bank is BANK0.
To access a register that is located in another bank, one should access it inside the program.
There are special registers which can be accessed from any bank, such as STATUS register.
ADDRESSING MODES
Direct and Indirect addressing
Direct Addressing: Using this method we are accessing the registers directly by detecting
location inside Data Memory from Opcode and by selecting the bank using bits RP1 and RP0
of the STATUS register.
Indirect Addressing: To implement indirect addressing, a File Select Register (FSR) and
indirect register (INDF) are used. In addition, when using this method we choose bank using
bit IRP of the STATUS register. Indirect addressing treated like a stack pointer, allowing
much more efficient work with a number of variables. INDF register is not an actual register
(it is a virtual register that is not found in any bank).
There is SFR (Special Function Register) - special registers of RAM, and there is FSR (File
Select Register).The following figure shows the two addressing methods:
To the left you can see the direct addressing method, where the bank selection is made by RP
bits and the referencing is made directly from memory Opcode by using the variable name.
To the right you can see the indirect addressing method, where the bank selection is made by
IRP bit and accessing the variable by pointer FSR.
We want to assign number 5 to the variable TEMP located at address 0X030. In the first row
of each example, we will define the variable TEMP at the address 0X030.
It's easy to understand, that direct addressing method means working directly with the
variables. In the second line we put the number 5 into the working register W, and in the line
3, the content of the W passes to the TEMP variable .
In the second line, we put a value into the W register. In the third line, the value passes to the
FSR register, and from this moment FSR points to the address of the TEMP variable. In the
fourth line, the number 5 passes to the W register, and in the fifth line, we move the contents
of W register (which is 5) to the INDF. In fact INDF performs the following: it takes the
number 5 and puts it in the address indicated by FSR register.
The data EEPROM and Flash program memory is readable and writable during normal
operation (over the full VDD range). This memory is not directly mapped in the register file
space. Instead, it is indirectly addressed through the Special Function Registers.
There are six SFRs used to read and write to this memory:
1. EECON1
2. EECON2
3. EEDATA
4. EEDATH
5. EEADR
6. EEADRH
When interfacing to the data memory block, EEDATA holds the 8-bit data for read/write and
EEADR holds the address of the EEPROM location being accessed. These devices have 128
or 256 bytes of data EEPROM (depending on the device), with an address range from 00h to
FFh. On devices with 128 bytes, addresses from 80h to FFh are unimplemented.
At this point there is no need to learn how to use this memory with special registers, because
there are functions (writing and reading) that are ready.
Instruction Set
It has been already mentioned that microcontrollers differs from other integrated circuits.
Most of them are ready for installation into the target device just as they are, this is not the
case with the microcontrollers. In order that the microcontroller may operate, it needs precise
instructions on what to do. In other words, a program that the microcontroller should execute
must be written and loaded into the microcontroller. This chapter covers the commands
which the microcontroller "understands". The instruction set for the 16FXX includes 35
instructions in total. Such a small number of instructions is specific to the RISC
microcontroller because they are well-optimized from the aspect of operating speed,
simplicity in architecture and code compactness. The only disadvantage of RISC architecture
is that the programmer is expected to cope with these instructions.
Move constant to
MOVLW k k -> w 1
W
f(7:4),(3:0) ->
SWAPF f,d Swap nibbles in f 1 1, 2
f(3:0),(7:4)
Arithmetic-logic Instructions
Add W and
ADDLW k W+k -> W C, DC, Z 1
constant
Subtract W from
SUBLW k k-W -> W C, DC, Z 1
constant
Logical OR with
IORLW k W OR k -> W Z 1
W with constant
Logical OR with
IORWF f,d W OR f -> d Z 1 1, 2
W with f
Logical exclusive
XORLW k OR with W with W XOR k -> W Z 1 1, 2
constant
Logical exclusive
XORWF f,d W XOR f -> d Z 1
OR with W with f
Rotate left f
RLF f,d through CARRY C 1 1, 2
bit
Rotate right f
RRF f,d through CARRY C 1 1, 2
bit
Bit-oriented Instructions
Test bit b of f.
Skip the
Skip if f(b)
BTFSC f,b following 1 (2) 3
=0
instruction if
clear.
Test bit b of f.
Skip the
Skip if f(b)
BTFSS f,b following 1 (2) 3
=1
instruction if
set.
Increment f.
Skip the
f+1 -> d skip
INCFSZ f,d following 1 (2) 1, 2, 3
if Z = 0
instruction if
set.
PC -> TOS,
CALL k Call subroutine 2
k -> PC
Return from
RETURN TOS -> PC 2
subroutine
Other instructions
0 -> WDT, 1
Clear watchdog
CLRWDT -> TO, 1 -> TO, PD 1
timer
PD
0 -> WDT, 1
Go into sleep
SLEEP -> TO, 0 -> TO, PD 1
mode
PD
Arithmetic-logic Instructions
Similar to most microcontrollers, PIC supports only two arithmetic instructions- addition and
subtraction. Flags C, DC, Z are automatically set depending on the results of addition or
subtraction. The only exception is the flag C. Since subtraction is performed as addition with
negative value, the flag C is inverted after subtraction. It means that the flag C is set if it is
possible to perform operation and cleared if the larger number is subtracted from smaller one.
Logic one (1) of the PIC is able to perform operations AND, OR, EX-OR, inverting (COMF)
and rotation (RLF and RRF). Instructions which rotate a register actually rotate its bits
through the flag C by one bit left (toward bit 7) or right (toward bit 0). The bit shifted from
the register is moved to the flag C which is automatically moved to the bit on the opposite
side of the register.
Bit-oriented Instructions
Instructions BCF and BSF clear or set any bit in memory. Although it seems to be a simple
operation, it is not like that. CPU first reads the entire byte, changes one its bit and rewrites
the whole byte to the same location.
Legend
d - Destination bit; [label] - Set of 8 characters indicating start of particular address in the
program;
Description: The content of the register W is added to the 8-bit literal k. The result is stored
in the W register.
Operand: 0 ≤ k ≤ 255
Number of cycles: 1
EXAMPLE:
ADDLW 0x15
Before instruction execution: W=0x10
After instruction: W=0x25
C=0 (the result is not greater than 0xFF, which means that Carry has not occurred).
Description: Add the contents of the W and f registers. If d = w or d = 0 the result is stored
in the W register.
If d = f or d = 1 the result is stored in register f.
Number of cycles: 1
EXAMPLE 1:
ADDWF REG,w
Before instruction execution: W = 0x17
REG = 0xC2
After instruction: W = 0xD9
REG = 0xC2
C=0 (No carry occurs, i.e. the result is maximum 8-bit long).
EXAMPLE 2:
ADDWF INDF,f
Before instruction execution: W=0x17
FSR = 0xC2 Register at address 0xC2 contains the value 0x20
After instruction: W = 0x17
FSR=0xC2, Register at address 0xC2 contains the value 0x37
Description: The content of the register W is AND’ed with the 8-bit literal k. It means that
the result will contain one (1) only if both corresponding bits of operand are ones (1). The
result is stored in the W register.
Operand: 0 ≤ k ≤ 255
Status affected: Z
Number of cycles: 1
EXAMPLE 1:
....
[label] ANDLW 0x5F
Before instruction execution: W = 0xA3 ; 1010 0011 (0xA3)
; 0101 1111 (0x5F)
------------------
After instruction: W = 0x03 ; 0000 0011 (0x03)
Z = 0 (result is not 0)
EXAMPLE 2:
....
[label] ANDLW 0x55
Before instruction execution: W = 0xAA ; 1010 1010 (0xAA)
; 0101 0101 (0x55)
------------------
After instruction: W = 0x00 ; 0000 0000 (0x00)
Z = 1( result is 0)
Status affected: Z
Number of cycles: 1
EXAMPLE 1:
....
[label] ANDWF REG,f
Before instruction execution: W = 0x17, REG = 0xC2 ; 0001 0111 (0x17)
; 1100 0010 (0xC2)
------------------
After instruction: W = 0x17, REG = 0x02 ; 0000 0010 (0x02)
EXAMPLE 2:
....
[label] ANDWF FSR,w
Before instruction execution: W = 0x17, FSR = 0xC2 ; 0001 0111 (0x17)
; 1100 0010 (0xC2)
------------------
After instruction: W = 0x02, FSR = 0xC2 ; 0000 0010 (0x02)
Operand: 0 ≤ f ≤ 127, 0 ≤ b ≤ 7
Status affected: -
Number of cycles: 1
EXAMPLE 1:
....
[label] BCF REG,7
Before instruction execution: REG = 0xC7 ; 1100 0111 (0xC7)
After instruction: REG = 0x47 ; 0100 0111 (0x47)
EXAMPLE 2:
....
[label] BCF INDF,3
Before instruction execution: W = 0x17
FSR = 0xC2
Register at address (FSR)contains the value 0x2F
After instruction: W = 0x17
FSR = 0xC2
Register at address (FSR)contains the value 0x27
Status affected: -
Number of cycles: 1
EXAMPLE 1:
....
[label] BSF REG,7
Before instruction execution: REG = 0x07 ; 0000 0111 (0x07)
After instruction: REG = 0x87 ; 1000 0111 (0x87)
EXAMPLE 2:
....
[label] BSF INDF,3
Before instruction execution: W = 0x17
FSR = 0xC2
Register at address (FSR)contains the value 0x20
After instruction: W = 0x17
FSR = 0xC2
Register at address (FSR)contains the value 0x28
Description: If bit b of register f is 0, the next instruction is discarded and a NOP is executed
instead, making this a two-cycle instruction.
Operand: 0 ≤ f ≤ 127, 0 ≤ b ≤ 7
Status affected: -
EXAMPLE:
....
LAB_01 BTFSC REG,1 ; Test bit 1 of REG
LAB_02 .... ; Skip this line if bit = 1
LAB_03 .... ; Jump here if bit = 0
Before instruction execution: The program counter was at address LAB_01.
After instruction:
- if bit 1 of REG is cleared, program counter points to address LAB_03.
- if bit 1 of REG is set, program counter points to address LAB_02.
Operand: 0 ≤ f ≤ 127, 0 ≤ b ≤ 7
Status affected: -
EXAMPLE:
....
LAB_01 BTFSS REG,3 ; Test bit 3 of REG
LAB_02 .... ; Skip this line if bit = 0
LAB_03 .... ; Jump here if bit = 1
Before instruction execution: The program counter was at address LAB_01
After instruction:
- if bit 3 of REG is cleared, program counter points to address LAB_03.
- if bit 3 of REG is cleared, program counter points to address LAB_02.
Description: Calls subroutine. First the address of the next instruction to execute is pushed
onto the stack. It is the PC+1 address. Afterwards, the subroutine address is written to the
program counter.
Operation: (PC) + 1 -> (Top Of Stack - TOS) k -> PC (10 : 0), (PCLATH (4 : 3)) -> PC (12
: 11)
Operand: 0 ≤ k ≤ 2047
EXAMPLE:
....
LAB_01 CALL LAB_02 ; Call subroutine LAB_02
....
....
LAB_02 ....
Before instruction execution: PC = address LAB_01
TOS (top of stack) = x
After instruction: PC = address LAB_02
TOS (top of stack) = LAB_01
CLRF - Clear f
Syntax: [label] CLRF f
Description: The content of register f is cleared and the Z flag of the STATUS register is
set.
Operation: 0 -> f
Operand: 0 ≤ f ≤ 127
Status affected: Z
Number of cycles: 1
EXAMPLE 1:
....
[label] CLRF TRISB
Before instruction execution: TRISB=0xFF
After instruction: TRISB=0x00
Z=1
EXAMPLE 2:
CLRW - Clear W
Syntax: [label] CLRW
Description: Register W is cleared and the Z flag of the STATUS register is set.
Operation: 0 -> W
Operand: -
Status affected: Z
Number of cycles: 1
EXAMPLE 1:
....
[label] CLRW
Before instruction: W=0x55
After instruction: W=0x00
Z=1
Description: Resets the watchdog timer and the WDT prescaler. Status bits TO and PD are
set.
Operand: -
Number of cycles: 1
EXAMPLE :
....
[label] CLRWDT
Before instruction execution: WDT counter = x
WDT prescaler = 1:128
After instruction: WDT counter = 0x00
WDT prescaler = 0
TO = 1
PD = 1
WDT prescaler = 1: 128
COMF - Complement f
Syntax: [label] COMF f, d
Description: The content of register f is complemented (logic zeros (0) are replaced by ones
(1) and vice versa).
Status affected: Z
Number of cycles: 1
EXAMPLE 1:
....
[label] COMF REG,w
Before instruction execution: REG = 0x13 ; 0001 0011 (0x13)
; complementing
------------------
After instruction: REG = 0x13 ; 1110 1100 (0xEC)
W = 0xEC
EXAMPLE 2:
....
[label] COMF INDF, f
Before instruction execution: FSR = 0xC2
Register at address (FSR)contains the value 0xAA
After instruction: FSR = 0xC2
Register at address (FSR)contains the value 0x55
DECF - Decrement f
Syntax: [label] DECF f, d
Status affected: Z
Number of cycles: 1
EXAMPLE 1:
....
[label] DECF REG,f
Before instruction execution: REG = 0x01
Z=0
After instruction: REG = 0x00
Z=1
EXAMPLE 2:
....
[label] DECF REG,w
Before instruction execution: REG = 0x13
W = x, Z = 0
After instruction: REG = 0x13
W = 0x12, Z = 0
Status affected: -
EXAMPLE 1:
....
MOVLW .10
MOVWF CNT ;10 -> CNT
Loop ......
...... ;Instruction block
......
DECFSZ CNT,f ; decrement REG by one
GOTO Loop ; Skip this line if = 0
LAB_03 ....... ; Jump here if = 0
In this example, instruction block is executed as many times as the initial value of the
variable CNT is, which in this example is 10.
Operand: 0 ≤ k ≤ 2047
Status affected: -
Number of cycles: 2
EXAMPLE :
....
LAB_00 GOTO LAB_01 ; Jump to LAB_01
.....
.....
LAB_01 ..... ; Program continues from here
Before instruction execution: PC = LAB_00 address
After instruction: PC = LAB_01 address
INCF - Increment f
Syntax: INCF f, d
Status affected: Z
Number of cycles: 1
EXAMPLE 1:
....
[label] INCF REG,w
Before instruction execution: REG = 0x10
W = x, Z = 0
After instruction: REG = 0x10
W = 0x11, Z = 0
EXAMPLE 2:
....
[label] INCF REG,f
Before instruction execution: REG = 0xFF
Z=0
After instruction: REG = 0x00
Z=1
Status affected: -
EXAMPLE :
....
LAB_01 INCFSZ REG,f ; Increment REG by one
LAB_02 ....... ; Skip this line if result is 0
LAB_03 ....... ; Jump here if result is 0
The content of program counter Before instruction execution, PC= LAB_01address. The
content of REG after instruction, REG = REG+1. If REG=0, the program counter points to
the address of label LAB_03. Otherwise, the program counter points to address of the next
instruction, i.e. to LAB_02 address.
Operand: 0 ≤ k ≤ 255
Status affected: -
Number of cycles: 1
EXAMPLE :
....
[label] IORLW 0x35
Before instruction execution: W = 0x9A
After instruction: W = 0xBF
Z=0
Status affected: Z
Number of cycles: 1
EXAMPLE 1:
....
[label] IORWF REG,w
Before instruction execution: REG = 0x13,
W = 0x91
After instruction: REG = 0x13,
W = 0x93 Z = 0
EXAMPLE 2:
....
[label] IORWF REG,f
Before instruction execution: REG = 0x13,
W = 0x91
After instruction: REG = 0x93,
W = 0x91 Z = 0
MOVF - Move f
Syntax: MOVF f, d
Status affected: Z
Number of cycles: 1
EXAMPLE 1:
....
[label] MOVF FSR,w
Before instruction execution: FSR=0xC2
W=0x00
After instruction: W=0xC2
Z=0
EXAMPLE 2:
....
[label] MOVF INDF,f
Before instruction execution: W=0x17
FSR=0xC2, register at address 0xC2 contains the value 0x00
After instruction: W=0x17
FSR=0xC2, register at address 0xC2 contains the value 0x00,
Z=1
Status affected: -
Number of cycles: 1
EXAMPLE 1:
....
[label] MOVLW 0x5A
After instruction: W=0x5A
EXAMPLE 2:
MOVWF - Move W to f
Syntax: MOVWF f
Operand: 0 ≤ f ≤ 127
Status affected: -
Number of cycles: 1
EXAMPLE 1:
....
[label] MOVWF OPTION_REG
Before instruction execution: OPTION_REG=0x20
W=0x40
After instruction: OPTION_REG=0x40
W=0x40
EXAMPLE 2:
....
[label] MOVWF INDF
Before instruction execution: W=0x17
FSR=0xC2, register at address 0xC2 contains the value 0x00
After instruction: W=0x17
FSR=0xC2, register at address 0xC2 contains the value 0x17
NOP - No Operation
Syntax: NOP
Description: No operation.
Operation: -
Operand: -
Status affected: -
Number of cycles: 1
EXAMPLE :
....
[label] NOP ; 1us delay (oscillator 4MHz)
Before instruction execution: PC = x
After instruction: PC = x + 1
Description: Return from subroutine. The value is popped from the stack and loaded to the
program counter. Interrupts are enabled by setting the bit GIE of the INTCON register.
Operand: -
Status affected: -
Number of cycles: 2
EXAMPLE :
....
[label] RETFIE
Before instruction execution: PC = x
GIE (interrupt enable bit of the SATUS register) = 0
After instruction: PC = TOS (top of stack)
GIE = 1
Description: 8-bit literal k is loaded into register W. The value from the top of stack is
loaded to the program counter.
Operand: -
Status affected: -
Number of cycles: 2
EXAMPLE :
....
[label] RETLW 0x43
Before instruction execution: W = x
PC = x
TOS (top of stack) = x
After instruction: W = 0x43
PC = TOS (top of stack)
TOS (top of stack) = TOS - 1
Description: Return from subroutine. The value from the top of stack is loaded to the
program counter. This is a two-cycle instruction.
Operand: -
Status affected: -
Number of cycles: 2
EXAMPLE :
....
[label] RETURN
Before instruction execution: PC = x
TOS (top of stack) = x
After instruction: PC = TOS (top of stack)
TOS (top of stack) = TOS - 1
Description: The content of register f is rotated one bit to the left through the Carry flag.
If d = w or d = 0, the result is stored in register W. If d= f or d = 1, the result is stored in
register f.
Status affected: C
Number of cycles: 1
EXAMPLE 1:
....
[label] RLF REG,w
Before instruction execution: REG = 1110 0110
C=0
After instruction: REG = 1110 0110
W = 1100 1100
C=1
EXAMPLE 2:
....
[label] RLF REG,f
Before instruction execution: REG = 1110 0110
C=0
After instruction: REG = 1100 1100
C=1
Description: The content of register f is rotated one bit right through the Carry flag.
If d = w or d = 0, the result is stored in register W. If d = for d = 1, the result is stored in
register f.
Operation: (f(n)) -> d(n-1), f(0) -> C, C -> d(7);
Status affected: C
Number of cycles: 1
EXAMPLE 1:
....
[label] RRF REG,w
Before instruction execution: REG = 1110 0110
W=x
C=0
After instruction: REG = 1110 0110
W = 0111 0011
C=0
EXAMPLE 2:
....
[label] RRF REG,f
Before instruction execution: REG = 1110 0110, C = 0
After instruction: REG = 0111 0011, C = 0
Description: The processor enters sleep mode. The oscillator is stopped. PD bit (Power
Down) of the STATUS register is cleared. TO bit of the same register is set. The WDT and
its prescaler are cleared.
Operand: -
Status affected: TO, PD
Number of cycles: 1
EXAMPLE :
....
[label] SLEEP
Before instruction execution: WDT counter = x
WDT prescaler = x
After instruction: WDT counter = 0x00
WDT prescaler = 0
TO = 1
PD = 0
Description: The content of register W is subtracted from the literal k. The result is stored in
register W.
Operand: 0 ≤ k ≤ 255
Number of cycles: 1
EXAMPLE :
....
[label] SUBLW 0x03
Before instruction execution: W = 0x01, C = x, Z = x
After instruction: W = 0x02, C = 1, Z = 0 result is positive
Number of cycles: 1
EXAMPLE :
....
[label] SUBWF REG,f
Before instruction execution: REG = 3, W = 2, C = x, Z = x
After instruction: REG = 1, W = 2, C = 1, Z = 0 result is positive
Description: The upper and lower nibbles of register f are swapped. If d = w or d = 0, the
result is stored in register W. If d = f or d = 1, the result is stored in register f.
Status affected: -
Number of cycles: 1
EXAMPLE 1:
....
[label] SWAPF REG,w
Before instruction execution: REG=0xF3
After instruction: REG=0xF3
W = 0x3F
EXAMPLE 2:
....
[label] SWAPF REG,f
Before instruction execution: REG=0xF3
After instruction: REG=0x3F
Description: The content of register W is XOR’ed with the 8-bit literal k . The result is
stored in register W.
Operand: 0 ≤ k ≤ 255
Status affected: Z
Number of cycles: 1
EXAMPLE 1:
....
[label] XORLW 0xAF
Before instruction execution: W = 0xB5 ; 1011 0101 (0xB5)
; 1010 1111 (0xAF)
------------------
After instruction: W = 0x1A ; 0001 1010 (0x1A)
Z=0
EXAMPLE 2:
Status affected: Z
Number of cycles: 1
EXAMPLE 1:
....
[label] XORWF REG,f
Before instruction execution: REG = 0xAF, W = 0xB5 ; 1010 1111 (0xAF)
; 1011 0101 (0xB5)
------------------
After instruction: REG = 0x1A, W = 0xB5 ; 0001 1010 (0x1A)
EXAMPLE 2:
....
[label] XORWF REG,w
Before instruction execution: REG = 0xAF, W = 0xB5 ; 1010 1111 (0xAF)
; 1011 0101 (0xB5)
------------------
After instruction: REG = 0xAF, W = 0x1A ; 0001 1010 (0x1A)
In addition to the preceding instructions, Microchip has also introduced some other
instructions. To be more precise, they are not instructions as such, but macros supported by
MPLAB. Microchip calls them "Special Instructions" since all of them are in fact obtained by
combining already existing instructions.
EQUIVALENT STATUS
INSTRUCTION DESCRIPTION
INSTRUCTION AFFECTED
B k Branch GOTO
BC k Branch on Carry BTFSC GOTO STATUS,C
Branch on Digit
BDC k BTFSC GOTO STATUS,DC
Carry
Branch on No
BNC k BTFSS GOTO STATUS,C
Carry
Branch on No
BNDC k BTFSS GOTO STATUS,DC
Digit Carry
Skip on Digit
SKPDC BTFSS STATUS,DC
Carry
Subtract Carry
SUBCF f, d BTFSC DECF STATUS,C
from File
Subtract Digit
SUBDCF f, d BTFSC DECF STATUS,DC
Carry from File
CPU REGISTERS
1. Status register
2. PORT register
3. TRIS register
4. INTCON register
5. OPTION register
6. PIE1
7. PIR1
8. PIE2
9. PIR2
[Link] Register
Generally speaking, each interrupt source has two bits associated with it. One enables
interrupts, and the other detects when interrupts occur. There is one common bit called GIE
which can be used to disable or enable all interrupts simultaneously. This bit is very useful
when writing a programme because it allows for all interrupts to be disabled for a period of
time, so that execution of some important part of a programme would not be interrupted.
When the instruction which resets GIE bit is executed (GIE=0, all interrupts disallowed), any
interrupt that remained unsolved would be ignored. Interrupts which remained unsolved and
were ignored, are processed when GIE bit (GIE=1, all interrupts allowed) would be cleared.
When interrupt was answered, GIE bit was cleared so that any additional interrupts would be
disabled, return address was pushed onto stack and address 0004h was written in programme
counter - only after this does replying to an interrupt begin! After interrupt is processed, the
bit which caused an interrupt must be cleared, or the interrupt routine would automatically be
processed over again during a return to the main programme.
The INTCON register is a readable and writable register which contains the various enable
bits for all interrupt sources.
The meaning of INTCON might be (though this is only me thinking) INTurrpt CONtrol
register. I have serached the net for the source of this acronym with no avail; if you find it
please e-mail me.
Anyway, interrupt flag bits get set when an interrupt condition occurs regardless of the state
of its corresponding enable bit or the global enable bit, GIE (INTCON<7>).
This register is used to configure the interrupt control logic circuitry. Bits 0 to 6 are used to
configure the interrupt enable/disable statuses and the interrupt flags for the four interrupt
sources. No interrupt to the CPU will result unless the GIE bit is set. The GIE bit is the bit
INTCON<7> and when set, enables all un-masked interrupts.
R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-0 R/W-x
R = Readable bit
GIE EEIE T0IE INTE RBIE T0IF INTF RBIF W= Writable bit
U = Unimplemented bit, read as ‘0’
Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0 -n= Value at POR reset
bit 7:
GIE: The Global Interrupt Enable bit is like the master switch for all the different interrupts.
SETTING this bit will enable all the interrupts to function, CLEARING this bit will
disable ALL interrupts.
1 = Enables all un-masked interrupts
0 = Disables all interrupts
bit 6
EEIE: EE Write Complete Interrupt Enable bitEE Write Complete Interrupt Enable bit
allows an interrupt to occur when a write operation to the EEPROM has completed. This
interrupt may be required in your programs because it takes time for a write operation to
EEPROM to complete. This interrupt capability allows the program to do other things instead
of halting while the write operation is accomplished. SETTING the EEIE bit allows an
interrupt when the write to EEPROM operation is complete; CLEARING the bit disables the
interrupt.
1 = Enables the EE write complete interrupt
0 = Disables the EE write complete interrupt
bit 5
T0IE: The TMR0 Overflow Interrupt Enable bit allows an interrupt when the TMR0 counter
overflows from 255 (0xff) to 0 (0x00). Setting this bit allows the TMR0 interrupt,
CLEARING this bit will disable the interrupt.
1 = Enables the TMR0 interrupt
0 = Disables the TMR0 interrupt
bit 4:
INTE: The RB0/INT External interrupt Enable bit allows an interrupt from a clocking signal
applied to pin RB0. Whether the interrupt occurs on the rising or falling edge of this clocking
signal is determined by the state of the INTEDG bit in the OPTION_REG. SETTING the
INTE bit allows an interrupt from the signal on RB0, CLEARING this bit disables the
interrupt.
1 = Enables the RB0/INT interrupt
0 = Disables the RB0/INT interrupt
bit 3:
RBIE: The Port Change Interrupt Enable bit allows an interrupt when there is a change of
state on pins RB7, RB6, RB5 and RB4 on PORTB. SETTING the RBIE bit will allow the
PORTB change interrupts; CLEARING this bit disables the interrupts.
1 = Enables the RB port change interrupt
0 = Disables the RB port change interrupt
bit 2:
T0IF: The TMR0 Overflow Interrupt Flag bit is used by the device to indicate if the interrupt
was the result of a TMR0 overflow. As you may have noticed, an interrupt code will be
triggered by any of the different resources available on the MCU. It is up to you, the
programmer, to determine through your software code which of the resources generates the
interrupt. Flag bits allow you to make that determination. In this case, when a TMR0
overflow interrupt occurs, the TOIF flag bit is SET. Early in the interrupt service routine (the
subroutine program that you will write to deal with an interrupt) , a check of the various flags
is accomplished - in this case, the TOIF flag, and if it is SET, a TMR0 interrupt occurred and
the program will take the desired action. You reset the TMR0 interrupt by CLEARING the
TOIF bit. If you fail to reset the TOIF bit, additional TMR0 interrupts will occur immediately
once the interrupt service routine has completed.
1 = TMR0 has overflowed (must be cleared in software)
0 = TMR0 did not overflow
bit 1:
INTF: The RB0/INT External Interrupt Flag bit is used by the device to indicate if the
interrupt was the result of a clocking signal on the RB0 pin. You will need to check the state
of INTF in the interrupt service routine to determine if the interrupt occurred because of a
clock signal on RB0. At completion of the interrupt service routine, the INTF pin must be
CLEARED to prevent unintended interrupts.
1 = The RB0/INT interrupt occurred
0 = The RB0/INT interrupt did not occur
BIT 0:
RBIF: The Port Change Interrupt Flag bit is used likewise by the device to indicate if the
interrupt was the result
1 = When at least one of the RB7:RB4 pins changed state (must be cleared in software)
0 = None of the RB7:RB4 pins have changed state.
Fig. The PIC 16F84’s interrupt logic.
PIE1 Register
The PIE1 register contains the individual enable bits for the peripheral
interrupts. The structure of this register is shown below.
• Bit 6 (ADIE): A/D Converter Interrupt Enable bit which control the analog to
digital converter interrupt.
(1 = Enables the A/D converter interrupt
• Bit 5 (RCIE): USART Receive Interrupt Enable bit which control the USART
data reception interrupt.
(1 = Enables the USART receive interrupt
• Bit 4 (TXIE): USART Transmit Interrupt Enable bit that control USART data
transmission.
(1 = Enables the USART transmit interrupt
• Bit 3 (SSPIE): Synchronous Serial Port Interrupt Enable bit that control SSP data
interrupt.
(1 = Enables the SSP interrupt
• Bit 2 (CCP1IE): CCP1 Interrupt Enable bit which control the capture-compare-
pulse width modulation interrupt.
(1 = Enables the CCP1 interrupt
PIR1 Register
The PIR1 register contains the individual flag bits for the peripheral interrupt.
The structure of PIR1 register is given below.
• Bit 6 (ADIF): A/D Converter Interrupt Flag bit that control the interrupt flag for
that analog to digital converter.
(1 = An A/D conversion completed
• Bit 3 (SSPIF): Synchronous Serial Port (SSP) Interrupt Flag bit that control the
SSP interrupt flag in a PIC.
(1 = The SSP interrupt condition has occurred and must be cleared in software
before returning from the Interrupt Service Routine. The conditions that will
set this bit are:
• I2C Master
– A Start condition occurred while the SSP module was Idle (multi-master
system).
– A Stop condition occurred while the SSP module was Idle (multi-master
system).
2. Compare mode:
3. PWM mode:
PIE2 Register
The PIE2 register contains the individual enable bits for the CCP2 peripheral
interrupt, the SSP bus collision
Interrupt, EEPROM write operation interrupt and the comparator interrupt. The
structure of this register is given below.
——
——
————— CMIE ————— EEIE BCLIE —- ————— CCP2IE
• Bit 7 Unimplemented: Read as ‘0’
• Bit 6 (CMIE): Comparator Interrupt Enable bit
(1 = Enables the comparator interrupt
PIR2 Register
The PIR2 register contains the flag bits for the CCP2 interrupt, the SSP bus
collision interrupt, EEPROM write operation interrupt and the comparator
interrupt. The structure of this register is given below.
——
——
————— CMIF ————— EEIF BCLIF - ————— CCP2IF
• Bit 7 Unimplemented: Read as ‘0’
• Bit 6 (CMIF): Comparator Interrupt Flag bit
(1 = the comparator input has changed (must be cleared in software)
Compare mode:
1 = A TMR1 register compare match occurred (must be cleared in software)
2. PWM mode:
PCON Register
The Power Control (PCON) register contains flag bits to allow differentiation
between a Power-on Reset
STATUS register
In most cases, this register is used to switch between the banks (Register Bank Select), but
also has other capabilities.
• IRP - Register Bank Select bit, used for indirect addressing method.
• RP1:RP0: - Register Bank Select bits, used for direct addressing method.
To distinguish between the two methods, at this point, the will use the definition of
fundamental concepts. Later on, the two methods will be studied in detail.
When the IRP Equal to 0, the program will work with banks 0, 1.
When the IRP Equal to 1, the program will work with banks 2, 3.
The following table demonstrates, which of the Banks the program is working with, based on
the selection of the RP0 and RP1 bits:
RP1:RP0 BANK
00 0
01 1
10 2
11 3
In the first line, we are in changing/setting the 5th bit, RP0, in the STATUS register to 1, and
thus, base on the table we are switching/selecting Bank 1. After PortB was set as output in the
second line, we switched back to Bank 0 by in changing/setting the 5th bit, RP0, in the
STATUS register to 0, in the third line.
1. Movlw 200
2. Addwf 100, 0
In this example, we are assigning value of 200 to the W (working) register. Then, we are
adding the value of 100 and the W register together. The result is stored in W register and
should be 300 (200+100).
However, the maximum value is 256, resulting in carry out. The C (bit 0) of the STATUS
register becomes 1 (C = 1). Register W will contain the reminder: 44.
PORT register
The role of the PORT register is to receive the information from an external source (e.g.
sensor) or to send information to the external elements (e.g. LCD). The 28-pin devices have 3
I/O ports, while the 40/44-pin devices, like PIC16F877, have 5 I/O ports located in the
BANK 0.
1. PORTA is a 6-bit wide, bidirectional port. The corresponding data direction register is
TRISA.
Setting a TRISA bit (= 1) will make the corresponding PORTA pin an input. Clearing a
TRISA bit (= 0) will make the corresponding PORTA pin an output.
2. PORTB is an 8-bit wide, bidirectional port. The corresponding data direction register is
TRISB.
Setting a TRISB bit (= 1) will make the corresponding PORTB pin an input. Clearing a
TRISB bit (= 0) will make the corresponding PORTB pin an output.
3. PORTC is an 8-bit wide, bidirectional port. The corresponding data direction register is
TRISC.
Setting a TRISC bit (= 1) will make the corresponding PORTC pin an input. Clearing a
TRISC bit (= 0) will make the corresponding PORTC pin an output.
4. PORTD is an 8-bit port with Schmitt Trigger input buffers. Each pin is individually
configurable as an input or output.
5. PORTE has three pins (RE0/RD/AN5, RE1/WR/AN6 and RE2/CS/AN7) which are
individually configurable as inputs or outputs. These pins have Schmitt Trigger input
buffers.
For
example:
# define SWITCH PORTA, 0
We define a variable named SWITCH, which received a value of bit number 0 of the
PORTA. Usually we define the ports at the beginning of the program, and then we use only
the given names.
TRIS register
The TRIS register is data direction register which defines if the specific bit or whole port will
be an input or an output. Each PORT has its own TRIS register. Here's a map of the
locations:
BANK0 BANK1
PORTA TRISA
PORTB TRISB
PORTC TRISC
PORTD TRISD
PORTE TRISE
The default mode of each TRIS is input. If you want to set a specific port as exit you must
change the state of the TRIS to 0.
Keep in mind: to change a specific port to an output, one should first move to the BANK1,
make the change, and then return to BANK0. The default state of the banks is BANK0.
The running program is working only with one bank at all time. If not set otherwise, then as
stated, the default bank is BANK0. Part of the registers located inside BANK0, and some are
not. When we need to access a register that is not located inside BANK0, we are required to
switch between the banks.
For example, the access to PORT registers is done inside BANK0. However, to change port
from an input to an output and vice versa, we need to access TRIS register that is located
inside BANK1. From the moment we moved to the BANK1, the program will always work
with BANK1; at this time, to access registers inside BANK0, we will have to return to the
situation in which our program will work with BANK0.
OSCILLATOR AND RESET CIRCUIT
Most of the modern PIC CPU’s like PIC16F87XA devices are built with many types of advanced
features that are capable of performing additional special tasks and operations. These features
increases the stability of the PIC and increases its functional reliability. It is also helpful for
designers to decrease the entire cost of the designed circuit by the integration and replacement of
external components and also by providing a lot of power saving protections. The general special
features of a modern PIC chip are given below (basis of PIC6F877A).
• Oscillator Selection
• Reset
o Power-on Reset (POR)
o Power-up Timer (PWRT)
o Oscillator Start-up Timer (OST)
o Brown-out Reset (BOR)
MCLR
MCLR is an advanced reset path that helps to filter the unwanted noise. This filter helps to detect unwanted clock
pulses and other noise signals and filter such pulses. It should be noted that a WDT Reset does not drive MCLR pin
low. The behaviour of the ESD protection on the MCLR pin differs from previous devices of this family. Voltages
applied to the pin that exceed its specification can result in both Resets and current consumption outside of device
specification during the Reset event. For this reason, Microchip recommends that the MCLR pin no longer be tied
directly to VDD.
Power-on Reset (POR)
The power –on-reset signals are generated by on-chip when VDD rise is detected. In the normal conditions, the VDD
rise is in the range of 1.2v to1.7v. To take the advantage of POR, connect the MCLR pin to VDD by the resistor
capacitor (RC) network. Before the device is set to start for its normal operation, the different operating parameters
such as frequency, voltage, temperature, and so on must be checked to see if they are normal or proper. If not, the
chip/circuit must be held in reset until the operating conditions are proper or normal. Brown-out-reset function
provides support for these operations.
Power-up Timer (PWRT)
The Power-up Timer provides a fixed 72 ms nominal time-out on power-up only from the POR. The Power-up Timer
operates on an internal RC oscillator. The chip is kept in Reset as long as the PWRT is active. The PWRT’s time
delay allows VDD to rise to an acceptable level. A configuration bit is provided to enable or disable the PWRT. The
power-up time delay will vary from chip to chip due to VDD, temperature and process variation
Oscillator Start-up Timer (OST)
The Oscillator Start-up Timer (OST) provides a delay of 1024 oscillator cycles (from OSC1 input) after the PWRT
delay is over (if PWRT is enabled). This helps to ensure that the crystal oscillator or resonator has started and
stabilized. The OST time-out is invoked only for XT, LP and HS modes and only on Power-on Reset or wake-up
from Sleep.
Brown-out Reset (BOR)
Brown-out-reset is a special resetting function in modern controllers. Once the brown-out occurs, the device will
remain in Brown-out Reset until VDD rises above VBOR. The Power-up Timer then keeps the device in Reset for
TPWRT. If VDD should fall below VBOR during TPWRT, the Brown-out Reset process will restart when VDD
rises above VBOR with the Power-up Timer Reset. The Power-up Timer is always enabled when the Brown-out
Reset circuit is enabled, regardless of the state of the PWRT configuration bit.
Time-out Sequence
On power-up, the time-out sequence is as follows: the PWRT delay starts (if enabled) when a POR Reset occurs.
Then, OST starts counting 1024 oscillator cycles when PWRT ends (LP, XT, and HS). When the OST ends, the
device comes out of Reset. If MCLR is kept low long enough, the time-outs will expire. Bringing MCLR high will
begin execution immediately. This is useful for testing purposes or to synchronize more than one PIC16F87XA
device operating in parallel. Table shows the Reset conditions for the Status, PCON and PC registers, while Table
shows the Reset conditions for all the registers.