Chapter 2
MICROCONTROLLER
ARCHITECTURE & ASSEMBLY
LANGUAGE
PROGRAMMING
Microcontroller Packaging and Appearance
PIC 12F508
PIC 16F84A
Motorola 68000
PIC 16C72
PIC 16F877
Motorola 68HC05B16
PIC microcontrollers in DIP and QFN packages
16-bit 28-pin PDIP PIC24 microcontroller next to a
metric ruler
PIC18 Architecture
Von Neumann Architecture:
Fetches instructions and data from a single memory
space
Limits operating bandwidth
Von Neumann
Architecture
8-bit Bus
CPU
Program
& Data
Memory
PIC18 Architecture
Harvard Architecture:
Uses two separate memory spaces for program instructions and
data
Improved operating bandwidth
Allows for different bus widths
Harvard
Architecture
8-bit Bus
16-bit Bus
Program
Memory
CPU
Data
Memory
PIC18 Microcontroller Families
PIC microcontrollers are designed using
the Harvard Architecture which includes:
Microprocessor unit (MPU)
Program memory for instructions
Data memory for data
I/O ports
Support devices such as timers
Microcontroller with the
Harvard Architecture
A Brief History of PIC C
8-bit Architecture Overview
Compare 8-bit PIC MCU Architectures
PIC18 Architecture Block Diagram
PIC18F4450 Block Diagram
PIC18F MCU and Memory
16 bit
2 MB
221
8 bit
4 KB
212
Includes Arithmetic Logic Unit (ALU), Registers,
and Control Unit
Arithmetic Logic Unit (ALU)
WREG working register
Status register that stores flags
Instruction decoder when the instruction is fetched it goes into
the ID
Registers
Bank Select Register (BSR)
4-bit register used in direct addressing the data memory
File Select Registers (FSRs)
16-bit registers used as memory pointers in indirect addressing
data memory
Program Counter (PC)
21-bit register that holds the program memory address while
executing programs
Control unit
Provides timing and control signals to various
Read and Write operations
Address bus
21-bit address bus for
program memory
addressing capacity: 2 MB
of memory
12-bit address bus for data
memory addressing
capacity: 4 KB of memory
Data bus
16-bit instruction/data bus
for program memory
8-bit data bus for data
memory
Control signals
Read and Write
Data RAM File Register
In the CPU, registers are used to store information temporarily.
It could be a byte of data to be processed, or an address
pointing to the data to be fetched.
Majority of PIC registers are 8-bit registers.
In the PIC there is only one data type: 8-bit.
The range goes from the MSB (most-significant bit) D7 to the
LSB least-significant bit) D0.
With an 8-bit data type, any data larger than 8 bits must be
broken into 8-bit chunks before it is processed.
WREG register
The 8-bit WREG register is the most widely used
register in the PIC micro controller.
WREG stands for working register, as there is only one.
The WREG register is the same as the accumulator in
other microprocessors.
The WREG register is used for all arithmetic and logic
instructions.
To understand the use of the WREG register, we will
show it in the context of two simple instructions: MOVE
and ADD.
MOVLW
The MOVLW instruction MOVES 8-bit data into the
WREG register. It has the following format:
MOVLW K
;move literal value K into WREG
MOVLW 25H ; move value 25H into WREG (WREG = 25H)
Is the following code correct?
MOVLW 9H
MOVLW A23H
ADDLW
The ADDLW instruction ADD 8-bit data into the WREG
register. It has the following format:
ADDLW K
ADDLW 34H
;ADD literal value K to WREG
;ADD literal value 34H to WREG
Example:
MOVLW 12H
ADDLW 16H
ADDLW 32H
;move literal value 12H into WREG
; add value 16H into WREG (WREG = 28H)
; add value 32H into WREG (WREG = 60H)
PIC WREG and ALU Using Literal Value
File register (SFRs and GPR)
It is the data memory.
Read/Write - Static RAM
Used for data storage, scratch pad and
registers for internal use and function
8-bit width
Register relationship in PIC
PIC File Register
General Purpose RAM
GP RAM
EEPROM
Special Function Register
Register File
concept
All data memory
is part of register
file
Any location in
data memory can
be operated
directly
All peripherals
mapped into data
memory as a
series of registers
SFR Special Function Register
8 bit registers
Dedicated to specific functions such as ALU status, timers,
serial communication, I/O ports, ADC etc.
Each SFR functions is fixed by the CPU designer at the time of
design and it is used for controlling microcontroller/peripheral
GPR General Purpose Register
8 bit registers
A group of RAM locations in the file register that are used for
data storage.
Larger than SFR
The microchip website provides the data RAM size, which is the same as
GPR size
File Register Size
File Register
SFR
GPR
(Bytes)
(Bytes)
(Bytes)
PIC12F508
PIC16F84
32
80
7
12
25
68
PIC18F1220
PIC18F452
PIC18F2220
PIC18F458
PIC18F8722
PIC18F4550
512
1792
768
1792
4096
2048
256
256
256
256
158
160
256
1536
512
1536
3938
1888
File Registers of PIC12, PIC16, and PIC18
PIC Status Register
To indicate arithmetic conditions
It is a 8-bit register
Five bits are used
D0:
C
Carry Flag
D1:
DC
Digital Carry Flag
D2:
Z
Zero Flag
D3:
OV
Overflow Flag
D4:
N
Negative Flag
Bits of Status Register
Example 1
Show the status of the C, DC, Z flags after the
following addition instruction
MOVLW 38H
ADDLW 2FH
Solution
38H + 2FH = 67H WREG=67H
C=0
DC=1
Z=0
Example 2
Show the status of the C, DC, Z flags after the
following addition instruction
MOVLW 9CH
ADDLW 64H
Solution
9CH + 64H = 100H WREG= 00H
C=1
DC=1
Z=1
Instruction That Affect
Flag Bits
Flag Bits and Decision Making
PIC Data Format and Directives
There is one data type
8 bits
It is the job of the programmer to break down
data larger 8 bits ( 00 to FFH or 0 to 255)
Data type can be positive or negative number
Data format are
Hex (default in PIC) 12 or 0x12 or H'12' or 12H or
Binary B'00010010 or
Decimal .12 or D'12 or
ASCII A'c' or a'c'
Assembler Directives
What is the difference
between
Instruction and Directives?
Instruction
Tells the CPU what to do
Directives (pseudo-instruction)
Give directions to the assembler
Assembly language Format
LABEL
START
INSTRUCTION/
ASS. DIRECTIVE
MOVLW
OPERAND
12H
COMMENT
;move literal value 12H into
WREG
Label:
use to represent the location of an instruction or a value no
spacing not more than 8 characters including number.
Instruction:
tells the controller what to do. Ex. MOVWF, GOTO..
Assembler directive:
tells the assembler suppose to do. Ex. EQU, ORG, END
Operand:
data to operate on / source of data to operate on and its
destination after processing.
PIC Language Fundamental
Please refer to notes
Arithmetic and Logic Instruction
Please refer to notes
Chapter 5: Arithmetic, Logic Instructions and
Programs
Muhammad Ali Mazidi, Rolin D. Mckinlay & Danny Causey (2008).
PIC Microcontroller and Embedded Systems:
Using Assembly and C for PIC18.
Pearson Prentice Hall. (ISBN: 9780136009023)