You are on page 1of 35

Introduction to PICmicro® MCU

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 1


What is a Microcontroller?

 A computer that comes is a variety of


packages and sizes

 Personal Computer vs. Microcontroller


 PC performs a number of tasks or runs variety of
programs simultaneously

 MCU - dedicated to a single task

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 2


PC vs. Microcontroller

 PC  MCU
 CPU- brain of the  CPU – brain of the system
system  Memory – store program
 Hard drive/RAM specific to single task
 Clock - speed of  System Clock
CPU, processing of (oscillator) – computer’s
info engine
 Peripherals –  Resolution, response
input information and power consumption
keyboard, mouse, etc.  Peripherals – associated
with a particular pin in the
package

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 3


PICmicro Architecture
RISC-like Features

The high performance of the PICmicro MCU


family can be attributed to the following
architectural features:

 Harvard architecture  Instruction pipelining

 Register file concept  LWI (Long Word Instruction)


 Most instructions single-  Reduced instruction set
word/single cycle
 Orthogonal instruction set

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 4


Architecture

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 5


PICmicro Architecture
Harvard Architecture

Von Neumann  Fetches instructions and


data from one memory.
 Limits Operating Bandwidth
8-Bits
Program
and Data  Two separate memory
Memory spaces for instructions
Harvard and data.
 Increases throughput
 Different program and data
8-Bits
bus widths are possible
Data
12/14/16-Bits Memory
Program
Memory
© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 6
PIC® Microcontroller Architecture
PIC Microcontroller
 Separate Program
and Data Memory Oscillator Data
Program
12, 14 or
 Two dedicated Memory Memory
16 bits 8 bits
busses of
different widths CPU
 Long Instruction
Words
 Improved
operating Peripheral I/O
Peripheral I/ODrivers
Drivers
bandwidth
 Allows for Program Memory Bus
different bus
Data Memory Bus
widths
© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 7
Microchip PIC® MCU Pyramid
16 bit 24 bits
MCU dsPIC

16 bits
Data Memory PIC18 Program Memory
Width Width

14 bits
8 bit PIC16
MCU PIC12F

12 bits
PIC16C5X
PIC12C5XX

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 8


PICmicro MCU Architecture
Pipelining

 In most microcontrollers, instructions are fetched


and executed sequentially.
 Allows overlap of fetch and execution.
 Makes single cycle execution.
 Program branches (e.g. GOTO, CALL or Write to
PC) takes two cycles.
Tcy0 Tcy1 Tcy2 Tcy3 Tcy4
1. MOVLW 55h Fetch 1 Execute 1

2. MOVWF PORTB Fetch 2 Execute 2

3. CALL SUB_1 Fetch 3 Execute 3

4. BSF PORTA,3. Fetch 4 Flush Fetch 4

Fetch SUB_1

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 9


PICmicro Architecture
Register File Concept
Data
Memory INDF  RAM is a bank of
TMR0 special function and
PCL
general purpose
STATUS
FSR registers.
ALU
PORTA
Other SFRs  Peripherals (I/O) are
registers.
W Register
General Purpose  All instructions operate on
Registers (RAM) any register.
14-bit Instruction Format Example:

Opcode <7> Direct data addr <7>


Long word instructions allow direct addressing of registers in 1 instruction.
© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 10
PICmicro Architecture
Example Instruction

 PIC MCU instructions are encoded with an


action (OPCODE) and parameters

 Encoding is done in 1 word

Instruction for Literal Instructions


OP CODE k k k k k k k k

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 11


PIC MCU Oscillator

 Instruction rate is
1/4 of the input fosc
PIC MCU
clock
4 MHz
(250 ns)

 Input oscillator
frequency is Tcy
called fosc

 An instruction 1 MHz
cycle takes 1 Tcy (1 us)

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 12


Memory

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 13


PICmicro Architecture
Memory

 2 types of memory
 Program
 Data (RAM and EEPROM)

 Organization
 Pages (program memory)
 Banks (data memory)

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 14


PICmicro Architecture
Program and Data Memory Table

FLASH RAM
Program Memory Data Memory
PICmicro MCU Code Size (words) GPR Size (bytes)

PIC12F675 1K 64

PIC16F877A 8K 368

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 15


Paging Program Memory
(14-bit core)

CALL or GOTO instruction

OP CODE k k k k k k k k k k k

3 bits for 11 bits for


instruction destination

 In a 14-bit core device:


 11 bits accesses 211 or 2048 (2k) locations
 To access more locations more bits are needed
 Paging of program memory provides these bits

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 16


14-bit Core Architecture
Program Memory
PCLATH<4:3> = 00 PCLATH<4:3> = 10

Reset 0000h 1000h  Maximum 8K Words


(13 bits) of program
Interrupt 0004h
Page 2
memory space
 Four Pages, each 2k
Page 0 bytes (11 bits)
07FFh 17FFh
PCLATH<4:3> = 01 PCLATH<4:3> = 11
 Page access using
0800h 1800h
PCLATH<4:3>
 Reset Vector at 0000h
Page 1 Page 3
 Interrupt Vector at
0004h
0FFFh 1FFFh

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 17


PICmicro Architecture
Program Memory: PC Absolute Addressing

 Used by control instructions CALL and GOTO


to modify the PC (Program Counter)

PCLATH Register 14-bit Instruction for call and goto


x x x k k x x x OP CODE k k k k k k k k k k k
2-bits
From
11-bits From Instruction
PCLATH
k k k k k k k k k k k k k
Effective 13-bit Program Memory Address
© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 18
14-bit Core Architecture
Paging program memory

 Paging needs consideration ONLY when executing


program CALL or GOTO operation
 Modify page bits ONLY when jumping to different
page than last jumped
– page bits define desired page execution
 Instructions invoking page bits:
– GOTO <address>
– CALL <address>
– <Instruction> PCL,F ; e.g. ADDWF PCL,F
 There are no paging considerations when returning

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 19


PICmicro Architecture
PC Relative Addressing (14-bit core)

 First write high byte to PCLATH.


 Next write low byte to PCL, this loads the entire
13-bit value to PC.
Internal Data Bus <8>

5
movlw HIGH Delay
movwf PCLATH PCLATH <5> 8

movlw LOW Delay 5


movwf PCL
PCH <5> PCL <8>
13-Bit Program Counter
Note: PCH cannot be read

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 20


Banking Data Memory
(14 bit core)

Data Modifying Instruction

OP CODE f f f f f f f

7 bits for 7 bits for


instruction destination

 In a 14-bit core device:


 7 bits accesses 27 or 128 locations
 To access more locations more bits are needed
 Banking of data memory provides these bits

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 21


PICmicro Architecture
Data Memory: Direct Addressing

 7-bit direct address from the instruction


 2-bits from STATUS register

STATUS Register 14-bit Instruction


IRP RP1RP0 TO PD Z DC C OP CODE f f f f f f f

2-bits From
STATUS
Register 7-bits From Instruction Word
RP1 RP0 f f f f f f f

Effective 9-bit Register Address


© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 22
14-bit Core Architecture
Data Memory Organization

 Up to Four banks
Bank0 Bank1 Bank2 Bank3
each of 128 bytes
SFRs 000h
of Data Memory
SFRs 080h SFRs 100h SFRs 180h
 Special Function
Registers (SFRs)
01Fh
020h
09Fh
0A0h
11Fh
120h
19Fh
1A0h
are mapped in top
32 locations
GPRs GPRs GPRs
GPRs
 Banks selected by
07Fh
0F0h
0FFh
170h
17Fh
1F0h
1FFh
RP0,1 and IRP in
RP<1:0> RP<1:0> RP<1:0> RP<1:0> Status register
= 00 = 01 = 10 = 11

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 23


File Address File Address
(hex) (hex)
00 Indirect addr. (1) Indirect addr. (1) 80
01 TMR0 OPTION_REG 81
02 PCL PCL 82
03 STATUS STATUS 83
04 FSR FSR 84
05 GPIO TRISIO 85

0A
PICmicro
PCLATH PCLATH 8A
0B INTCON INTCON 8B
0C PIR1 PIE1 8C
0D 8D
Architecture 0E
0F
TMR1L
TMR1H
PCON 8E
8F

Data memory map 10 T1CON OSCCAL 90

Register File Map


15 WPU 95
16 IOC 96 PIC12F675
Unimplemented data memory location; read as '0'.
19 CMCON VRCON 99
(14-bit core)
Special Function Registers (SFR).
General Purpose Registers (GPR), user RAM. 1A EEDATA 9A
1 Not a Physical Register. 1B EEADR 9B
1C EECON1 9C
1D EECON2(1) 9D
1E ADRESH ADRESL 9E
1F ADCON0 ANSEL 9F
20 General A0
Purpose accesses
Registers 20h-5Fh
5F (64 Bytes) DF

Bank 0 Bank 1

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 24


PICmicro MCU Architecture
PC Relative Addressing

movlw HIGH Decode  Look-up Table Example


movwf PCLATH
movf DisplayValue,W RB0 a
call Decode
movwf PORTB RB1 b a
goto Continue RB2 c f b
Decode RB3 d
addwf PCL,F g
RB4 e
retlw B’00111111’ ;decode 0
retlw B’00000110’ ;decode 1 RB5 f e c
retlw B’01011011’ ;decode 2 RB6 g
d
retlw B’01001111’ ;decode 3
retlw B’01100110’ ;decode 4
retlw B’01101101’
B’01101101’ ;decode
;decode 55
retlw B’01111101’ ;decode 6 01101101
XXXXXXXX
00000101 00000000
01101101
retlw B’00000111’ ;decode 7
retlw B’01111111’ ;decode 8 W Register I/O Port B
retlw B’01101111’ ;decode 9
Continue

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 25


PICmicro Architecture
Data Memory: Indirect Addressing

 8-bit indirect address from the FSR


(File Select Register).
 1-bit from STATUS register.
STATUS Register 8-bit FSR Register
IRP RP1 RP0 TO PD Z DC C f f f f f f f f

1-bit From
STATUS
8-bits From FSR
Register
IRP f f f f f f f f

Effective 9-bit Register Address


© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 26
PICmicro Architecture
Data Memory: Indirect Addressing

 FSR and INDF are used for Indirect Addressing


 FSR is an Address pointer
 INDF holds the Value to be moved
Writing Reading

22 21 22 21
22 22
FSR 23 5 FSR 23 5
24 24
5 5
INDF INDF
movlw 22 movlw 22
movwf FSR movwf FSR
movlw 5 movf INDF,W
movwf INDF movwf 5

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 27


PICmicro Architecture
Data Memory: Indirect Addressing

 Clear all RAM locations from 0x20 to 0x7F.


 Indirect address is loaded into FSR.
 Every time INDF is used as operand, register
pointed to by FSR is actually used.
Data Memory
00h INDF movlw 0x20
movwf FSR
04h FSR = 20h LOOP clrf INDF
incf FSR,F
btfss FSR,7
20h 0000 0000
goto LOOP
<next instruction>
7Fh 0000 0000
© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 28
Instruction Set

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 29


PICmicro MCU Instruction Set
(14-bit core)

 35 Instructions
 Easy to learn
 High compaction
 Very powerful single-word instructions
 Upward compatibility of instructions

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 30


PICmicro Instruction Set
14-bit core Summary

Byte-Oriented Operations Bit-Oriented Operations


ADDWF f,d Add W and f BCF f,b Bit clear f
ANDWF f,d AND W and f BSF f,b Bit set f
CLRF f Clear f BTFSC f,b Bit test f, skip if clear
CLRW - Clear W BTFSS f,b Bit test f, skip if set
COMF f,d Complement f Literal and Control Operations
DECF f,d Decrement f
DECFSZ f,d Decrement f, skip if zero SLEEP - Go into standby mode
INCF f,d Increment f CLRWDT - Clear watchdog timer
INCFSZ f,d Increment f, skip if zero RETLW k Return, place literal in W
IORWF f,d Inclusive OR W and f RETFIE - Return from interrupt
MOVF f,d Move f RETURN - Return from subroutine
MOVWF f Move W to f CALL k Call subroutine
NOP - No Operation GOTO k Go to address (k is 9-bit)
RRF f,d Rotate right f through MOVLW k Move literal to W
carry IORLW k Inclusive OR literal with W
RLF f,d Rotate left f through carry ADDLW k Add literal with W
SUBWF f,d Subtract W from f SUBLW k Subtract W from literal
SWAPF f,d Swap nibbles of f ANDLW k AND literal with W
XORWF f,d Exclusive OR W and f XORLW k Exclusive OR literal with W

f = File Register, k = literal value (8-bit), b = bit address <0 to 7>, d = destination (W or file register)

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 31


PICmicro Instruction Set
Byte-Oriented Operations
Byte-Oriented
Operations
NOP -
14-bit Instruction for Byte Oriented Operations
MOVWF f
CLRW -
CLRF f OP CODE d f f f f f f f
SUBWF f,d
DECF f,d
IORWF f,d
d = Destination Bit
ANDWF f,d d = 0 for destination W
XORWF f,d
d = 1 for destination F
ADDWF f,d
MOVF f,d
COMF f,d f = 7-bit Register Address
INCF f,d
DECFSZ f,d
RRF f,d Example:
RLF f,d
SWAPF f,d ADDWF REG, W
INCFSZ f,d ADDWF f, d
© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 32
PICmicro Instruction Set
Bit-Oriented Operations

Bit-Oriented
Operations 14-bit Instruction for Bit Oriented Operations

BCF f,b OP CODE b b b f f f f f f f


BSF f,b
BTFSC f,b
BTFSS f,b b = 3-Bit Address
(Bit Number)

f = 7-bit Register Address


0000 0000
Bit 7 Bit 0 Example:
BTFSC STATUS, C
BTFSC f, b
© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 33
PICmicro Instruction Set
Literal and Control Operations
Literal and Control
Operations 14-bit Instruction for Literal Operations
SLEEP -
CLRWDT - OP CODE k k k k k k k k
RETLW k
RETFIE -
RETURN - k = 8-bit Immediate Value
CALL k
GOTO k
MOVLW k
IORLW k
ADDLW k
SUBLW k Example:
ANDLW k MOVLW 0x2F
XORLW k MOVLW k

© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 34


© Microchip Technology Incorporated. All Rights Reserved. Introduction to PIC Microcontrollers 35

You might also like