Вы находитесь на странице: 1из 58

Advanced Microcontrollers

EC 38105

Prof. Kanade D.G.
What you learn?
Features of PIC 18Fxx8
PIC 18Fxx8 architecture
Addressing Modes
Instruction Set
On chip Peripherals
Programming PIC Microcontroller
ARM 7 Processor basics
Addressing Modes & Instruction set
330_02 2
What is PIC Microcontroller?
In 1989 Microchip Technology Corp.
introduced 8-bit microcontroller called PIC.
PIC-Peripheral Interface Controller.
8 Pin Microcontroller
Small amount of RAM
Hundred bytes of on chip ROM
One Timer
Few pins for I/O
330_02 3
What is PIC Microcontroller?
Is leading supplier of 8 bit microcontroller.
PIC 8 bit microcontroller families
330_02 4
PIC microcontroller?
Not 100% upward compatible.
PIC 12xxx/16xxx have 12 bit and 14 bit wide
instructions and PIC 18xxx has 16 bit wide

No 8 pin version of PIC 18xxx exists.

330_02 5
PIC18F Microcontrollers
Microcontroller Unit (MCU)
Microprocessor unit (MPU)
Harvard Architecture
Program memory for instructions
Data memory for data
I/O ports
Support devices such as timers
330_02 6
Microcontroller Unit
330_02 7
PIC18F MPU and Memory
330_02 8
Microprocessor Unit
Includes Arithmetic Logic Unit (ALU),
Registers, and Control Unit
Arithmetic Logic Unit (ALU)
Instruction decoder
16-bit instructions
Status register that stores flags
WREG working register
8-bit accumulator
330_02 9
Microprocessor Unit
Program Counter (PC)
21-bit register that holds the Program Memory address
Bank Select Register (BSR)
4-bit register used in direct addressing the Data Memory

File Select Registers (FSRs)
12-bit registers used as memory pointers in indirect
addressing Data Memory
Control unit
Provides timing and control signals
Read and Write operations
330_02 10
PIC18F - Address Buses
Address bus
21-bit address bus for Program Memory
Addressing capacity: 2 MB
12-bit address bus for Data Memory
Addressing capacity: 4 KB
330_02 11
Data Bus and Control Signals
Data bus
16-bit instruction/data bus for Program Memory
8-bit data bus for Data Memory
Control signals
Read and Write
330_02 12
Features of 18Fxx8
High-Performance RISC CPU:
Linear program memory addressing up to 2 Mbytes
Linear data memory addressing to 4 Kbytes
Up to 10 MIPS operation
DC 40 MHz clock input
4 MHz-10 MHz oscillator/clock input with PLL active
16-bit wide instructions, 8-bit wide data path
Priority levels for interrupts
8 x 8 Single-Cycle Hardware Multiplier
330_02 13
Peripheral Features:
High current sink/source 25 mA/25 mA
Three external interrupt pins
Timer0module: 8-bit/16-bit timer/counter with 8-bit
programmable prescaler
Timer1module: 16-bit timer/counter
Timer2module: 8-bit timer/counter with 8-bit period
register (time base for PWM)
Timer3module: 16-bit timer/counter
Secondary oscillator clock option Timer1/Timer3
Capture/Compare/PWM (CCP) modules
330_02 14
Features of 18Fxx8
Peripheral Features
Enhanced CCP module
Master Synchronous Serial Port (MSSP) with two
modes of operation:
3-wire SPI (Supports all 4 SPI modes)
- I2C Master and Slave mode
Addressable USART module
CAN bus Module

330_02 15
Features of 18Fxx8
Advanced Analog Features:
10-bit, up to 8-channel Analog-to-Digital Converter
module (A/D) with:
Conversion available during Sleep
Analog Comparator module:
Programmable input and output multiplexing
Comparator Voltage Reference module
Programmable Low-Voltage Detection (LVD) module:
Supports interrupt-on-Low-Voltage Detection
Programmable Brown-out Reset (BOR)
330_02 16
Features of 18Fxx8
Special Microcontroller Features:
Power-on Reset (POR), Power-up Timer (PWRT) and
Oscillator Start-up Timer (OST)
Watchdog Timer (WDT) with its own on-chip RC
Programmable code protection
Power-saving Sleep mode
Selectable oscillator options, including:
4x Phase Lock Loop (PLL) of primary oscillator
Secondary Oscillator (32 kHz) clock input
In-Circuit Serial ProgrammingTM (ICSPTM) via two pins
330_02 17
Features of 18Fxx8
Flash Technology:
Low-power, high-speed Enhanced Flash technology
Fully static design
Wide operating voltage range (2.0V to 5.5V)
Industrial and Extended temperature ranges
330_02 18
Features of 18Fxx8
PIC 18F458 Memory
Program Memory: 32 K flash
Address range: 000000 to 007FFF
Data Memory : 1536 Bytes
Address range: 000 to 5FF
256 Bytes
Not part of the data memory space
Addressed through special function registers
330_02 19
PIC 18Fxx8 Memory
Program Memory
330_02 20
PIC 18Fxx8
330_02 21
Access Bank
The Access Bank is comprised of the
Upper 160 bytes in Bank 15 (SFRs) and
Lower 96 bytes in Bank 0.
These two sections will be referred to as
Access Bank High and Access Bank Low,
Minimum bank switching
Used to store data & parameters by
programmer and c compilers

330_02 22
Using Access Bank
MOVWF f, a
If a=0 access bank will be selected
If a=1 BSR3:0 is used for selecting one of the
16 banks.

330_02 23
Working Register
W- working register-8 bit
Similar to accumulator
Used in arithmetic and logical operations
MOVLW 0xff
ADDLW 0x56
330_02 24
Status Register
330_02 25
X X X N ov Z DC C
Direct Addressing Mode
330_02 26
Direct Addressing Mode
To copy file register 0x100 to W( working
MOVLB 0x01
MOVWF 0x00
330_02 27
Indirect Addressing
330_02 28
Indirect Addressing
Three FSR (File Select Register)-12 bit register
To copy W register to file register 0x249
LFSR 0,0x249
Will copy the contents of W register to Internal
RAM pointed by the contents of FSR0.
330_02 29
PIC 18FXX8 I/O Ports
Five I/O ports
PORT A through PORT E
Most I/O pins are multiplexed
Generally have eight I/O pins
Addresses already assigned to these ports
Each port is identified by its assigned SFR
330_02 30
I/O Ports A and B
330_02 31
MCU Support Devices
Capture, Compare and PWM (CCP Modules)
Serial Communications
Master Synchronous Serial Port (MSSP)
Addressable USART
A/D converter
Parallel Slave Port (PSP)
330_02 32
MCU Support Devices
330_02 33
Block Diagram
PIC18FXX8 Programming Model
330_02 35
PIC18F Instructions
77 assembly language instructions
Earlier PIC families have 33 or 35 instructions
PIC18F instruction set
Most instructions are 16-bit word length
Four instructions are 32-bit length
330_02 36
Instruction Set

330_02 37
Field Operation
a RAM access bit:
a = 0: RAM location in Access RAM (BSR register is ignored)
a = 1: RAM bank is specified by BSR register
bbb Bit address within an 8-bit file register (0 to 7).
BSR Bank Select Register. Used to select the current RAM bank
d Destination select bit:
d = 0: store result in WREG
d = 1: store result in file register f
dest Destination either the WREG register or the specified register file location
f 8-bit register file address (0x00 to 0xFF).
Instruction Set
330_02 38
Field Operation
fs 12-bit register file address (0x000 to 0xFFF). This is the source address.
fd 12-bit register file address (0x000 to 0xFFF). This is the destination address.
k Literal field, constant data or label (may be either an 8-bit, 12-bit or a 20-
bit value).
Label Label name.

The mode of the TBLPTR register for the table read and table write
instructions. Only used with table read and table write instructions:
No change to register (such as TBLPTR with table reads and writes)
Post-Increment register (such as TBLPTR with table reads and writes)
Post-Decrement register (such as TBLPTR with table reads and writes)
Pre-Increment register (such as TBLPTR with table reads and writes)
n The relative address (2s complement number) for relative branch
instructions or the direct address for Call/Branch and Return instructions.
Instruction Set
330_02 39
Field Operation
PROH Product of Multiply High Byte.
PRODL Product of Multiply Low Byte.
s Fast Call/Return mode select bit:
s = 0: do not update into/from shadow registers
s = 1: certain registers loaded into/from shadow registers (Fast mode)
u Unused or unchanged
WREG Working register (accumulator).
x Dont care (0 or 1).
The assembler will generate code with x = 0. It is the recommended form of
use for compatibility with all Microchip software tools.
TBLPTR 21-bit Table Pointer (points to a program memory location).
Instruction Set
330_02 40
Field Operation
TABLAT 8-bit Table Latch..
TOS Top-of-Stack.
PC Program Counter
PCL Program Counter Low Byte
PCH Program Counter High Byte
PCLATH Program Counter High Byte Latch.
PCLATU Program Counter Upper Byte Latch.
GIE Global Interrupt Enable bit.
WDT Watchdog Timer.
Instruction Set
330_02 41
Field Operation
TO Time-out bit.
PD Power-Down bit.
C DC Z OV N ALU status bits: Carry, Digit Carry, Zero, Overflow, Negative
[ ] Optional.
( ) Contents.
Assigned to.
PCLATU Program Counter Upper Byte Latch.
< > Register bit field.
In the set of.
italics User defined term (font is courier).
Instruction Set
330_02 42
General Format For Instructions
Instruction Set
330_02 43
General Format For Instructions
330_02 44
Instruction Set
General Format For Instructions
330_02 45
Instruction Set
General Format For Instructions
330_02 46
Instruction Set
General Format For Instructions
330_02 47
Instruction Set
General Format For Instructions
330_02 48
Instruction Set
General Format For Instructions
Instruction Set
Byte Oriented Operations

ADDLW (all flags affected)
Syntax ADDLW k
Before Instruction W= 10h
After Instruction W=10h+15h=25h

330_02 49
Instruction Set
ADDWF Add W to f
Syntax ADDWF f, d, [a]
d= 0 0r 1
a= 0 or 1
W + f dest
All flags affected
Before Instruction: W= 17h, REG= 0C2h
After Instruction: W=0D9h, REG= 0C2h

330_02 50
Instruction Descriptions
Copy (Move) 8-bit number (Literal) into W register
Mnemonics: MOVLW 8-bit
Binary format:
0000 1110 XXXX XXXX (any 8-bit number)
Copy (Move) contents of W register into PORTC (File)
Mnemonics: MOVWF PORTC, a
(a indicates that PORTC is in the Access Bank)
Binary format:
0110 1110 1000 0010 (82
is PORTC address)
330_02 51
Illustrative Program
Problem statement:
Write instructions to light up alternate LEDs at
Bidirectional (input or output) port
Setup as output port for display
Logic 1 will turn on an LED
330_02 52
Interfacing LEDs to PORTC
330_02 53
Program (software)
Logic 0 to TRISC sets up PORTC as an output port
Byte 55
turns on alternate LEDs

MOVLW 00 ;Load W register with 0
MOVWF TRISC ;Set up PORTC as output
MOVLW 0x55 ;Byte 55
to turn on LEDS
SLEEP ;Power down
330_02 54
Address Hex Mnemonics Comments
000000 0E00 MOVLW 00 ;Load W with 0s
000002 6E94 MOVWF TRISC ;Set PORTC as output
000004 0E55 MOVLW 0x55 ;Load 55 to turn on LEDs
000006 6E82 MOVWF PORTC ;Turn on LEDs
000008 0003 SLEEP ;Power Down
330_02 55
Execution of the
330_02 56
PIC18 Simulator IDE
330_02 57
Embedded System
330_02 58