You are on page 1of 89

Chapter 2:

PIC ARCHITECTURE Embedded


AND ASSEMBLY System Design,

LANGUAGE
Bahria University
Islamabad
PROGRAMMING
“PIC Microcontroller and Embedded Systems”, Muhammad
Ali Mazidi, Rolin D. Mckinlay, Danny Causey Prentice Hall,
2008.

Embedded Systems, Bahria University Islamabad 1


• To program in assembly language, we must
understand the registers and architecture of a given
CPU.
• CPU includes
1. Arithmetic Logic unit (ALU)
2. Registers
Introduction 3. Control Unit

Embedded Systems, Bahria University Islamabad 2


• ALU

CPU
1.
Arithmetic
Logic
Unit(ALU)

Embedded Systems, Bahria University Islamabad 3


• Performs logical and arithmetic functions
• It has following registers
 WREG – stands for working register (acts as an accumulator) – 8 bit
CPU register used to perform arithmetic or logical functions
 Status register that stores flags – indicates the status of the operation
1. done by ALU
 Instruction decoder (ID)– when the instruction is fetched, it goes into the
Arithmetic ID to be interpreted – tell the processor what to do

Logic
Unit(ALU)
(1)

Embedded Systems, Bahria University Islamabad 4


To understand use of WREG register, two instructions
are used
• MOVLW instruction
CPU MOVLW K ; move literal value K into WREG
1. K is an 8-bit value
L stands for literal which means, literally, a number
Arithmetic must be used
Logic Examples
Unit(ALU) • MOVLW 25H
(2) • MOVLW 87H

Embedded Systems, Bahria University Islamabad 5


• ADDLW instruction
ADDLW K ; add literal value K to WREG
It add literal value k to register WREG and put the
CPU result back in the WREG register
1. MOVLW 25H
Arithmetic ADDLW 34H
Executing the above lines results in WREG=59H
Logic (25H+34H=59H)
Unit(ALU)
(3)

Embedded Systems, Bahria University Islamabad 6


• This is the block diagram of ALU.
• Form above example
first instruction move 25H(8-bit literal) in WREG
CPU (via ALU).
1. Second instruction add 34H with content of WREG
(25H) and store result (59H) in WREG
Arithmetic
Logic MOVLW 25H
ADDLW 34H
Unit(ALU)
(4)

Embedded Systems, Bahria University Islamabad 7


• Register

CPU
2. Registers

Embedded Systems, Bahria University Islamabad 8


• Registers – hold memory address
• Bank Select Register (BSR)
4-bit register used in direct
addressing the data memory
• File Select Registers (FSRs)
CPU 16-bit registers used as memory
pointers in indirect addressing data
2. Registers memory
• Program Counter (PC)
21-bit register that holds the
program memory address while
executing programs

Embedded Systems, Bahria University Islamabad 9


• Control Unit

CPU
3. Control
Unit

Embedded Systems, Bahria University Islamabad 10


• Provides timing and control signals to various Read
and Write operations

CPU
3. Control
Unit

Embedded Systems, Bahria University Islamabad 11


SECTION 2.2
THE PIC File Register

Embedded Systems, Bahria University Islamabad 12


• PIC File Register / Data memory

PIC file
register

Embedded Systems, Bahria University Islamabad 13


• PIC has many other registers in addition to the WREG
• They are called data memory space to distinguish
them form program(code)memory
• Data memory space is a read/write (static RAM)
• It is also called file register
PIC file • It is used by CPU for data storage, scratch pad and
register registers for internal use and functions
• In PIC it ranges from 32 bytes to several thousand
bytes depending on the chip

Embedded Systems, Bahria University Islamabad 14


• It has maximum of 4096
(4K) bytes
• With 4096 bytes, it has
addresses of 000-FFF (12-
bit address bus )
• The file register (data RAM)
PIC file is divided into two section
register a) General-Purpose
Registers(GPR) or
General-Purpose RAM
(GP RAM)
b) Special function
Register (SFR)

Embedded Systems, Bahria University Islamabad 15


• Special Function Registers (SFRs) are dedicated to
specific functions such as I/O ports, ADC, ALU status,
timer and so on
• The PIC SFRs are 8-bit register
• 128 bytes in RAM (F80h-FFFh)
PIC File
register
(SFR)

Embedded Systems, Bahria University Islamabad 16


• Special Function Register (SFRs) of PIC18 we will study
it in late chapters

PIC File
register
(SFR)

Embedded Systems, Bahria University Islamabad 17


• 128 bytes (000h-07Fh) of Storage are used for
read/write storage, normally called scratch pad.
• Location 080h – F7Fh to be used for general purpose
by various member of PIC18.

PIC File
register
(GP RAM)

Embedded Systems, Bahria University Islamabad 18


SECTION 2.3
Assembly Language Instructions

Embedded Systems, Bahria University Islamabad 19


• MOVWF instruction
MOVWF FileReg

We saw MOVLW instruction previous that move


Assembly literal (8-bits) to WREG
Language In MOVWF value of WREG is move to ( F ) means
file register
instructions The location in the file register can be one of SFRs
(MOVWF) or a location in the general-purpose register region
After execution of instruction location in the file
register will have the same value as WREG

Embedded Systems, Bahria University Islamabad 20


• Location with SFR

Assembly • Location with General Purpose register


Language
instructions
(MOVWF)

Embedded Systems, Bahria University Islamabad 21


Assembly
Language
instructions
(MOVWF)

Embedded Systems, Bahria University Islamabad 22


• ADDWF instruction
It add the content of WREG and file register.
File register can be one of SFRs or a General-
purpose register.
The destination can be WREG or the File register
Assembly
Format for this instruction is
Language
instructions
(ADDWF) fileReg is the file register location
D indicate destination bit (D=0 for WREG, D=1 for
file register)

Embedded Systems, Bahria University Islamabad 23


Assembly
Language
instructions
(ADDWF)

Embedded Systems, Bahria University Islamabad 24


Assembly
Language
instructions
(ADDWF)

Embedded Systems, Bahria University Islamabad 25


• We can also write w or f instead of 0 or 1

• Rewrite of the last program


Assembly
Language
instructions
(ADDWF)

Embedded Systems, Bahria University Islamabad 26


Assembly
Language
instructions
(ADDWF)

Embedded Systems, Bahria University Islamabad 27


Assembly
Language
instructions
(ADDWF)

Embedded Systems, Bahria University Islamabad 28


Assembly
Language
instructions

Embedded Systems, Bahria University Islamabad 29


• COMF instruction

This instruction complement (inverts) the content


Assembly of fileReg and place result in WREG or fileReg.
Language
instructions
(COMF)

Embedded Systems, Bahria University Islamabad 30


Assembly
Language
instructions
(COMF)

Embedded Systems, Bahria University Islamabad 31


• DECF instruction

 This instruction Decrements (subtracts one from)


the contents of fileReg and place the result in
Assembly WREG or fileReg

Language
instructions
(DECF)

Embedded Systems, Bahria University Islamabad 32


• MOVF instruction

It intended to perform MOVFW


Assembly If D=0, it copies content of fileReg to WREG
Language If D=1, the content of fileReg is copied to itself
It is used to bring data into WREG from I/O ports
instructions Sometimes it is used to copy fileReg to itself for
(MOVF) the purpose of testing fileReg contents.

Embedded Systems, Bahria University Islamabad 33


Assembly
Language
instructions
(MOVF)

Embedded Systems, Bahria University Islamabad 34


Assembly
Language
instructions
(MOVF)

Embedded Systems, Bahria University Islamabad 35


• MOVFF instruction

It copies data from one location of fileReg to


another location in fileReg.
Assembly fileReg location for source and destination can be
any of 4096 location of RAM
Language MOVFF allows us to move data within RAM
instructions without going through the WREG register
(MOVFF)

Embedded Systems, Bahria University Islamabad 36


Assembly
Language
instructions
(MOVFF)

Embedded Systems, Bahria University Islamabad 37


Assembly
Language
instructions
(MOVFF)

Embedded Systems, Bahria University Islamabad 38


• So far, we have done following instructions
MOVLW value
ADDLW value
MOVWF fileReg
ADDWF fileReg, D
Assembly COMF fileReg, D
Language DECF fileReg, D
instructions MOVF fileReg, D
MOVFF fileReg, fileReg

Embedded Systems, Bahria University Islamabad 39


Assembly
Language
instructions

Embedded Systems, Bahria University Islamabad 40


Assembly
Language
instructions

Embedded Systems, Bahria University Islamabad 41


SECTION 2.4
PIC STATUS REGISTER

Embedded Systems, Bahria University Islamabad 42


• The status register is an 8-bit register
• It is also referred as flag register
• Only 5 bits used by PIC18
• The three unused bits are unimplemented and read as
0.
PIC • The five flags called conditional flags, meaning that
STATUS they indicated some conditions after an instruction is
REGISTER executed

Embedded Systems, Bahria University Islamabad 43


• These five flags are
C (Carry)
DC (Digital carry)
Z (Zero)
OV (overflow)
PIC N (negative)
STATUS
REGISTER

Embedded Systems, Bahria University Islamabad 44


• C (the carry flag)
This is set whenever there is a carry out from D7
bit. This flag bit is affected after an 8-bit addition or
subtraction.
• DC (the digital carry flag)
PIC If there is a carry form D3 to D4 during an ADD or
STATUS SUB operation, this bit is set; otherwise, it is
cleared. This is used to perform BCD arithmetic.
REGISTER • Z (the zero flag)
It reflects the result of arithmetic or logical
operations. Z=1 if result is zero, If not Z=0.

Embedded Systems, Bahria University Islamabad 45


• OV (The overflow flag)
This flag is set whenever the result of a signed
number operation is too large, causing the high-
order bit to overflow into the sign bit.
The OV and N flag bits are used for the signed
PIC number arithmetic operations.
• N (The negative flag)
STATUS Binary representation of signed numbers uses D7 as
REGISTER the sign bit.
The negative flag reflects the result of an arithmetic
operation.
If the D7 bit of the result is zero, then N = 0 and the
result is positive. If the D7 bit is 1, then N = 1 and the
result is negative.
Embedded Systems, Bahria University Islamabad 46
• Some instructions affect all the five flag bits.
• But some instructions affect no flag bits at all. The
move instructions are in this category (except MOVF)
• Some instructions affect only the Z or N flag bits or
both. The logic instructions are in this category (e.g.
PIC ANDWL)
STATUS
REGISTER

Embedded Systems, Bahria University Islamabad 47


PIC
STATUS
REGISTER

Embedded Systems, Bahria University Islamabad 48


PIC
STATUS
REGISTER

Embedded Systems, Bahria University Islamabad 49


PIC
STATUS
REGISTER

Embedded Systems, Bahria University Islamabad 50


PIC
STATUS
REGISTER

Embedded Systems, Bahria University Islamabad 51


SECTION 2.5
PIC DATA FORMAT AND
DIRECTIVES

Embedded Systems, Bahria University Islamabad 52


• The PIC microcontroller has only one data type.
• It is 8 bits, and the size of each register is also 8 bits.
• The programmer break down data larger than 8 bits,
00 to FFH, or 0 to 255 in decimal to be processed by
the CPU.
PIC data • The data types used by the PIC can be positive or
negative.
type

Embedded Systems, Bahria University Islamabad 53


• There are 4 ways to represent a byte of data in the PIC
assembler. The numbers can be in
1. hex,
2. binary,
Data 3. decimal or
4. ASCII formats.
Format
Representat
ion

Embedded Systems, Bahria University Islamabad 54


• 1. Hex Numbers
There are 4 ways to show hex numbers:
1.Can use 'h' or 'H' right after the number like this: MOVLW 88H
or MOVLW 88h

Data 2.Put 0x or 0X in front of the number like this: MOVLW 0x88 or


MOVLW 0X88
Format 3.Put nothing in front or back of the number like this:
MOVLW 88
Representat 4.Put 'h' in front of the number, but with this single quotes
around the number like this: MOVLW h'88'
ion
(1.HEX)

Embedded Systems, Bahria University Islamabad 55


• If the value starts with the hex digits A - F, then it must
be preceded with a zero.

Data
Format
Representat
ion
(1. HEX)

Embedded Systems, Bahria University Islamabad 56


• 2. Binary Number
There is only on way to represent  binary numbers
in a PIC assembler. It is as follows.

Data
Format The lowercase b will also work. ' is the single quote
Representat key
ion
(2. Binary)

Embedded Systems, Bahria University Islamabad 57


• 3. Decimal Number
There are 2 ways to represent decimal numbers in
a PIC assembler, One way is as follows.
Data
Format
Representat The lowercase d will work also.
ion The other way to represent decimal numbers is to
use ".value"
(3.
DECIMAL)

Embedded Systems, Bahria University Islamabad 58


• 3. ASCII data
Use the letter A to represent the ASCII data in PIC
assembler

Data
Format To define ASCII strings, more then one character,
Representat we use the DB, define byte directive( we will study
ion DB later).
(3. ASCII)

Embedded Systems, Bahria University Islamabad 59


• Directives
Instructions tell the CPU what to do
Directives, also called pseudo-instructions give
directions to the assembler
The MOVLW and ADDLW instructions are
commands to the CPU, but EQU, ORG and END are
Assembler directives to the assembler.
Directives The widely used directives of the PIC are EQU and
SET.

Embedded Systems, Bahria University Islamabad 60


• EQU (equate) :-
This is used to define a constant value or a fixed
address.
The EQU directive does not set aside storage for a
data item, but associates a constant number with a
Assembler data or an address label
Directives
(EQU)
Advantage:
Using EQU, the programmer can change it once and the
assembler will change all its occurrences throughout the
program, rather than search the entire program trying to find
every occurrence
Embedded Systems, Bahria University Islamabad 61
• SET
This directive is used to define a constant value or
a fixed address.
In this regard, the SET and EQU directives are
identical.
Assembler The only difference is the value assigned by the SET
directive may be reassigned later.
Directives
(SET)

Embedded Systems, Bahria University Islamabad 62


• EQU to assign fix data

Assembler
Directives
EQU
Examples

Embedded Systems, Bahria University Islamabad 63


• EQU for SFR address assignment

Assembler
Directives • EQU for RAM address assignment
EQU
Examples

This is helpful in order to change the address

Embedded Systems, Bahria University Islamabad 64


• The following program will move value 9 into RAM
locations 0 - 4, then add them together and place the
sum in location 10H

Assembler
Directives
EQU
Examples

Embedded Systems, Bahria University Islamabad 65


• ORG (Origin) :-
The ORG directive is used to indicate the beginning
of the address.
It can be used for both code and data. The number
Assembler that comes after ORG must be in Hex.
Directives • END Directive:-
ORG The important pseudocode directive is END.
This indicates to the assembler the end of the
END source, asm file.
The END directive is that last line of the PIC
program, meaning that anything after the END
directive in the source code is ignored by the
assembler.

Embedded Systems, Bahria University Islamabad 66


• LIST Directive:-
END and ORG are used by all assemblers. LIST
directive is unique to the PIC assembler.
It indicates to the assembler the specific PIC chip
for which the program should be assembled. It is
Assembler used as follows.
Directives
LIST
The above tells the PIC assembler to assemble the
program specifically for the PIC18F458
microcontroller. We use LIST to state the target
chip.

Embedded Systems, Bahria University Islamabad 67


• Rules for Labels in Assembly Language
Each label name must be unique.
The names used for labels in assembly language
programming consist of
Alphabetic letters in both upper and lower case,

Assembler The digits 0 through 9, and


The special characters question mark (?), period (.), at (@),
Directives underline (_), and a dollar sign ($).
The first character of the label must be an alphabetic character. In
other words, it cannot be a number.
Every assembler has some reserved words that must not be used as
labels in the program. Foremost among the reserved words are the
mnemonics for the instructions. E.g MOVWL and ADDWL. There
are some other reserved words too.

Embedded Systems, Bahria University Islamabad 68


SECTION 2.6
INTRODUCTION TO PIC
ASSEMBLY PROGRAMMING

Embedded Systems, Bahria University Islamabad 69


• CPU can work only in binary, it can do so at a very high
speed
• A program consist of 0s and 1s is called Machine
INTRODU Language
CTION TO • It is quit tedious and slow for Humans to deal with 0s
PIC and 1s.
ASSEMBL • So assembly languages were developed, which
provided mnemonics for the machine code
Y
• Assembly language programs must be translated to
PROGRA machine code by a program called assembler.
MMING • Assembly language is referred to as a low-level
because it deals directly with the internal structure of
CPU and register inside it
Embedded Systems, Bahria University Islamabad 70
• BASIC, Pascal, C, C++, Java etc. These languages are
called high-level languages because the programmer
does not have to be concerned with the internal
INTRODU details of the CPU
CTION TO • High level languages are translated into machine code
PIC by a program called a compiler
ASSEMBL • To write a program in C, one must use a C compiler to
translate the program into machine language.
Y
PROGRA
MMING

Embedded Systems, Bahria University Islamabad 71


• An assembly language program is a series of
statements, or lines, which are either
assembly language instructions such as ADDLW
STRUCTU and MOVWF, or
statements called directives
RE OF
• The directive ORG tells the assembler to place the
ASSEMBL opcode at memory location 0 while END indicates the
Y end of the source code to the assembler
LANGUA • An assembly language instruction consists of four
fields:
GE
• Brackets indicate that a field is optional and not all
lines have them. Brackets should not be typed in.
Embedded Systems, Bahria University Islamabad 72
STRUCTU
RE OF
ASSEMBL
Y
LANGUA
GE

Embedded Systems, Bahria University Islamabad 73


SECTION 2.8
THE PROGRAM COUNTER AND
PROGRAM ROM SPACE IN THE PIC

Embedded Systems, Bahria University Islamabad 74


•  Program counter (PC)
It is used by the CPU to point to the address of the
next instruction to be fetched
As the CPU fetches the opcode from the ROM, the
program counter incremented automatically to the
PROGRA next instruction
M The program counter in PIC18 is 21-bit
COUNTER PIC18 can access program address 000000H to
1FFFFFH total 2M (
(PC)

Embedded Systems, Bahria University Islamabad 75


• PIC18F458 has 32K of on-chip ROM
• No member of PIC family can access more than 2M of
opcode because of 21-bit Program counter
• First location of program ROM has address of 000000 ,
the last location ca be different depending on the size
ROM of the ROM on chip
memory • In PIC18F458 the last location is 07FFF (32K)
map

Embedded Systems, Bahria University Islamabad 76


ROM
memory
map

Embedded Systems, Bahria University Islamabad 77


• Let's examine the action of the Program Counter PC as
each instruction is fetched and executed
• The list file of code show how code is placed in the
ROM
Placing • The opcode and operand for each instruction are listed
on the left side of the list file
code in
program
ROM

Embedded Systems, Bahria University Islamabad 78


Placing
code in
program
ROM

Embedded Systems, Bahria University Islamabad 79


• ROM width in the PIC18
The data bus is like traffic lanes on the highway
The more lanes the more information can bring to
CPU
In PIC18, the internal data bus between the code ROM
ROM width and the CPU is 16-bits.
Therefore, the 64K ROM space is shown as 32K x 16
in the using a 16-bits word data size.
PIC18 Two reasons to make 16-bit data bus
1. Increase the processing power
2. Match the instruction with of PIC18 (most of the instructions of
PIC18 is of two bytes). This will make instruction fetch a single cycle.
In PIC18 all instructuions are either 2-byte or 4 bytes
(no 1-byte or 3-byte instruction). This is part of RISC
architectural philosophy
Embedded Systems, Bahria University Islamabad 80
ROM width
in the
PIC18

Embedded Systems, Bahria University Islamabad 81


• Little endian vs big endian war
The low byte goes to low memory address and
high byte goes to high memory address this is little
endian
Big endian is opposite of it. Low byte goes to high
Little memory address and high byte goes to low
memory address.
endian vs PIC18 is follows big endian convention as shown
Big endian  0E is high byte goes to 0000h location. Where 25h
goes to 00001h

Embedded Systems, Bahria University Islamabad 82


• Instruction size of the PIC18
PIC18 program memory is byte-addressable, and
instructions are either 2-byte of 4-byte.
Almost all the instruction are of 2-bytes
The exceptions are MOVFF, GOTO, and a few
Instruction others
size of the We will se some instruction

PIC18

Embedded Systems, Bahria University Islamabad 83


• MOVLW instruction formation
MOVLW is a 2-byte (16-Bit) instruction
The first 8-bits are OPCODE
The other 8-bits are for literal value of 00H to FFH

Instruction
size of the
PIC18

Embedded Systems, Bahria University Islamabad 84


• ADDLW instruction formation
ADDLW is a 2-byte (16-Bit) instruction
The first 8-bits are OPCODE
The other 8-bits are for literal value of 00H to FFH

Instruction
size of the
PIC18

Embedded Systems, Bahria University Islamabad 85


• MOVWF instruction formation
MOVWF is a 2-byte (16-Bit) instruction
The first 8-bits are OPCODE
The other 8-bits are for location of the file register
in the data RAM
Instruction The LSB bit of the OPCODE is for access bank
size of the If a=0, the fileReg is in access bank, if a=1 then we
have to use bank switching (will covered in Chapter
PIC18 6)

Embedded Systems, Bahria University Islamabad 86


• MOVFF instruction formation
It is 4-Bytes (32 bits) instruction
First 16 bits are of OPCODE and the address of the
source fileReg
The other 16 bits are used for the opcode and the
Instruction address of the destination
size of the
PIC18

Embedded Systems, Bahria University Islamabad 87


• GOTO instruction formation
It is 4 byte
12-bits are OPCODE
20-bits are target address
However, 20 bits give us only 1M of address and
Instruction PIC18 has 2M of ROM. This is solved by making LSB
size of the to 0
PIC18
Making LSB to zero will make sure that the target
address is an even. That's what we want because
instruction are either 2-byte or 4-byte. Avoid
landing middle of the instruction
Embedded Systems, Bahria University Islamabad 88
CHAPTER 2
END

Embedded Systems, Bahria University Islamabad 89

You might also like