Sei sulla pagina 1di 27

INTRODUCTION TO EMBEDDED SYSTEM

An embedded system is a special purpose microprocessor system designed to perform one or few dedicated
functions, often with real-time computing constraints.
It is usually embedded as part of complete device including hardware and mechanical parts and
has fixed software. In contrast, a general purpose computer (PC) can do many functions depending upon
programming. Since, the embedded system is dedicated to specific tasks; it is optimized to reduce size and cost
of the product and increase reliability and performance.
Typically, an embedded system is housed on a single microprocessor board with program stored in
ROM. Complexity of an embedded system varies from low, with a single microcontroller chip, to very high
with multiple units, peripherals and networks housed inside a large chassis or enclosure.

Embedded systems span all aspects of modern life and there are many examples of their use.
Telecommunications system employs numerous embedded systems from telephone switches for the network to
mobile phones at the end user. Consumer electronics like mp3 player, digital watches, video-game consoles,
digital cameras, DVD players, GPS receivers and many houses hold appliances like microwave ovens, washing
machines, and dishwashers have embedded systems to provide flexibility, efficiency and features.
Transportation systems from flight to automobiles increasingly use embedded systems. Medical equipment is
continuing to advance with more embedded systems for vital signs monitoring, electronic stethoscopes for
amplifying sounds, and various medical imaging (CT, MRT, and PET) for non-invasive internal inspections.
Designing a microprocessor based system is expensive. Such systems requires additional hardware like time
circuit, peripheral interface circuit, RAM, ROM etc. in addition to the microprocessor chip.
To solve this problem, microprocessor system is implemented with a single chip called Microcontroller.

INTRODUCTION
Intel first produced a microcontroller in 1976 under the name MCS-48, which was an 8 bit microcontroller.
Later in 1980 they released a further improved version (which is also 8 bit), under the name MCS-51. The most
popular microcontroller 8051 belongs to the MCS-51 family of microcontrollers by Intel. Following the success
of 8051, many other semiconductor manufacturers released microcontrollers under their own brand name but
using the MCS-51 core. Global companies and giants in semiconductor industry like Microchip, Zilog, Atmel,
Philips, and Siemens released products under their brand name. The specialty was that all these devices could be
programmed using the same MCS-51 instruction sets. They basically differed in support device configurations
like improved memory, presence of an ADC or DAC etc. Intel then released its first 16 bit microcontroller in
1982, under name MCS-96

OVERVIEW TO 8051
The Intel 8051 is a very popular general purpose microcontroller widely used for small scale embedded
systems. Many vendors such as Atmel, Philips, and Texas Instruments produce MCS-51 family
microcontroller chips.
The 8051 is an 8-bit microcontroller with 8 bit data bus and 16-bit address bus. The 16 bit address bus
can address a 64K( 216) byte code memory space and a separate 64K byte of data memory space. The
8051 has 4K on-chip read only code memory and 128 bytes of internal Random Access Memory (RAM)
Besides internal RAM, the 8051 has various Special Function Registers (SFR) such as the Accumulator,
the B register, and many other control registers.
34 8-bit general purpose registers in total.
Two 16 bit /Counter timers
3 internal interrupts (one serial), 2 external interrupts.
4 8-bit I/O ports (3 of them are dual purposed). One of them used for serial port,
Some 8051 chips come with UART for serial communication and ADC for analog to digital conversion.

FEATURES OF 8051

Some of the features that have made the 8051 popular are:
4 KB on chip program memory.
128 bytes on chip data memory (RAM).
4 register banks.
128 user defined software flags.
8-bit data bus.
16-bit address bus.
32 general purpose registers each of 8 bits.
16 bit timers (usually 2, but may have more, or less).
3 internal and 2 external interrupts.
Bit as well as byte addressable RAM area of 16 bytes.
Four 8-bit ports, (short models have two 8-bit ports).
16-bit program counter and data pointer.
1 Microsecond instruction cycle with 12 MHz Crystal.

8051 MICROCONTROLLER ARCHITECTURE


Its possible to explain microcontroller architecture to a great detail, but we are limiting scope of this article to
internal architecture, pin configuration, program memory and data memory organization. The basic architecture
remains same for the MCS-51 family. In general all microcontrollers in MCS- 51 family are represented by
XX51, where XX can take values like 80, 89 etc.

Schematic and Features:

The general schematic diagram of 8051 microcontroller is shown below. We can see 3 system inputs, 3 control
signals and 4 ports (for external interfacing). A Vcc power supply and ground is also shown. Now lets explain
and go through each in detail. System inputs are necessary to make the micro controller functional. So the first
and most important of this is power, marked as Vcc with a GND (ground potential). Without proper power
supply, no electronic system would work. XTAL 1 and XTAL 2 are for the system clock inputs from crystal
clock circuit. RESET input is required to initialize microcontroller to default/desired values and to make a new
start.

There are 3 control signals, EA, PSEN and ALE. These signals known as External Access (EA), Program Store
Enable (PSEN), and Address Latch Enable (ALE) are used for external memory interfacing.

As mentioned above, control signals are used for external memory interfacing. If there is no requirement of
external memory interfacing then, EA pin is pulled high (connected to Vcc) and two others PSEN and ALE are
left alone. You can also see a 0.1 micro farad decoupling capacitor connected to Vcc (to avoid HF oscillations at
input).
There are four ports numbered 0,1,2,3 and called as Port 0, Port 1, Port 2 and Port 3 which are used for external
interfacing of devices like DAC, ADC, 7 segment display, LED etc. Each port has 8 I/O lines and they all are bit
programmable.

40 PINS ON THE CHIP


For describing pin diagram and pin configuration of 8051, we are taking into consideration a 40 pin DIP (Dual
inline package). Now lets go through pin configuration in detail.
Pin-40:- Named as Vcc is the main power source. Usually it is +5V DC.
You may note some pins are designated with two signals (shown in brackets).
Pins 32-39:- Known as Port 0 (P0.0 to P0.7) In addition to serving as I/O port, lower order address and data
bus signals are multiplexed with this port (to serve the purpose of external memory interfacing). This is a bi
directional I/O port (the only one in 8051) and external pull up resistors are required to function this port as I/O.
Pin-31:- ALE aka Address Latch Enable is used to de-multiplex the address-data signal of port 0 (for external
memory interfacing.) 2 ALE pulses are available for each machine cycle.
Pin-30:- EA/ External Access input is used to enable or disallow external memory interfacing. If there is no
external memory requirement, this pin is pulled high by connecting it to Vcc.
Pin- 29:- PSEN or Program Store Enable is used to read signal from external program memory.
Pins- 21-28:- Known as Port 2 (P 2.0 to P 2.7) in addition to serving as I/O port, higher order address bus
signals are multiplexed with this quasi bi directional port.
Pin 20:- Named as Vss it represents ground (0 V) connection.
Pins 18 &19 :- Used for interfacing an external crystal to provide system clock.
Pins 10 17:- Known as Port 3. This port also serves some other functions like interrupts, timer input, control
signals for external memory interfacing RD and WR , serial communication signals RxD and TxD etc. This is a
quasi bi directional port with internal pull up.
Pin 9:- As explained before RESET pin is used to set the 8051 microcontroller to its initial values, while the
microcontroller is working or at the initial start of application. The RESET pin must be set high for 2 machine
cycles.

Pins 1 8:- Known as Port 1. Unlike other ports, this port does not serve any other functions. Port 1 is an
internally pulled up, quasi bi directional I/O port.

System Clock and Oscillator Circuits

The 8051 requires an external oscillator circuit. The oscillator circuit usually runs the crystal generates
11.0592 MHz frequency. pulses in one second. The pulse is used to synchronize the system operation in
a controlled pace.
A machine cycle is minimum amount time a simplest machine instruction must take

An 8051 machine cycle consists of 12 crystal pulses (ticks).

Instruction with a memory oprand needs multiple memory access (machine cycles).

The first 6 crystal pulses (clock cycle) is used to fetch the opcode and the second 6 pulses are used to
perform the operation on the operands in the ALU. This gives an effective machine cycle rate at 1MIPS
(Million Instructions / Second).

8051 Internal Architecture

The CPU has many important registers. The Program Count (PC) always holds the code memory
location of next instruction. The CPU is the heart of any computer which is in charge of computer
operations.

It fetches instructions from the code memory into the instruction Register (IR), analyzes the op-code of
the instruction, updates the PC to the location of next instruction, fetches the oprand from the data
memory if necessary, and finally performs the operation in the Arithmetic-Logic Unit (ALU) within the
CPU.

The B register is a register just for multiplication and division operation which requires more register
spaces for the product of multiplication and the quotient and the remainder for the division.

The immediate result is stored in the accumulator register (Acc) for next operation and the Program
Status Word (PSW) is updated depending on the status of the operation result.

Memory

The 8051 code (program) memory is read-only, while the data memory is read/write accessible. The
program memory (in EPROM) can be rewritten by the special programmer circuit.

The 8051 memory is organized in a Harvard Architecture. Both the code memory space and data
memory space begin at location 0x00 for internal or external memory which is different from the
Princeton Architecture where code and data share same memory space.

The advantage of the Harvard Architecture is not only doubling the memory capacity of the
microcontroller with same number of address lines but also increases the reliability of the
microcontroller, since there are no instructions to write to the code memory which is read only.

8051 Memory Organization


Before going deep into the memory architecture of 8051, let us talk a little bit about two variations available for
the same. They are Princeton architecture and Harvard architecture. Princeton architecture treats address
memory and data memory as a single unit (does not distinguish between two) where as Harvard architecture
treats program memory and data memory as separate entities. Thus Harvard architecture demands address, data
and control bus for accessing them separately where as Princeton architecture does not demand any such
separate bus.
Example: - 8051 micro controller is based on Harvard architecture and 8085 micro processor is based on
Princeton architecture.
Thus 8051 have two memories:1.) Program memory
2.) Data memory

Program memory organization

Now let us dive into the program memory organization 0f 8051. It has an internal program of 4K size and if
needed an external memory can be added (by interfacing) of size 60K maximum. So in total 64K size memory
is available for 8051 micro controller. By default, the External Access (EA) pin should be connected Vcc so that
instructions are fetched from internal memory initially. When the limit of internal memory (4K) is crossed,
control will automatically move to external memory to fetch remaining instructions. If the programmer wants to
fetch instruction from external memory only (bypassing the internal memory), then he must connect External
Access (EA) pin to ground (GND).

You may already know that 8051 has a special feature of locking the program memory (internal) and hence
protecting against software piracy. This feature is enabling by program lock bits. Once these bits are
programmed, contents of internal memory cannot be accessed using an external circuitry. However locking the
software is not possible if external memory is also used to store the software code. Only internal memory can be
locked and protected. Once locked, these bits can be unlocked only by a memory-erase operation, which in turn
will erase the programs in internal memory too.

8051 is capable of pipelining. Pipelining makes a processor capable of fetching the next instruction while
executing previous instruction. It is something like multi tasking, doing more than one operation at a time. 8051
is capable of fetching first byte of the next instruction while executing the previous instruction.

Data memory organization

In the MCS-51 family, 8051 has 128 bytes of internal data memory and it allows interfacing external data
memory of maximum size up to 64K. So the total size of data memory in 8051 can be upto 64K (external) +
128 bytes (internal). Observe the diagram carefully to get more understanding. So there are 3
separations/divisions of the data memory:1) Register banks.
2) Bit addressable area.
3) Scratch pad area.
Register banks form the lowest 32 bytes on internal memory and there are 4 register banks designated bank #0,
#1, #2 and #3. Each bank has 8 registers which are designated as R0, R1R7. At a time only one register bank
is selected for operations and the registers inside the selected bank are accessed using mnemonics R0...R1... Etc.
Other registers can be accessed simultaneously only by direct addressing. Registers are used to store data or
operands during executions. By default register bank #0 is selected (after a system reset).

The bit addressable area of 8051 is usually used to store bit variables. The bit addressable area is formed by the
16 bytes next to register banks. They are designated from address 20H to 2FH (total 128 bits). Each bit can be
accessed from 00H to 7FH within 128 bits from 20H to 2FH. Bit addressable area is mainly used to store bit
variables from application program, like status of an output device like LED or Motor (ON/OFF) etc. We need
only a bit to store this status and using a complete byte addressable area for storing this is really bad
programming practice, since it results in wastage of memory.
The scratch pad area is the upper 80 bytes which is used for general purpose storage. Scratch pad area is from
30H to 7FH and this includes stack too.

INPUT / OUTPUT PORTS

PORT P0 (pins 32 to
39): PORT P0 can
be used as a general purpose 8 bit port when no external memory is present, but if external memory access is
required then PORT P0 acts as a multiplexed address and data bus that can be used to access external memory
in conjunction with PORT P2. P0 acts as AD0-AD7.

PORT P1 (Pins 1 to 8): The port P1 is a general purpose input/output port which can be used for a variety of
interfacing tasks. The other ports P0, P2 and P3 have dual roles or additional functions associated with them
based upon the context of their usage. The port 1 output buffers can sink/source four TTL inputs. When 1s are
written to portn1 pins are pulled high by the internal pull-ups and can be used as inputs.

PORT P2 (pins 21 to 28): PORT P2 can also be used as a general purpose 8 bit port when no external memory
is present, but if external memory access is required then PORT P2 will act as an address bus in conjunction
with PORT P0 to access external memory. PORT P2 acts as A8-A15, as can be seen from fig 1.1

PORT P3 (Pins 10 to 17): PORT P3 acts as a normal IO port, but Port P3 has additional functions such as,
serial transmit and receive pins, 2 external interrupt pins, 2 external counter inputs, read and write pins for
memory access.

8051 Addressing modes

Let us begin this article with a simple question. What is an addressing mode ?. A simple question always has
a simple answer too. Addressing mode is a way to address an operand. Operand means the data we are
operating upon (in most cases source data). It can be a direct address of memory, it can be register names, it can
be any numerical data etc. I will explain this with a simple data move instruction of 8051.
MOV A,#6AH
Here the data 6A is the operand, often known as source data. When this instruction is executed, the data 6AH is
moved to accumulator A. There are 5 different ways to execute this instruction and hence we say, we have got 5
addressing modes for 8051. They are
1) Immediate Addressing Mode

2) Direct Addressing Mode

3) Register Direct Addressing Mode

4) Register Indirect Addressing Mode

5) Indexed Addressing Mode

Immediate Addressing Mode


Lets begin with an example.
MOV A, #6AH
In general we can write MOV A, #data
This addressing mode is named as immediate because it transfers an 8-bit data immediately to the
accumulator (destination operand).

The
picture
above describes the above instruction and its execution. The opcode for MOV A, # data is 74H. The opcode is
saved in program memory at 0202 address. The data 6AH is saved in program memory 0203. (See, any part of
the program memory can be used, this is just an example) When the opcode 74H is read, the next step taken
would be to transfer whatever data at the next program memory address (here at 0203) to accumulator A (E0H
is the address of accumulator). This instruction is of two bytes and is executed in one cycle. So after the
execution of this instruction, program counter will add 2 and move to o204 of program memory.
Note: The # symbol before 6AH indicates that operand is a data (8 bit). If # is not present then the
hexadecimal number would be taken as address.

Direct Addressing Mode

This is another way of addressing an operand. Here the address of the data (source data) is given as operand. Let
us take an example.
MOV A, 04H
Here 04H is the address of register 4 of register bank#0. When this instruction is executed, what ever data is
stored in register 04H is moved to accumulator. In the picture below we can see, register 04H holds the data
1FH. So the data 1FH is moved to accumulator.
Note: We have not used # in direct addressing mode, unlike immediate mode. If we had used #, the data
value 04H would have been transferred to accumulator instead 0f 1FH.

As shown in picture above this is a 2 byte instruction which requires 1 cycle to complete. Program counter will
increment by 2 and stand in 0204. The op-code for instruction MOV A, address is E5H. When the instruction at
0202 is executed (E5H), accumulator is made active and ready to receive data, Then program control goes to
next address that is 0203 and look up the address of the location (04H) where the source data (to be transferred
to accumulator) is located. At 04H the control finds the data 1F and transfers it to accumulator and hence the
execution is completed.

Register Direct Addressing Mode

In this addressing mode we use the register name directly (as source operand). An example is shown below.
MOV A, R4
At a time registers can take value from R0,R1to R7. You may already know there are 32 such registers. So
how you access 32 registers with just 8 variables to address registers? Here comes the use of register banks.
There are 4 register banks named 0, 1, 2 and 3. Each bank has 8 registers named from R0 to R7. At a time only
one register bank can be selected. Selection of register bank is made possible through a Special Function
Register (SFR) named Processor Status Word (PSW). PSW is an 8 bit SFR where each bit can be programmed.
Bits are designated from PSW.0 to PSW.7 Register banks are selected using PSW.3 and PSW.4.These two bits

are known as register bank select bits as they are used to select register banks. A picture below shows the PSW
register and the Register Bank Select bits with status.

So in register direct addressing mode, data is transferred to accumulator from the register (based on which
register bank is selected).

So we see that op-code for MOV A, R4 is EC. The op-code is stored in program memory address 0202 and
when it is executed the control goes directly to R4 of the respected register bank (that is selected in PSW). If

register bank #0 is selected then the data from R4 of register bank #0 will be moved to accumulator. (Here it is
2F stored at 04 H). 04 H is the address of R4 of register bank #0. Movement of data (2F) in this case is shown
as bold line. Now please take a look at the dotted line. Here 2F is getting transferred to accumulator from data
memory location 0C H. Now understand that 0C H is the address location of Register 4 (R4) of register bank
#1. Programmers usually get confused with register bank selection. Also keep in mind that data at R4 of register
bank #0 and register bank #1 (or even other banks) will not be same. So, wrong selection of register banks will
result in undesired output.
Also note that the instruction above is 1 byte and requires 1 cycle for complete execution. This means using
register direct addressing mode can save program memory.

Register Indirect Addressing Mode

So in this addressing mode, address of the data (source data to transfer) is given in the register operand.
MOV A, @R0
Here the value inside R0 is considered as an address, which holds the data to be transferred to accumulator.
Example: If R0 holds the value 20H, and we have a data 2F H stored at the address 20H, then the value 2FH
will get transferred to accumulator after executing this instruction.

So the op-code for MOV A, @R0 is E6H. Assuming that register bank #0 is selected. So the R0 of register bank
#0 holds the data 20H. Program control moves to 20H where it locates the data 2FH and it transfers 2FH to
accumulator.
This is a single byte instruction and the program counter increments 1 and moves to 0203 of program memory.
Note: Only R0 and R1 are allowed to form a register indirect addressing instruction. In other words
programmer can make any instruction either using @R0 or @R1. All register banks are allowed.

Indexed Addressing Mode

Well let us see two examples first.


MOVC A, @A+DPTR and MOVC A, @A+PC
where DPTR is data pointer and PC is program counter (both are 16 bit registers). Let us take the first example.
MOVC A, @A+DPTR
Whats the first impression you have now? The source operand is @A+DPTR and we know we will get the
source data (to transfer) from this location. It is nothing but adding contents of DPTR with present content of
accumulator. This addition will result a new data which is taken as the address of source data (to transfer). The
data at this address is then transferred to accumulator. Take a look at the picture below.

The op-code for the instruction is 93H. DPTR holds the value 01FE, where 01 is located in DPH (higher 8 bits)
and FE is located in DPL (lower 8 bits). Accumulator now has the value 02H. A 16 bit addition is performed and
now 01FE H+02 H results in 0200 H. What-ever data is in 0200 H will get transferred to accumulator. The
previous value inside accumulator (02H) will get replaced with new data from 0200H. New data in the
accumulator is shown in dotted line box.
This is a 1 byte instruction with 2 cycles needed for execution. What you infer from that? The execution time
required for this instruction is high compared to previous instructions (which all were 1 cycle).
The other example MOVC A, @A+PC works the same way as above example. The only difference is, instead
of adding DPTR with accumulator, here data inside program counter (PC) is added with accumulator to obtain
the target address.

8051 Instruction Set

Introduction
The process of writing program for the microcontroller mainly consists of giving instructions (commands) in
the specific order in which they should be executed in order to carry out a specific task. As electronics cannot
understand what for example an instruction if the push button is pressed- turn the light on means, then a
certain number of simpler and precisely defined orders that decoder can recognize must be used. All commands
are known as INSTRUCTION SET. All microcontrollers compatible with the 8051 have in total of 255
instructions, i.e. 255 different words available for program writing.
At first sight, it is imposing number of odd signs that must be known by heart. However, It is not so
complicated as it looks like. Many instructions are considered to be different, even though they perform the
same operation, so there are only 111 truly different commands. For example: ADD A,R0, ADD A,R1, ... ADD
A,R7 are instructions that perform the same operation (addition of the accumulator and register). Since there are
8 such registers, each instruction is counted separately. Taking into account that all instructions perform only 53
operations (addition, subtraction, copy etc.) and most of them are rarely used in practice, there are actually 2030 abbreviations to be learned, which is acceptable.
Types of instructions:
Depending on operation they perform, all instructions are divided in several groups:

Arithmetic Instructions

Branch Instructions

Data Transfer Instructions

Logic Instructions

Bit-oriented Instructions

The first part of each instruction, called MNEMONIC refers to the operation an instruction performs (copy,
addition, logic operation etc.). Mnemonics are abbreviations of the name of operation being executed. For
example:

INC R1 - Means: Increment register R1 (increment register R1);

LJMP LAB5 - Means: Long Jump LAB5 (long jump to the address marked as LAB5);

JNZ LOOP - Means: Jump if Not Zero LOOP (if the number in the accumulator is not 0, jump to the
address marked as LOOP);
The other part of instruction, called OPERAND is separated from mnemonic by at least one whitespace and
defines data being processed by instructions. Some of the instructions have no operand, while some of them
have one, two or three. If there is more than one operand in an instruction, they are separated by a comma. For
example:

RET - return from a subroutine;

JZ TEMP - if the number in the accumulator is not 0, jump to the address marked as TEMP;

ADD A,R3 - add R3 and accumulator;

CJNE A, #20, LOOP - compare accumulator with 20. If they are not equal, jump to the address marked
as LOOP;

Arithmetic instructions
Arithmetic instructions perform several basic operations such as addition, subtraction, division, multiplication
etc. After execution, the result is stored in the first operand. For example:
ADD A,R1 - The result of addition (A+R1) will be stored in the accumulator.

Mnemonic

Description

Byte

Cycle

ADD A, Rn

Adds the register to the accumulator

ADD A, direct

Adds the direct byte to the accumulator

ADD A, @Ri

Adds the indirect RAM to the accumulator

ADD A, #data

Adds the immediate data to the accumulator

ADDC A, Rn

Adds the register to the accumulator with a carry flag

ADDC A, direct Adds the direct byte to the accumulator with a carry flag

ADDC A, @Ri

Adds the indirect RAM to the accumulator with a carry flag

ADDC A, #data

Adds the immediate data to the accumulator with a carry flag

SUBB A, Rn

Subtracts the register from the accumulator with a borrow

SUBB A, direct

Subtracts the direct byte from the accumulator with a borrow

SUBB A, @Ri

Subtracts the indirect RAM from the accumulator with a borrow

SUBB A, #data

Subtracts the immediate data from the accumulator with a borrow

INC A

Increments the accumulator by 1

INC Rn

Increments the register by 1

INC Rx

Increments the direct byte by 1

INC @Ri

Increments the indirect RAM by 1

DEC A

Decrements the accumulator by 1

DEC Rn

Decrements the register by 1

DEC Rx

Decrements the direct byte by 1

DEC @Ri

Decrements the indirect RAM by 1

INC DPTR

Increments the Data Pointer by 1

MUL AB

Multiplies A and B

DIV AB

Divides A by B

DA A

Decimal adjustment of the accumulator

according to BCD code

Branch Instructions
There are two kinds of branch instructions:
Unconditional jump instructions: Upon their execution a jump to a new location from where the program
continues execution is executed.
Conditional jump instructions: A jump to a new program location is executed only if a specified condition is
met. Otherwise, the program normally proceeds with the next instruction.
Branch Instructions
Mnemonic

Description

Byte

Cycle

ACALL addr11

Absolute subroutine call

LCALL addr16

Long subroutine call

RET

Returns from subroutine

RETI

Returns from interrupt subroutine

AJMP addr11

Absolute jump

LJMP addr16

Long jump

SJMP rel

Short jump

(from 128 to +127 locations relative to the


following instruction)
JC rel

Jump if carry flag is set. Short jump.

JNC rel

Jump if carry flag is not set. Short jump.

JB bit,rel

Jump if direct bit is set. Short jump.

JBC bit,rel

Jump if direct bit is set and clears bit. Short jump.

JMP @A+DPTR

Jump indirect relative to the DPTR

JZ rel

Jump if the accumulator is zero. Short jump.

JNZ rel

Jump if the accumulator is not zero. Short jump.

CJNE A rel

Compares direct byte to the accumulator

and jumps if not equal. Short jump.


CJNE A,#data

Compares immediate data to the accumulator


and jumps if not equal. Short jump.

CJNE Rn,#data

Compares immediate data to the register


and jumps if not equal. Short jump.

CJNE @Ri,#data

Compares immediate data to indirect register


and jumps if not equal. Short jump.

DJNZ Rn,rel

Decrements register and jumps if not 0. Short jump.

DJNZ Rx,rel

Decrements direct byte and jump if not 0. Short jump.

NOP

No operation

Data Transfer Instructions


Data transfer instructions move the content of one register to another. The register the content of which is
moved remains unchanged. If they have the suffix X (MOVX), the data is exchanged with external memory.

Mnemonic

Description

Byte

Cycle

MOV A,Rn

Moves the register to the accumulator

MOV A,direct

Moves the direct byte to the accumulator

MOV A,@Ri

Moves the indirect RAM to the accumulator

MOV A,#data

Moves the immediate data to the accumulator

MOV Rn,A

Moves the accumulator to the register

MOV Rn,direct

Moves the direct byte to the register

MOV Rn,#data

Moves the immediate data to the register

MOV direct,A

Moves the accumulator to the direct byte

MOV direct,Rn

Moves the register to the direct byte

MOV direct,direct

Moves the direct byte to the direct byte

MOV direct,@Ri

Moves the indirect RAM to the direct byte

MOV direct,#data

Moves the immediate data to the direct byte

MOV @Ri,A

Moves the accumulator to the indirect RAM

MOV @Ri,direct

Moves the direct byte to the indirect RAM

MOV @Ri,#data

Moves the immediate data to the indirect RAM

MOV DPTR,#data

Moves a 16-bit data to the data pointer

3-10

3-10

4-11

4-11

MOVC A,@A+DPTR Moves the code byte relative to the DPTR


to the accumulator (address=A+DPTR)
MOVC A,@A+PC

Moves the code byte relative to the PC


to the accumulator (address=A+PC)

MOVX A,@Ri

Moves the external RAM (8-bit address)


to the accumulator

MOVX A,@DPTR

Moves the external RAM (16-bit address)


to the accumulator

MOVX @Ri,A

Moves the accumulator to the external RAM


(8-bit address)

MOVX @DPTR,A

Moves the accumulator to the external RAM

(16-bit address)
PUSH direct

Pushes the direct byte onto the stack

POP direct

Pops the direct byte from the stack/td>

XCH A,Rn

Exchanges the register with the accumulator

XCH A,direct

Exchanges the direct byte with the accumulator

XCH A,@Ri

Exchanges the indirect RAM with the accumulator

XCHD A,@Ri

Exchanges the low-order nibble indirect RAM

with the accumulator

Logic Instructions
Logic instructions perform logic operations upon corresponding bits of two registers. After execution, the result
is stored in the first operand
Mnemonic

Description

Byte

Cycle

ANL A,Rn

AND register to accumulator

ANL A,direct

AND direct byte to accumulator

ANL A,@Ri

AND indirect RAM to accumulator

ANL A,#data

AND immediate data to accumulator

ANL direct,A

AND accumulator to direct byte

ANL direct,#data

AND immediae data to direct register

ORL A,Rn

OR register to accumulator

ORL A,direct

OR direct byte to accumulator

ORL A,@Ri

OR indirect RAM to accumulator

ORL direct,A

OR accumulator to direct byte

ORL direct,#data

OR immediate data to direct byte

XRL A,Rn

Exclusive OR register to accumulator

XRL A,direct

Exclusive OR direct byte to accumulator

XRL A,@Ri

Exclusive OR indirect RAM to accumulator

XRL A,#data

Exclusive OR immediate data to accumulator

XRL direct,A

Exclusive OR accumulator to direct byte

XORL direct,#data Exclusive OR immediate data to direct byte

CLR A

Clears the accumulator

CPL A

Complements the accumulator (1=0, 0=1)

SWAP A

Swaps nibbles within the accumulator

RL A

Rotates bits in the accumulator left

RLC A

Rotates bits in the accumulator left through carry

RR A

Rotates bits in the accumulator right

RRC A

Rotates bits in the accumulator right through carry

Bit-oriented Instructions
Similar to logic instructions, bit-oriented instructions perform logic operations. The difference is that these are
performed upon single bits.
Bit-oriented Instructions
Mnemonic

Description

Byte

Cycle

CLR C

Clears the carry flag

CLR bit

Clears the direct bit

SETB C

Sets the carry flag

SETB bit

Sets the direct bit

CPL C

Complements the carry flag

CPL bit

Complements the direct bit

ANL C,bit

AND direct bit to the carry flag

ANL C,/bit

AND complements of direct bit to the carry flag

ORL C,bit

OR direct bit to the carry flag

ORL C,/bit

OR complements of direct bit to the carry flag

MOV C,bit

Moves the direct bit to the carry flag

MOV bit,C

Moves the carry flag to the direct bit

Here is a list of the operands and their meanings:

A - accumulator;

Rn - is one of working registers (R0-R7) in the currently active RAM memory bank;

Direct - is any 8-bit address register of RAM. It can be any general-purpose register or a SFR (I/O port,
control register etc.);

@Ri - is indirect internal or external RAM location addressed by register R0 or R1;

#data - is an 8-bit constant included in instruction (0-255);

#data16 - is a 16-bit constant included as bytes 2 and 3 in instruction (0-65535);

addr16 - is a 16-bit address. May be anywhere within 64KB of program memory;

addr11 - is an 11-bit address. May be within the same 2KB page of program memory as the first byte of
the following instruction;

rel - is the address of a close memory location (from -128 to +127 relative to the first byte of the
following instruction). On the basis of it, assembler computes the value to add or subtract from the number
currently stored in the program counter;

bit - is any bit-addressable I/O pin, control or status bit; and

C - is carry flag of the status register (register PSW).

8051 Programming

Microcontroller doesnt do anything by himself. We have to give corresponding instructions to microcontroller


to perform a particular task. We know that microcontroller do one or two particular task for which we have
designed for. So we store the sequence of instructions inside the microcontroller memory. This sequence of
instructions is called Programming of microcontroller.
The CPU can work only in binary, although it work at very high speed. But for human beings, it is very difficult
and time consuming to write a program using 0s and 1s. This language of 0s and 1s can only be understood by
microcontrollers and some of the experts working on its development. In order to bridge this gap between
machine and humans, the first high-level programming language called Assembly language was created.
The main problem of remembering codes recognized as instructions by electronics was solved therefore, but
another one, equally complicated to both us and them (microcontrollers) arose. This problem was also easily
solved by means of the program for a PC called assembler and a simple device called programmer.
This program enables the PC to receive commands in the form of abbreviations and convert them unerringly
into so called executable file. The moment of compiling a program into machine language is crucial as this
file, called HEX file, represents a series of binary numbers understandable to microcontrollers only. The
program written in assembly language cannot be executed practically unless this file is loaded into the

microcontroller memory. This is the moment when the last link in the chain - the programmer - appears on the
scene. It is a small device connected to a PC via some of the ports and has a socket for placing chip in.

Elements of Assembly Language


Assembly language is basically like any other language, which means that it has its words, rules and syntax. The
basic elements of assembly language are:

Labels;

Orders;

Directives; and

Comments.

Syntax of Assembly language


When writing a program in assembly language it is necessary to observe specific rules in order to enable the
process of compiling into executable HEX-code to run without errors. These compulsory rules are called
syntax and there are only several of them:

Every program line may consist of a maximum of 255 characters;

Every program line to be compiled must start with a symbol, label, mnemonics or directive;

Text following the mark ; in a program line represents a comment ignored (not compiled) by the
assembler; and

All the elements of one program line (labels, instructions etc.) must be separated by at least one space
character. For the sake of better clearness, a push button TAB on a keyboard is commonly used instead of it, so
that it is easy to delimit columns with labels, directives etc. in a program.

Typical applications:8051 chips are used in a wide variety of control systems, telecom applications, robotics as well as in the
automotive industry. By some estimation, 8051 family chips make up over 50% of the embedded chip market.

INTERFACING 16*2 LCD WITH 8051


In this session we will have brief discussion on how to interface 162 LCD module to AT89C51which is a
8051 family microcontroller. We use LCD display for the messages for more interactive way to operate
the system or displaying error messages etc. interfacing LCD to microcontroller is very easy if you
understanding the working of LCD, in this session I will not only give the information of LCD and also
provide the code in C language which is working fine without any errors.

Interfacing 162 LCD with 8051 Circuit Diagram:

Potrebbero piacerti anche