100% found this document useful (2 votes)
13 views

Unit 2 Combined

The document provides an overview of microcontrollers, focusing on their features, components, and the architecture of the 8051 microcontroller. It details the pin configuration, ports, and addressing modes used in programming microcontrollers. Additionally, it compares microcontrollers with microprocessors and outlines the instruction set for the 8051 microcontroller.

Uploaded by

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

Unit 2 Combined

The document provides an overview of microcontrollers, focusing on their features, components, and the architecture of the 8051 microcontroller. It details the pin configuration, ports, and addressing modes used in programming microcontrollers. Additionally, it compares microcontrollers with microprocessors and outlines the instruction set for the 8051 microcontroller.

Uploaded by

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

Unit-2

Embedded System and Internet of Things (ESIOT)

Microcontrollers and Programming

B.E.-CSE 3rd Sem.

Department of Interdisciplinary Courses in Engineering (DICE)

1
Introduction
 A microcontroller is a small computer integrated onto a single chip.
 It combines one or more CPUs (processor cores) with memory and
programmable input/output peripherals.
 Microcontrollers have contributed exponentially in the development of many
computer applications, industrial instrumentation and controls.
 The whole CPU of a computer gets fabricated on a single chip using LSI and VLSI
technology with the advancement of semiconductor technology.

2
Features of microcontrollers
On-chip peripherals
• Many microcontrollers have a variety of on-chip peripherals, such as timers, serial ports, and analog-to-
digital converters, which allow them to interface with external devices.
Memory
• Microcontrollers have both program memory, which stores the instructions that are executed by the
processor, and data memory, which is used to store variables and other data.
Input/output (I/O) pins
• Microcontrollers have a set of I/O pins that can be used to interface with external devices, such as sensors
or actuators.
Low power consumption
• Microcontrollers are designed to be low-power, which makes them suitable for use in battery-powered
devices.
Cost
• Microcontrollers are typically less expensive than general-purpose processors, as they are designed for
specific tasks and do not have as many capabilities
Size
• Microcontrollers are small, which makes them suitable for use in compact devices.

Flexibility
• Microcontrollers are highly flexible and can be programmed to perform a wide range of tasks.
3
Basic components of
microcontrollers
Basic components of
Microcontroller includes:
• CPU: Executes instructions and
performs calculations.
• Memory: Stores program and
data.
• I/O Ports: Interface with other
devices.
• Timers/Counters: Measure time
intervals or count events.
• Communication Interfaces:
Connect to other systems (e.g.,
UART, SPI, I2C).
4
Microprocessor
• A microprocessor is a processor that is contained on a
microchip, or integrated circuit (IC). It is a central
processing unit (CPU) that executes the instructions of
a computer program. Some features of
microprocessors include:
• Instruction set: Microprocessors have a specific
instruction set that defines the operations that they
can perform.
• Clock speed: The clock speed of a microprocessor
determines how fast it can execute instructions.
Microprocessors typically have high clock speeds,
which allows them to perform tasks quickly.
• Data bus: The data bus is a communication pathway
that is used to transfer data between the
microprocessor and other components in a system.
• Address bus: The address bus is a communication
pathway that is used to transfer the address of a
memory location between the microprocessor and
other components in a system.
• Cache: Many microprocessors have one or more levels
of cache, which is a small amount of high-speed
memory that is used to store frequently accessed data.
This helps to improve the performance of the
microprocessor.
5
Microprocessor vs
Microcontroller

6
Architecture of
Microcontrollers

7
Memory Organization
The 8051 microcontroller's memory is divided into Program
Memory and Data Memory.

Program Memory (ROM) is used for permanent saving


program being executed, while Data Memory (RAM) is used
for temporarily storing and keeping intermediate results and
variables.

Up to 256 bytes of internal data memory is available.

Locations available to the user occupy addressing space


from 0 to 7Fh.

The first 128 bytes of internal data memory are both directly
and indirectly addressable and the upper 128 bytes of data
memory (80-FFH) can be addressed only indirectly.

8051 can use up to 64K Bytes of external data memory with


EA=0.

8
Pin diagram of 8051 Microcontroller

• The 8051 microcontroller is a popular 8-bit


microcontroller widely used in embedded
systems. It is a single-chip microcontroller with
a Harvard architecture that includes a CPU,
RAM, ROM, and several peripherals.
• The 8051 microcontroller has a 40-pin dual in-
line package (DIP) that provides various inputs
and outputs for communication with external
devices.
• These 40 pins serve different functions like
read, write, I/O operations, interrupts etc.
• 8051 has four I/O ports wherein each port has
8 pins which can be configured as input or
output depending upon the logic state of the
pins
8051 Pin configuration
Description of Pins
• Pin 1 to Pin 8 (Port 1) – Pin 1 to Pin 8
are assigned to Port 1 for simple I/O
operations. They can be configured
as input or output pins depending on
the logic control i.e. if logic zero (0) is
applied to the I/O port it will act as
an output pin and if logic one (1) is
applied the pin will act as an input
pin. These pins are also referred to as
P1.0 to P1.7 (where P1 indicates that
it is a pin in port 1 and the number
after ‘.’ tells the pin number i.e. 0
indicates first pin of the port. So, P1.0
means first pin of port 1, P1.1 means
second pin of the port 1 and so on).
These pins are bidirectional pins.
Description of Pins
• Pin 9 (RST) – Reset pin.
It is an active-high, input pin.
Therefore if the RST pin is high
for a minimum of 2 machine
cycles, the microcontroller will
reset i.e. it will close and
terminate all activities. It is often
referred as “power-on-reset” pin
because it is used to reset the
microcontroller to it’s initial
values when power is on (high).
Description of Pins
• Pin 10 to Pin 17 (Port 3) – Pin
10 to pin 17 are port 3 pins
which are also referred to as
P3.0 to P3.7. These pins are
similar to port 1 and can be
used as universal input or
output pins. These pins are
bidirectional pins.
Description of Pins
These pins also have some additional functions which are as
follows:
• P3.0 (RXD) : 10th pin is RXD (serial data receive pin)
which is for serial input. Through this input signal
microcontroller receives data for serial communication.
• P3.1 (TXD) : 11th pin is TXD (serial data transmit pin)
which is serial output pin. Through this output signal
microcontroller transmits data for serial
communication.
• P3.2 and P3.3 (INT0′, INT1′ ) : 12th and 13th pins are
for External Hardware Interrupt 0 and Interrupt 1
respectively. When this interrupt is activated(i.e. when
it is low), 8051 gets interrupted in whatever it is doing
and jumps to the vector value of the interrupt (0003H
for INT0 and 0013H for INT1) and starts performing
Interrupt Service Routine (ISR) from that vector
location.
• P3.4 and P3.5 (T0 and T1) : 14th and 15th pin are for
Timer 0 and Timer 1 external input. They can be
connected with 16 bit timer/counter.
• P3.6 (WR’) : 16th pin is for external memory write i.e.
writing data to the external memory.
• P3.7 (RD’) : 17th pin is for external memory read i.e.
reading data from external memory.
Description of Pins
Pin 18 and Pin 19 (XTAL2 And XTAL1) –
These pins are connected to an external
oscillator which is generally a quartz
crystal oscillator. They are used to provide
an external clock frequency of 4MHz to
30MHz.
Pin 20 (GND) – This pin is connected to
the ground. It has to be provided with 0V
power supply. Hence it is connected to
the negative terminal of the power
supply.
Pin 21 to Pin 28 (Port 2) – Pin 21 to pin 28
are port 2 pins also referred to as P2.0 to
P2.7. When additional external memory is
interfaced with the 8051 microcontroller,
pins of port 2 act as higher-order address
Description of Pins
Pin 29 (PSEN) – PSEN stands for Program Store
Enable. It is output, active-low pin. This is used to
read external memory. In 8031 based system
where external ROM holds the program code, this
pin is connected to the OE pin of the ROM.
Pin 30 (ALE/ PROG) – ALE stands for Address Latch
Enable. It is input, active-high pin. This pin is used
to distinguish between memory chips when
multiple memory chips are used. It is also used to
de-multiplex the multiplexed address and data
signals available at port 0. During flash
programming i.e. Programming of EPROM, this pin
acts as program pulse input (PROG).
Pin 31 (EA/ VPP) – EA stands for External Access
input. It is used to enable/disable external memory
interfacing. In 8051, EA is connected to Vcc as it
comes with on-chip ROM to store programs. For
other family members such as 8031 and 8032 in
which there is no on-chip ROM, the EA pin is
connected to the GND.
Description of Pins
• Pin 32 to Pin 39 (Port 0) – Pin 32 to pin
39 are port 0 pins also referred to as P0.0
to P0.7. They are bidirectional
input/output pins. They don’t have any
internal pull-ups. Hence, 10 K? pull-up
registers are used as external pull-ups.
Port 0 is also designated as AD0-AD7
because 8051 multiplexes address and
data through port 0 to save pins.
• Pin 40 (VCC) – This pin provides power
supply voltage i.e. +5 Volts to the circuit.
Ports in 8051
• 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 P register. The output FE 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 .
Ports in 8051
• 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.
Addressing Modes in 8051
Method of specifying the data to be operated by the
instruction
CPU can access the data in various ways.
-- data could be in reg./Memory/immediate value

Is a way the MC to access data / operand from internal


memory /external memory (or)Register specific Ports

The use of efficient modes of a program (addressing


mode) will increase the processing speed of CPU as well
as processing time can be shortened
Addressing Modes in 8051
⚫Register Addressing Mode
⚫Direct Addressing Mode
⚫Register indirect Addressing Mode
⚫Immediate Addressing Mode
⚫Indexed Addressing mode
⚫Implied Addressing mode
Register Addressing mode
• Instruction will specify the name of the
Reg. in which data is available
• This addressing instruction involves information transfer
between registers (at least one of the R0-R7 register
involved.
• Source & Destination reg.’s match in Size:
Eg: MOV A, R0
MOV R2, A
ADD A, R5
Direct Addressing mode
⚫ The address of the data is directly specified in the instruction
 MOV A, P3 ; Transfer the contents of Port 3 to the
accumulator
 MOV A, 020H ; Transfer the contents of RAM location 20H
to the accumulator
 MOV P1, AA H ; Transfer the contents of A to Port 1
 MOV 20H, 40H ; Transfer the contents of the address 40H to
the address 20H
 ADD A, 55H ; Add the contents of A with the contents of
the address 55H

MOV R0, 40H


MOV R4, 7FH
MOV 40H, A
Register Indirect Addressing mode
 In this mode, the source or destination address is given in the register. By
using register indirect addressing mode, the internal or external addresses
can be accessed. The R0 and R1 are used for 8-bit addresses, and DPTR is
used for 16-bit addresses, no other registers can be used for addressing
purposes.
 MOV 0E5H, @R0;
 MOV @R1, 80H
 In the instructions, the @ symbol is used for register indirect addressing. In
the first instruction, it is showing that the R0 register is used. If the content of
R0 is 40H, then that instruction will take the data which is located at location
40H of the internal RAM.
Immediate Addressing mode
 In this Immediate Addressing Mode, the data is provided in the instruction
itself. The data is provided immediately after the opcode.
 MOV A, #0AFH;
 MOV R3, #45H;
 MOV DPTR, #FE00H;
Indexed Addressing mode
 In the indexed addressing mode, the source memory can only be accessed
from program memory only. The destination operand is always the register
A..
 MOVC A, @A+PC;
 MOVC A, @A+DPTR

 The C in MOVC instruction refers to code byte. For the first instruction, let us
consider A holds 30H. And the PC value is1125H. The contents of program
memory location 1155H (30H + 1125H) are moved to register A.
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.
 RL A;- rotate the accumulator left
 SWAP A;-interchanges the low-and high-order nibbles (four-bit fields) of the
Accumulator (bits 3-0 and bits 7-4)
Instruction set of 8051

• 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.
• The first part of each instruction, called MNEMONIC refers to the operation an
instruction performs (copy, addition, logic operation etc.)
• Depending on operation they perform, all instructions are divided in several
groups:
– Arithmetic Instructions
– Branch Instructions
– Data Transfer Instructions
– Logic Instructions
– Bit-oriented Instructions
• 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.
• 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.
• 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.
• Logic Instructions
• Logic instructions perform logic operations upon corresponding bits of two
registers. After execution, the result is stored in the first operand.
• Bit-oriented Instructions
• Similar to logic instructions, bit-oriented instructions perform logic operations.
The difference is that these are performed upon single bits.
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 eg: ADD A,R1
The result of addition (A+R1)
will be stored in the
accumulator.
• 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.
Data Transfer Instructions: They
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.
Logic Instructions : They
perform logic operations
upon corresponding bits of
two registers. After
execution, the result is stored
in the first operand.
• Bit-oriented Instructions:
Similar to logic instructions,
bit-oriented instructions
perform logic operations.
The difference is that these
are performed upon single
bits.
Program Status Word (PSW) register is an 8-bit register, also referred to as Flag
register.
Timers of 8051
• There are two timers in 8051, both are 16 bit timers . These are
up counters. They can count from 0000 to FFFF ( 0 to
65,535 ). However, in each timer, one register is not enough to
store 16bits, so both the timers have a total 4 SFRs, each SFR
to store 8 bits. To trigger these timers they require a clock
( either external or internal depends on many parameters ).
• As 8051 has 8-bit architecture, each of these 16 bit timers can
be accessed by two separate 8-bit registers.
• The TIMERS are divided into two 8-bit SFR called Timer LOW (TL0,
TL1) & Timer HIGH (TH0, TH1) & these registers contain the latest
count of the TIMER. The TIMER action is controlled by two
more SFR's called Timer Mode Control Register(TMOD) &
Timer/Counter Control Register (TCON).
• TMOD and TCON registers.
TCON register:

It is also bit addressable( each of 8 bits can be controlled individually ) . Out of 8 bits of TCON,
4 bits are for timer and the other 4 bits are for INT0 and INT1 related purposes.
TCON flag bits Description

TF1 Timer overflow flag for timer 1. Whenever a timer overflow event occurs in
timer 1, this flag becomes 1 and then interrupt is sent to the processor and
as the processor goes to Interrupt Service Routine (ISR), this flag
becomes zero.
TF0 Timer overflow flag for timer 0. Whenever a timer overflow event occurs in
timer 0, this flag becomes 1 and sends an interrupt to the processor and as
the processor goes to ISR, this flag becomes zero.

TR1 Timer run for timer 1. Whenever the TR1 flag is 1, it means enabling the
timer 1 ( enabling timer / counter depends on another flag of another
SFR ). Cleared to 0 by program to halt timer.
TR0 Timer run for timer 0. Whenever the TR0 flag is 1, it means enabling the
timer 0 ( enabling timer / counter depends on another flag of another
SFR ). Cleared to 0 by program to halt timer.
IE0 This stands for interrupt external for INT0. whenever this flag is HIGH, it
means an interrupt occurs at INT0.
IE1 This stands for interrupt external for INT1. whenever this flag is HIGH, it
means an interrupt occurs at INT1.
IT1 IT1 stands for interrupt type for INT1.
If IT1 = 0, then it is level triggering otherwise edge triggering.

IT0 IT0 is for interrupt type for INT0.


If IT0 = 0, then it is level triggering or else edge triggering.
TMOD register:

TMOD is an 8-bit register used to set timer mode of timer0 and timer1.
TMOD flag bits Description

GATE 1 = Enable Timer/Counter only when the INT0/INT1 pin is high and
TR0/TR1 is set.
0 = Enable Timer/Counter when TR0/TR1 is set.

C/T(Counter/Timer) Timer or Counter select bit


1 = Use as Counter
0 = Use as Timer

M0/M1 Timer/Counter mode select bit

To select modes:

M1 M0 Mode

0 0 0 (13-bit timer mode, 8-bit of THx & 5-bit of TLx)


0 1 1 (16-bit timer mode, THx cascaded with TLx)
1 0 2 (8-bit auto-reload mode, TLx reload with the
value held by THx each time TLx overflow)
1 1 3 (split timer mode) Split the 16-bit timer into two 8-bit
timers i.e. THx and TLx like two 8-bit timer
Serial Ports of 8051
• Serial transfer: In serial transfer, data is transfer to device located many meters
away this method is used for long distance data transfer. Serial communication
is mostly used for transmitting and receiving the signal. The 8051
microcontroller is consisting of Universal Asynchronous Receiver
Transmitter (UART) used for serial communication. The signals are
transmitted and received by the Rx and Tx pins of microcontroller.

•Parallel transfer: In parallel transfer, data is transferred in 8 or more lines. In


this wire conductor is used for transferring data to a device that is only a few feet
away.
Modes of Serial transmission
• There are two ways to transmit serial data: Simplex and Duplex.
• In simplex transmissions, the computer can only send data. There is only one
wire.
• If the data can be transmitted and received, then it is a duplex transmission.
Duplex transmissions can be half or full duplex depending on whether or not the
data transfer can be simultaneous
• If the communication is only one way at a time, it is half duplex.
• If both sides can communicate at the same time, it is full duplex
• Full duplex requires two wire conductors for the data lines (in addition to the
signal ground)

45
• Serial Communication can be : Asynchronous and Synchronous.
• Synchronous Communication: Transfer a block of data (characters) at a
time. The events are referenced to a clock. Example: SPI bus, I2C bus.
• Asynchronous Communication: Transfer a single byte at a time. There is
no clock. The bytes are separated by start and stop bits. Example: UART.
• The rate of data transfer in serial data communication is state as bps (bits
per second)/ baud rate.
• IC’s for serial communication : UART (Universal asynchronous receiver-
transmitter), USART (Universal synchronous-asynchronous receiver-
transmitter).
• 8051 uses SBUF (serial buffer) register to hold data, SCON (serial port
control) register to control data communication and PCON (power mode
control) register to control data rates.

46
SCON register:

It is a bit addressable register used to set the mode in which serial communication takes place in
the controller
Interrupt
• An interrupt is a signal to the processor emitted by hardware or software
indicating an event that needs immediate attention.
• Whenever an interrupt occurs, the controller completes the execution of the
current instruction and starts the execution of an Interrupt Service Routine (ISR)
or Interrupt Handler.
• ISR tells the processor or controller what to do when the interrupt occurs. The
interrupts can be either hardware interrupts or software interrupts.

Hardware Interrupt
• A hardware interrupt is an electronic alerting signal sent to the processor from an
external device, like a disk controller or an external peripheral.
• For example, when we press a key on the keyboard or move the mouse, they
trigger hardware interrupts which cause the processor to read the keystroke or
mouse position.
Software Interrupt
• A software interrupt is caused either by an exceptional condition or a special
instruction in the instruction set which causes an interrupt when it is executed
by the processor.
• For example, if the processor's arithmetic logic unit runs a command to divide a
number by zero, to cause a divide-by-zero exception, thus causing the computer
to abandon the calculation or display an error message.
What is Polling?
• The state of continuous monitoring is known as polling. The microcontroller
keeps checking the status of other devices; and while doing so, it does no other
operation and consumes all its processing time for monitoring. This problem can
be addressed by using interrupts.
• In the interrupt method, the controller responds only when an interruption
occurs. Thus, the controller is not required to regularly monitor the status (flags,
signals etc.) of interfaced and inbuilt devices.
Interrupt Service Routine (ISR)
• For every interrupt, there must be an interrupt service routine (ISR), or interrupt
handler.
• When an interrupt occurs, the microcontroller runs the interrupt service
routine.
• For every interrupt, there is a fixed location in memory that holds the address
of its interrupt service routine, ISR.
• The table of memory locations set aside to hold the addresses of ISRs is called
as the Interrupt Vector Table.
Interrupt Vector Table
There are six interrupts including RESET in 8051.
Stack, Stack Pointer and Program Counter

The stack is a section of RAM used by the CPU to store information


temporarily. The registers used to access the stack is called the Stack
Pointer (SP) register. The Stack pointer in 8051 is only 8 bit wide and can
take values of 00 to FFH. When the 8051 is powered up, the SP register
contains value 07H.

The Program Counter (PC) register keeps track of the memory location of
the current instruction, while the stack pointer (SP) register keeps track of
the top of the stack.
Steps to handle an Interrupts
There are six interrupts including RESET in 8051.
• The microcontroller closes the currently executing instruction and saves the
address of the next instruction (PC) on the stack.
• It also saves the current status of all the interrupts internally (i.e., not on the
stack).
• It jumps to the memory location of the interrupt vector table that holds the
address of the interrupts service routine.
• The microcontroller gets the address of the ISR from the interrupt vector table
and jumps to it. It starts to execute the interrupt service subroutine, which is
RETI (return from interrupt).
• Upon executing the RETI instruction, the microcontroller returns to the location
where it was interrupted. First, it gets the program counter (PC) address from
the stack by popping the top bytes of the stack into the PC. Then, it start to
execute from that address.
Enabling and Disabling the Interrupts
• Upon Reset, all the interrupts are disabled even if they are activated. The
interrupts must be enabled using software in order for the microcontroller to
respond to those interrupts.
• IE (interrupt enable) register is responsible for enabling and disabling the
interrupt. IE is a bit addressable register.
Enabling and Disabling the Interrupts
• To enable an interrupt, we take the following steps −
• Bit D7 of the IE register (EA) must be high to allow the rest of register to take
effect.
• If EA = 1, interrupts will be enabled and will be responded to, if their
corresponding bits in IE are high.
• If EA = 0, no interrupts will respond, even if their associated pins in the IE
register are high.
List of Arithmetic instructions of 8051:
• In 8051 Microcontroller there are 24 different instructions under the
Arithmetic Group.
• In total there are 64 opcodes.
• The Carry Flag (CY), Auxiliary Carry (AC) and Overflow flag (OV) are affected
based on the result of ADD, ADDC, SUBB etc. instructions.

• ADD- Addition without carry


• ADDC- ADDC stands for addition with carry
• SUB- Subtraction
• SUBB- Subtraction with Borrow
• MUL-Multiplication
• DIV-Division
Arithmetic instruction: Addition
Arithmetic instruction: Subtraction and Increment
Arithmetic instruction: Decrement, Multiplication, Division, and
Decimal Adjust
Arithmetic instruction: Few Examples
Arithmetic instruction: Few Examples
Arithmetic instruction: Few Examples
Example
Example
Example
Example
Example: ADD two number 5FH and D8H at location 20H and 21H, After adding
them, the result will be stored at location 30H and 31H. Initial Values

ORG 00H ; start at location 0


MOV A, 20H ; Move 5FH to register A
MOV R1, #30H ; set destination address 30H to R1
MOV R4, #00H ; Clear register R4 to store carry
ADD A, 21H ; Add D8 with A and store to register A
JNC SAVE ; jump if carry flag is not set
INC R4 ; Increment R4 to get carry
MOV B, R4 ; Get carry to register B
MOV @R1, B ; Store the carry first
INC R1 ; Increase R1 to point to the next address
SAVE: MOV @R1, A ; Store the result
LOOP: SJMP LOOP ; Stay in this loop
END ; end of asm source file
Example: ADD two number 5FH and D8H at location 20H and 21H, After adding
them, the result will be stored at location 30H and 31H. Initial Values
ORG 00H ;start at location 0
MOV R0, #20H;set source address 20H to R0
MOV R1, #30H;set destination address 30H to R1
MOV A, @R0 ; take the value from source to register A
MOV R5, A ; Move the value from A to R5
MOV R4, #00H; Clear register R4 to store carry
INC R0 ; Point to the next location
MOV A, @R0 ; take the value from source to register A
ADD A, R5 ;Add R5 with A and store to register A
JNC SAVE ; jump if carry flag is not set
INC R4 ; Increment R4 to get carry
MOV B, R4 ;Get carry to register B
MOV @R1, B ; Store the carry first
INC R1 ; Increase R1 to point to the next address
SAVE: MOV @R1,A ;Store the result
LOOP: SJMP LOOP ;Stay in this loop
END ;end of asm source file
Example
Example

ORG 00H ; start at location 0


MOV A,#45H ; move 45H to accumulator
CLR C ; clear carry flag
SUBB A, #30H ; A = A – 30H – Carry = 15H
MOV 80H, A ; store result in 80H memory location
LOOP: SJMP LOOP ; stop the program
Example
Example

ORG 00H ; start at location 0


MOV A, 51H ; move data stored in 51H to accumulator
CLR C ; clear carry flag
SUBB A, 50H ; A = A – Data stored in 50H - Carry
MOV 52H, A ; store result in 52H memory location
LOOP: SJMP LOOP ; stop the program
The "B" Register
The "B" register is very similar to the
Accumulator in the sense that it may hold
an 8-bit (1-byte) value. The "B" register is
used only by two 8051 instructions: MUL
AB and DIV AB. To quickly and easily
multiply or divide A by another number, you
may store the other number in "B" and
make use of these two instructions. Apart
from using MUL and DIV instructions, the
A = Multiplicand "B" register is often used as yet another
B = Multiplier temporary storage register, much like a
ninth R register.
Address of A = 0E0H
Address of B = 0F0H
Example
Example
Example

ORG 00H ; start at location 0


MOV A, 40H ; move data stored in 40H to accumulator
MOV 0F0H, 41H ; move data stored in 41H to B
MUL AB ;AxB
MOV 42H, A ; store LB in 42H memory location
MOV 43H, 0F0H ; store HB in 43H memory location
LOOP: SJMP LOOP ; stop the program
A / B = Dividend / Divisor
• Dividend is stored in A
• Divisor is stored in B
ORG 00H ; start at location 0
MOV A, 40H ; move data stored in 40H to accumulator
MOV 0F0H, 41H ; move data stored in 41H to B
DIV AB ; A/B
MOV 42H, A ; store quotient in 42H memory location
MOV 43H, 0F0H ; store remainder in 43H memory location
LOOP: SJMP LOOP ; stop the program
List of Logical instructions of 8051:
• In 8051 Microcontroller there are 25 logical instructions in 8051. AND , OR,
XOR , NOT …. These instructions come under logical instructions.
• In total there are 49 opcodes.
• The Carry Flag (CY) affects only by instruction RRC and RLC.

• ANL- AND Operation


• ORL-OR operation
• XRL- XOR Operation
• CLR-Clear
• CPL-Complement
• RL/RLC- Rotate Left
• RR/RRC- Rotate Right
• SWAP-Swap
Logical instruction: AND & OR
Logical instruction: XOR, Clear, Complement, Rotate Left and right,
and Swap
Logical Instruction: Few Examples
Logical Instruction: Few Examples
Logical Instruction: Few Examples
Logical Instruction: Few Examples
Logical Instruction: Few Examples
Logical Instruction: Few Examples
Logical Instruction: Few Examples
Thank you

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy