29-11-2012, 05:32 PM
PIC Assembly Language for the Complete Beginner
PIC Assembly Language.pdf (Size: 153.83 KB / Downloads: 30)
ABSTRACT
Microcontrollers are one-chip computers designed to control other equipment,
and almost all electronic equipment now uses them. The average
American home now contains about 100 computers, almost all of which
are microcontrollers hidden within appliances, clocks, thermostats, and
even automobile engines.
Although some microcontrollers can be programmed in C or BASIC,
you need assembly language to get the best resultswith the least expensive
micros. The reason is that assembly language lets you specify the exact
instructions that the CPU will follow; you can control exactly how much
time and memory each step of the program will take. On a tiny computer,
this can be important. What’s more, if you’re not already an experienced
programmer, you may well find that assembly language is simpler than
BASIC or C. In many ways it’s more like designing a circuit than writing
software.
The trouble with assembly language is that it’s different for each kind
of CPU. There’s one assembly language for Pentiums, another for PIC microcontrollers,
still another for Motorola 68000s, and so forth. There are
even slight differences from one model of PIC to another. And that leads
to a serious problem – each assembly-language manual seems to assume
that you already know the assembly language for some other processor!
So as you look from one manual to another in puzzlement, there’s no way
to get started.
MEET THE PIC
What’s inside a PIC?
Figure 1 shows the pinout of the PIC16F84, and Figure 2 shows the most
important parts inside. The PIC is a tiny but complete computer. It has a
CPU (central processing unit), program memory (PROM), working mem- ory (RAM), and two input-output ports.
The CPU is, of course, the “brain” of the computer. It reads and executes
instructions fromthe programmemory. As it does so, it can store and
retrieve data in working memory (RAM). Some CPUs make a distinction
between “registers” located within the CPU and “RAM” located outside
it; the PIC doesn’t, and its general-purpose working RAM is also known
as “file registers.” On the ’F84, there are 68 bytes of general-purpose RAM,
located at addresses hex 0C to hex 4F.
Besides the general-purpose memory, there is a special “working register”
or “W register” where the CPU holds the data it’s working on. There
are also several special-function registers each of which controls the oper- ation of the PIC in some way.
The program memory of the ’F84 consists of flash EPROM; it can be
recorded and erased electrically, and it retains its contents when powered
off. Many other PICs require ultraviolet light for erasure and are not
erasable if you buy the cheaper version without the quartz window. The
’F84, however, is always erasable and reprogrammable.
Power and clock requirements
The PIC16F84 requires a 5-volt supply; actually, any voltage from 4.0 to
6.0 volts will do fine, so you can run it from three 1.5-volt cells. Figure
3 shows several power-supply options. The PIC consumes only 1 mA –
even less, at low clock speeds – but the power supply must also provide
the current flowing through LEDs or other high-current devices that the
PIC may be driving. Thus, the last circuit, with the Zener diode, is only
for PICs that aren’t driving LEDs.
All four power supply circuits rely on a 0.1-μF capacitor from pin 14
(V+) to ground, mounted close to the PIC, to protect the PIC and adjacent
components from electrical noise. This capacitor should be present
no matter how clean you think your DC supply is.
The MCLR pin is normally connected to V+ through a 10k resistor.
Grounding itmomentarilywill clear RAMand reset the PIC. If your power
supply voltage comes up slowly, the PIC may start up in a confused state;
in that case you should add a normally-open reset button from MCLR to
ground.