Eet 303 M4
Eet 303 M4
MODULE-4
SYLLABUS
d. Move the contents of a memory location to A or A to a memory location using direct and
indirect addressing
i. MOV A, 65H (Move data at RAM location 65 to A)
ii. MOV A, @R0 (Move data at RAM location addressed by R0 to A)
iii. MOV 45H, A (Move data at A to RAM location 45)
iv. MOV @R1, A (Move data at A to RAM location addressed by R1)
j. Exchange instructions
The content of source ie, register, direct memory or indirect memory will be exchanged
with the contents of destination ie, accumulator.
i. XCH A,R3 Exchange the data at A and register R3
ii. XCH A,@R1 Exchange the data at A and RAM location addressed by R1
iii. XCH A,54h Exchange the data at A and RAM location 54
k. Exchange digit. Exchange the lower order nibble of Accumulator (A0-A3) with lower
order nibble of the internal RAM location which is indirectly addressed by the register.
i. XCHD A,@R1
ii. XCHD A,@R0
2. Arithmetic instructions.
The 8051 can perform addition, subtraction. multiplication and division operations on 8 bit
numbers.
Addition
In this group, we have instructions to
i. Add the contents of A with immediate data with or without carry.
i. ADD A, #45H (A = A + 45)
ii. ADDC A, #OB4H (A = A + 45 + CY Flag)
ii. Add the contents of A with register Rn with or without carry.
i. ADD A, R5 (A = A + Data at R5)
ii. ADDC A, R2 (A = A + Data at R2+ CY Flag)
iii. Add the contents of A with contents of memory with or without carry using direct and
indirect addressing
i. ADD A, 51H (A = A + Data at RAM location 51)
ii. ADDC A, 75H (A = A + Data at RAM location 75 + CY Flag)
iii. ADD A, @R1 (A = A + Data at RAM location addressed by R1)
iv. ADDC A, @R0 (A = A + Data at RAM location addressed by R0 + CY Flag)
Subtraction
In this group, we have instructions to
i. Subtract the contents of A with immediate data with carry.
SUBB A, #45H (A = A - 45 - CY Flag)
ii. Subtract the contents of A with register Rn with carry.
SUBB A, R5 (A = A - Data at R2 - CY Flag)
i. Subtract the contents of A with contents of memory with or without carry using direct and
indirect addressing
i. SUBB A, 51H (A = A + Data at RAM location 51 - CY Flag)
ii. SUBB A, @R1 (A = A - Data at RAM location addressed by R1 - CY Flag)
Multiplication
MUL A B.
This instruction multiplies two 8 bit unsigned numbers which are stored in A and B
registers.
After multiplication the lower byte of the result will be stored in accumulator and higher
byte of result will be stored in B register.
When two BCD numbers are added, the answer is a non-BCD number sometimes. To
get the result in BCD, we use DA A instruction after the addition. DA A works as
follows.
INC A A=A+1
INC Rn Eg; INC R4; R4= R4+1
INC DIRECT Eg; INC 54; RAM location 54= RAM location 54 +1
INC @Ri Eg; INC @R3; RAM location addressed by R3 = RAM location addressed by R3
+1
INC DPTR DPTR=DPTR+1
DEC A A=A-1
DEC @Ri Eg; DEC @R3; RAM location addressed by R3 = RAM location addressed by
R3 -1
3. Logical Instructions
Logical AND
Logical NOT
RL A
Rotate left the accumulator. Each bit is shifted one location to the left, with bit 7 going to bit 0
RRC A
Rotate right through the carry. Each bit is shifted one location to the right, with bit 0 going into the
carry bit in the PSW, while the carry was at goes into bit 7
RLC A
Rotate left through the carry. Each bit is shifted one location to the left, with bit 7 going into the carry
bit in the PSW, while the carry goes into bit 0.
A jump or call instruction can replace the contents of program counter with a new
address given in the instruction that causes the program execution to begin at the code
stored at the new address.
1. Relative Jump
Jump allows only to a range of bytes within +127d or -128d from the instruction following jump.
a. Unconditional jump:
SJMP <relative address> ; Jump to the address specified unconditionally. Jump allows only to
relative range.
b. Conditional jump:
Applications that need to access the entire program memory from 0000H to
FFFFH use long absolute jump.
Jump allows to any locations among the 64 K memory.
LJMP <address> ; Jump to the address specified unconditionally. Jump allows to anywhere.
Subroutines are subprograms used to simplify the programs whenever a particular task is
to be done repeatedly.
Subroutines are handled by CALL and RET instructions
2. ACALL address
This is absolute call instruction which unconditionally calls the subroutine located at the
specified address. Call the subroutine located on the same page. Push the address of instruction
immediately after the LCALL instruction on to the stack.
RET instruction
RET instruction pops top two contents from the stack and load it to PC
6. Bit Manipulation Instructions.
8051 has 128 bit addressable memory, bit addressable SFRs and bit addressable PORT pins.
It is possible to perform following bit wise operations for these bit addressable locations.
1. LOGICAL AND
a. ANL C,BIT(BIT ADDRESS) ; ‘LOGICALLY AND’ CARRY AND CONTENT OF BIT ADDRESS, STORE RESULT IN CARRY
b. ANL C, /BIT; ; ‘LOGICALLY AND’ CARRY AND COMPLEMENT OF CONTENT OF BIT ADDRESS, STORE RESULT IN CARRY
2. LOGICAL OR
a. ORL C,BIT(BIT ADDRESS) ; ‘LOGICALLY OR’ CARRY AND CONTENT OF BIT ADDRESS, STORE RESULT IN CARRY
b. ORL C, /BIT; ; ‘LOGICALLY OR’ CARRY AND COMPLEMENT OF CONTENT OF BIT ADDRESS, STORE RESULT IN CARRY
3. CLR bit
a. CLR bit ; CONTENT OF BIT ADDRESS SPECIFIED WILL BE CLEARED.
b. CLR C ; CONTENT OF CARRY WILL BE CLEARED.
4. CPL bit
a. CPL bit ; CONTENT OF BIT ADDRESS SPECIFIED WILL BE COMPLEMENTED.
b. CPL C ; CONTENT OF CARRY WILL BE COMPLEMENTED.
The method of specifying the data to be operated by the instruction is called addressing
8051 addressing modes are classified as follows.
1. Immediate addressing.
2. Register addressing.
3. Direct addressing.
4. Indirect addressing.
5. Indexed addressing
1. Immediate addressing.
In this addressing mode the data is provided as a part of instruction itself.
In other words data immediately follows the instruction.
Eg. MOV A,#30H It means that immediate data 30h provided in instruction is moved into A register.
ADD A, #83 It means that immediate data 30h provided in instruction is added with A register.
2. Register addressing.
In this addressing mode the register will hold the data.
One of the eight general registers (R0 to R7) can be used and specified as the
operand.
Eg. MOV A,R0 Content of R0 register is copied into Accumulator.
ADD A,R6 Content of R0 register is added with Accumulator.
R0 – R7 will be selected from the current selection of register bank. The default register bank will be bank 0.
3. Direct addressing
In this mode the direct address of memory location is provided in instruction to fetch
the operand.
Only internal RAM and SFR's address can be used in this type of instruction.
Ex: MOV A, 30H => Content of RAM address 30H is copied into Accumulator.
4. Indirect addressing
Here the address of memory location is indirectly provided by a register.
The '@' sign indicates that the register holds the address of memory location i.e.
fetch the content of memory location whose address is stored in register.
Ex: MOV A,@R0 => Copy the content of memory location whose address is given in
R0 register.
5. Indexed addressing
This addressing mode is basically used for accessing data from look up table.
Here the address of memory is indexed i.e. added to form the actual address of memory.
Ex: MOVC A,@A+DPTR => here 'C' means Code. Here the content of A register is added with
content of DPTR and the resultant is the address of memory location from where the data is
copied to A register.
· The 8051 microcontroller has only one data type in assembly language programming. It is
8 bits, and the size of each register is also 8 bits.
Directives or assembler directives are instructions given to the assembler. The following are
directives used in 8051
DB (Define Byte):
· It is used to define the 8-bit data.
· When DB is used to define data, the numbers can be in decimal, binary, hex, or ASCII
formats.
Example: DATA1 DB 24 ; 1 byte of memory is assigned for variable DATA1 and set its value with
decimal 24
DATA2 DB 34H ; 1 byte of memory is assigned for variable DATA2 and set its value with hex
value 34
EQU (Equate):
· This is used to define a constant without occupying a memory location
Example:
When executing the instruction “MOV R3, #COUNT”, the register R3 will be loaded with the
value 25.
ORG (Origin):
· The ORG directive is used to indicate the beginning of the address.
· The number that comes after ORG can be either in hex or in decimal. If the number is not
followed by H, it is decimal and the assembler will convert it to hex.
END:
· Another important DIRECTIVE is the END directive.
· This indicates to the assembler the end of the source (asm) file.
· The END directive is the last line of an 8051 program, meaning that in the source code
anything after the END directive is ignored by the assembler.
Ø There are four ports P0, P1, P2 and P3 each use 8 pins, making them 8-bit ports.
Ø All the ports upon RESET are configured as output, ready to be used as output ports.
Ø To use any of these ports as an input port, it must be programmed.
Port 0:
Ø Port 0 occupies a total of 8 pins (pins 32-39) .It can be used for input or output.
Ø To use the pins of port 0 as both input and output ports, each pin must be connected externally to
a 10K ohm pull-up resistor.
To program Port0 as output port:
MOV A,#00H
BACK: MOV P0,A
ACALL DELAY
CPL A
SJMP BACK
Ø In order to make it an input, the port must be programmed by writing 1 to all the bits.
Ø In the following code, port 0 is configured first as an input port by writing 1’s to it, and then
data is received from the port and sent to P1.
MOV A,#0FFH ; A = FF hex
MOV P0,A ; initialize P0 as an input port
BACK: MOV A,P0 ;get data from P0
MOV P1,A ;send it to port 1
SJMP BACK
Port 1:
Ø Port 1 occupies a total of 8 pins (pins 1 through 8). It can be used as input or output.
Ø This port does not need any pull-up resistors since it already has pull-up resistors
internally.
Ø To make port1 an input port, it must programmed as such by writing 1 to all its bits.
Ø In the following code port1 is configured first as an input port by writing 1’s to it, then data
is received from the port and saved in R7.
MOV A,#0FFH ;A=FF HEX
MOV P1,A ;initialize P1as an input port by writing all 1’s to it
MOV A,P1 ;get data from P1
MOV R7,A ;save it in register R7
Port 2 :
Ø Port 2 occupies a total of 8 pins (pins 21- 28). It can be used as input or output.
Ø P2 does not need any pull-up resistors since it already has pull-up resistors internally.
Ø To make port2 an input port, it must programmed as such by writing 1 to all its bits.
Ø In the following code port2 is configured first as an input port by writing 1’s to it, then data
is received from the port and saved in R7.
Port 3 :
Ø Port 3 occupies a total of 8 pins, pins 10 through 17. It can be used as input or output.
Ø P3 does not need any pull-up resistors.
Ø Port 3 is configured as an output port upon reset.
Frequency=1KHz. Therefore, T=1/1KHz = 1mS. Therefore, ON Time= 500 µS and OFF Time=500 µS
ORG 000H
MOV A,#00h
MAIN MOV P1,A
ACALL DELAY
CPL A
SJMP MAIN
END
2. Signed char
Ø The signed char is an 8-bit data type that uses the most significant bit (D7 of D7 – DO) to
represent the – or + value.
Ø As a result, we have only 7 bits for the magnitude of the signed number, giving us values
from -128 to +127.
Ø Notice that if we do not use the keyword unsigned, the default is the signed value.
3. Unsigned int
Ø The unsigned int is a 16-bit data type that takes a value in the range of 0 to 65535 (0000 –
FFFFH).
Ø In the 8051, unsigned int is used to define 16-bit variables such as memory addresses.
4. Signed int
Ø Signed int is a 16-bit data type that uses the most significant bit (D15 of D15 – DO) to
represent the – or + value.
Ø As a result, we have only 15 bits for the magnitude of the number, or values from -32,768
to +32,767.
5. Sbit (single bit)
Ø The sbit keyword is a widely used 8051 C data type designed specifically to access single-
bit addressable registers.
Ø It allows access to the single bits of the SFR registers.
Ex:- Write an 8051 C program to toggle bit DO of the port PI (Pl.O) 50,000 times.
6. Bit
Ø The bit data type allows access to single bits of bit-addressable memory spaces 20 – 2FH.
7. Sfr
Ø To access the full byte of SFR registers, we use the sfr data type.
Ø 8051 has two different ways to generate time delay using C programming.
Ø The first method is simply using Loop program function in which Delay() function is made or by
providing for(); delay loop in Embedded C programming.
Ø For example- for(i=0;i<"any decimal value";i++); this is the delay for loop used in embedded C.
Program: Write an 8051C program toggle bits of Port1 continuously with 250mS delay.
#include "REG51.h"
void MSDelay(unsigned int);
void main(void)
{
while (1) //repeat forever
{
P1=0x55; // To make alternate bits of port1 0 and 1(0101 0101)
MSDelay(250);
P1=0xAA; // To invert bits of port1. (1010 1010)
MSDelay(250);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++) //this is For loop delay used to define delay value in C
for (j=0;j<1275;j++);
}
1. LEDs are connected to bits PI and P2. Write an 8051 C program that shows the count from
0 to FFH (0000 0000 to 1111 1111 in binary) on the LEDs.
Solution:
2. Write an 8051 C program to get a byte of data from PI, wait 1/2 second, and then send it to
P2.
Solution:
Bit-addressable I/O programming
1. Write an 8051 C program to toggle only bit P2.4 continuously without disturbing the rest of the bits of
P2.
Solution:
2. Write an 8051 C program to monitor bit PI.5. If it is high, send 55H to P0; otherwise, send AAH to
P2.
Solution: