We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 25
PIC STATUS REGISTER
The status register is an 8-bit register. It is also referred to as the flag
register. Although the status register is 8 bits wide, only 5 bits of it are
used by the PIC 18. The three unused bits are unimplemented and read
as 0. The five flags are called conditional flags. These five flags are C
(carry), DC (digital carry), Z (zero), OV (overflow), and N (negative). Each
of the conditional flags can be used to perform a conditional branch
(jump). o7 Do
Lx Tx Tx ys Tor 2 fot < |
CG ~ Cary flag
DC - Digital Carry flag
2Z- Zero flag
OV - Overflow flag
N — Negative flag
X—-D65, D6, and D7 are not implemented,PIC STATUS REGISTER
C, the carry flag
This flag is set whenever there is a carry out from the D7 bit. This flag
bit is affected after an 8-bit addition or subtraction.
DC, the digital carry flag
If there is a carry from D3 to D4 during an ADD or SUB operation, this
bit is set; otherwise, it is cleared. This flag bit is used by instructions that
perform BCD (binary coded decimal) arithmetic. In some
microprocessors this is called the AC flag (Auxiliary Carry flag).PIC STATUS REGISTER
Z, the zero flag
The zero flag reflects the result of an arithmetic or logic operation. If the
result is zero, then Z = 1. Therefore, Z = 0 if the result is not zero.
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. In general,
the carry flag is used to detect errors in unsigned arithmetic operations
while the overflow flag is used to detect errors in signed arithmetic
operations. The OV and N flag bits are used for the signed number
arithmetic operations.PIC STATUS REGISTER
N, the negative flag
Binary representation of signed numbers uses D7 as 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
one, then N = 1 and the result is negative. The negative and OV flag bits
are used for the signed number arithmetic operations.EXAMPLE
Show the status of the C, DC, and Z flags after the addition of 3 8H and
2FH in the following instructions:
MOVLW 38H
ADDLW 2PH jadd 2FH to WREG
Solution:
38H 0011 1000
+2FH 0010 1111
67H 0110 0111 WREG = 67H
C= 0 because there is no carry beyond the D7 bit. DC = 1 because there
is a carry from the D3 to the D4 bit. Z = 0 because the WREG has a value
other than 0 after the addition.EXAMPLE
Show the status of the C, DC, and Z flags after the addition of 9CH and
64H in the following instructions:
MOVLW 9CH
ADDLW 64H yadd 64H to WREG
Solution:
9CH 1001 1100
+_64H Q110 0100
100H 00000000 =WREG = 00
C= 1 because there is a carry beyond the D7 bit. DC = 1 because there is
a carry from the D3 to the D4 bit. Z = 1 because the WREG has a value 0
in it after the addition.EXAMPLE
Show the status of the C, DC, and Z flags after the addition of 88H and
93H in the following instructions:
MOVLW 88H
. ADDLW 93H jadd 93H to WREG
Solution:
88H 1000 1000
+93H 001 001]
11BH 0001 1011 WREG = 1BH
C= 1 because there is a carry beyond the D7 bit. DC = 0 because there is
no carry from the D3 to the D4 bit. Z = 0 because the WREG has a value
other than 0 after the addition.Flag bits and decision making
Because status flags are also called conditional flags, there are instruc-
tions that will make a conditional jump (branch) based on the status of
the flag bits.
Instructions Using Flag Bits
Instruction Action
BC Branch ifC = 1
BNC Branch i
BZ Branch if Z = 1
BNZ Branch if Z
BN Branch if N
BNC Branch if N#0
BOV Branch if OV
BNOV. Branch ifOV #0PIC data type
The PIC microcontroller has only one data type. It is 8 bits, and the size
of each register is also 8 bits. It is the job of the programmer to break
down data larger than 8 bits (00 to FFH, or Oto 255 in decimal) to be
processed by the CPU.
Data format representation
There are four ways to represent a byte of data in the PIC assembler.
The numbers can be in hex, binary, decimal, or ASCII formats.HEX numbers
There are four ways to show hex numbers:
1. We can use h ( or H) right after the number like this: MOVLW 99H
2. Put Ox ( or OX) in front of the number like this: MOVLW 0x99
3. Put nothing in front or back of the number like this: MOVLW 99
4. Put h in front of the number, but with single quotes around the
number like this: MOVLW h'99"
Notice that some PIC assemblers might give you a warning (but no
error) when you use 99H because the assembler already knows that
data is in hex and there is no need to remind it. We do that simply to
remind ourselves (and it is a good reminder) when we do coding in
Assembly.HEX numbers
MOWLW 25 ;WREG
ADDLW Ox1l ;WREG 11H = 36H
ADDLW 12H j WREG 12H = 48H
ADDLW H'2A' ;WREG 2aH = 72H
ADDLW 2CH =; WREG 2CH = SEH
The following are invalid:
MOVLW ESH jinvalid, it must be MOVLW OESH
ADDLW Cé sinvalid, it must be ADDLW océ
Notice in the last two instructions that if the value starts with the hex
digits A-F, then it must be preceded with a zero. However, the following
is valid:
MOVLW OF valid, WREG = OFH (or 00001111 in binary)Binary numbers
There is only one way to represent binary numbers in a PIC assembler. It
is as follows:
MOVLW B'10011001' ;WREG = 19011001 or 99 in hex
The lowercase b will also work. Note that ' is the single quote key.
MOVLW B'C0100101' ;WREG = 25H
ADDLW B'00010001' ;WREG = 25H + 11H = 36HBinary numbers
There is only one way to represent binary numbers in a PIC assembler. It
is as follows:
MOVLW B'10011001' ;WREG = 19011001 or 99 in hex
The lowercase b will also work. Note that ' is the single quote key.
MOVLW B'C0100101' ;WREG = 25H
ADDLW B'00010001' ;WREG = 25H + 11H = 36HDecimal numbers
There are two ways to represent decimal numbers in a PIC assembler.
One way is as follows:
MOVLW D‘'12° ;WREG = 00001100 or OC in hex
The lowercase d will work also. Here are some examples of how to use
it:
MOVLW D'37' ;WREG = 25H {37 in decimal is 25 in hex)
ADDLW D'17' ;WREG = 37 + 17 = 54 where 54 in dec is 36H
The other way to represent decimal numbers is to use ". value" as seen
in some application notes for PIC microcontrollers. This is shown as
follows:
MOVLW .12 #WREG = 00001100 = OCH = 12Assembler directives
While instructions tell the CPU what to do, directives ( also called
pseudo-instructions) give directions to the assembler. For example, the
MOVLW and ADDLW instructions are commands to the CPU, but EQU,
ORG, and END are directives to the assembler.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 data or an address label so that when the label
appears in the program, its constant will be substituted for the label.
COUNT EQU 0x25
MOVLW COUNT ;WREG = 25H
What is the advantage of using EQU? Assume that a constant (a fixed
value) is used throughout the program, and the programmer wants to
change its value everywhere. By the use of EQU, the programmer can
change it once and the assembler will change all of its occurrences
throughout the program, rather than search the entire program trying
to find every occurrence.SET
This directive is used to define a constant value or a fixed address. In
this regard, the SET and EQU directives are identical. The only difference
is the value assigned by the SET directive may be reassigned later.Using EQU for fixed data assignment
DATA1 EQU
DATA2 EQU
DATA3 EQU
DATA EQU
DATAS EQU
DATA6 EQU
DATA? EQU
DATA8 EQU
DATAS EQU
DATALO EQU
DATA11 EQU
DATAL2 EQU
jin hexadecimal
39 tjhex data is the default
0x39 janother way for hex
39H ;another way for hex (redundant)
H'39" janother way for hex
h'39! janother way for hex
;in binary
b'00110101' ;binary (35 in hex)
B'00110101" ;binary (35 in hex}
yin decimal
D'28' ;decimal numbers (1C in hex)
d'2g' ;second way for decimal
rin ASCIT
AMD ;ASCIT characters
af2" ;another way for ASCII char
2' another way for ASCII charUsing EQU for SFR address assignment
COUNTER EQU 0x00 ;counter value 00
PORTB EQU OxFF6 ;SFR Port B address
MOVLW COUNTER ;WREG = 00H
MOVWF PORTB 7Port B now has 00 too
INCF PORTR, F ;Port B has 01
INCF PORTE, F jinerement Port B (Port B = 02)
INCF PORTE, F jincrement Port B (Port B = 03)
The above is for the PIC 18 family. If you use a different PIC controller
such as PIC16F, where Port B is a different address, then change the
EQU address for Port B and re-assemble the program and run it.
COUNTER EQU 0x0G ;counter value 00
PORTB EQU 0x07 Port B addr in PIC16F
MOVLW COUNTER iWREG = OCH
MOVWF PORTB 7Port B now has 00 too
INCP PORTB, F Port B has 01
INCF PORTB, F 7Port B has 02
INCF PORTB, F 7Port B has 03Using EQU for RAM address assignment
MYREG EQU 0x12
MOVLW
MOVWE
MOVLW
ADDWE
ADDWF
ADDWF
ADDWW
0
MYREG
22H
MYREG,
MYREG,
MYREG,
MYREG,
P
F
F
F
jassign RAM loc to MYREG
;clear WREG (WREG = 0)
iclear MYREG (loc 12H has 0}
;WREG = 22H
GMYREG = WREG + MYREG
:MYREG = WREG + MYREG
;MYREG = WREG + MYREG
;MYREG = WREG + MYREG
This is especially helpful when the address needs to be changed in order
to use a different PIC chip for a given project. It is much easier to refer
to a name than a number when accessing RAM address locations.Using EQU for RAM address assignment
The following program will move MYvAP Bop) 5 ithVAL
RO EQU oO iassign RAM addresses to RO
value 9 into RAM locations 0---4, m1 sq 1 yto BL
then add them together and fH?
place the sum in location 10H: Ra BQU 4
suUM EQU 10H
MOVLW MYVAL |WREG = 9
MOVWE RO 7RAM loc 0 has 9
MOVWE R1 +RAM loc 1 has 9
MOVWF R2 7RAM loc 2 has 9
MOVWE R3 ;RAM loc 3 has 9
MOVWE R4 ;RAM loc 4 has 9
MOVLW O ;WREG = 0
ADDWF RO, W ;WREG = RO + WREG
ADDWF Ri, W ;WREG = Rl + WREG
ADDWF R2, W ;WREG = R2 + WREG
ADDWF R3, W ;WREG = R3 + WREG
ADDWF R4, W ;WREG = R4 + WREG
MOVWE SUMORG (origin)
The ORG directive is used to indicate the beginning of the address. It can
be used for both code and data. The number that comes after ORG must
be in hex.
END directive
Another important pseudocode is the END directive. This indicates to
the assembler the end of the source (asm) file. The END directive is the
last line of the PIC program, meaning that anything after the END
directive in the source code is ignored by the assembler.LIST directive
Unlike ORG and END, which are used by all assemblers, the 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 used as
follows:
The above tells the PIC assembler to assemble the program specifically
for the PIC 18F458 microcontroller. We use LIST to state the target chip.
#include directive
The #include directive tells the PIC assembler to use the libraries
associated with the specific chip for which we are compiling the
program._config directive
The _ config directive tells the assembler the configuration bits for the
targeted PIC chip. It is important to use the correct_ config directive,
because incorrect use may make the chip unusable. The configuration
bits are read during power-up of the PIC device and are stored at
location 300000H. Microchip has defined the _config directive symbols
to ease the configuration, These symbols are located in the .INC file for
the device that is being used.
. . .
radix directive
We can use the radix directive to indicate whether the numbering
system is hexadecimal or decimal. The default is hex if we do not use the
radix directive. If we use "radix dec", the default representation will
change to decimal and any unformatted number will be interpreted as
decimal rather than hex, as seen before.Rules for labels in Assembly language
By choosing label names that are meaningful, a programmer can make a
program much easier to read and maintain. There are several rules that
names must follow. First, 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, the digits 0 through 9, and the
special characters question mark (?), period(.), at(@), underline U, and
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. For example, "MOVLW" and "ADDLW" are reserved
because they are instruction mnemonics. In addition to the mnemonics
there are some other reserved words, Check your assembler for the list
of reserved words.