 |
Introduction
While reading through the lessons you should keep the simulator
open in another window, so that you can try out things you
learn them.
What's inside a microcontroller?
From the programmer's perspective, a microcontroller consists of
the following things:
- some pins that communicate with the outside world,
- some RAM where numbers may be stored temporarily
(i.e., while the microcontroller has power).
- some permanent memory that stores your program.
- a CPU that executes the instructions of your program one by one.
Of these we do not need to worry much about the last two. We
shall just assume that the microcontroller stores our program
somewhere, and somehow manages to run the program. What we shall be
interested in is the effect of the program, i.e., how the
contents of the RAM and voltages of the pins change as a result
of program execution.
Lab session 1
Visit the lab page and make sure you understand the different
parts.
Notice the little PORT/PIN table. There are 32 checkboxes there,
arranged in 4 rows consisting of 8 checkboxes each. These
represent 32 of the pins. The microcontroller has 40 pins in all,
of which the remaining 8 are used for technical things like
providing power etc that need not concern the programmer.
For the sake of programming convenience the 32 pins are divided
into 4 groups of 8 pins each, each group being called a
port. The ports are called P0, P1, P2 and P3. The pins of
each port are numbered from 0 to 7.
Next we come to the RAM, which comes in two varieties: general
purpose and SFR (special function registers).
You can think of the general purpose RAM as an array of 128
bytes. The position is counted starting from 0.
The first 8 bytes of RAM are also called
registers (nothing to do with the SFRs). These have names
R0 to R7.
The SFRs may be considered as a collection of byte variables
with special names. There are 26 of these.
Playing with ports
The microcontroller's sole interface with the external world
consists of the pins. So it is easy to guess that if we connect a
battery to one of the pins, something changes inside the
microcontroller. This is what we are about to explore in the lab
session below.
Lab session 2
You'll notice that initially all the pins are checked. This means
when a microcontroller starts running it automatically makes the
pin voltage equal to about 5V (which is the voltage denoting a
logic 1). We can of course make it 0 by externally connecting a
pin to ground. In the simulator we do this by unchecking the
checkbox for that pin.
Uncheck pin 5 of P0. Look at the SFRs. Does anything change?
You'll find that the SFR names P0 now holds the value
11011111. Play around with the other pins and convince
yourself that the pins of P0 are internally connected to
the SFR variable P0. Thus this variable is the bridge
between the pins and your program. By reading the value of this
variable, your programmer can learn about the pin voltages.
Now play with the other ports and notice that each has a
corresponding SFR variable.
Lab session 3
Now that we know how the external world can change the values of
variables inside the microcontroller, it is time to exlore the
other extreme: how your program can change the values.
Type the one line program
mov P0, #5
in the program box. Hit load, and then step. What this command
achieved is a simple assignment: P0 = 5.
You might be wondering what that # is there for! Well, it
simply means that the 5 after it is to be treated as a
number. To make sense out of this try out the following.
mov 8, #5
Try it out (hit load and then step). Do you understand the
effect? What has happened is the assignment "RAM[8] = 5".
Thus the 5 with # before it is interpreted as the
number 5, while the 8 (without any # before
it) is treated as a RAM address: RAM[8].
You should be able to guess the effect of
mov P0, 5
Mentally make a note of what the value of P0 should be
after this program is executed. Then try it out, and check!
Different modes
In the examples above mov is a command which
requires two arguments: where to move and what to move. As
we have already seen these arguments may be supplied in a number
of different ways. Let's first see how the second argument (what
to move) may be supplied:
- As a number with a
# before it. This is called
immediate mode.
- As a RAM address or SFR names. This is called direct
mode. We have not yet seen the use of an SFR name in the
second argument. Here is an example:
mov 4, P0
It may strike you as odd that the same term direct mode applies
to the use of either a RAM address or an
SFR name. This is because the SFR name is just a conveneient
mnemonic for the RAM addresses. For example, the SFR name P0 is
just a code for 128, which is the RAM address for that SFR. Thus,
when you type
mov 4, P0
the assembler converts it to
mov 4, 128
before translating to machine language.
-
There is a mode called indirect mode which will remind you
of arrays in C-like languages. The line
mov 6, @R0
means RAM[6] = RAM[R0]. Thus, here R0 acts like the
index of an array. You could also use R1 as an index, but
these are the only two possibilities.
For the destination argument we have all these modes except of
course the immediate mode.
Incidentally, there is actually another mode which may be called
the accumulator mode. This is when you are using the SFR
name A. This SFR is somewhat special in the sense that it
is used more frequently than anything else, and the
microcontroller has quicker ways to send data to and from it.
So everytime you type
something like
mov P0, A
you get a little bonus: the instruction runs faster than say
mov P0, B
We say that in the first example the second argument is in
accumulator mode, while in the second example it is in
direct mode. But during the intial stage of learning assembly
language you might as well forget this distinction. Just keep it
at the back of your mind that the microcontroller gives you a
little bonus now and then.
|