 |
8051 Microcontroller
INTRODUCTION
The 8151 is an 8-bit microcontroller originally designed in the
1980's by Intel. Its standard form includes several standard on-chip
peripherals, including timers, counters, and UART, plus 4kbytes of
on-chip program memory and 128 bytes of data memory, making
single-chip implementations possible.
The 8051 have modified Harvard architecture with separate address
spaces for program memory and data memory. The program memory can be
up to 64K.Up to 4 Kbytes of program instructions can be stored in
the internal memory of the 8051. The 8051 can address up to 64K of
external data memory, and is accessed only by indirect addressing.
The 8051 memory architecture includes 128 bytes of data memory that
are accessible directly by its instructions. segment of this
128-byte memory block is bit addressable by a subset of the 8051
instructions, namely the bit-instructions. The majority of the
8051's instructions are executed within 12 clock cycles.
The 8051 instruction set is optimized for the one-bit operations so
often desired in real
Time control applications. The 8051 features include so-called
"boolean processor" feature. The Boolean processor
provides direct support for bit manipulation. This refers to the way
instructions can single out bits just about anywhere (RAM,
accumulators, I/O registers, etc.), perform complex bit tests and
comparisons, and then execute relative jumps based on the results.
This leads to more efficient programs that need to deal with binary
input and output conditions inherent in digital-control problems.
INTERNAL ARCHITECTURE OF 8051
The 8 architecture consists of:
- Eight-bit CPU with registers. A and B and boolean processor
- 5 interrupts: 2 external and 3 internal with 2 priority levels
- 2 16-bit timer/counters: T 0 and T 1
- Programmable full-duplex serial port (baud rate provided by
one ofthe timers)
- 32 I/O lines (four 8-bit ports)
- Internal RAM of 128 bytes
- Internal ROM/EPROM (8751) of 0k (8031) to 4k (8051)
- 16-bit PC and data pointer (DPTR)
- 8-bit PSW
- 8-bit SP
- Oscillator and clock circuits
The 8051 operates based on an external crystal oscillator. This
is an electrical device which, when energy is applied, emits pulses
at a fixed frequency. Most often a quartz crystal oscillator is
connected to inputs XTAL1 (pin 19) and XTAL2 (pin 18). XTAL1 is
input to the inverting oscillator amplifier and input to the
internal clock generating circuits and XTAL2 is output from the
inverting oscillator amplifier.
The quartz crystal oscillator connected to XTAL1 and XTAL2 also
needs two capacitors of 30-pf values. One side of each capacitor is
connected to the grounds
When the 8051 is connected to a crystal oscillator and is powered
up, we can observe the frequency on the XTAL2 pin using the
oscilloscope.
When using an 8051, the most common crystal frequencies are 12
megahertz and 11.059 megahertz, with 11.059 being much more common.
The 8051 uses the crystal to synchronize it's operation.
Effectively, the 8051 operation is measured in what are called
"machine cycles." An Instruction cycle is the minimum
amount of time in which a single 8051 instruction can be executed,
although many instructions take multiple m/c cycles. The m/c cycle
is the smallest amount of time to complete a well defined job. e.g.
read, write.
One 8051 machine cycle consists of twelve oscillator periods. The
time (T) required for any 8051 instruction can be computed by
dividing the clock frequency by 12, inverting that result and
multiplying it by the number of machine cycles required by the
instruction in question.
T = No of m/c cycles x 12
Crystal frequency
Therefore, if a system which is using an 11.059MHz clock, the
number of instruction per second can be computed by dividing this
value by 12.
11,059,000/12 = 921,5B3
This means that the 8051 can execute 921,583 single-cycle
instructions per second. Inverting this will provide the amount of
time taken by each instruction cycle (1.085 microseconds).
The Accumulator is used as a general register to accumulate the
results of a large number of instructions. It can hold an 8-bit
(I-byte) value and is the most versatile register the 8051 has due
to the shear number of instructions that make use of the
accumulator. More than half of the 8051's instructions manipulate
or use the accumulator in some way. For example, if we add the
number 10 and 20, the resulting 30 will be stored in the
Accumulator. Once the value stored in the Accumulator one may
continue processing the value or store it in another register or in
memory.
THE "B" REGISTER
The "B" register is very similar to the Accumulator in
the sense that it may hold an 8-bit (1byte) value.
The "B" register is only used by two 8051 instructions:
MUL AB and DIV AB. Thus, if one want to quickly and easily multiply
or divide A by another number, he may store the other number in
"B" and make use of these two instructions.
THE "R" REGISTERS
The "R" registers are a set of eight registers that are
named R0, R1, R2, R3, R4, R5, R6 and R7.These registers are used as
auxiliary registers in many operations.
For example, if we add the number 10 and 20, the original number 10
may be stored in the Accumulator whereas the value 20 may be stored
in, say, register R4. To process the addition the command required
will be
ADD A, R4
After executing this instruction the Accumulator will contain the
value 30.
The Accumulator alone would not be very useful if it were not for
these "R" registers. The "R" registers are also
used to temporarily store values.
For example, let's say programmer want to add the values in R1
and R2 together and then subtract the values of R3 and R4. One way
to do this would be:
MOV A, R3; Move the value of R3 into the accumulator
ADD A, R4; Add the value of R4
MOV R5, A; Store the resulting value temporarily in R5
MOV A, R1; Move the value of R1 into the accumulator
ADD A, R2; Add the value of R2
SUBB A, R5; Subtract the .value of R5 (which now contains R3 + R4)
In this example, R5 is used to temporarily hold the sum of R3 and
R4. Of course, this isn't the most efficient way to calculate
(Rl+R2)-(R3 +R4) but it does illustrate the use of the "R"
registers as a way to store values temporarily.
SFRs
The 8051 is a flexible microcontroller with a relatively large
number of modes of operations. Program may inspect and/or change the
operating mode of the 8051 by manipulating the values of the 8051's
Special Function Registers (SFRs).
SFRs are accessed as if they were normal Internal RAM. The only
difference is that Internal RAM is from address 00h through 7Fh
whereas SFR registers exist in the address range of 80h through FFh.
Each SFR has an address (80h through FFh) and a name.
Although the address range of 80h through FFh offers 128 possible
addresses, there are only 21 SFRs in a standard 8051. All other
addresses in the SFR range (80h through FFh) are considered invalid.
Writing to or reading from these registers may produce undefined
values or behavior.
Note: One should not read or write to SFR addresses that
have not been assigned to an SFR. Doing so may provoke undefined
behavior and may cause program to be incompatible with other
8051derivatives that use the given SFR for some other purpose.
SFR TYPES
I/O PORT SFRs
P0, P1, P2, P3 are SFRs related to the I/O ports. The 8051 has
four I/O ports of 8 bits, for a total of 32 I/O lines. Whether a
given I/O line is high or low and the values read from the line are
controlled by the others SFRs. .
CONTROL SFRs
The PCON, TCON, TMOD, SCON, IE, IP, PSW are SFRs which in some
way control the operation or the configuration of some aspect of the
8051. For example, TCON controls the timers, SCON controls the
serial port.
OTHER SFRs
The remaining SFRs SP, DPL, DPH, TLO, TL1, THO, TH1, SBUF, ACC
and B are "other SFRs." These SFRs can be thought of as
auxiliary SFRs in the sense that they don't directly configure the
8051 but obviously the 8051 cannot operate without them. For
example, once the serial port has been configured using SCON, the
program may read or write to the serial port using the SBUF
register.
Note: The PO, TCON, PI, SC0N, P2, IE, P3, IP, PSW, ACC,
Bare SFRs that may be accessed via bit operations (i.e., using the
SETB and CLR instructions). The other SFRs cannot be accessed using
bit operations. As one can see, all SFRs whose addresses are
divisible by 8 can be accessed with bit operations.
SFR DESCRPTIONS
A general idea of what each SFR does is describing in short as
follows.
PO (PORT 0, ADDRESS SOH, BIT.ADDRESSABLE)
This is input/output port 0.. Each bit of this SFR corresponds
to one of the pins on the microcontroller. For example, bit 0 of
port 0 is pin P0.0, bit 7 is pin P0.7. Writing a value of 1 to a bit
of this SFR will send a high level on the corresponding I/O pin
whereas a value of 0 will bring it to a low level.
Note: While the 8051 has four I/O port (P0, P1, P2, and P3)
but if hardware uses external RAM or external code memory (i.e.,
program is stored in an external ROM or EPROM chip or if using
external RAM chips) one may not use P0 or P2. This is because the
8051 uses ports P0 and P2 to address the external memory. Thus if
one is using external RAM or code memory he may only use ports Pl
and P3 for own use.-
SP (STACK POINTER, ADDRESS 81H)
This is the stack pointer of the microcontroller. This SFR
indicates where the next value to be taken from the stack will be
read from in Internal RAM. When data is to be placed on the stack,
the SP increments before storing data on the stack so that the stack
grows up as data is stored. If one pushes a value onto the stack,
the value will be written to the address of SP + 1. That is to say,
if SP holds the value 07h, a PUSH instruction will push the value
onto the stack at address 08h. This SFR is modified by all
instructions which modify the stack, such as PUSH, POP, LCALL, RET,
RETI, and whenever interrupts are provoked by the microcontroller.
Note: The SP, SFR, on startup, is initialized to 07h. This
means the stack will start at 08h and start expanding upward in
internal RAM. Since alternate register banks 1, 2, and 3 as well as
the user bit variables occupy internal RAM from addresses 08h
through 2Fh, it is necessary to initialize SP in program to some
other value if someone will be using the alternate register banks
and/or bit memory.
DPL\DPH (DATA POINTER LOWIHIGH, ADDRESSES 82H/83H)
The SFRs DPL and DPH work together to represent a 16-bit value
called the Data Pointer. The data pointer is used in operations
regarding external RAM and some instructions involving code memory.
Since it is an unsigned two-byte integer value, it can represent
values from 0000h to FFFFh (0 through 65,535 decimal).
Note: DPTR is really DPH and DPL taken together as a 16-bit
value. In reality, you almost always have to deal with DPTR one byte
at a time. For example, to push
DPTR onto the stack. you must first push DPL and then DPH. You can't
simply plush DPTR onto the stack. Additionally, there is an
instruction to "increment DPTR." When you execute this
instruction, the two bytes are operated upon as a 16-bit value.
However, there is no instruction that decrements DPTR. If you wish
to decrement the value of DPTR, you must write your own code to do
so.
PCON (POWER CONTROL, ADDRESSES 87H)
The Power Control SFR is used to control the 8051's power
control modes. Certain operation modes of the 8051 allow the 8051 to
go into a type of "sleep" mode which requires much less power.
These modes of operation are controlled through PCON. Additionally,
one of the bits in PCON is used to double the effective baud rate of
the 8051's serial port.
TCON (TIMER CONTROL, ADDRESSES 88H, BIT-ADDRESSABLE)
The timer Control SFR is used to configure and modify the way in
which the 8051's two timers operate. This SFR controls whether each
of the two timers is running or stopped and contains a flag to
indicate that each timer has overflowed. Additionally, some
non-timer related bits are located in the TCON SFR. These bits are
used to configure the way in which the external interrupts are
activated .and also contain the external interrupt flags' which are
set when an external interrupt has occurred.
TMOD (TIMER MODE, ADDRESSES 89H)
The timer mode SFR is used to configure the mode of operation of
each of the two timers. Using this SFR program may configure each
timer to be a 16-bit timer, an 8-bit auto reload timer, a 13-bit
timer, or two separate timers. Additionally, one may configure the
timers to only count when an external pin is activated or to count
"events" that are indicated on an external pin.
TL0/TH0 (TIMER 0 LOW/HIGH, ADDRESSES 8AH/8CH)
These two SFRs, taken together, represent timer 0. Their exact
behavior depends on how the timer is configured in the TMOD SFR;
however, these timers always count up. What is configurable is how
and when they increment in value.
TL1/TH1 (TIMER 1 LOW/HIGH, ADDRESSES 8BH/8DH)
These two SFRs, taken together, represent timer 1. Their exact
behavior depends on how the timer is configured in the TMOD SFR;
however, these timers always count up. What is configurable is how
and when they increment in value.
P1 (PORT 1, ADDRESS 90H, BIT-ADDRESSABLE)
This is input/output port 1. Each bit of this SFR corresponds to
one of the pins on the microcontroller. For example, bit 0 of port 1
is pin P1.0, bit 7 is pin P1.7. Writing a value of 1 to a bit of
this SFR will send a high level on the corresponding I/O pin whereas
a value of 0 will bring it to. a low level.
SCON (SERIAL CONTROL, ADDRESSES 08H, BIT-ADDRESSABLE)
The Serial Control SFR is used to configure the behavior of the
8051's on-board serial port. This SFR controls the baud rate of the
serial port, whether the serial port is activated to receive data,
and also contains flags that are set when a byte is successfully
sent or received.
Note: To use the 8051's on-board serial port, it is
necessary to initialize the following SERs: SCON, TCON, and TMOD.
This is because SCON controls the serial port. However, in most
cases the program will wish to use one of the timers to establish
the serial port's baud rate. In this case, it is necessary to
configure timer 1 by initializing TCON and TMOD.
SBUF (SERIAL CONTROL, ADDRESSES 99H)
The Serial Buffer SFR is used to send and receive data via the
on-board serial port. Any value written to SBUF will be sent out the
serial port's TXD pin. Likewise, any value which the 8051 receives
via the serial port's RXD pin will be delivered to the user program
via SBUF. In other words, SBUF serves as the output port when
written to and as an input port when read from.
P2 (PORT 2, ADDRESS A0H, BIT-ADDRESSABLE)
This is input/output port 2. Each bit of this SFR corresponds to
one of the pins on the microcontroller. For example, bit 0 of port 2
is_ pin P2.0, bit 7 is pin P2.7. Writing a value of 1 to a bit of
this SFR will send a high level on the corresponding I/O pin whereas
a value of 0 will bring it to a low level.
Note: While the 8051 has four I/O port (P0, P1, P2, and P3)
but if hardware uses external RAM or external code memory (i.e.,
program is stored in an external ROM or EPROM chip or if using
external RAM chips) one may not use PO or P2. This is because the
8051 uses ports P0 and P2 to address the external memory. Thus if
one is using external RAM or code memory he may only use ports P1
and P3.
IE (INTERRUPT ENABLE, ADDRESSES A8H)
The Interrupt Enable SFR is used to enable and disable specific
interrupts. The low 7 bits of the SFR are used to enable/disable the
specific interrupts, where as the highest bit is used to enable or
disable ALL interrupts. Thus, if the high bit of IE is 0 all
interrupts are disabled regardless of whether an individual
interrupt is enabled by setting a lower bit.
P3 (PORT 3, ADDRESS B0H, BIT-ADDRESSABLE)
This is input/Output port 3. Each bit of this SFR corresponds to
one of the pins on the Microcontroller. For example, bit 0 of port 3
is pin P3.0, bit 7 is pin P3.7. Writing a value of 1 to a bit of
this will send a high level on the corresponding I/O pin whereas a
value of 0 will bring it to a low level.
IP (INTERRUPT PRIORITY, ADDRESSES B8H, BIT-ADDRESSABLE)
The Interrupt Priority SFR is used to specify the relative
priority of each interrupt. On the 8051, an interrupt may either be
of low (0) priority or high (1) priority. An interrupt may only
interrupt interrupts of lower priority. For example, if we configure
the 8051 so that all interrupts are of low priority except the
serial interrupt, the serial interrupt will always be able to
interrupt the system, even if another interrupt is currently
executing. However, if a serial interrupt is executing no other
interrupt will be able to interrupt the serial interrupt routine
since the the serial interrupt routine has the highest priority.
PSW (PROGRAM STATUS WORD, ADDRESSES DOH, BIT-ADDRESSABLE)
The Program Status Word is used to store a number of important
bits that are set and cleared by 8051 instructions. The PSW SFR
contains the carry flag, the auxiliary carry flag, the overflow
flag, and the parity flag. Additionally, the PSW register contains
the register bank select flags that are used to select which of the
"R" register banks are currently selected. PSW register
are:
-CY : Carry Flag.
-AC : Auxiliary Carry Flag.
-F0 : Flag 0 (available for user).
-RS1 : Register Select 1.
-RS0 : Register Select 0.
-QV : Arithmetic Overflow Flag.
-P : Accumulator Parity Flag.
Note: If one writes an interrupt handler routine, it is
advisable to save the PSW SFR on the stack and restore it when the
interrupt is complete. Many 8051 instructions modify the bits of PSW.
If interrupt routine does not guarantee that PSW is the same upon
exit as it was upon entry, program is bound to behave rather
erratically and unpredictably, and it will be tricky to debug since
the behavior will tend not to make any sense.
ACC (ACCUMULATOR, ADDRESSES E0H, BIT-ADDRESSABLE)
The Accumulator is one of the most-used SFRs on the 8051 since
it is involved in so many instructions. The Accumulator resides as
an SFR at E0h, which means the instruction MOV A, #20h is really the
same as MOV E0h, #20h. However, it is a good idea to use the first
method since it only requires two bytes whereas the second option
requires three bytes.
B (B REGISTER, ADDRESSES FOB, BIT-ADDRESSABLE)
The "B" register is used in two instructions: the
multiply and divide operations. The B register is also used by
programmers as an auxiliary register to temporarily store values.
BACK |
 |