unit5_8051_microcontroller presentation.pdf

tchandoo1 68 views 53 slides May 28, 2024
Slide 1
Slide 1 of 53
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53

About This Presentation

8085 Microcontroller


Slide Content

The8051
Introduction to
Programming

Overview
•Basic Assembly language
Programming
•Addressingmodes
•8051 Instruction Set
•Classification of instructions, simple
programs

Addressing Modes
•The CPU can access data in various ways. The data could be in a register, or
in memory, or provided as an immediate value.
•The various ways of accessing data are called addressing modes.
•The 8051 provides a total of 5-different addressing modes. They are
1)Immediate
2)Register
3)Direct
4)Register indirect
5)Indexed

IMMEDIATE ADDRESSING MODE
•In this addressing mode, the source operand is a constant. In immediate
addressing mode, as the name implies, when the instruction is assembled,
the operand comes immediately after the opcode.
•Examples:
•MOV A,#25H ; load 25h to A
•MOV R4,#62 ; load the decimal value 62 into R4
•MOV B,#40H ; load 40h into B
•MOV DPTR,#4521H ; DPTR=4512H
•* Note that immediate data must be preceded by the pound sign.”#”

•Although the DPTR register is 16-bit,it can also be accessed as two 8-bit
registers, DPH and DPL, where DPH is the high byte and DPL is the low
byte.
•MOV DPTR,#2550H
• is same as:
•MOV DPL,#50H
•MOV DPH,#25H

Register addressing mode
•Register addressing mode involves the use of registers to hold the data to
be manipulated.
•Examples of register addressing modes:
•MOV A,R0 ; copy the contents of R0 into A
•MOV R2,A ; copy the contents A into R2
•ADD A,R5; add the contents of R5 to the contents of A
•It should be noted that source and destination registers must match in
size.
•“MOV DPTR,A” will give an error, since the source is an 8-bit and
destination is a 16-bit register.

•*Notice that we can move data between the accumulator and Rn( for n=0
to 7) but movement of data between Rnregisters is not allowed.
•For example,
•MOV R4,R7 is invalid.
•In the first two addressing modes , the operands are either inside one of
the register or tagged along with the instruction itself.
•In most programs, the data to be processed is often in some memory
location of RAM or in the code space of ROM.
•There are many ways to access this data.

Direct addressing mode
In this mode, the data is in a RAM memory location whose address is known,
and this address is given as part of the instruction.
There are 128 bytes of RAM in the 8051. the RAM has been assigned
addresses 00h to 7Fh. The following is the summary
1.RAM locations 00-1Fhare assigned to the register banks and stack.
2.RAM locations 20-2Fhare set aside as bit-addressable space to save
single-bit data.
3.RAM locations 30-7Fhare available as place to save byte-sized data.
Direct addressing mode is most often used to access RAM locations 30-7Fh
This is due to the fact that register bank locations are accessed by the register
bank names of R0-R7

Summary of 8051 RAM

Examples of direct addressing mode
•MOV R0,40H ; save the content of RAM location 40h into R0
•MOV 56H,A ; save content of A in RAM location 56h
•MOV R4,7FH ; move contents of RAM location 7Fh into R4
•The “#” sign distinguishes between immediate and direct modes.
•RAM locations 0 to 7 are allocated to bank 0 registers R0-R7. these
registers can be accessed in two ways
•MOV A,4 ; is same as
•MOV A,R4 ; which means copy contents of R4 into A.
•MOV A,7 ; is same as
•MOV A,R7 ; which means copy contents of R7 into A

•MOV 2H,A ; is same as
•MOV R2,A ; which means copy contents of A into R2
•MOV 7H,2H; which means copy contents RAM location 2 into RAM
location 7
that is copy contents of R2 to R7 register.
•MOV 40H,60H ; copy the content of RAM location 60h to RAM location
40h.
•* Major use of direct addressing mode is the stack. In 8051, only direct
addressing mode is allowed to save data on stack memory.

Register Indirect mode
•In the register indirect addressing mode , a register is used as a pointer to
the data. If the data is inside the CPU, only registers R0 and R1 are used for
this purpose.
•In other word R2-R7 registers cannot be used to hold the address of an
operand located in RAM when using this mode.
•When R0 and R1 hold the addresses of RAM locations, they must be
preceded by the “@” sign, as shown below.
•Examples:
•MOV A,@R0 ; move contents of RAM location whose address is held by R0
into A register. If R0=35h then value at 35h is copied to A.
•MOV @R1, B; move contents of B register into RAM location whose
address is in R1.

Indexed addressing mode and on-chip ROM access
•Indexed addressing mode is widely used in accessing data elements of
look-up table entries located in the program ROM space of 8051.
•Example:
•MOVC A,@A+DPTR ; in this instruction the data is available at a location
in code memory and it is computed by adding A value and DPTR value.
Form the calculated address the data is copied to A.
in “MOVC” instruction , “C” means code memory.
MOVX A,@A+DPTR ; in this instruction the data is available in a loacation
calculated by adding A value to DPTR value.
in “MOVX” instruction ,”X” means external Data Memory.

ARITHMETIC INSTRUCTIONS OF
8051 MICROCONTROLLER

Addition of unsigned numbers
•In the 8051, in order to add numbers together, the accumulator register
(A) must be involved. The form of the ADD instruction is
•ADD A, source ; A=A + source
•The instruction ADD is used to add two operands. The destination operand
is always in register A, while the source operand can be a register,
immediate data, or in memory.
•Example
MOV A,#0F5H ; A=F5 hex
ADD A,#0BH ; A=F5+0B=00H
F5H 1111 0101
+ 0BH +0000 1011
_____ __________
100h 0000 0000

After the addition, register A (destination) contains 00 and the flags are as
follows:
CY=1 since there is a carry out from D7(MSB)
PF=1 because the number of 1s is zero
AC=1 since there is a carry from D3 to D4.
* Remember that memory –to-memory arithmetic operations are never
allowed in 8051.

ADDC and addition of 16-bit numbers
when adding two 16-bit data operands, we need to be concerned with the
propagation of carry from lower byte to higher byte. The instruction ADDC
is used on such occasions.
Syntax:
ADDC A, source ; A= A+source+CY
Example of addition of 3CE7h+3B8Dh as
1
3C E7
+ 3B 8D
________
78 74

16-bit addition using ADDC
CLR C ; make CY=0
MOV A,#0E7H ; load the lower byte now A=E7h
ADD A,#8Dh ; add the lower byte now A=74h and CY=1
MOV R6,A ; save the lower byte of the sum in R6
MOV A,#3Ch ; load the high byte
ADDC A,#3Bh ; add with the carry
; 3B+3C+1 =78(hex)
MOV R7,A ; save the high byte of sum in R7

DA instruction
The DA( decimal adjust for addition) instruction in the 8051 is provided to
correct the result of addition to BCD form.
The DA instruction will add 6 to the lower nibble or higher nibble if needed.
Otherwise, it will leave the result alone
Example:
MOV A,#47H ; A=47 first BCD operand
MOV B,#25H ; B=25 second BCD operand.
ADD A,B ; hex addition (A=6Ch)
DA A ; adjust for BCD addition (A=72)
Summary of DA action
After an ADD or ADDC instruction,

1.If the lower nibble (4 bits) is greater than 9, or if AC=1, add 0110 to the lower
4 bits.
2.If the upper nibble is greater than 9, or if CY=1, add 0110 to the upper 4 bits.
For Example, adding 29 and 18 will result in 41h which incorrect as far as BCD
addition
hex BCD
29 0010 1001
+ 18 + 0001 1000
___ _________
41 0100 0001 AC=1
+ 6 + 0110
___ __________
47 0100 0111

Subtraction of unsigned numbers
SUBB A, source ; A=A-source-CY
In the 8051 we have only SUBB. To make SUB out of SUBB, we have to make
CY=0 prior to the execution of the instruction.
The following steps are involved when SUBB instruction is executed for
unsigned numbers.
1.Take the 2’s compliment of the subtrahend(source oerand)
2.Add it to the minuend(A).
3.Invert the carry
The following example shows the subtraction of 23h from 3Fh

CLR C ; make CY=0
MOV A,#3Fh ; load 3Fh into A (A=3Fh)
SUBB A,#23h ; subtract A –23h, result is in A
Solution:
A = 3Fh 0011 11110011 1111
-23h 0010 0011 + 1101 1101 (2’s compliment)
____ __________
1C 1 0001 1100
0 CY=0 ( step 3)

Multiplication of unsigned numbers
The 8051 supports byte by byte multiplication only. The bytes are assumed to
be unsigned data. The syntax is as follows
MUL AB ; AxB, place 16-bit result in B and A
In byte by byte multiplication one of the operand must be in A and other
operand must be in B register.
After multiplication the result is in A and B registers
Example:
MOV A,#25h ; load 25h to A (A=25h)
MOV B,#65h ; load 65h to b(B=65h)
MUL AB ; 25h*65h = 0E99h where B=0Eh and A=99h.
; A=lower byte of result and B=higher byte of result.

Division of unsigned numbers
In the division of unsigned numbers, the 8051 supports byte over byte only.
The syntax is as follows,
DIV AB ; divide A by B
; result A=quotient, B=remainder
In dividing a byte by byte, the numerate must be in register A and the
denominator must be in B register.
Example:
MOV A,#95; load 95 into A
MOV B,#10; load 10 into B
DIV AB ; A/B is performed,
A=09(quotient), B=05(remainder)

Notice the following points for instruction “DIV AB”
1. The instruction is always makes CY=0 and OV=0 if the numerator is not 0.
2. If the denominator is 0( B=0), OV=1 indicates an error, and CY=0. the
standard procedure in all microcontrollers when dividing a number by 0 is
to indicate in someway the invalid result of infinity.
In the 8051, the OV flag is set to 1.

LOGIC AND COMPARE INSTRUCTIONS
LOGICAL AND:
This instruction will perform a logical AND on the two operands and place the
result in the destination.
Syntax:
ANL destination, source ; dest= destAND source
The source operand can be a register, in memory, or immediate.
Example:
MOV A,#35h ; A=35h
ANL A, #0Fh ; A= A AND 0Fh (now A=05h)
35h 0011 0101
0Fh0000 1111
___ _________
050000 0101 35h AND 0Fh = 05h

LOGICAL OR:
This instruction performs logical OR between source and destination , and the
result of OR operation is placed in destination.
Syntax:
ORL destination, source ; dest= destOR source
Example:
MOV A,#04h; A=04h
ORL A,#68h ; A= A OR 68h and A= 6C
04h 0000 0100
68h 0110 1000
___ _________
6C 0110 1100 04h OR 68h = 6Ch

LOGICAL XOR:
This instruction performs logical XOR operation between two operands, and
place the result in a destination.
Syntax:
XRL destination, source ; dest= destXOR source
Example:
MOV A,#54h ; A=54h
XRL A,#78h ; A = A XOR 78h, A=2Ch(result)
54h 0101 0100
78h 0111 1000
___ _________
6Ch 0110 110054h XOR 78h = 2Ch

COMPLIMENT ACCUMULATOR
This instruction complements the contents of register A. the compliment
action changes the 0s to 1s and 1s to 0s. This is also called 1’s compliment
Syntax:
CPL A ; A= compliment of A
Example: MOV A,#55h ; A=55h
CPL A ; A= 1’s compliment of 55h = Aah(result).
Example: find 2’s compliment of value 85h.
MOV A,#85h ; A=85h
CPL A ; A=1’s compliment of A
ADD A,#1 ; A= 1’s compliment+ 1= 2’s compliment
85h = 1000 0101
1’s = 0111 1010
+1
_________
0111 1011 = 7Bh

Compare instruction
The 8051 has an instruction for the compare operation.
Syntax:
CJNE destination, source, relative address
In the 8051, the actions of comparing and jumping are combined into a single
instruction called CJNE( compare and jump if not equal).
The CJNE instruction compares two operands, and jumps if they are not equal.
Example: a) will it jump to NEXT? B) what is in A after CJNE is executed?
MOV A,#55h ;
CJNE A,#99h, NEXT
-----
NEXT:------
Answer: a) Yes, it jumps because 55h and 99h are not equal
b) A = 55h, its original value before the comparision.

ROTATE AND SWAP INSTRUCTIONS
Rotating the bits of A right and left
Syntax: RR A ; rotate the right A by one time
In rotate right, the 8 bits of the
Accumulator are rotated right one
Bit, bit LSB exits and enters into MSB
Example: MOV A,#36h ; A= 0011 0110
RR A ; A= 0001 1011
RR A ; A= 1000 1101
RR A ; A= 1100 0110
RR A ; A= 0110 0011
MSB LSB

ROTATE AND SWAP INSTRUCTIONS
Rotating the bits of A right and left
Syntax: RL A ; rotate the left A by one time
In rotate left, the 8 bits of the
Accumulator are rotated leftone
Bit, bit MSB exits and enters into LSB
Example: MOV A,#72h ; A= 0111 0010
RL A ; A= 1110 0100
RL A ; A= 1100 0100
Note that RR and RL instructions do not affect any flags.
MSB LSB

ROTATE AND SWAP INSTRUCTIONS
Rotating through the carry
Syntax: RRC A ; rotate the right A by one time with carry
In RRC A , the 8 bits are rotated
From left to right, they exit LSB
To the carry flag, and CY enters MSB.
Example: CLR C ; make CY=0
MOV A,#26h ; A= 0010 0110
RRC A ; A= 0001 0011 and CY=0
RRC A ; A= 0000 1001 and CY=1.
Note that RR and RL instructions do not affect any flags.
MSB LSB
C
Y

ROTATE AND SWAP INSTRUCTIONS
Rotating the bits of A right and left
Syntax: RL C A ; rotate the left A by one time with carry
In RLC A, the 8 bits of are
Rotated left with carry, MSB is
Moved to CY and CY is moved to LSB
Example: SETB C; CY=1
MOV A,#15h ; A= 0001 0101
RLC A ; A= 0010 1011 and CY=0
RLC A ; A= 0101 0110 and CY=0
MSB LSB
C
Y

SWAP A:
another useful instruction is SWAP. It works only with accumulator (A).
It swaps the lower nibble and higher nibble.
before:
after :
Example: MOV A,#72h; A=72h
SWAP A ; A=27h
72h= 0111 0010
27h= 0010 0111
D7-D4D3-D0
D3-D0D7-D4

JUMP, LOOP AND CALL INSTRUCTIONS
Looping in the 8051:
Repeating a sequence of instructions a certain number of times is called a loop.
The loop action is performed by the instruction “DJNZ reg,label”.
in this instruction, the register is decremented; if it is not zero, it jumps to the target
address refereed to by the label.
Prior to the start of loop the register is loaded with the counter for the number of
repetitions.
Example : write a program to a) clear ACC,thenb) add 3 to accumulator ten times.
MOV A,#0; A=0, clear ACC
MOV R2,#10 ; load counter R2=10
AGAIN:ADD A,#03 ; add 03 to A
DJNZ R2,AGAIN; repeat until R2=0(10 times)
MOV R5,A

Conditional jump instructions
Conditional jump instructions jump to the target address only if a certain
condition is met.
Instruction Action
JZ Jump if A=0
JNZ Jump if A≠0
DJNZ decrement and jump if A≠0
CJNE A,byte jump if A≠0
CJNE reg,#data jump if byte ≠ #data
JC jump if CY = 1

Conditional jump instructions
Conditional jump instructions jump to the target address only if a certain
condition is met.
Instruction Action
JNC Jump if CY=0
JB Jump if bit=1
JNB Jump if bit = 0
JBC Jump if bit =1 and clear bit

JZ (jump if A =0)
in this instruction the content of register A is checked. If it is zero,itjumps
to the target address.
Example: MOV A,R0; A=R0
JZ OVER ; jump if A=0
MOV A,R1 ; A=R1
JZ OVER
-----
OVER:-------
In this program if either R0 or R1 is zero, it jumps to the label OVER.

Unconditional jump instructions
The unconditional jump is a jump in which control is transferred
unconditionally to the target location.
There are two unconditional jump instructions
LJMP( long jump)
LJMP is an unconditional long jump. It is a 3-byte instruction
in which the first byte is the opcode, and the second and third bytes
represent the 16-bit address of the target location.
The 2-byte target address allows a jump to any memory location from 0000h
to FFFFh

SJMP (short jump)
in this 2-byte instruction, the first byte is the opcodeand
the second byte is the relative address of target location.
The relative address range of 00h-FFh is divided into forward and backward
jumps.
That is within -128 to +127 bytes of memory relative to the address of the
current PC(program counter)
While in the case of forward jump, the displacement value is positive
between 0 to 127( 00h to 7Fh).
For the backward jump, the displacement value is negative between 0 to
-128(80h to FFh)

CALL INSTRUCTIONS
CALL instruction is used to call a subroutine. Subroutines are often used to
need to be performed frequently.
There are two call instructions: LCALL and ACALL
LCALL (long call)
it is a 3-byte instruction, the first byte is the opcodeand the
second and third bytes are used for the theaddress for the target
subroutine.
Therefore LCALL can be used to call subroutines loacatedanywhere within the
64K byte address space of the 8051.
To make sure that after execution of the called subroutine the 8051 knows
where to come back to, it automatically saves on stack the address of the
instruction immediately below the LCALL.

ACALL (absolute call)
ACALL is a 2-byte instruction, since ACALL is a 2-byte
instruction, the target address of the subroutine must be within 2K bytes
address because only 11 bits of the 2-bytes (16-bits) are used for the
address.
There is no difference between ACALL and LCALL in terms of saving the
program counter on the stack or the function of the RWT instruction.
The only difference is that the target address for LCALL can be anywhere
within the 64K byte address space of the 8051 while the target address of
ACALL must be within 2K-byte range

8051 simple programs

Write a program to subtract 1296h from 2762h
Program:
CLR C ; CY=0
MOV A,#62h ; A=62h
SUBB A,#96h; A=62h-96h-0=CChwith CY=1
MOV R7,A ; save the lower byte of result
MOV A,#27h ; A=27h
SUBB A,#12h; A=27h-12h-1 = 14h
MOV R6,A ; save the higher byte of result
27 62h
12 96h
-______
14 CC R6=14 and R7=CCh

Assume that RAM locations 40-44 have five numbers. Write
a program to find the sum of the values
RAM location 40= (7D), 41=(EB), 42=(C5), 43= (5B) and 44= (30) values.
Program: MOV R0,#40h; load R0 as pointer to 40h location
MOV R2,#5 ; load 5 into R2, R2 is counter
CLR A ; A=0
MOV R7,A ; R7=0
AGAIN: ADD A,@R0; add the first number to A
JNC NEXT ; if CY=0, go to NEXT and add one more
INC R7 ; if CY=1, above condition is not met, increment R7
NEXT: INC R0 ; increment address by 1
DJNZ R2,AGAIN ; repeat until count becomes zero, all numbers are
added
SJMP ; end the program

Write a program to convert 9Ch to decimal using division
Program: 9C=156(in decimal) (answer:r0=6,r1=5,r2=1)
mova,#9ch ; a=9ch
movb,#0ah ; b=0ah( 10 in decimal)
div ab ; a/b ( 9ch/0ah) quotient =0fh(15 in decimal)
; remainder 6, A=quotient, B=remainder
movr0,b ; save the first digit 6 into r0
movb,#0ah ; load again 10 into B for one more division
div ab ; a/b( now 0fh/0ah) i.e15/10 ( in decimal)
; A=1(quotient), B=5(remainder)
movr1,b ; save the second digit into r1
movr2,a ; save the third digit into r2
sjmp ; end the program

Assume internal RAM memory locations 40h-44h contain the daily
temperature for five days. Search if any of the values equals 65. if the 65 does
not exist in the list, give its location to R4. otherwise make R4=0
Given temperatures: 40h=(76), 41h=(79), 42h=(69), 43h=(65), 44h=(62)
Program: MOV R4,#0 ;make R4=0
MOV R0,#40h; load pointer to R0 for first location
MOV R2,#05 ;load 5 into R2 as counter value
MOV A,#65 ; value to be searched is loaded in A
BACK: CJNE A,@R0, NEXT; compare RAM data with 65 in A
MOV R4,R0 ; if 65 is present save the address to R4
SJMP EXIT ; now exit the program
NEXT: INC R0 ; otherwise increment address
DJNZ R2,BACK; decrement counter for remaining
; values of temperatures
EXIT: ------------- ; stop the program

Write s program to find number of 1’s in 97h
Program:
MOV R1,#0 ; R1=0, it keeps track of no of 1’s
MOV R7,#8 ;counter =08, rotate 8 times
MOV A,#97h ;find the number of 1’s in 97h.
AGAIN: RLC A ;rotate it through the CY once
JNC NEXT ; check for CY=0
INC R1 ; if CY=1, the add 1 to R1
NEXT: DJNZ R7, AGAIN; repeat for remaining bits.
Answer: 97h= 10010111, no of ones =R1=5

Write a program to ASCII to packed BCD conversion
Decimal ASCII Unpacked BCD Packed BCD
4 34 00000100
7 37 00000111 01000111 or 47
Program:
MOV A,# ’4’; A=34h, hex for ASCII char 4
MOV R1,# ‘7’; R1=37h, hex for ASCII char 7
ANL A,#0Fh ; mask upper nibble (A=04)
ANL R1,# 0Fh; mask upper nibble(R1= 07)
SWAP A ; A=40h
ORL A,R1 ; A=47 packed BCD

Write a program to add 897F9Ah to 34BC48h and save the
result in RAM locations starting at 40h
Program:
MOV A,#9Ah; save 9A to A reg
ADD A, #48h ; first byte addition 9Ah+48h
MOV 40h,A ; save the lower byte of result in 40h
MOV A,#7Fh ; save the second byte of no to A
ADDC A,# BCh ; add the second byte with carry, 7F+BC+CY
MOV 41h,A ; save the second byte of result
MOV A,#89h ; save the higher byte of first no in A
ADDC A,#34h ; add the higher byte of second no
MOV 42h,A ; save the higher byte of result
ans 89 7F 9A
+ 34 BC 48
__1_______
BD 3B D2 40h=(D2), 41h=(3B), 42h(BD)

Write a program to a) write the value 55h to RAM locations 40h-
4Fh, and b) add all these numbers together, and save the result in
RAM locations 60h and 61h
Program:
MOV R0,#40h ; load R0 to point the 40h location
MOV R1,#10h ; load to counter in R1 reg
MOV A,#55h ; copy the value 55h to A
MOV R2,#0 ; R2=0 used for high byte of result
MOV @R0,A ;copy 55h to RAM location 40h
AGAIN: INC R0 ; increment the RAM location value
ADD A,@R0 ; add the number to A
JNC NEXT; check for CY, if CY=0 add next no
INC R2 ; if CY=1, store it in R2
NEXT:DJNZ R1,AGAIN ; decrement the count value and copy 55h
MOV 60h,R2 ; save higher byte of result
MOV 61h,A ; save lower byte of result
SJMP ; end the program
RESULT:60h=(05), 61h=(50)
Tags