You are on page 1of 12

EEE111A/B Microprocessors

Lecture 4: Introduction to the PIC16F84 MCU Software


1 Objectives
To introduce the basic idea of a programin the context of software for the PIC mid-range
family.
To investigate the binary structure of a typical direct address instruction.
To inspect the machine-code structure of a typical literal instruction.
To examine the Move and Addition instructions.
To introduce the concept of assembly-level symbolic coding and its translation to ma-
chine code.
2 Revision
In Lecture 3 we:
Introduced the Microchip mid-range PIC family of Microcontrollers, specically the
PIC16F84, in terms of:
A Fetch unit characterized by:
A Program store holding the codes dening the instructions making up a pro-
gram. Each location comprises a 14-bit binary code specifying a single instruc-
tion.
A 13-bit Program counter which acts as an Instruction pointer addressing the
instruction currently being fetched into the pipeline. The Program counter
potentially can address up to 2
13
= 8K = 8, 192 instructions, although the
PIC16F84 has only 1,024 = 1K instruction capacity. The Program counter nor-
mally increments up frominstruction1 at location h000, but can skip or jump
if commanded by a relevant instruction.
A Pipeline storing two 14-bit instructions. The top register holds the n
th
in-
struction just fetched from the Program store. The bottom register outputs
the previously fetched n1
th
instruction and feeds the Instruction decoder.
The Instruction decoder which takes the n1
th
instruction and generates the
sequence of events in the Execute unit appropriate to the instruction.
An Execute unit characterized by:
An 8-bit Arithmetic Logic Unit (ALU) to do the logic and number crunching.
1
An 8-bit Working register to hold one of the operands fed to the ALU and also
serve as a possible destination for the outcome of the operation.
A Status register which holds (amongst other things) the Carry and Zero ags,
allowing the programmer to test conditions regarding a past operation carried
out by the ALU.
A Data store which holds up to (for the PIC16F84) 68 8-bit general-purpose
operands (variables) and also special-purpose registers; for example the Status
register. All registers in the Data store have addresses entirely dierent from
the Program store and are called Files; e.g. File3 is the Status register.
Introduced the concepts of a program as a logical sequence of elementary steps or
instructions.
In the last two lectures we dened a program as a coherent series of simple steps or
instructions. We know that these instructions are located in the Program store, as shown
in Fig. 3 in Lecture 3. The only thing we know from this diagram is that each instruction is
made up of 14 bits and that each instruction takes up one location in this store. We also
know that the mid-range PIC family has 33 dierent instructions although in this module
we will only be looking at rather less than a half of these.
In the example given in Lecture 2 Fig. 4, we used three instructions to copy the byte content
of File5 (NUM_1) plus 4 into File6 (NUM_2); i.e.:
1
movf NUM_1,w ; Copy (move) the contents of NUM_1 (File 5) to W
addlw 4 ; Add the literal constant 4 to it
movwf NUM_2 ; Copy it from the Working register into NUM_2 (File 6)
The rst instruction movf NUM_1,w simply loads a copy of the contents of File05 (called
NUM_1) into the Working register (connected to the ALU). The second (addlw 4) adds the lit-
eral (constant) four to it, with the outcome still in the Working register. Finally movwf NUM_2
stores the contents of this Working register back up into the Data store at location File06.
Notice by the way, how frequently this 8-bit Working register occurs in our program it
acts a little like a telephone exchange.
1
In C we would program this as NUM_2 = NUM_1 + 4;
2
Before looking at a few new instructions, let us briey examine the binary structure of
two of these instructions. The structure of the movf (MOV File) instruction is shown at the
top of Fig. 1. It is described as Direct, as the operand in the Data store has an explicit xed
address as part of the instruction we will look at Indirect addressing until next year. The
leftmost six binary bits b000100 shown in red, are known as the operation code or op-code
for short. Every instruction has an op-code, and it is this pattern that the decoding circuits
use to dene what type of instruction it is.
The middle bit labelled d (coloured yellow) denes the Destination of the outcome; for
example, addwf h26,w means ADD the Working register to Fileh26 and put the an-
swer in the Working register whereas addwf h26,f means Add the Working register to
Fileh26,f and put the answer back in the File. In the former case the destination is W and
the d bit is 0 and the the latter case the destination is the File and the d bit is 1. We will look
at these instructions later in Fig. 6 and 7.
The right-most seven bits (coloured blue) dene the File address. Thus in our example
Fileh26, the address is b0100110 or h26. The fact that the address eld is only seven
bits wide, means that only 2
7
= 128 Files can be directly addressed; that is addresses h00
through h7F. This constitutes one bank in the Data store, as shown in Fig. 5 in Lecture 3.
Instructions that deal with constants or literals, are coded in a slightly dierent manner,
as shown in the lower half of Fig. 1. Again the op-code (coloured red) comprise the leftmost
six bits. In our example addlw has the op-code b111110 or h3E. The right-most eight
bits (coloured yellow) are the constant itself; which can vary from b00000000 through
b11111111 or h00 through hFF (decimal 0 through 255). This makes sense, as the ALU
and Working register are only 8-bits wide. Thus the instruction addlw d500 (where d500
means decimal 500) doesnt make sense would you try and pump 80 litres into a 50 litre
petrol tank?
Let us now look more closely at our three instructions. Figure 2 shows what functionally
happens when the instruction movf h26,w is executed. Whatever 8-bit byte is in Fileh26,
labelled in the diagramas XXXXXXXX, will be copied to the Working register Thats all! Despite
the use of the mnemonic movf, the original contents of Fileh26 remain unaltered.
Figure 3 shows the reverse instruction which copies or stores the byte contents of the
Working register back into the Data store. The instruction movwf h27 shows this data
ow. Remember, that the datum byte YYYYYYYY is left behind in W and remains unchanged.
Figure 4 shows the execution of the addlw 4 instruction. The byte contents of the Work-
ing register and the 8-bit constant which is specied by the instruction (04 in this case) are
added using the ALU and the outcome overwrites the original contents of W. Again, remem-
ber that this constant can never be more than decimal 255 (hex FF). You can specify a negative
literal; for example, addlw -4 will eectively subtract four from the contents of W. The
processor actually treats the literal 4 as a 2s complement number (see your class notes):
00000100 (+4)
11111011 Invert
11111100 plus one gives 4 or hFC.
and thus is the same as addlw hFC but looks more like what the programmer wanted to
do assuming that he/she actually intended to subtract four.
3
If the addition generates a Carry-out then the C ag will be set to 1, otherwise cleared
to 0. The C ag is located as bit 0 of the Status register, File03. If the outcome is zero then
the Z ag (bit 2 of STATUS) is set to 1 else cleared to 0.
To complete our rst look at instructions, I want to look at two new instructions.
movlw
This instruction simply copies a byte-sized constant/literal into the Working register. Figure 5
shows the instruction movlw d10 which completes with the constant decimal ten (which
is the same as hexadecimal 0A or binary 00001010) in the Working register.
addwf
Our nal instruction to be discussed in this lecture is somewhat like the addlw instruction,
but this time an 8-bit variable located in the Data store is added to the byte contents of W
rather than an 8-bit constant. Unlike addlw, the destination of the outcome can be specied
to be either in the Working register or in the original File.
Figure 6 shows the data ow for the instruction addwf h26,w . This simply copies the
byte contents of Fileh26 to the ALU which is set up to ADD. The other operand is in the 8-
bit Working register. The 8-bit output, written as [F26]+W (the [ ] brackets mean contents
of), is then put back in the Working register overwriting the original value. In the usual way,
the C ag in the Status register follows the Carry-out of the addition and the Z ag is set if
the outcome is zero and cleared otherwise.
The programmer can specify that the outcome be put back into the original File, leaving
the contents of W unchanged. The addwf h26,f instructions data owis shown in Fig. 7.
Apart from the dierent location of the outcome, the instruction works in the same manner,
altering the C and Z ags in the identical manner to all the other Addition instructions.
Self Assessment Question
The operation code for addwf is b000111. Deduce the 14-bit machine codes for the instruc-
tion addwf h20,w and addwf h22,f .
Translating instructions by hand from symbolic form (called assembly language) is for the
birds. It is not terribly dicult, but it is tedious, time consuming and error prone if the
program is of any length. Furthermore, it is impossible to read and therefore debug and
subsequently alter. This is an easy task for a computer, as each mnemonic usually translates
to one machine instruction. For example:
addwf h30,w b00011100110000
4
The process is shown in Fig. 8, and the name of a program to do this is called a assembler.
Here the symbolic code is typed into a very simple wordprocessor, called in this context, an
editor. Once saved on disk, the assembler can do the code translation and generate a machine
code le often called a .hex le. This le can then be blasted into the micros Program
store and denes the executable program called a machine code program or sometimes
object code. Although we will not be doing this blasting in this module, we will be able to
translate simple programs in the laboratory and use the PC to simulate our target PIC16F84
device. That is to get the PC to pretend that it is a PIC16F84.
Self Assessment Questions
1. Write a program to increment the contents of File h20.
2. Write a program to decrement the contents of File h20.
3. How could you clear the contents of the Working register in the absence of an explicit
Clear W instruction?
4. Write a program to clear (set to all zeros) Fileh20 through Fileh24.
5. Write a program to subtract ten from the contents of Fileh30.
6. Write a program to multiply the contents of Fileh26 by two. What problem might you
have?
7. Write a program to add together the contents of Fileh20 and Fileh21 and put the
outcome in Fileh30. Again, what problem might arise here?
8. In words (you dont have all the instructions to do this yet) howcould you add ten to a 16-
bit number stored in Fileh26 and Fileh27; Most-Signicant Byte
Fileh

26

Least-Signicant Byte
Fileh

27

.
5
3 Key Concepts
Assembly-level source code Symbolic language based on the native instructions of the processor
Each instruction mnemonic translates to one machine instruction
Assembler
Flag
Instruction, addlw
Instruction, addwf
Instruction, movlw
Instruction, movf
Instruction, movwf
Machine (object) code
Operation code
Status register Register located in File3 that holds the
Carry and Zero ags
L
A
T
E
X2

eee111_4.tex Version 2.1.1 S.J. Katzen February 24, 2005


6
000100 d fffffff
movf <file>,d
movf h26,w
000100 0 0100110
op-code File address
D
e
s
t
i
n
a
t
i
o
n
W

o
r

F
i
l
e
For example to move (copy) a byte from File h26 to the Working register:
Direct addressing
111110 KKKKKKKK
Constant
For example to move (copy) a the constant byte 04 to the Working register:
Literal addressing
addlw kk
addlw 4
op-code
04
111110 00000100
addlw
000100 d fffffff
111110 KKKKKKKK
111110 00000100
movf from File h26
t
o

W
Figure 1: Binary machine code structure for a Direct address and a literal instruction.
7
0C
0D
0E
0F
10
11
12
13
14
15
16
17
18
19
1A
1B
1C
1D
1E
1F
20
21
22
23
24
25
26
27
28
Working register
movf h26,w
X X X X X X X X
X X X X X X X X
26
Data store
STATUS
Z
Figure 2: Showing the movf h26,w instruction.
8
0C
0D
0E
0F
10
11
12
13
14
15
16
17
18
19
1A
1B
1C
1D
1E
1F
20
21
22
23
24
25
26
27
28
Working register
movwf h27
Y Y Y Y Y Y Y Y
Y Y Y Y Y Y Y Y
26
Data store
27
Figure 3: Showing the movwf h27 instruction.
9
W
#4
ALU
W+4
(Add)
STATUS
C Z
Figure 4: Showing the addlw 4 instruction.
W
#d10
ALU
d10
(PASS)
Figure 5: Showing the movlw d10 instruction.
10
0C
0D
0E
0F
10
11
12
13
14
15
16
17
18
19
1A
1B
1C
1D
1E
1F
20
21
22
23
24
25
26
27
28
Working register
addwf h26,w
26
Data store
ALU
(ADD)
[F26] + W
Z C
STATUS
Figure 6: Showing the addwf h26,w instruction which copies the outcome to the Working
register.
11
0C
0D
0E
0F
10
11
12
13
14
15
16
17
18
19
1A
1B
1C
1D
1E
1F
20
21
22
23
24
25
26
27
28
Working register
addwf h26,f
26
Data store
ALU
(ADD)
[F26] + W
C Z
STATUS
Figure 7: Showing the addwf h26,f instruction which copies the outcome back to the File.
incf COUNT,f
movf COUNT,w
addlw 6
btfsc STATUS,DC
movwf COUNT
Translate
00101010100000
00100000100000
11111000000110
01100100000101
00000010100000
return 00000000001000
Source code Machine code
Assembler
Figure 8: Conversion from assembly-level source code to machine code.
12

You might also like