Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

Microcomputer and Interfacing

(ECEg 4501)
Lecture II

Text Book: PIC Microcontroller and Embedded Systems


By Ali Mazid e.al.

03/06/2024 1
Outline
• Intro to Assembly Language Programming
• PIC Registers and Simple Assembly
Instructions

03/06/2024 2
Intro to Assembly Language Programming
• CPU works in binary (0s and 1s)
• Programming in binary (i.e. machine language) is
tedious, cumbersome, slow and error prone for
human, though understood very fast by CPU.
• Hence programmers developed assembly language
and replaced the binary codes with mnemonics
(abbreviation of instructions)
• But still assembly language is low-level language
thus programmers developed more humanly
languages categorized as high-level languages such as
C, C++, Java, …
03/06/2024 3
Structure of Assembly Language
• Has four fields
– [label] mnemonic [operands] [;comment]
* The brackets mean the fields are optional
• The labels are to name the specific line of code
• The mnemonic(also called opcode or operation code)
and the operand(also called operation address) are the
main parts doing the actual task
• Comments must begin with a semicolon

03/06/2024 4
Rules for labels
• Must be unique
• Can include alphanumeric characters, digits 0-
9, special characters ?,.,_, @, and $
• The first character must be alphabet
• Must not be reserved words e.g. must not be
mnemonics and directives.
• Must not exceed 32 characters(the number
could vary among PICs)
03/06/2024 5
Example Assembly Language Code

03/06/2024 6
Assembling and Linking A PIC Program
• How do we create, assemble and make an executable
assembly code? The steps to create executable assembly
code is:
1. First using text editor we type an assembly code and save
the file as .asm file extension
2. The .asm source file fed to PIC assembler compiled to
produce object file(.o) and error file(.err)
3. Using linker various object files(.o) will be linked and
finally .hex file plus list file(.lst), map file(.map), an
intermediate object(.out) and debug file(.cod) produced.
• Finally the .hex file will be burned in PICs ROM

03/06/2024 7
03/06/2024 8
PIC File Registers and Simple Assembly Instructions

03/06/2024 9
WREG Register
• WREG = Working Register
• Is An 8-bit register & it acts as an accumulator
• Most widely used register in PIC µC
• Lets see its use with two simple instructions:
MOVE and ADD
MSB LSB

03/06/2024 10
MOVLW instruction
• Moves an 8-bit data into WREG register
– Format: MOVLW K ; move literal k into WREG
– K is an 8-bit value of 0-255 in decimal or 00-FF in hex or
00000000-11111111
– In the MOVLW the ‘L’ indicates literal or just number
– The W represents the WREG and the sequence shows the literal
L stored in W or WREG.
– E.g. MOVLW 25H ; move value 25H into WREG

K= an 8-bit data

03/06/2024 11
ADDLW Instruction
• Adds an 8-bit data with the data in the WREG and stores the
result in WREG itself.
– Format: ADDLW K ; Add literal value K into WREG

K= an 8-bit data
– E.g.
MOVLW 25H ; load 25H into WREG
ADDLW 34H ; add value 34 to WREG
When executed, WREG = 25H+34H= 59H
• Q1: write a code to add values 12H, 16H, 31H and 43H using
ADDLW instruction.
03/06/2024 12
Points to remember when using WREG
with literal value
1. If values 0 to F are to be moved to an 8-bit
microcontroller, the microcontroller will assume
the upper nibble as 0. E.g. MOVLW 5H is equal
to MOVLW 05H and WREG will be loaded with
05H or 00000101b
2. Moving value larger than FFH or 255 in decimal
the upper bytes will be truncated and warning is
generated in .err file.
E.g. MOVLW 7F2H ; is illegal and WREG =
F2H
03/06/2024 13
MOVWF Instruction
• Copies value in WREG W to a file register F
• After the instruction executed the file register location will
have same value to WREG
• File register location can be one of the SFRs or a particular
location in the GP RAM
– Format: MOVWF ‘file register’ ;from wreg to file register location
MOVLW 55H ;WREG = 55H
MOVWF PORTB ;PORTB = WREG = 55H
MOVWF 99H; ;Location 99H = 55H
• Note: literal cannot be moved directly to a file register in
PIC18.
03/06/2024 14
…cont’d
• Q2: State the contents of file Register RAM locations after
the following program:
MOVLW 99H
MOVWF 12H
MOVLW 85H Answer

MOVWF 13H
MOVLW 3FH
MOVWF 14H
MOVLW 63H
MOVWF 15H
MOVLW 12H
MOVWF 16H
03/06/2024 15
ADDWF Instruction
– Format: ADDWF fileReg, d
• Adds the content of WREG and file register
location
• The destination register of the result is
identified by the value of d; if d=0 or ‘d=w’
the result is saved on WREG else if d=1 or
‘d=f’ the result is on the file register location.

03/06/2024 16
… cont’d
Example 1:
Address --- Value ---WREG
5H ----------- 22H----22H
6H ----------- 22H----22H
7H ----------- 22H----22H

5H ----------- 22H----44H
6H ----------- 22H----66H
7H ----------- 22H----88H

Example 2:

Address --- Value----WREG


5H ----------- 22H----22H
6H ----------- 22H----22H
7H ----------- 22H----22H

5H ----------- 22H----44H
6H ----------- 22H----66H
7H ----------- 88H----66H
03/06/2024 17
…cont’d
Q3:

Solution

03/06/2024 18
MOVF Instruction
– Format: MOVF fileReg,d
* if d=0=w, then fileReg copied to WREG
* If d=1=f, then fileReg copied to fileReg itself
• Widely used to copy data from input ports to
WREG
• Also from other SFRs/GP RAM to WREG to
perform arithmetic on the them
• It is equivalent of saying MOVFW, though no
such instruction as MOVFW is present.
03/06/2024 19
…cont’d

03/06/2024 20
MOVFF Instruction
• Copies data from one file register location to another file
register location
• Source and destination can be any of 4k (4096) locations
in the data RAM.

Format:
MOVFF fileReg1,fileReg2

03/06/2024 21
… cont’d
• E.g.
MOVFF PORTB, PORTC

03/06/2024 22
PIC Status Register
• Also called flag registers
• An 8-bit register but PIC18 uses 5-bits of this register and
the rest unimplemented bits are read as zero,0.
• The 5-bits are called conditional flags because they
illustrate change in conditions resulted due to execution
of instruction
• These flags are C(carry), DC(digital carry),Z(zero),
OV(overflow) and N(negative).

03/06/2024 23
… cont’d
• C: sets(become 1) whenever there is a carry out from D7 bit
otherwise it remain 0. This bit is affected after an 8-bit
addition or subtraction.
• DC: sets when there is a carry from D3 to D4 bit otherwise
it remain 0. Sometimes also called AC(Auxiliary Cary) and
usually used during BCD(binary coded decimal) operation.
• Z: shows whether the result of arithmetic operation is zero
or not. Z=1 if arithmetic operation become zero else z=0.
• OV: set when signed number D7 overflow to D8 bit.
• N: Shows if the result is negative or positive. D7=1 and N=1
then result is negative else D7=0 and N=0, positive result.
C,DC,Z= used for unsigned operation
N,OV = used for signed operation
03/06/2024 24
Instructions that
affect flag bits

•Not all instructions


affect flag bits.
•Some instructions
affect all flag bits,
some affect partly and
some doesn’t affect
the flag bits at all.

03/06/2024 25
Example 1: C,DC and Z

03/06/2024 26
Example 2: C,DC and Z

03/06/2024 27
Example 3: C,DC and Z

03/06/2024 28
Flag bits and Decision making instructions
• Since flag bits are also called conditional flags, there are
instructions that will make a conditional jump (branch)
based on the status of flag bits. We will discus these
instructions in next lecture.

PIC18 Branch(Jump)
Instructions using flag bits

BNN

03/06/2024 29
Program Counter(PC) register
• An important register used by CPU to point to the address
of the next instruction to be executed.

• It is 21-bit wide register hence the CPU can fetch op-codes


from 2M possible locations(000000H to 1FFFFFH).

• Earlier PICs such as PIC12F has 12bit and PIC16F has


14bit wide PCs hence restrict program memory to
maximum of 4k and 16K respectively, which was too
small.

03/06/2024 30
ROM memory map in PIC18
• PIC18 family has 21 bit PC leading to 2Mbyte on chip
ROM(program) memory at a maximum.
• However, not all PIC18 has 2M instead they have few
kilo bytes.

03/06/2024 31
…cont’d

03/06/2024 32
Where do PIC wakes up on power on?
• All PIC family on power
up they woke up at
address 0H,which means
program stored at 0H is
executed during reset or
start up.
• Thus the first opcode
must be stored at 0H and
this is achieved using
ORG directive.

03/06/2024 33
03/06/2024 34
PIC Data Type
• PIC18 MCU have only one data type and that is byte or
8-bits and its registers are all 8-bit wide.
• Data’s more than 8-bit wide must be managed by the
programmer to be processed by the CPU of PIC18 and we
will discuss the techniques to manage/operate with data
above 8-bit when we see about arithmetic operation.

03/06/2024 35
PIC Data format representation
• There are 4 ways to represent a byte data in PIC
assemblers.

• These are in hex(hexadecimal), binary, decimal and


ASCII formats.

03/06/2024 36
Hex numbers
• Four ways to represent hex numbers

or MOVLW H’99’

Valid hex Example Invalid hex Example

**If hex value starts with value A-F


then it must be preceded with a zero.
03/06/2024 37
Binary numbers
• Only two ways to represent binary number in PIC.

– Put b(B) in front of the binary number enclosed in


single quote like MOVLW B’10011001’ OR MOVLW
b’10011001’
– Put 0b(0B) in front of the binary number like
MOVLW 0b10011001 or MOVLW 0B10011001
Example

MOVLW 0b10011001 ;WREG = 99H

03/06/2024 38
Decimal Numbers
• Two ways to represent decimal numbers
1. Put D or d at a front enclose the following
decimal number in a single quote like MOVLW
D’12’
2. Put the decimal value directly like MOVLW 12 is
equal to MOVLW d’12’

03/06/2024 39
ASCII character
• To represent ASCII character in a PIC assembler we use
letter A(a) as follow:
– MOVLW A‘the character’
• E.g.
MOVLW A’2’ ; WREG=32H=00110010b
because a hex value of ASCII
character ‘2’ is 32H
More e.g.:

03/06/2024 40
03/06/2024 41
03/06/2024 42
Assembler Directives
• Assembler directives are instructions that are directed
to the assembler to do a specific thing.
• It is not translated into machine code.
(Assembler directives are executed by the assembler
at assembly time, not by the CPU at run time).
• Directives can be used to :
– Link symbolic names to actual values.
– Set up pre-defined constants.
– Allocate storage for data in memory.
– Control the assembly process.
– Include additional source files.
– starting address for the program.

03/06/2024 43
Common assembler directives
• EQU:- used to define a constant value or a constant address
• SET:- same as EQU except value assigned by SET can be reassigned
later in the code
• ORG:- used to indicate beginning of address for code or data
• END:- indicates end of the assembly code, must be present in every
assembly file(.asm)
• LIST:- indicates the specific PIC for which the assembly code is to be
assembled
• #include:- tells the assembler to include libraries associated with the
specific chip for which we are compiling the program
• _config:- tells the assembler the configuration bits of the targeted PIC.
• Radix:- indicates weather the numbering system is hexadecimal or
decimal. Default is hexadecimal.
03/06/2024 44
Example use of EQU

03/06/2024 45

You might also like