0% found this document useful (0 votes)
13 views

Unit-3

mpmc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Unit-3

mpmc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Unit -3

Input/Output Ports and circuits:


8051 microcontrollers have 4 I/O ports each of 8-bit, which can be configured as input or
output. Hence, total 32 input/output pins allow the microcontroller to be connected with the
peripheral devices.
Pin configuration, i.e. the pin can be configured as 1 for input and 0 for output as per the
logic state.

Input/Output (I/O) pin − All the circuits within the microcontroller must be connected to one of its
pins except P0 port because it does not have pull-up resistors built-in.

Input pin − Logic 1 is applied to a bit of the D register. The output of transistor is turned off and
the other pin remains connected to the power supply voltage over a pull-up resistor of high
resistance.

Port 0 − The P0 (zero) port is characterized by two functions −

When the external memory is used then the lower address byte (addresses A0A7) is applied on it,
else all bits of this port are configured as input/output.

When P0 port is configured as an output then other ports consisting of pins with built-in pull-up
resistor connected by its end to 5V power supply, the pins of this port have this resistor left out.

Input Configuration

If any pin of this port is configured as an input, then it acts as if it “floats”, i.e. the input has
unlimited input resistance and in-determined potential.
Output Configuration
When the pin is configured as an output, then it acts as an “open drain”. By applying logic 0 to a
port bit, the appropriate pin will be connected to ground (0V), and applying logic 1, the external
output will keep on “floating”. In order to apply logic 1 (5V) on this output pin, it is necessary to
build an external pull up resistor.
Port 1
P1 is a true I/O port as it doesn’t have any alternative functions as in P0, but this port can be
configured as general I/O only. It has a built-in pull-up resistor and is completely compatible with
TTL circuits.
Port 2
P2 is similar to P0 when the external memory is used. Pins of this port occupy addresses intended
for the external memory chip. This port can be used for higher address byte with addresses A8-
A15. When no memory is added then this port can be used as a general input/output port similar to
Port 1.
Port 3
In this port, functions are similar to other ports except that the logic 1 must be applied to
appropriate bit of the P3 register.

Pins Current Limitations

When pins are configured as an output (i.e. logic 0), then the single port pins can receive a current
of 10mA.

When these pins are configured as inputs (i.e. logic 1), then built-in pull-up resistors provide very
weak current, but can activate up to 4 TTL inputs of LS series.

If all 8 bits of a port are active, then the total current must be limited to 15mA (port P0: 26mA).

If all ports (32 bits) are active, then the total maximum current must be limited to 71mA.
Interrupt of 8051 microcontroller
Interrupts are the events that temporarily suspend the main program, pass the control to the
external sources and execute their task. It then passes the control to the main program where it had
left off.

8051 has 5 interrupt signals, i.e. INT0, TFO, INT1, TF1, RI/TI. Each interrupt can be
enabled or disabled by setting bits of the IE register and the whole interrupt system can be
disabled by clearing the EA bit of the same register.

Steps in executing an interrupt

 Upon receiving the interrupt signal the Microcontroller , finish current instruction and saves
the PC on stack

 Jumps to a fixed location in memory depending on type of interrupt

 Starts to execute the interrupt service routine until RETI (return from interrupt)

 Upon executing the RETI the microcontroller returns to the place where it was interrupted.
Get pop PC from stack
 Each interrupt has a specific place in code memory where program execution (interrupt
service routine) begins.
 External Interrupt 0: 0003h
 Timer 0 overflow: 000Bh
 External Interrupt 1: 0013h
 Timer 1 overflow: 001Bh
 Serial : 0023h
 Timer 2 overflow(8052+) 002bh

IE (Interrupt Enable) Register


This register is responsible for enabling and disabling the interrupt. EA register is set to one for
enabling interrupts and set to 0 for disabling the interrupts. Its bit sequence and their meanings are
shown in the following figure.
EA - It disables all interrupts. When EA = 0 no interrupt will be acknowledged and EA = 1
enables the interrupt individually.
ES - Enables/disables serial port interrupt.
ET1 - Enables/disables timer1 overflow interrupt.
EX1 - Enables/disables external interrupt1.
ET0 - Enables/disables timer0 overflow interrupt. EX0
- Enables/disables external interrupt0.

IP (Interrupt Priority) Register


We can change the priority levels of the interrupts by changing the corresponding bit in the
Interrupt Priority (IP) register as shown in the following figure.
A low priority interrupt can only be interrupted by the high priority interrupt, but not
interrupted by another low priority interrupt.
If two interrupts of different priority levels are received simultaneously, the request of
higher priority level is served.
If the requests of the same priority levels are received simultaneously, then the internal
polling sequence determines which request is to be serviced.

PS - It defines the serial port interrupt priority level. PT1


- It defines the timer interrupt of 1 priority.
PX1 - It defines the external interrupt priority level. PT0
- It defines the timer0 interrupt priority level.
PX0 - It defines the external interrupt of 0 priority level.
TCON register specifies the type of external interrupt to the microcontroller.

Timers and Counters


The 8051 has two timers: timer0 and timer1. They can be used either as timers or as counters. Both timers
are 16 bits wide. Since the 8051 has an 8-bit architecture, each 16-bit is accessed as two separate registers
of low byte and high byte. First we shall discuss about Timer0 registers.

Timer0 registers is a 16 bits register and accessed as low byte and high byte. The low byte is referred as a
TL0 and the high byte is referred as TH0. These registers can be accessed like any other registers.

Timer1 registers is also a 16 bits register and is split into two bytes, referred to as TL1 and TH1.

TMOD (timer mode) Register: This is an 8-bit register which is used by both timers 0 and 1 to set the
various timer modes. In this TMOD register, lower 4 bits are set aside for timer0 and the upper 4 bits are
set aside for timer1. In each case, the lower 2 bits are used to set the timer mode and upper 2 bits to specify
the operation.

TMOD
In upper or lower 4 bits, first bit is a GATE bit. Every timer has a means of starting and stopping.
Some timers do this by software, some by hardware, and some have both software and hardware controls.
The hardware way of starting and stopping the timer by an external source is achieved by making GATE=1
in the TMOD register. And if we change to GATE=0 then we do no need external hardware to start and
stop the timers.

The second bit is C/T bit and is used to decide whether a timer is used as a time delay generator or
an event counter. If this bit is 0 then it is used as a timer and if it is 1 then it is used as a counter.

In upper or lower 4 bits, the last bits third and fourth are known as M1 and M0 respectively. These are used
to select the timer mode.

M0 M1 Mode Operating Mode


0 0 0 13-bit timer mode, 8-bit timer/counter THx and TLx as 5-bit prescalar.
0 1 1 16-bit timer mode, 16-bit timer/counters THx and TLx are cascaded; There are no
prescalar.

1 0 2 8-bit auto reload mode, 8-bit auto reload timer/counter; THx holds a value which is
to be reloaded into TLx eachtime it overflows.

1 1 3 Spilt timer mode.

Mode 1- It is a 16-bit timer; therefore it allows values from 0000 to FFFFH to be loaded into the timer’s
registers TL and TH. After TH and TL are loaded with a 16-bit initial value, the timer must be started. We
can do it by “SETB TR0” for timer 0 and “SETB TR1” for timer 1. After the timer is started. It starts count
up until it reaches its limit of FFFFH. When it rolls over from FFFF to 0000H, it sets high a flag bit called
TF (timer flag). This timer flag can be monitored. When this timer flag is raised, one option would be stop
the timer with the instructions “CLR TR0“ or CLR TR1 for timer 0 and timer 1 respectively. Again, it must
be noted that each timer flag TF0 for timer 0 and TF1 for timer1. After the timer reaches its limit and rolls
over, in order to repeat the process the registers TH and TL must be reloaded with the original value and TF
must be reset to 0.

Mode 1 programming
The following are the characteristics and operations of mode 1:
 It is a 16-bit timer; therefore, it allows values of 0000 to FFFFH to be loaded into the timer’s
registers TL and TH.
 After TH and TL are loaded with a 16-bit initial value, the timer must be start ed. This is done by
“SETB TRO” for Timer 0 and “SETB TR1″ for Timer 1.
 After the timer is started, it starts to count up. It counts up until it reaches its limit of FFFFH. When
it rolls over from FFFFH to 0000, it sets high a flag bit called TF (timer flag). This timer flag can be
monitored. When this timer flag is raised, one option would be to stop the timer with the
instructions “CLR TRO” or “CLR TR1″, for Timer 0 and Timer 1, respectively. Again, it must be
noted that each timer has its own timer flag: TFO for Timer 0, and TF1 for Timer 1.
 After the timer reaches its limit and rolls over, in order to repeat the process the registers TH and TL
must be reloaded with the original value, and TF must be reset to 0.

Steps to program in mode 1


 To generate a time delay, using the timer’s mode 1, the following steps are taken. To clarify these
steps, see Example 9-4.
 Load the TMOD value register indicating which timer (Timer 0 or Timer 1) is to be used and which
timer mode (0 or 1) is selected.
 Load registers TL and TH with initial count values.
 Start the timer.
 Keep monitoring the timer flag (TF) with the “JNB TFx, target” instruc tion to see if it is raised. Get
out of the loop when TF becomes high.
 Stop the timer.
 Clear the TF flag for the next round.
 Go back to Step 2 to load TH and TL again.

Mode0- Mode 0 is exactly same like mode 1 except that it is a 13-bit timer instead of 16-bit. The 13- bit
counter can hold values between 0000 to 1FFFH in TH-TL. Therefore, when the timer reaches its
maximum of 1FFH, it rolls over to 0000, and TF is raised.

Mode 2- It is an 8 bit timer that allows only values of 00 to FFH to be loaded into the timer’s register TH.
After TH is loaded with 8 bit value, the 8051 gives a copy of it to TL. Then the timer must be started. It is
done by the instruction “SETB TR0” for timer 0 and “SETB TR1” for timer1. This is like mode 1. After
timer is started, it starts to count up by incrementing the TL register. It counts up until it reaches its limit
of FFH. When it rolls over from FFH to 00. It sets high the TF (timer flag). If we are using timer 0, TF0
goes high; if using TF1 then TF1 is raised. When Tl register rolls from FFH to 00 and TF is set to 1, TL is
reloaded automatically with the original value kept by the TH register. To repeat the process, we must
simply clear TF and let it go without any need by the programmer to reload the original value. This makes
mode 2 auto reload, in contrast in mode 1 in which programmer has to reload TH and TL.

Mode 2 programming

The following are the characteristics and operations of mode 2.


 It is an 8-bit timer; therefore, it allows only values of 00 to FFH to be loaded into the timer’s
register TH.
 After TH is loaded with the 8-bit value, the 8051 gives a copy of it to TL. Then the timer must be
started. This is done by the instruction “SETB TRO” for Timer 0 and “SETB TR11‘for Timer 1.
This is just like mode 1.
 After the timer is started, it starts to count up by incrementing the TL register. It counts up until it
reaches its limit of FFH. When it rolls over from FFH to 00, it sets high the TF (timer flag). If we
are using Timer 0, TFO goes high; if we are using Timer 1, TF1 is raised.

 When the TL register rolls from FFH to 0 and TF is set to 1, TL is reloaded automatically with the
original value kept by the TH register. To repeat the process, we must simply clear TF and let it go
without any need by the programmer to reload the original value. This makes mode 2 an auto-
reload, in contrast with mode 1 in which the programmer has to reload TH and TL.
It must be emphasized that mode 2 is an 8-bit timer. However, it has an auto- reloading capability. In auto-
reload, TH is loaded with the initial count and a copy of it is given to TL. This reloading leaves TH
unchanged, still holding a copy of the original value. This mode has many applications, including setting
the baud rate in serial communication, as we will see in Chapter 10.
Steps to program in mode 2
To generate a time delay using the timer’s mode 2, take the following steps.
 Load the TMOD value register indicating which timer (Timer 0 or Timer 1) is to be used, and select
the timer mode (mode 2).
 Load the TH registers with the initial count value.
 Start the timer.
 Keep monitoring the timer flag (TF) with the “JNB TFx, target” instruc tion to see whether it is
raised. Get out of the loop when TF goes high.
 Clear the TF flag.
 Go back to Step 3, since mode 2 is auto-reload.

Mode3- Mode 3 is also known as a split timer mode. Timer 0 and 1 may be programmed to be in mode 0, 1
and 2 independently of similar mode for other timer. This is not true for mode 3; timers do not operate
independently if mode 3 is chosen for timer 0. Placing timer 1 in mode 3 causes it to stop counting; the
control bit TR1 and the timer 1 flag TF1 are then used by timer0.

TCON register- Bits and symbol and functions of every bits of TCON are as follows:
BIT Symbol Functions

Timer1 over flow flag. Set when timer rolls from all 1s to 0.
Cleared
7 TF1 When the processor vectors to execute interrupt service
routine Located at program address 001Bh.

Timer 1 run control bit. Set to 1 by programmer to enable


6 TR1
timer to count; Cleared to 0 by program to halt timer.

5 TF0 Timer 0 over flow flag. Same as TF1.

4 TR0 Timer 0 run control bit. Same as TR1.


External interrupt 1 Edge flag. Not related to timer
3 IE1
operations.

External interrupt1 signal type control bit. Set to 1 by


program to Enable external interrupt 1 to be triggered by a
2 IT1
falling edge signal. Set To 0 by program to enable a low
level signal on external interrupt1 to generate an interrupt

External interrupt 0 Edge flag. Not related to timer


1 IE0
operations.

0 IT0 External interrupt 0 signal type control bit. Same as IT0.


I

IE1 External interrupt 1 Edge flag. Not related to timer operations.

IT1 External interrupt1 signal type control bit. Set to 1 by program to

Enable external interrupt 1 to be triggered by a falling edge signal. Set

To 0 by program to enable a low level signal on external interrupt1 to

generate an interrupt.

1 IE0 External interrupt 0 Edge flag. Not related to timer operations.

IT0 External interrupt 0 signal type control bit. Same as IT0.


8051- SERIAL COMMUNICATION

Types of Serial communications

 The 8051 has two pins for transferring and receiving data by serial communication.
 These two pins are part of the Port3(P3.0 &P3.1)
 These pins are TTL compatible and hence they require a line driver to make them
RS232 compatible
 Max232 chip is one such line driver in use.
 Serial communication is controlled by an 8-bit register called SCON register; it is a
bit addressable register.
Interfacing to PC
SCON (Serial control) register

These two bits of SCON register determine the framing of data by specifying the number of bits per
character and start bit and stop bits. There are 4 serial modes.
SM0 SM1
0 0 Serial Mode 0

0 1 Serial Mode 1, 8 bit data,1 stop bit, 1 Start bit

1 0 Serial Mode 2

1 1 Serial Mode 3

REN (Receive Enable) also referred as SCON.4.When it is high; it allows the 8051 to receive data
on the RxD pin. So to receive and transfer data REN must be set to 1.When REN=0, the receiver is
disabled. This is achieved as below

SETB SCON.4

& CLR SCON.4

TI (Transmit interrupt) is the D1 bit of SCON register. When 8051 finishes the transfer of 8-bit
character, it raises the TI flag to indicate that it is ready to transfer another byte. The TI bit is
raised at the beginning of the stop bit.

RI (Receive interrupt) is the D0 bit of the SCON register. When the 8051 receives data serially, via
RxD, it gets rid of the start and stops bits and places the byte in theSBUF register. Then it raises the
RI flag bit to indicate that a byte has been received and should be picked up before it is lost. RI is raised
halfway through the stop bit.
ADDRESSING MODES OF 8051:

The way in which the data operands are accessed by different instructions is known as the addressing
modes. There are various methods of denoting the data operands in the instruction. The 8051
microcontroller supports mainly 5 addressing modes. They are

1. Immediate addressing mode


2. Direct Addressing mode
3. Register addressing mode
4. Register Indirect addressing mode
5. Register Specific addressing mode
6. Indexed addressing mode

1. Immediate addressing mode :


The addressing mode in which the data operand is a constant and it is a part of the instruction itself
is known as immediate addressing mode. Normally the data must be preceded by a # sign. This addressing
mode can be used to transfer the data into any of the registers including DPTR.
xample:
MOV A, # 27 H : The data (constant) 27 is moved to the accumulator register
ADD R1,#45 H : Add the constant 45 to the contents of the accumulator
MOV DPTR ,# 8245H :Move the data 8245 into the data pointer register. MOV P1,#21 H

2. Direct addressing mode:


The addressing mode in which the data operand is in the RAM location (00 -7FH) and the address of the
data operand is given in the instruction is known as Direct addressing mode. The direct addressing mode
uses the lower 128 bytes of Internal RAM and the SFRs.
Example:
MOV R1, 42H: Move the contents of RAM location 42 into R1 register
MOV 49H,A: Move the contents of the accumulator into the RAM location 49.
ADD A, 56H: Add the contents of the RAM location 56 to the accumulator

3. Register addressing mode:


The addressing mode in which the data operand to be manipulated lies in one of the registers is
known as register addressing mode.
Example:
MOV A, R0: Move the contents of the register R0 to the accumulator ADD A, R6 :Add the contents of R6
register to the accumulator MOV P1, R2 : Move the contents of the R2 register into port 1
MOV R5, R2 : This is invalid .The data transfer between the registers is not allowed.

4. Register Indirect addressing mode:


The addressing mode in which a register is used as a pointer to the data memory block is known as
Register indirect addressing mode.
Example:
MOV A,@ R0 :Move the contents of RAM location whose address is in R0 into A (accumulator)
MOV @ R1 , B : Move the contents of B into RAM location whose address is held by R1 When R0 and
R1 are used as pointers, they must be preceded by @ sign
One of the advantages of register indirect addressing mode is that it makes accessing the data
more dynamic than static as in the case of direct addressing mode.

5.Register Specific addressing mode/ implied addressing mode:


In the implied addressing mode, there will be a single operand. These types of instruction can work on
specific registers only. These types of instructions are also known as register specific instruction.
RLA;

This is a 1-byte instruction. This instruction rotates the A register content to the left.

6.Indexed addressing mode :


This addressing mode is usedin accessing the data elements of lookup table entries located in program
ROM space of 8051.
Example : MOVC A,@ A+DPTR
The 16-bit register DPTR and register A are used to form the address of the data element stored in on-chip
ROM. Here C denotes code .In this instruction the contents of A are added to the 16-bit DPTR register to
form the 16-bit address of the data operand.

Instruction Syntax
The Structure or Syntax of the 8051 Microcontroller Assembly Language is discussed here. Each line or
statement of the assembly language program of 8051 Microcontroller consists of three fields: Label,
Instruction and Comments.
Before seeing about these three fields, let us first see an example of how a typical statement or line in an
8051 Microcontroller Assembly Language looks like.
Fig: 8051 Instruction Structure
TESTLABEL: MOV A, 24H ; THIS IS A SAMPLE COMMENT

In the above statement, the “TESTLABEL” is the name of the Label, “MOV A, 24H” is the Instruction and
the “THIS IS A SAMPLE COMMENT” is a Comment.

NOTE: The brackets for Label and Comments mean that these fields are optional and may not be used in
all statements in a program.
Label :
The Label is programmer chosen name for a Memory Location or a statement in a program. The Label part
of the statement is optional and if present, the Label must be terminated with a Colon (:).
An important point to remember while selecting a name for the Label is that they should reduce the need
for documentation.
Instruction :
The Instruction is the main part of the 8051 Microcontroller Assembly Language Programming as it is
responsible for the task performed by the Microcontroller. Any Instruction in the Assembly Language
consists of two parts: Op-code and Operand(s).

Fig: Assembly Language inside 2.


The first part of the Instruction is the Op-code, which is short for Operation Code, specifies the
operation to be performed by the Microcontroller. Op-codes in Assembly Language are called as
Mnemonics. Op-codes are in binary format (used in Machine Language) while the Mnemonic (which are
equivalent to Op-codes) are English like statements.

The second part of the instruction is called the Operand(s) and it represents the Data on which the
operation is performed. There are two types of Operands: the Source Operand and the Destination Operand.
The Source Operand is the Input of the operation and the Destination Operand is where the result is stored.

The assembly language mnemonics are in the form of op-code, such as MOV, ADD, JMP, and so
on, which are used to perform the operations.
Fig: Assembly Language Syntax
Op-codeThe op-code is a single instruction that can be executed by the CPU. Here the op-code is a MOV
instruction.

Operands The operands are a single piece of data that can be operated by the op-code. Example,
multiplication operation is performed by the operands that are multiplied by the operand.
Syntax: MUL a,b;
CommentsThe last part of the Structure of 8051 Assembly Language is the Comments. Comments are
statements included by the developer for easier understanding of the code and is used for proper
documentation of the Program.
Comments are optional and if used, they must begin with a semicolon (;) or double slash (//) depending on
the Assembler.
The following statements will show a few possible ways of using Label, Instruction and Comments

Label without instruction and comment: LABEL:


Line with Label and Instruction: LABEL: MOV A, 22H
Line with Instruction and Comment: MOV A, 22H ; THIS IS A COMMENT
Line with Label and Comment: LABEL: ; THIS IS A COMMENT
Line with only Comment: ; THIS IS A COMMENT

8051 Microcontroller Assembly Directives


Assembly Language Directives are not the instructions to the 8051 Microcontroller Assembler even though
they are written in the Mnemonic field of the program. Assembly Language Directives are actually
instructions to the Assembler and directs the Assembler Program what to do during the process of
Assembling.

The Assembly Language Directives do not have any effect on the contents of the 8051 Microcontroller
Memory (except DB and DW directives). These Directives are dependent on the Assembler Program and in
case of ASM51 Assembler, the following are the categories of Directives.
Fig:
Assembly Language Directives.
We will now see about few of the important and frequently used Assembly Language Directives. The most
useful directives are 8051 programming, such as: ORG (Set Origin)

Fig: Assembly Language Directives ORG.

The 8051 Microcontroller Assembly Language Program will start assembling from the Program Memory
Address 0000H. This is also the address from which the 8051 Microcontroller will start executing the code.

In order place the Program and Data anywhere in the Address Space of the 8051 Microcontroller, you can
use the ORG Directive.

Example:

ORG 0000H ; Tells the Assembler to assemble the next statement at 0000H
LJMP MAIN ; Code Memory at 0000H. Jump to MAIN.
ORG 000BH ; Tells the Assembler to assemble the next statement at 000BH
MAIN: NOP ; Code Memory at 000BH. MAIN starts here.
Fig: Assembly Language Directives DB
DB (Define Byte)
The DB Directive is used to define a Byte type variable. Using this directive, you can define data in
Decimal, Binary, HEX or ASCII formats. There should be a suffix of ‘B’ for binary and ‘H’ for HEX. The
ASCII Characters are placed in single quotation marks (like ‘string’).

Example:

ORG 0000H ; Tells the Assembler to assemble the next statement at 0000H
DB 10 ; Define Byte 10 (Decimal) and store at 0000H
DB 30H ; Define Byte 30 (HEX) and store at 0001H
DB ‘STRING’ ; Define String ‘STRING’ and store at 0002H to 0007H
DB 00001111B ; Define Byte 00001111 (Binary) and store at 0008H
DB 1234H ; Define Byte 34 (HEX) and store at 0009H. Only lower byte is accepted as DB can allocate
only a Byte of Memory.
DW (Define Word)
The Define Word (DW) Directive is used to include a 16-bit data in a program. The functionality
of DW is similar to that of DB except that DW generates 16-bit values.

EQU (Equate)
Using the EQU Directive, you can associate a Symbol (or Label) with a Value.

Example:

TMP EQU #30 ; Assigns the value #30 to the name TMP
RED_LED EQU P1.0 ; P1.0 is defined as RED_LED
END
The END Directive is used to stop the assembling process. This should be the last statement in the
program. END Directive cannot have a Label and the statements beyond END will not be processed by the
Assembler.

Example:

ORG 0000H
MOV A, 20H
MOV R0, #30
END
Data Type
The 8051 microcontroller contains a single data type of 8-bits, and each register is also of 8-bits size. The
programmer has to break down data larger than 8-bits (00 to FFH, or to 255 in decimal) so that it can be
processed by the CPU.

INSTRUCTION SET

8051 Microcontroller have set of instruction to perform different operations. There are five group of
instruction which are listed below.
 Arithmetic Instructions
 Logic Instructions
 Data Transfer Instructions
 Branch Instructions
 Bit Manipulation Instructions
1. ARITHMETIC INSTRUCTION:
Arithmetic instructions perform several basic operations such as addition, subtraction, division,
multiplication etc. After execution, the result is stored in the first operand.
1. ADD A,Rn;

Adds the register Rn to the accumulator


Description:

Instruction adds the register Rn (R0-R7) to the accumulator. After addition, the result is stored
in the accumulator
Before execution:
A=2Eh R4=12h
After execution:
A=40h R4=12h
2. ADD A,@Ri-
Adds the indirect RAM to the accumulator.
Ri: Register R0 or R1
Description:
Instruction adds the indirect RAM to the accumulator. Address of indirect RAM is stored in the
Ri register (R0 or R1). After addition, the result is stored in the accumulator. Register address:
R0=4Fh
Before execution:
A= 16h SUM= 33h
After execution :
A= 49h
3. ADDA,#DATA
Data: constant within 0-255 (0-FFh)
Description:
Instruction adds data (0-255) to the accumulator. After addition, the result is stored in the
accumulator.
ADD A,#33h
Before execution:

A= 16h
After execution:
A= 49h )

4. SUBB A,direct-

Subtracts the direct byte from the accumulator witha borrow


Direct: arbitrary register with address 0-255(0-FFh)
Description:
Instruction subtracts the direct byte from the accumulator with a borrow. If the higher bit is
subtracted from the lower bit then the carry flag is set. As it is direct addressing, the direct byte can be
any SFRs or general-purpose register with address 0- 7Fh. (0-127 dec.). The result is stored in the
accumulator.
SUBB A,Rx
Before execution:

A=C9h, DIF=53h, C=0


After execution:
A=76h, C=0
5. INC A - Increments the accumulator by1
Description:
This instruction increments the value in the accumulator by 1. If the accumulator includes the
number 255, the result of the operation will be 0.
Before execution:
A=E4h
After execution:
A=E5h
6. DEC A - Decrements the accumulator by1
Description: Instruction decrements the value in the accumulator by 1. If there is a 0 in the
accumulator, the result of the operation is FFh. (255 dec.)
Syntax: DEC A;

Byte: 1 (instruction code);

STATUS register flags:


No flags are affected;
Before execution:
A=E4h
After execution:
A=E3h
7. DIV AB - Divides the accumulator by the registerB
Description:

Instruction divides the value in the accumulator by the value in the B register. After division the
integer part of result is stored in the accumulator while the register contains the remainder. In case of
dividing by 1, the flag OV is set and the result of division is unpredictable. The 8-bit quotient is stored
in the accumulator and the 8-bit remainder is stored in the B register.
Before execution:
A=FBh (251dec.) B=12h (18 dec.)
After execution:
A=0Dh (13dec.) B=11h (17dec.) 13·18
+ 17 =251
8. DA A - Decimal adjust accumulator
Description: Instruction adjusts the contents of the accumulator to correspond to a BCD number after
two BCD numbers have been added by the ADD and ADDC instructions. The result in form of two 4-
digit BCD numbers is stored in the accumulator.
Before execution:
A=56h (01010110) 56BCD
B=67h (01100111)67BCD
After execution:
A=BDh (10111101)
After BCD conversion:
A=23h (00100011), C=1 (Overflow)
(C+23=123) = 56+67

9. MUL AB - Multiplies A andB


Description: Instruction multiplies the value in the accumulator with the value in the B register. The
low-order byte of the 16-bit result is stored in the accumulator, while the high byte remains in the B
register. If the result is larger than 255, the overflow flag is set. The carry flag is not affected.
Before execution:

A=80 (50h) B=160 (A0h)


After execution:

A=0 B=32h A·B=80·160=12800 (3200h)

2. LOGICAL INSTRUCTION OF 8051 MICRO-CONTROLLER


Logic instructions perform logic operations upon corresponding bits of two registers. After execution,
the result is stored in the first operand.
ANL A,Rn - AND register to the accumulator A: accumulator Rn: any R register(R0- R7)

Instruction performs logic AND operation between the accumulator and Rn register. The result is stored
in the accumulator.
Syntax:
ANL A,Rn
Before execution:
A= C3h (11000011 Bin.) R5= 55h (01010101 Bin.)
After execution:
A= 41h (01000001 Bin.)

ORL A,Rn - OR register to the accumulator Rn: any R register (R0-R7)


Instruction performs logic OR operation between the accumulator and Rn register. The result is stored
in the accumulator.
Syntax:
ORLA,Rn
Before execution:
A= C3h (11000011 Bin.) R5= 55h (01010101 Bin.)
After execution:
A= D7h (11010111 Bin.)

XRL A,Rn - Exclusive OR register to accumulator Rn: any R register (R0-R7) Instruction performs
exclusive OR operation between the accumulator and the Rn register. The result is stored in the
accumulator.
Syntax:
XRL A,Rn
Before execution:
A= C3h (11000011 Bin.) R3= 55h (01010101 Bin.)
After execution:
A= 96h (10010110 Bin.)
CLR A - Clears the accumulator A: accumulator Instruction clears the accumulator.
Syntax:
CLR A
After execution:
A=0

CPL A - Complements the accumulator

Instruction complements all the bits in the accumulator (1==>0, 0==>1). Syntax:
CPL A
Before execution:
A=(00110110)
After execution:
A=(11001001)

RL A - Rotates the accumulator one bit left A: accumulator


Eight bits in the accumulator are rotated one bit left, so that the bit 7 is rotated into the bit 0 position.
Syntax:
RL A
Before execution:
A= C2h (11000010 Bin.)
After execution:
A=85h (10000101 Bin.)

RR A - Rotates the accumulator one bit right


A: accumulator All eight bits in the accumulator are rotated one bit right so that the bit 0 is rotated into
the bit 7 position.
Syntax:
RR A
Before execution:
A= C2h (11000010Bin.)
After execution:
A= 61h (01100001Bin.)

RLC A - Rotates the accumulator one bit left through the carry flag A: accumulator
All eight bits in the accumulator and carry flag are rotated one bit left. After this operation, the bit 7 is
rotated into the carry flag position and the carry flag is rotated into the bit 0 position.
Syntax:
RLC A
Before execution:
A= C2h(11000010 Bin.) C=0
After execution:
A= 85h(10000100Bin.) C=1

RRC A - Rotates the accumulator one bit right through the carry flag A: accumulator All eight bits in
the accumulator and carry flag are rotated one bit right. After this operation, the carry flag is
rotated into the bit 7 position and the bit 0 is rotated into the carry flag position.
Syntax:
RRC A
Before execution:
A= C2h(11000010 Bin.) C=0
After execution:
A= 61h(01100001Bin.) C=0
SWAP A - Swaps nibbles within the accumulator A:
accumulator
A nibble refers to a group of 4 bits within one register (bit0-bit3 and bit4-bit7). This instruction
interchanges high and low nibbles of the accumulator.
Syntax:

SWAPA
Before execution: A=E1h (11100001)bin.
After execution:
A=1Eh (00011110)bin.

3. DATA TRANSFER INSTRUCTION OF 8051


These instructions are used to copy the content of source operand to Destination operand
MOV A,Rn - Moves the Rn register to the accumulator

The instruction moves the Rn register to the accumulator. The Rn register is not affected. Syntax
MOV A,Rn
Beforeexecution:
R3=58h
After execution:
R3=58h A=58h

MOV A,@Ri - Moves the indirect RAM to the accumulator

Instruction moves the indirectly addressed register of RAM to the accumulator. The register address is
stored in the Ri register (R0 or R1). The result is stored in the accumulator. The register is not affected.
Syntax:
MOV A,@Ri
Register Address SUM=F2h R0=F2h Before
execution:
SUM=58h
After execution:
A=58h SUM=58h

MOV A,#data - Moves the immediate data to the accumulator Instruction


moves the immediate data to the accumulator.
Syntax:
MOV A, #28
After execution:
A=28h

MOV direct,@Ri - Moves the indirect RAM to the direct byte

Instruction moves the indirectly adressed register of RAM to the direct byte. The register is not affected.

Syntax:
MOV Rx,@Ri Register
Address SUM=F3 Before
execution:
SUM=58h R1=F3
After execution:
SUM=58h TEMP=58h
MOVC A,@A+DPTR - Moves the code byte relative to the DPTR to the accumulator Instruction first
adds the 16-bit DPTR register to the accumulator. The result of addition is then used as a memory
address from which the 8-bit data is moved to the accumulator.

4. BIT Manipulation INSTRUCTIONS


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

ANL C,bit - AND direct bit to the carry flag C: Carry flag Bit: any
bit of RAM,
Instruction performs logic AND operation between the direct bit and the carry flag. Syntax:
ANL C,bit
Before execution:
ACC= 43h (01000011 Bin.)C=1
After execution:
ACC= 43h(01000011 Bin.) C=0

CLR C - clears the carry flag

C: Carry flag, Instruction clears the carry flag. After execution: C=0

CLR bit - clears the direct bit

Bit: any bit of RAM, Instruction clears the specified bit. Syntax:
CLR P0.3
Before execution:
P0.3=1 (input pin)
After execution:
P0.3=0 (output pin)

CPL bit - Complements the direct bit

Bit: any bit of RAM, Instruction complements the specified bit of RAM (0==>1, 1==>0). Syntax:
CPL P0.3
Before execution:
P0.3=1 (input pin) After
execution: P0.3=0
(output pin)

CPL C - Complements the carry flag

C: Carry flag, Instruction complements the carry flag (0==>1, 1==>0). Syntax:
CPL C
Before execution:
C=1
After execution:
C=0
MOV bit,C - Moves the carry flag to the direct bit C: Carry flag, Bit: any bit ofRAM Instruction moves
the carry flag to the direct bit. After executing the instruction, the carry flag is not affected.
Syntax:
MOVP1.2,C
After execution:
If C=0 P1.2=0 If C=1 P1.2=1
MOV C,bit - Moves the direct bit to the carryflag C: Carry flag, Bit: any bit of RAM Instruction moves
the direct bit to the carry flag. After executing the instruction, the bit is not affected.
Syntax:
MOV C, P1.4
After execution:
If P1.4=0 C=0 If P1.4=1 C=1

SETB C - Sets the carry flag

C: Carry flag, Instruction sets the carry flag. Syntax:


SETB C
After execution: C=1
SETB bit - Sets the direct bit Bit:
any bit of RAM

Instruction sets the specified bit. The register containing that bit must belong to the group of the so called
bit addressable registers.
Syntax:
SETB P0.1
Before execution:
P0.1 = 34h (00110100) pin 1 is configured as an output After
execution:
P0.1 = 35h (00110101) pin 1 is configured as an inputs

5. BRANCH INSTRUCTION OF 8051 CONTROLLER


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.
1. Program for addition of two 8-bit numbers
CLR C
MOV A,# data 1 ADD
A,# data 2 MOV
DPTR, #8500 MOVX
@ DPTR,A LOOP:
SJMP LOOP
2. Program for Subtraction Of Two 8-bit Number
CLR C
MOV A,# data 1
SUBB A,# data 2
MOV DPTR, #8500
MOVX @ DPTR,A
LOOP: SJMP LOOP

3. Program for Multiplication Of Two 8-bit Number


MOV A,# data 1
MOV B,# data 2
MUL AB
MOV DPTR, #8500
MOVX @ DPTR,A
INC DPTR
MOV A, B
MOVX @ DPTR,A
LOOP: SJMP LOOP

4. Program for Division of Two 8-bit Number


MOV A,# dividend
MOV B,# divisor
DIV AB
MOV DPTR, #8500
MOVX @ DPTR,A
INC DPTR
MOV A, B
MOVX @ DPTR,A
LOOP: SJMP LOOP
5. Write an assembly language program to perform the addition of two 16-bit numbers.
MOV R0,#34H //LOWER NIBBLE OF NO.1
MOV R1,#12H //HIGHER NIBBLE OF NO.1
MOV R2,#0DCH //LOWER NIBBLE OF NO.2
MOV R3,#0FEH //HIGHER NIBBLE OF
NO.2 CLR C
MOV A,R0
ADD A,R2
MOV 22H,A
MOV A,R1
ADDC A,R3
MOV 21H,A
MOV 00H,C
END

6. Write an assembly language program to perform the subtraction of two 16-bit numbers.
MOV R0,#0DCH //LOWER NIBBLE OF NO.1
MOV R1,#0FEH //HIGHER NIBBLE OF NO.1
MOV R2,#34H //LOWER NIBBLE OF NO.2
MOV R3,#12H //HIGHER NIBBLE OF NO.2
CLR C // MOV
A,R0 SUBB
A,R2 MOV
22H,A MOV
A,R1 SUBB
A,R3 MOV
21H,A MOV
00H,C END
7. Program to find the sum of 10 numbers stored in the array
MOV R0,#50H
MOV R2,#6
CLR A
MOV R7,A
XYZ: ADD A,@R0
JNC NEXT
INC R7
NEXT: INC R0
DJNZ R2 , XYZ
END

8. Program to find the largest number in a set


LABEL MNEMONICS
MOV DPTR,#4200

MOV 50H,#00

MOVX A,@DPTR

MOV R0,A
INC DPTR
L1 MOVX A,@DPTR
CJNE A,50H,4115
L2 INC DPTR
DJNE R0,L1
MOV A,50H
MOVX @DPTR,A
L3 SJMP L3
JC L2
MOV 50H,A
SJMP L2
Calculating Time Delay: (By Assuming XTAL Frequency as 11.0592MHz)
In Hex In Decimal
(FFFF – YYXX + 1) * 1.085us Convert YYXX values of the TH, TL
Where YYXX are TH, TL initial values registers to decimal to get a NNNNN
respectively. decimal number, then
(65536 – NNNNN) * 1.085 us

Finding the values to be loaded into the timer:

 Assuming XTAL = 11.0592MHz


1. Divide the desired time delay by 1.085us
2. Perform 65536 – n, where n is the decimal value we got from step 1
3. Convert the result of step 2 to hex, where YYXX is the initial hex value to be loaded
into the timer’s registers
4. Set TL =xx and TH =yy

Example

 Indicate which mode and which timer are selected for each of the following.
(a) MOV TMOD, #01H (b) MOV TMOD, #20H (c) MOV TMOD, #12H

Solution:

We convert the value from hex to binary. From Figure 9-3 we have:
(a) TMOD = 00000001, mode 1 of timer 0 is selected.
(b)TMOD = 00100000, mode 2 of timer 1 is selected.
(c) TMOD = 00010010, mode 2 of timer 0, and mode 1 of timer 1 are

 Find the timer’s clock frequency and its period for various 8051-based
system, with the crystal frequency 11.0592 MHz when C/T bit of TMOD is 0.
Solution:

(1/12 )× 11.0529 MHz = 921.6 MHz;


T = 1/921.6 kHz = 1.085 us selected.

 Write a program to create square wave of 50% duty cycle (with equal portions
high and low) on the P1.5 bit use Timer 0 to generate the time delay.
Solution:

MOV TMOD,#01 ;Timer 0, mode 1(16-bit


mode) HERE: MOV TL0,#0F2H ;TL0=F2H, the low
byte
MOV TH0,#0FFH ;TH0=FFH, the
high byte CPL P1.5 ;toggle P1.5
ACALL
DELAY
SJMP
HERE
DELAY:
SETB TR0 ;start the timer
0 AGAIN: JNB TF0,AGAIN ;monitor timer
flag 0
;until it rolls over
CLR TR0 ;stop timer 0
CLR TF0 ;clear timer
0 flag RET

 Write a program to continuously generate a square wave of 2 kHz frequency on


pin P1.5 using timer 1. Assume the crystal oscillator frequency to be 12 MHz.
The period of the square wave is T = 1/(2 kHz) = 500
us. Each half pulse = 250 us.
The value n for 250 us is: 250 us /1 us = 250
65536 - 250 = FF06H.
TL = 06H and TH = 0FFH.

MOV TMOD,#10 ;Timer 1,


mode 1 AGAIN: MOV TL1,#06H ;TL0 = 06H
MOV TH1,#0FFH ;TH0 = FFH
SETB TR1 ;Start timer 1
BACK: JNB TF1,BACK ;Stay until timer
rolls over CLR TR1 ;Stop timer 1
CPL P1.5 ;Complement P1.5 to get Hi, Lo
CLR TF1 ;Clear timer flag 1
SJMP AGAIN ;Reload timer

 Write a program segment that uses timer 1 in mode 2 to toggle P1.0 once
whenever the counter reaches a count of 100. Assume the timer clock is taken
from external source P3.5 (T1).
The TMOD value is 60H
The initialization value to be loaded into
TH1 is 256 - 100 = 156 = 9CH

MOV TMOD,#60h ;Counter1, mode 2, C/T’= 1


MOV TH1,#9Ch ;Counting 100 pulses
SETB P3.5 ;Make T1 input
SETB TR1 ;Start timer 1
BACK: JNB TF1,BACK ;Keep doing it if
TF = 0 CPL P1.0 ;Toggle port bit
CLR TF1 ;Clear timer overflow flag
SJMP BACK ;Keep doing it

You might also like