Sunteți pe pagina 1din 13

EEE111A/B Microprocessors

Lecture 3: Introduction to the PIC16F84 Microcontroller


In Lecture 2 we learnt:
The dierence between a microcontroller and a microprocessor.
The structure of both a von Neumann and more relevantly a Harvard computer, in
particular its memory and common bus structures.
The CPU of a computer is characterized by:
An Arithmetic Logic Unit (ALU) to do the number crunching.
An 8-bit Working register to hold one byte operand for the ALU.
A Program Counter (PC) to act as an instruction pointer to the current program
code byte about to be fetched from the Program memory.
In this lecture we are going to introduce the microcontroller which is the object of this
part of the module. The Microchip PIC family of microcontrollers was introduced in 1989
by Arizona Microchip. Microchip (as they are now known) bought General Instruments mi-
croelectronics division as a start-up company in 1988. They re-engineered a programmable
interface device that General Instruments were using as a general-purpose recongurable
input/output port for their microprocessor as a stand-alone microcomputer. These were
called the PIC (Programmable Interface Controller) family. The second generation of this
family was introduced in 1994, and the PIC16F84A is the latest incarnation of one of the rst
introductions. From our point of view I am using the PIC16F84A as it represents the simplest
family member. However, the core processor is similar to all these 14-bit family members
and software is identical. The newer PIC18FXXX 16-bit family members introduced in 1999
are upwards compatible.
Figure 1 shows a birds eye view of the internal architecture of the PIC16F84 microcon-
troller. The chief characteristics of this device are:
Fetch section (red) see also Figs. 2 and 3.
The Program store (top left) holds 1,024 (1K) 14-bit instructions.
A 13-bit Program Counter to address (point to) each instruction in turn.
A 2-stage pipeline conducts instructions to the Instruction decoder.
Execution section (blue) see also Figs. 4 and 5.
An 8-bit Working register holds a single-byte operand for the Arithmetic Logic Unit.
A Status register holding status information from the ALU, such as any carry-out,
and switches.
1
AData store holding 68 general-purpose Files and other special-purpose Files, such
as the Status register, to be discussed.
Input/Output ports and Peripheral devices (green).
An 8-bit and 5-bit general purpose input/output port.
An 8-bit Timer/counter.
An EEPROM 64-byte non-volatile memory module.
Although this looks scary, we will not be discussing the Timer or EEPROM data memory in
this module, and the rest of the hardware will be covered over the next four weeks or so.
Figure 1 is rather complicated at rst sight, so we will zoom in and examine only the key
element of the Fetch and of the Execute part of the device. First of all Fig. 2 shows a close up
of the Fetch unit of the processor. Before listing the component parts notice that the various
functional blocks are interconnected with multiple conductors. For example, the output of
the Programstore (top left), is connected to the pipeline belowvia 14 conductors. To simplify
such diagrams it is usual to show this as a thick line with a direction arrow and a cross slash
giving the number of lines, or colloquially its width.
At the top we have the Program store. This holds 1,024 (2
10
= 1K) words, each 14-
bit word carrying one instruction. We will look at the binary structure of a typical
instruction in the next lecture.
A 13-bit Program counter is used to address (point to) the location holding the instruc-
tion being fetched (copied) into the top of the pipeline.
Two 14-bit registers comprise the Pipeline. The top of the pipeline holds the instruction
which has just been fetched from Program memory. The bottom of the pipeline feeding
the decoder, holds the instruction that was previously fetched, and is the instruction
which is in the process of being executed. The reason for having a pipeline is to allow
an instruction to be fetched whilst at the same time the previous instruction is being
executed.
The Instruction decoder fed by the instruction at the bottom of the pipeline, is a rather
sophisticated array of gates and ip ops which interprets what instruction is to be
executed; e.g. Move, Add, Subtract, AND etc, and sends data and sequences timing
actions in the Execution section.
We observe a stack of eight registers fed by and feeding back from/to the Program
counter. We cannot tell from the diagram what its purpose is, although we can guess
that it may be used as a temporary store for past states of the Program counter. We
will discuss the Stack in Lecture 7.
Understanding the working of the Program store is helpful in grasping how the MCU is
programmed. A simplied description of the Program store for the PIC16F84 is shown in
Fig. 3. Here the memory is shown as an array of 1024 cells, each of which holds a 14-bit
2
word dening a single instruction. The key point to recognize with a memory of this kind is
that besides the contents stored, each cell must have an address. The address is of course
another binary number, but is used to pin-point a single cell from which the datum is to be
read. For a 1K memory a 10-bit address is needed, going from b00 0000 0000 through
b11 1111 1111 or as shown in the diagram (top right of each cell) h000 through h3FF in
hexadecimal.
In the case of a Programstore, the address is provided by the Programcounter. This regis-
ter holds the address of the instruction being fetched into the pipeline. Normally this simply
increments (which is why it is called a counter) after each instruction has been fetched, al-
though as we will see in later lectures, there are some instructions that will force the Program
counter to either skip over the next instruction or even go directly to anywhere in the store.
Devices in this PIC mid-range family can have up to 8K (8,192 = 2
13
) Program stores, and a
13-bit address is needed (with cell addresses h0000 through h1FFF) which is why Fig. 1
shows the Program counter as a 13-bit device.
In the specic case of the mid-range PIC family, the Program counter is reset to location
h000 in which it will nd the rst instruction of the program. As the program is fetched
and executed the Program counter increments and in the diagram moves down. As the n
th
instruction at the bottomof the pipeline is decoded and executed, the next n+1
th
instruction
pointed-to by the Program counter is copied into the top 14-bit register in the pipeline.
In Fig. 3 the Programstore is described as Flash ROM. ROMmeans Read-Only Memory, and
this means that once programmed the data remains even when power is removed. Obviously
in embedded applications the program needs to be permanently available. After all, you
wouldnt want to wait for all your on-board processors in your car to boot up and load in
programs from, say, a CD or hard disc, before you were able to start the car! So in general
all embedded processors hold their program in permanent non-volatile memory of some
sort. In most PIC families this is a variety of Erasable Programmable Read-Only Memory
(EPROM), which means that once programmed it can be erased and reused say to update
memory in the eld, such as the BIOS in your PC. In general there are two ways of doing
this. Some devices have a quartz window in the package and if you shine high frequency
ultra-violet light on the silicon for around 20 minutes you will erase the device. Alternatively,
Electrically-Erasable PROMS (EEPROM) can be erased using high voltage pulses. Flash EEPROM
technology (giving the F in the part number) is simply a high-speed version of standard
EEPROM technology. You will be covering memory types in your Digital lectures, for this part
of the module you dont have to worry about this aspect of the device.
Now, turning to the execute unit in Fig. 4 we see that the processing circuits, sometimes
called a mill
1
are located at bottom right. This comprises the following:
The Arithmetic Logic Unit is the arithmetic logic circuits which can add, subtract, AND,
OR, NOT etc two 8-bit numbers. One of these bytes normally comes from either the
1
From Charles Babbages mechanical design of a general-purpose computer in the early/mid-1800s, where
he described the arithmetic circuits/registers as a mill, to grind together numbers outputting something
useful.
3
instruction (these literal instructions carry an 8-bit constant as part of their coding;
e.g. addlw 6) or more usually from a File in the Data store (e.g. addwf h20,f adds
the contents of Fileh20) to the Working register and put the answer back up into the
original File.
The Working register normally holds one of the operands used by the ALU and often
can be specied as the destination of the outcome.
The Status register holds three bits, called ags, that tell the programmer something
about the status of the last operation by the ALU, for example if there was a Carry-out
or if the outcome was Zero more details in Lecture 5. It also holds a bit, called a
switch, that is used to switch between banks in the Data store, as discussed below.
For this module we are going to ignore the File Select register (see Year 2) which then only
leaves the Data store at the top of the diagram to discuss. Unlike the Program store this
memory is readable from and writable to. Like the other registers in the Execute module, it
is 8-bits wide; that is each cell can hold only a single byte. Each cell has an address, which
should not be confused with addresses in the Program store, for as we saw in Lecture 2, the
Program store and Data store in the Harvard structure are completely separate with distinct
Data and Address buses. To distinguish between addresses in the Data store with addresses
of instructions in the Instruction store, locations in the former are called Files. For example,
we refer to Fileh20 being the contents of cell h20 in the Data store.
In the mid-range PIC devices Data stores can have up to 512 bytes storage capacity, with
a 9-bit address. The PIC16F84 has a much smaller Data store in which only 68 byte-sized
general-purpose variables are implmented.
Figure 5 is a simplied model of the PIC16F84s Data store. Like the Program store it
looks rather complicated at a rst glance even after a second glance! You can think of
the Data store as a ling cabinet, with each folder holding one data byte. Actually there are
two dierent types of folder in our cabinet. Some of these Files are named and have special
signicance. These are known as Special-Purpose Registers (SPRs). The other unnamed
Files can be given names by the programmer and used for general-purpose storage. These
are known as General-Purpose Registers (GPRs). SPRs are used to control and monitor the
state of the microcontroller. For example, File3 is the Status register holding, amongst other
things, the Carry ag (e.g. set when there is a carry-out after addition) and File6 is Port B
which is connected to pins RB70, shown in Fig.1. The PIC16F84 can hold 68 byte-sized
general-purpose variables, addressed from Fileh0C through Fileh4F. Other PICs generally
have more storage, although even the largest PICs have a maximum storage of 368 general-
purpose bytes. Even 368 bytes of RAM storage is not very much, so programs have to make
very ecient use of this limited capacity! Think of your PC with hundreds of megabytes (a
megabyte is 2
20
= 1, 048, 576) of RAM!!!
Actually the PIC16F84 has two drawers in its ling cabinet, each drawer has a potential
to hold 128 (2
7
) Files. The Programmer can move from one to the other drawers at will by
setting or clearing bit 5 in the Status register. Each drawer is called a bank. This bit can be
thought as the key to open the particular ling drawer. From the diagram you can see that
this bit is called RP0. Actually the PIC16F84 makes very little use of these banks. All the
4
GPRs are imaged in both banks; for example, Fileh20 and FilehA0 are the same! Apart
from the SPRs used for the EEPROM module, which we will not deal with in this module,
only the registers for setting up the port pins to either input or output known as the TRIS
registers and the Option register, which is mainly used for conguring the Timer0 module,
are unique to Bank1. However, this is unusual, and most PICs have four banks (with two RP
bits in the Status register to select them) and many unique GPRs and SPRs in the various
banks. Generally, Microchip put the most used SPRs in Bank0 and this is where the PIC
starts on Reset. We will not need to change banks in this module until we reach Lecture 8
and we will revise this bank structure there; in the meantime you can set this feature of the
Data store aside!
To nish our lecture, let us begin to think about software and programs. From now on
we will be mainly concentrating on microcontroller software, so as a lead in let us look at
what exactly a program is. Most people think that computers and their like are some kind of
complicated device that is impossible to learn and innitely intelligent, able to think better
than a person. The truth is less glamorous. A computer can only do what the programmer
has told it to do, in the form of a program.
2
A program is just a sequence of very simple
commands that lead the computer to solve some problem. Once the program is written and
debugged, the computer can execute the instructions very fast, and always do it the same,
every time, without mistake.
Herein lies the power of a computer. Even though the program consists of very simple
instructions, the overall result can be very impressive, due mostly to the speed of the proces-
sor. In other words a million dumb things a second gives pseudo intelligence. When taken
as a whole, the sequence of instructions can appear to give the hardware a complexity that
may appear very complicated. The trick is not to think of it as a whole, but as a series of
very simple steps, or commands. Some programs comprise several million instructions, and
so due to limitation of the human mind, need to be broken down into quite small routines in
any case.
Most microprocessors and microcontrollers (we will use the term micro to cover both)
have much the same commands, or instructions, they can perform. They vary mostly in the
names used to describe each command. In a typical micro, there are commands to move
(or more accurately copy) data around, do simple math (add, subtract, clear) and logic
operations. The mid-range PIC devices have 33 instructions and we will spend the next few
lectures looking at around a dozen or so.
To nish our introduction we want to clarify what exactly a program is. We have already
said that a program is a sequence or series of very simple commands or instructions. A
real-world example program might be the problem of crossing a busy street with a Pelican
pedestrian trac light. A task list from this program will go something like this:
1. Walk up to the corner of the street and stop.
2. Look at the trac lights.
3. Is it a green man for your direction of travel? Make a decision.
2
This American spelling is universally used to denote a computer program rather than a programme for
the opera or whatever.
5
4. IF the light is red THEN goto step2 ELSE continue.
5. Look to the right.
6. Are there cars still passing by?
7. IF yes THEN goto step5 ELSE continue.
8. Look to the left.
9. Are there cars still passing by (there shouldnt be any by now, but, you never know!)?
10. IF yes THEN goto step8 ELSE continue.
11. Proceed across the street carefully!
Alternatively, you may wish to use a visual representation of the task list called a owchart.
6
Now this may seem childish at rst glance, but this is exactly what you (should) do every
time you traverse a busy street with a pedestrian light controlled crossing. This is also exactly
how you would tell a micro-controlled robot to cross the street. This is what is meant by a
sequence or series of simple steps. Taken as a whole, the steps lead you across a busy road,
which if a robot did it, would seemvery intelligent. It is not intelligence, people are intelligent.
The programmer that programmed these steps into a micro, would impart that intelligence
to the micro.
Of course, the micro controlled robot would not know what to do when it got to the other
side of the road, since we didnt tell it! A person on the other hand, could decide what to
do next, at a moments notice, without any apparent programming. In the case of a person,
though, there has been some programming, its called past experiences!
Notice that since the steps are numbered, 1 through 11, that is the order they are executed.
The Program counter/Instruction pointer, in this case is you, reading each line, starting with
step1 (reset state) and ending with step11, doing what each one commands. In a micro, the
Program counter automatically advances to the next step, after doing what the current step
says, unless a Skip or Goto is encountered. A Skip type of instruction directs the Program
counter to hop over the next instruction, usually based on some condition or outcome. A
Goto instruction directs the Program counter to jump to any step in the program. Without
these type of instructions, a program would be unable to make decisions or implement loops
where an action is repeated many times over.
The most dicult problem to be solved in programming a micro is to dene the problem
you are trying to solve. This sounds like some kind of play on words, but be assured it is not.
This is the Logical Thought Process that humans are (quite) good at and machines are not.
The mark of a good programmer is one who has this ability. It is a developed skill, coupled
with some talent, and a good understanding of the problem youre trying to solve. In most
cases you were born with the talent and the ability to reason; all you need to do it to develop
the skill of problem solving.
Now you try and write a task list or ow chart which you would use to program a robot
to ll a glass with water from a tap:
7
Key Concepts
Address Location of a datum in memory which may be in the
Program memory or in Data memory, in which case it is called a File.
Arithmetic Logic Unit (ALU)
Data store
Execute module
Fetch module
File
Instruction
Pipeline
Program
Program Counter (PC)
Program store
Status register
Working register Working register holding one datum in the CPU.
L
A
T
E
X2

eee111_3.tex Version 2.1.0 S.J. Katzen February 2, 2004


8
Flash ROM
1K x 14
Instruction reg 1
Instruction reg 2
Pipeline
Program Counter
Program
data bus
14
Program
address bus
13
8-level stack
Register file
68 x 8
RAM
Address mux
File select reg.
F 04h
7 Direct address
8
Indirect
address
F 02h
Data mux
8 Literal data
ALU
Working reg.
Z DC C
Status register
F 03h
Instruction
decoder
& control
op-code 7
8
8
File
address bus
Program store
PLATH
F 0Ah
Data store
RP0
Q2
Oscillator
and timing
generation
Power-up
timer
Oscillator
start-up
Power-on
Reset
Watchdog
timer
EEPROM data
F 08h
EEPROM
data memory
64 x 8
F 09h
EEPROM address
EEPROM data memory
Q1 Q3 Q4
P
r
e
s
c
a
l
e
:
O
P
T
I
O
N
8
F

8
1
h
T
i
m
e
r


c
o
u
n
t
e
r
F

0
1
h
Timer
I/O port A
T
R
I
S
A
F

8
5
h
P
o
r
t


A
F

0
5
h
I
/
O


p
o
r
t


B
TRISB
F 86h
Port B
F 06h
RA4/TOCKI
RA3
RA2
RA1
RA0
R
B
7
R
B
5
R
B
3
R
B
4
R
B
6
R
B
1
R
B
0
/
I
N
T
R
B
2
Int control
F 0Bh
OSC2
OSC1
MCLR
Vdd
Vss
14
5
4
16
15
6789
1
0
1
1
1
2
1
3
17
18
1
2
3
SLEEP
PIC16F84
File data bus
Figure 1: The architecture of the PIC16F84 microcontroller.
9
Flash ROM
1K x 14
Instruction reg 1
Instruction reg 2
Pipeline
Program Counter
Program
data bus
14
Program
address bus
13
8-level stack
7 Direct address
F 02h
8 Literal data
Instruction
decoder
& control
op-code 7
Program store
PLATH
F 0Ah
P
Figure 2: A close-up look at the PIC16F84s fetch unit.
10
g g
Figure 8 : The PIC16F84 Program Store.
Instruction 10
009
Instruction 6
005
Instruction 3
002
Instruction 1
000
Instruction 2
001
Instruction 4
003
Instruction 5
004
Instruction 7
006
Instruction 8
007
Instruction 9
008
Instruction 11
00A
Instruction 12
00B
Instruction 1024
3FF
Instruction 1023
3FE
Instruction 1022
3FD
Instruction 1021
3FC
Instruction 1020
3FB
Instruction 1019
3FA
Instruction 1018
3F9
Instruction 1017
3F8
Program Counter
Instruction n
Instruction n+1
Pipeline
Fetch
Execute
0 13
13 0
Program store
Points to the
instruction
being fetched
down from the
Program store.
Increments
after each
fetch
Reset
Figure 3: A simplied look at the PIC16F84s Program store.
11
Register file
68 x 8
RAM
Address mux
File select reg.
F 04h
8
Indirect
address
Data mux
ALU
Working reg.
Z DC C
Status register
F 03h
8
8
File
address bus
Data store
RP0
8
File data bus
Figure 4: A close-up look at the PIC16F84s execute unit.
12
00
TMR0 (TiMeR 0)
PCL (Program Counter Low byte)
Status register
FSR (File Select Register pointer)
PORTA
PORTB
EEDATA (EEprom DATA)
EEADR (EEprom ADdRess)
PCLATH (Program Counter LATch High)
INTCON (INTerrupt CONtrol register)
INDF (INDirect pointer File)
01
02
03
04
05
06
07
08
09
0A
0B
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
29
2A
2B
2C
2D
2E
2F
30
31
32
33
34
35
36
37
38
39
3A
3B
3C
3D
3E
3F
40
41
42
43
44
45
46
47
48
49
4A
4B
4C
4D
4E
4F
8B
8A
89
88
87
86
85
84
83
82
81
80 INDF (INDirect pointer File)
OPTION_REGister
PCL (Program Counter Low byte)
Status register
FSR (File Select Register pointer)
TRISA
TRISB
EECON1 (EEprom CONtrol register 1)
EECON2 (EEprom CONtrol register 2)
PCLATH (Program Counter LATch High)
INTCON (INTerrupt CONtrol register)
Bank 0 Bank 1
TO PD Z DC C
Status register
RP0
S
e
le
c
t B
a
n
k
0
if R
P
0
=
0
Se
le
ct B
a
n
k
1
if R
P
0
=
1
Note: Bit 5 of the Status register @ File 03
can be set by the programmer by executing
the Bit Set File instruction:
bsf 3,5
or cleared by using Bit Clear File:
bcf 3,5
A2
CD
CF
CE
C9
CA
CC
CB
C8
C6
C7
C2
C4
C5
C3
C0
C1
BF
B0
BB
BC
BD
BE
BA
B9
B8
B4
B6
B7
B5
B2
B3
B1
A9
AD
AF
AE
AA
AC
AB
A8
A6
A7
A4
A5
A3
94
9B
A1
9F
A0
9C
9D
9E
9A
99
98
97
96
95
8D
92
93
91
8F
90
8E
8C
Imaged GPRs
Figure 5: A simplied look at the PIC16F84s Data store.
13

S-ar putea să vă placă și