Unit 1
Unit 1
UNIT I
INTRODUCTION TO PIC MICROCONTROLLER
Introduction to PIC Microcontroller–PIC 16C6x and PIC16C7x Architecture–
PIC16cxx–- Pipelining -Program Memory considerations – Register File
Structure - Instruction Set - Addressing modes –Simple Operations.
Low-end Architectures
Microchip PIC microcontrollers are available in various types. When PIC −
MicroCU first became available from General Instruments in early 1980’s, the
microcontroller consisted of a very simple processor executing 12-bit wide
instructions with basic I/O functions and limited program memory. These
devices are known as low-end architectures.
Example: PIC 12C5XX, PIC 16C5XX, PIC 16C505
Mid-range Architectures
Mid-range Architectures are built by upgrading low-end architecture with more
number of peripherals, more numbers of register and more data memory. Some
of the mid-range devices are PIC 16C6X PIC16C7X, PIC16F87X. C and F
indicates the types of program memory.
Type C = EPROM F = Flash and RC = Mask ROM
SALIENT FEATURES
Speed :
When operated at its maximum clock rate at its 200Mhz , i.e, PIC
executes most of its instructions in 0.2 s or five instructions per microsecond.
i.e 1 instruction cycle = 4 clock cycles.
Instruction set Simplicity :
The instruction set is so simple that it consists of only just 35 instructions (as
opposed to 111 instructions for 8051).
Power on Reset:
PIC will be Reset whenever the power is on,. A watch dog timer resets the
PIC if the chip malfunctions or deviates from its normal operation at any time.
Brown out Reset:
PIC will be Reset whenever voltage drops under 4.0 volts.
Programmable timer options:
Three timers can characterize inputs, control outputs and provide
internal timing for the program execution.
Powerful output pin control:
A single instruction can select and drive a single output pin high or low
in its 0.2 s instruction execution time. The PIC can drive a load of up to
25A.
UNIT-I Introduction to PIC Microcontroller
Harvard Architecture
Architecture separate buses are used for Data and Instruction as shown in the
diagram.
Instructions are fetched from program memory using buses that are distinct
from the buses used for accessing variables in data memory, I/O ports etc.
Every instruction is coded as a single 14-bit word and fetched over a 14-bit
wide bus.
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.
The PIC 16C6X devices have a 13-bit program counter capable of addressing
an 8Kx14 program memory space. The PIC 16FF876/877 devices have 8Kx 14
words of Flash program memory.
The low byte comes from the PCL register, which is readable and writable
register. The upper bits are not readable but are indirectly writable through
PCLATH register. On any reset, the upper bits of the PC will be cleared.
ii) Stack pointer(SP – 13 bit):
The Stack Pointer is not part of either program or data space. The Program
counter is pushed on to the stack when a CALL instruction is executed or an
interrupt or branch instruction to store the return address. The stack is
Popped in the event of return instruction execution (retrieving return address
from stack).
Execution:
iii) Arithmetic & logic Unit (ALU): (8-bit)
The ALU is a general purpose arithmetic unit. It performs arithmetic and
boolean operation between the data in the working register and any register
file. The ALU is 8 bits wide and capable of addition, subtraction, shift and
logical operations.
In two operand instructions, typically one operand is the working register (W
register) the other operand is a file register or an immediate constant. In a
single operand instructions, the operand is either the W register or a file
register.
iv) Working Register (W- 8 bit):
The W register is an 8 bit working register used for ALU operations. It is used
by many instructions as the source of an operand. It also serves as the
destination for the result of instruction execution (similar to accumulator). It is
not an addressable register.
UNIT-I Introduction to PIC Microcontroller
C: Carry/borrow bit
DC: Digit carry/borrow bit
Z: Zero bit
NOT_PD : Reset Status bit (Power-down mode bit)
NOT_TO : Reset Status bit (time- out 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.
• 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
UNIT-I Introduction to PIC Microcontroller
ix) Timers:
It has 3 timer modules such as Timer 0 (8 – bit overflow counter), Timer 1 (16 –
bit Timer/counter), Timer 2 (8 – bit timer). Each module can generate an
interrupt to indicate that an event has occurred (i.e timer overflow)
x) Watch Dog Timer (WDT):
A Watch dog timer is a simple timer circuit that performs a specific operation
after a certain period of time if something goes wrong. Suppose we have written
a program which is compiled successfully and when we simulate if every time
seems to work fine. Then we program the PIC. However after a long period of
time the program gets stuck somewhere. What needs it this case is some kind
of reset if the program is gets stuck. This is a purpose of a watchdog timer
circuit. When the WDT is enable counter starts at 00 and increment by 1 until
it reaches FF. When it goes from FF to 00 the PIC will be reset, irrespective of
what it is doing. The only way we can stop the WDT, from resetting the WDT
back to 00 throughout the program which is done by the processor. Which
indicates that the processor functioning is going on. Watch dog timer is thus
1.3 PIPELINING:
Overlapped movement of instruction to the processor is called pipelining.
The CPU executes each instruction during the cycle following its fetch,
pipelining instruction fetches and instruction executions to achieve the
execution of one instruction every cycle. This is illustrated in Figure 1.3.1. It
can be seen that while each instruction requires two cycles (a fetch cycle
followed by an execute cycle), the execution of nth unit is overlapped with the
fetch of (n+1)th instruction. In every cycle new instructions are fetched
The normal process is broken whenever an instruction includes a
branch operation, CALL instruction, GOTO Address as illustrated on Figure
1.3.2.
In this example, instruction is fetched during the second cycle, goto New
Address, whose job it is to change the normal flow of instruction fetches
from one address to the next address.
During the third cycle, the CPU carries out the sequential fetch from
address n+2.
At the end of that third cycle, the CPU executes the goto New Address
instruction by changing the program counter to New Address instead of
simply incrementing it to n+3.
On the fourth cycle while it is fetching the instruction at New Address , it
ignores the instruction automatically fetched from address n+2. While
this (n+2)th instruction is located in the program immediately after the
(n+1)th goto New Address instruction, it is never executed immediately
after the execution of that (n+1)th goto New Address instruction.
UNIT-I Introduction to PIC Microcontroller
Figure 1.4.1 Program memory access for PIC parts having 2K of program
memory.
UNIT-I Introduction to PIC Microcontroller
Figure 1.4.2 Program memory access for PIC parts having 4K of program
memory.
An independent 8-level stack is used for the program counter. As the program
counter is 13bit, the stack is organized as 8x13bit registers. When an interrupt
occurs, the program counter is pushed onto the stack. When the interrupt is
being serviced, other interrupts remain disabled. Hence, other 7 registers of
the stack can be used for subroutine calls within an interrupt service routine
or within the mainline program. The Program memory and Stack memory shown in
below.
UNIT-I Introduction to PIC Microcontroller
As shown in Figure 1.4.4, bits 10…0 of the call instruction are loaded into
the program counter. At the same time, bits 4 and 3 of a special register
called PCLATH (“program counter latch”) are loaded into bits 12 and 11 of
the program counter.
As long as the program memory is less than 2048(i.e.,2K) words, bits 4
and 3 of PCLATH can be left initialized to H'00', and then the 11 address
bits in the call instruction will identify the starting address of any
subroutine located up to address H'7FF'.
For the programs larger than this, it is necessary to ensure that bit 3 of
The goto instruction, which also has an 11-bit address field, requires an
identical treatment.
UNIT-I Introduction to PIC Microcontroller
C: Carry/borrow bit
DC: Digit carry/borrow bit
Z: Zero bit
NOT_PD : Reset Status bit (Power-down mode bit)
NOT_TO : Reset Status bit (tme- out 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.
The final feature of the CPU registers is the role of PCL and PCLATH.
PCL is actually the lower 8 bits of the 13-bit program counter. It
can be read, just like any other register.
PCLATH is not the upper 5 bits of the PC. PCLATH can be read from
or written to without affecting the PC. The upper 3 bits of PCLATH
the desired 8-bit address must first be written into FSR using direct
addressing. To make the possible value of the register bank select bit,
RP0, the FSR register is accessed at either address 04 or 84.
0000 0100 H = 04
1000 0100 H = 84
It can be seen that differ only in the bit controlled by RP0 during direct
addressing. Consequently a write to either 04 or 84 will write into the FSR
register.
CLASSIFICATION OF INSTRUCTIONS
All the instructions of the PIC microcontroller are classified into nearly 9
groups. They are given below with examples.
(i).Arithmetic Operations :
addlw k ; add literal value k to w
addwf f, d ; The contents of the W register are added with
the register f.
subwf f ,d ; the contents of W register are subtracted from
register f
(ii).Logical Instructions :
andlw k ; The contents of W register are ANDED with the 8-bit
literal k. The result is stored in the W register.
iorlw k ; Inclusive OR the literal value into W register
xorwf f,d ; The contents of W register are XORed with register f
and the result is stored in W or f.
comf f, d ; Complement f .
(iii).Increment/Decrement Instructions
incf f ,d ; Increment contents of f register by 1
decf f , d ; Decrement f by 1
(iv).Data Transfer instructions :
movf f,d ; Move f to W i.e The contents of register f is moved
to a destination depending on d
The various instructions used in PIC are presented in the Table below.
Single-bit manipulation Operation
bcf PORTB, 0 ;Clear bit 0 of PORTB
bsf STATUS, C ;Set the carry bit
Clear/move
Clrw ;Clear the working register, W
clrf TEMP1 ;Clear temporary variable TEMP1
movlw 5 ;Load 5 into W
movlw 10 ;Load D ‘10’ or H ‘10’ into W
;depending upon default representation
movwf TEMP1 ;Move W into TEMP1
movwf TEMP1, F ;Incorrect syntax
movf TEMP1, W ;Move TEMP1 into W
swapf TEMP1, F ;Swap 4-bit nibbles of TEMP1
swapf TEMP1, W ;Move TEMP1 to W, swapping nibbles
;and leave TEMP1 unchanged
Increment/decrement/compleme
nt
incf TEMP1, F ;Increment TEMP1
incf TEMP1, W ;W < - TEMP1 + 1; TEMP1 unchanged
decf TEMP1, F ;Decrement TEMP1
comf TEMP1, F ;Change 0s to 1s and 1s to 0s
Multiple-bit manipulation
andlw B’00000111’ ;Force upper 5 bits of W to zero
andwf TEMP1, F ;TEMP1 < - TEMP1 and W
andwf TEMP1, W ;W < - TEMP1 AND W
iorlw B’00000111’ ;Force lower 3 bits of W to one
iorwf TEMP1, F ;TEMP1 < - TEMP1 or W
xorlw B’00000111’ ;Complement lower 3 bits of W
xorwf TEMP1, W ;W < - TEMP1 XOR W
Addition/Subtraction
addlw 5 ;Add 5 to W
Assume that an instruction that affects the Z bit has just been executed.
Then depending on the result one instruction sequence or another is to be
executed, continuing on after either case.
Assume that the upper byte of the counter is called COUNTH and the
lower byte is called COUNTL
Note how the movf instruction is first used to test COUNTL for zero
The Ports of PIC controller are made either inpur or output ports by using
the TRISx register ,which is a SFR.To make the Port an output,0 must be
written to the TRISx register and to use the pPort as input ,a 1 must be
put in to the TRISx register. Fors example to make the PortB as input
port , the bits of TRISB are made 1(High).
The following example explains the I/O port programming.
Example 1: MOVLW 0x0
MOVWF TRISB ; make the Port B an output port.
L1 : MOVLW 0x55 ; WREG = 55
MOVWF PORTB ; Move 55 into portB.
CALL DELAY
MOVLW 0X AA
MOVWF PORTB ; Move AA into portB
CALL DELAY
GO TO L1
This program alternately loads Port B with 55 and AA.
Example 2: In this example Port B and PORT C are used to transfer the
data continuously.
MOVLW B ‘00000000’ ; WREG = 00000000(Binary)
MOVWF TRISB ; Port B an out port
MOVLW B ‘11111111’ ;WREG = 11111111 (Binary)
MOVWF TRISC ; Port C input Port
L2 MOVF PORTC , W ;Move data from Port C to WREG.
Addlw 5 ; Add literal 5 to it.
MOVWF PORTB ; send it to Port B
GOTO L2 ; Continue the loop.
So , it is clear that unless the TRIS bits are activated by putting a 1 ,the
data will not be transferred to WREG from the port pins of PORT C