Module 1
Module 1
EMBEDDED SYSTEM
• Reference
Shibu K V , Introduction to embedded systems, Tata Mcgraw Hill,
(2009)
• Based on triggering
• Program memory
• Data Memory
• Computational Engine
• I/O Unit
1. Write a program to add the values of locations 50H and 51H and store the result in
locations in 52h and 53H.
ADDRESSING MODES
Various methods of accessing the data are called addressing modes.
8051 addressing modes are classified as follows.
1. Immediate addressing.
2. Register addressing.
3. Direct addressing.
4. Indirect addressing.
5. Relative addressing.
6. Absolute addressing.
7. Long addressing.
8. Indexed addressing.
9. Bit inherent addressing.
10. Bit direct addressing.
6. Relative Addressing.
Relative addressing is used only with conditional jump instructions. The relative address,
(offset), is an 8 bit signed number, which is automatically added to the PC to make the
address of the next instruction. The 8 bit signed offset value gives an address range of +127
to —128 locations. The jump destination is usually specified using a label and the assembler
calculates the jump offset accordingly. The advantage of relative addressing is that the
program code is easy to relocate and the address is relative to position in the memory.
Eg. SJMP LOOP1
JC BACK
7. Absolute addressing
Absolute addressing is used only by the AJMP (Absolute Jump) and ACALL (Absolute Call)
instructions. These are 2 bytes instructions. The absolute addressing mode specifies the
lowest 11 bit of the memory address as part of the instruction. The upper 5 bit of the
destination address are within the current 2 Kbyte page of the program memory.
Eg. AJMP LOOP1
ACALL LOOP2
8. Long Addressing
The long addressing mode is used with the instructions LJMP and LCALL. These are 3-byte
instructions. The address specifies a full 16-bit destination address so that a jump or a call can
be made to a location within a 64 Kbyte code memory space.
Eg. LJMP FINISH
LCALL DELAY
9. Bit Inherent Addressing
In this addressing, the address of the flag which contains the operand, is implied in the
opcode of the instruction.
Eg. CLR C; Clears the carry flag to 0
10. Bit Direct Addressing
In this addressing mode the direct address of the bit is specified in the instruction. The RAM
space 20H to 2FH and most of the special function registers are bit addressable. Bit address
values are between 00H to 7FH.
Eg. CLR 07h; Clears the bit 7 of 20h RAM space
SETB 07H; Sets the bit 7 of 20H RAM space.
INSTRUCTION SET.
The 8051 internal operations and external read/write operations are controlled by the
oscillator clock. T-state, Machine cycle and Instruction cycle are terms used in instruction
timings.
T-state is defined as one subdivision of the operation performed in one clock period.
The terms 'Tstate' and 'clock period' are often used synonymously.
Machine cycle is defined as 12 oscillator periods. A machine cycle consists of six
states and each state lasts for two oscillator periods. An instruction takes one to four
machine cycles to execute an instruction.
Instruction cycle is defined as the time required for completing the execution of an
instruction. The 8051-instruction cycle consists of one to four machine cycles.
Eg. If 8051 microcontrollers are operated with 12 MHz oscillator, find the execution
time for the following four instructions.
1. ADD A, 45H.
2. SUBB A, #55H
3. MOV DPTR, #2000H
4. MUL AB
Since the oscillator frequency is 12 MHz, the clock period is, Clock period = 1/12
MHz = 0.08333 µS. Time for 1 machine cycle = 0.08333 µS x 12 =1 µS.
8051 Instructions
The instructions of 8051 can be broadly classified under the following headings.
1. Data transfer instructions
2. Arithmetic instructions
3. Logical instructions
4. Branch instructions
5. Subroutine instructions
6. Bit manipulation instructions
Data transfer instructions.
In this group, the instructions perform data transfer operations of the following types.
a. Move the contents of a register Rn to A
i. MOV A, R
ii. MOV A, R7
b. Move the contents of a register A to Rn
i. MOV R4,A
ii. MOV R1,A
c. Move an immediate 8 bit data to register A or to Rn or to a memory location(direct or
indirect)
i. MOV A, #45H
ii. MOV R6, #51H
iii. MOV 30H, #44H
iv. MOV @R0, #0E8H
v. MOV DPTR, #0F5A2H
vi. MOV DPTR, #5467H
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
ii. MOV A, @R0
iii. MOV 45H, A
iv. MOV @R1, A
e. Move the contents of a memory location to Rn or Rn to a memory location using
direct addressing
i. MOV R3, 65H
ii. MOV 45H, R2
f. Move the contents of memory location to another memory location using direct and
indirect addressing
i. MOV 47H, 65H
ii. MOV 45H, @R0
g. Move the contents of an external memory to A or A to an external memory
i. MOVX A, @R1
ii. MOVX @R0, A
iii. MOVX A, @DPTR
iv. MOVX @DPTR, A
h. Move the contents of program memory to A
i. MOVC A, @A+PC
ii. MOVC A, @A+DPTR
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
ii. XCH A,@R1
iii. XCH A,54h
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
Examples
1. Load the external memory address pointer DPTR with 0x1000. Show an
alternate solution in which 8 bit operands are used.
2. How can a byte be transferred from the address pointed by R0 to the register
R2? There is no MOV @Ri, Rn instruction available.
4. Using DPTR, transfer the codes from 1000H and 10001H to addresses 7EH
and 7FH in RAM.
5. Using DPTR, transfer the codes from 1000H and 10001H to external data
memory addresses 1000H and 1001H.
1. Write a program to add the values of locations 50H and 51H and store the result in
locations in 52h and 53H.
ADDRESSING MODES
Various methods of accessing the data are called addressing modes.
8051 addressing modes are classified as follows.
1. Immediate addressing.
2. Register addressing.
3. Direct addressing.
4. Indirect addressing.
5. Relative addressing.
6. Absolute addressing.
7. Long addressing.
8. Indexed addressing.
9. Bit inherent addressing.
10. Bit direct addressing.
6. Relative Addressing.
Relative addressing is used only with conditional jump instructions. The relative address,
(offset), is an 8 bit signed number, which is automatically added to the PC to make the
address of the next instruction. The 8 bit signed offset value gives an address range of +127
to —128 locations. The jump destination is usually specified using a label and the assembler
calculates the jump offset accordingly. The advantage of relative addressing is that the
program code is easy to relocate and the address is relative to position in the memory.
Eg. SJMP LOOP1
JC BACK
7. Absolute addressing
Absolute addressing is used only by the AJMP (Absolute Jump) and ACALL (Absolute Call)
instructions. These are 2 bytes instructions. The absolute addressing mode specifies the
lowest 11 bit of the memory address as part of the instruction. The upper 5 bit of the
destination address are within the current 2 Kbyte page of the program memory.
Eg. AJMP LOOP1
ACALL LOOP2
8. Long Addressing
The long addressing mode is used with the instructions LJMP and LCALL. These are 3-byte
instructions. The address specifies a full 16-bit destination address so that a jump or a call can
be made to a location within a 64 Kbyte code memory space.
Eg. LJMP FINISH
LCALL DELAY
9. Bit Inherent Addressing
In this addressing, the address of the flag which contains the operand, is implied in the
opcode of the instruction.
Eg. CLR C; Clears the carry flag to 0
10. Bit Direct Addressing
In this addressing mode the direct address of the bit is specified in the instruction. The RAM
space 20H to 2FH and most of the special function registers are bit addressable. Bit address
values are between 00H to 7FH.
Eg. CLR 07h; Clears the bit 7 of 20h RAM space
SETB 07H; Sets the bit 7 of 20H RAM space.
INSTRUCTION SET.
The 8051 internal operations and external read/write operations are controlled by the
oscillator clock. T-state, Machine cycle and Instruction cycle are terms used in instruction
timings.
T-state is defined as one subdivision of the operation performed in one clock period.
The terms 'Tstate' and 'clock period' are often used synonymously.
Machine cycle is defined as 12 oscillator periods. A machine cycle consists of six
states and each state lasts for two oscillator periods. An instruction takes one to four
machine cycles to execute an instruction.
Instruction cycle is defined as the time required for completing the execution of an
instruction. The 8051-instruction cycle consists of one to four machine cycles.
Eg. If 8051 microcontrollers are operated with 12 MHz oscillator, find the execution
time for the following four instructions.
1. ADD A, 45H.
2. SUBB A, #55H
3. MOV DPTR, #2000H
4. MUL AB
Since the oscillator frequency is 12 MHz, the clock period is, Clock period = 1/12
MHz = 0.08333 µS. Time for 1 machine cycle = 0.08333 µS x 12 =1 µS.
8051 Instructions
The instructions of 8051 can be broadly classified under the following headings.
1. Data transfer instructions
2. Arithmetic instructions
3. Logical instructions
4. Branch instructions
5. Subroutine instructions
6. Bit manipulation instructions
Data transfer instructions.
In this group, the instructions perform data transfer operations of the following types.
a. Move the contents of a register Rn to A
i. MOV A, R
ii. MOV A, R7
b. Move the contents of a register A to Rn
i. MOV R4,A
ii. MOV R1,A
c. Move an immediate 8 bit data to register A or to Rn or to a memory location(direct or
indirect)
i. MOV A, #45H
ii. MOV R6, #51H
iii. MOV 30H, #44H
iv. MOV @R0, #0E8H
v. MOV DPTR, #0F5A2H
vi. MOV DPTR, #5467H
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
ii. MOV A, @R0
iii. MOV 45H, A
iv. MOV @R1, A
e. Move the contents of a memory location to Rn or Rn to a memory location using
direct addressing
i. MOV R3, 65H
ii. MOV 45H, R2
f. Move the contents of memory location to another memory location using direct and
indirect addressing
i. MOV 47H, 65H
ii. MOV 45H, @R0
g. Move the contents of an external memory to A or A to an external memory
i. MOVX A, @R1
ii. MOVX @R0, A
iii. MOVX A, @DPTR
iv. MOVX @DPTR, A
h. Move the contents of program memory to A
i. MOVC A, @A+PC
ii. MOVC A, @A+DPTR
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
ii. XCH A,@R1
iii. XCH A,54h
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
Examples
1. Load the external memory address pointer DPTR with 0x1000. Show an
alternate solution in which 8 bit operands are used.
MOV DPTR, #1000H
Alternate Solution
MOV DPH, #10H
MOV DPL, #00H
2. How can a byte be transferred from the address pointed by R0 to the register
R2? There is no MOV @Ri, Rn instruction available.
Byte pointed by R0 can be moved into A and then to R2.
MOV A, @R0
MOV R2, A
3. Assuming the PC to be 1000H, transfer the code at 1000H to the internal
RAM at 70H.
First , we load A with 00H. Then , we can use the MOVC instruction to get
the code into A. From A, we can load it into 70H.
MOV A, #00H;
MOVC A, @A+PC;
MOV 70H, A;
4. Using DPTR, transfer the codes from 1000H and 1001H to addresses 7EH
and 7FH in RAM.
MOV DPTR, 1000H;
MOV A, #00H;
MOVC A, @A+DPTR;
MOV 7EH, A; // to read the code at 1000H into 7EH.
//With DPTR already loaded, move 01H into A and use MOVC instruction
again and move A to 7FH.
MOV A, #01H;
MOVC A, @A+DPTR;
MOV 7FH, A; // to read the code at 1001H into 7FH.
5. Using DPTR, transfer the codes from 1000H and 1001H to external data
memory addresses 1000H and 1001H.
MOV DPTR, 1000H;
MOV A, #00H;
MOVC A, @A+DPTR; //to read the code at 1000H into A.
//DPTR is already loaded. From A, we can store byte into external data
memory at 1000H because DPTR= 1000H and we can use
MOVX @DPTR, A;
//Now ,for next code save at the next address , we can perform the
operations as:
MOV DPTR, 1001H;
MOV A, #00H;
MOVC A, @A+DPTR;
MOVX @DPTR, A; //to read the code at 1001H and write into data
memory 1001H.
6. Assume that R0 to R7 have the array values in the ascending order.
Rearrange these in the descending order.
We can use the LIFO property of the push and pop stack operations as
follows:-
PUSH 00H;
PUSH 01H;
PUSH 02H;
PUSH 03H;
PUSH 04H;
PUSH 05H;
PUSH 06H;
PUSH 07H;
POP 00H; // The largest value is stored in R0
POP 01H;
POP 02H;
POP 03H;
POP 04H;
POP 05H;
POP 06H;
POP 07H;
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
ii. ADDC A, #OB4H
i. Add the contents of A with register Rn with or without carry.
i. ADD A, R5
ii. ADDC A, R2
iii. Add the contents of A with contents of memory with or without carry using direct and
indirect addressing
i. ADD A, 51H
ii. ADDC A, 75H
iii. ADD A, @R1
iv. ADDC A, @R0
CY, AC and OV flags will be affected by this operation.
Subtraction
In this group, we have instructions to
i. Subtract the contents of A with immediate data with or without carry.
i. SUBB A, #45H
ii. SUBB A, #OB4H
ii. Subtract the contents of A with register Rn with or without carry.
i. SUBB A, R5
ii. SUBB A, R2
iii. Subtract the contents of A with contents of memory with or without carry using direct
and indirect addressing
i. SUBB A, 51H
ii. SUBB A, 75H
iii. SUBB A, @R1
iv. SUBB A, @R0
CY AC and OV flags will be affected by this operation.
Multiplication
MUL AB.
This instruction multiplies two 8 bit unsigned numbers which are stored in A and B register.
After multiplication the lower byte of the result will be stored in accumulator and higher byte
of result will be stored in B register.
Eg. MOV A,#45H ;[A]=45H
MOV B,#0F5H ;[B]=F5H
MUL AB ;[A] x [B] = 45 x F5 = 4209 ;[A]=09H, [B]=42H
Division
DIV AB.
This instruction divides the 8 bit unsigned number which is stored in A by the 8 bit unsigned
number which is stored in B register. After division the result will be stored in accumulator
and remainder will be stored in B register.
Eg. MOV A,#45H ;[A]=0E8H
MOV B,#0F5H ;[B]=1BH
DIV AB ;[A] / [B] = E8 /1B = 08 H with remainder 10H ;[A] = 08H, [B]=10H
Eg 1: MOV A, #23H
MOV R1,#55H
ADD A,R1 // [A]=78
DA A // [A]=78 no changes in the accumulator after da a
Eg 2: MOV A,#53H
MOV R1,#58H
ADD A,R1 // [A]=ABh
DA A // [A]=11, C=1 . ANSWER IS 111. Accumulator data is changed after DA A
Increment: increments the operand by one.
INC A INC Rn INC DIRECT
INC @Ri INC DPTR
INC increments the value of source by 1. If the initial value of register is FFh, incrementing
the value will cause it to reset to 0. The Carry Flag is not set when the value "rolls over"
from 255 to 0.
In the case of "INC DPTR", the value two-byte unsigned integer value of DPTR is
incremented. If the initial value of DPTR is FFFFh, incrementing the value will cause it to
reset to 0.
Decrement: decrements the operand by one.
DEC A DEC Rn
DEC DIRECT DEC @Ri
DEC decrements the value of source by 1. If the initial value of is 0, decrementing the value
will cause it to reset to FFh. The Carry Flag is not set when the value "rolls over" from 0 to
FFh.
Logical Instructions
Logical AND
ANL destination, source:
ANL does a bitwise "AND" operation between source and destination, leaving the resulting
value in destination. The value in source is not affected. "AND" instruction logically AND
the bits of source and destination.
ANL A,#DATA ANL A, Rn ANL A,DIRECT
ANL A,@Ri ANL DIRECT,A ANL DIRECT, #DATA
Logical OR
ORL destination, source:
ORL does a bitwise "OR" operation between source and destination,leaving the resulting
value in destination. The value in source is not affected. " OR " instruction logically OR the
bits of source and destination.
ORL A,#DATA ORL A, Rn
ORL A,DIRECT ORL A,@Ri
ORL DIRECT,A ORL DIRECT, #DATA
Logical Ex-OR
XRL destination, source:
XRL does a bitwise "EX-OR" operation between source and destination, leaving the
resulting value in destination. The value in source is not affected. " XRL " instruction
logically EX-OR the bits of source and destination.
XRL A, #DATA XRL A,Rn
XRL A, DIRECT XRL A,@Ri
XRL DIRECT, A XRL DIRECT, #DATA
Logical NOT
CPL complements operand, leaving the result in operand. If operand is a single bit
then the state of the bit will be reversed. If operand is the Accumulator then all the bits in the
Accumulator will be reversed.
CPL A,
SWAP A – Swap the upper nibble and lower nibble of A.
Rotate Instructions
RR A
This instruction is rotate right the accumulator. Its operation is illustrated below. Each bit is
shifted one location to the right, with bit 0 going to bit 7.
RL A
Rotate left the accumulator. Each bit is shifted one location to the left, with bit 7 going to bit0
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.
Examples
1. What will be the effect of RRC A, assuming A = 02H and C = 1?
6. Write a program to subtract a 16- bit number stored at locations 51H-52H from 55H-
56H and store the result in locations 40H and 41H. Assume that the least significant
byte of data or the result is stored in low address. If the result is positive, then store
00H, else store 01H in 42H.
7. Write a program to add two Binary Coded Decimal (BCD) numbers stored at
locations 60H and 61H and store the result in BCD at memory locations 52H and
53H. Assume that the least significant byte of the result is stored in low address.
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
ii. ADDC A, #OB4H
i. Add the contents of A with register Rn with or without carry.
i. ADD A, R5
ii. ADDC A, R2
iii. Add the contents of A with contents of memory with or without carry using direct and
indirect addressing
i. ADD A, 51H
ii. ADDC A, 75H
iii. ADD A, @R1
iv. ADDC A, @R0
CY, AC and OV flags will be affected by this operation.
Subtraction
In this group, we have instructions to
i. Subtract the contents of A with immediate data with or without carry.
i. SUBB A, #45H
ii. SUBB A, #OB4H
ii. Subtract the contents of A with register Rn with or without carry.
i. SUBB A, R5
ii. SUBB A, R2
iii. Subtract the contents of A with contents of memory with or without carry using direct
and indirect addressing
i. SUBB A, 51H
ii. SUBB A, 75H
iii. SUBB A, @R1
iv. SUBB A, @R0
CY AC and OV flags will be affected by this operation.
Multiplication
MUL AB.
This instruction multiplies two 8 bit unsigned numbers which are stored in A and B register.
After multiplication the lower byte of the result will be stored in accumulator and higher byte
of result will be stored in B register.
Eg. MOV A,#45H ;[A]=45H
MOV B,#0F5H ;[B]=F5H
MUL AB ;[A] x [B] = 45 x F5 = 4209 ;[A]=09H, [B]=42H
Division
DIV AB.
This instruction divides the 8 bit unsigned number which is stored in A by the 8 bit unsigned
number which is stored in B register. After division the result will be stored in accumulator
and remainder will be stored in B register.
Eg. MOV A,#45H ;[A]=0E8H
MOV B,#0F5H ;[B]=1BH
DIV AB ;[A] / [B] = E8 /1B = 08 H with remainder 10H ;[A] = 08H, [B]=10H
Eg 1: MOV A, #23H
MOV R1,#55H
ADD A,R1 // [A]=78
DA A // [A]=78 no changes in the accumulator after da a
Eg 2: MOV A,#53H
MOV R1,#58H
ADD A,R1 // [A]=ABh
DA A // [A]=11, C=1 . ANSWER IS 111. Accumulator data is changed after DA A
Increment: increments the operand by one.
INC A INC Rn INC DIRECT
INC @Ri INC DPTR
INC increments the value of source by 1. If the initial value of register is FFh, incrementing
the value will cause it to reset to 0. The Carry Flag is not set when the value "rolls over"
from 255 to 0.
In the case of "INC DPTR", the value two-byte unsigned integer value of DPTR is
incremented. If the initial value of DPTR is FFFFh, incrementing the value will cause it to
reset to 0.
Decrement: decrements the operand by one.
DEC A DEC Rn
DEC DIRECT DEC @Ri
DEC decrements the value of source by 1. If the initial value of is 0, decrementing the value
will cause it to reset to FFh. The Carry Flag is not set when the value "rolls over" from 0 to
FFh.
Logical Instructions
Logical AND
ANL destination, source:
ANL does a bitwise "AND" operation between source and destination, leaving the resulting
value in destination. The value in source is not affected. "AND" instruction logically AND
the bits of source and destination.
ANL A,#DATA ANL A, Rn ANL A,DIRECT
ANL A,@Ri ANL DIRECT,A ANL DIRECT, #DATA
Logical OR
ORL destination, source:
ORL does a bitwise "OR" operation between source and destination,leaving the resulting
value in destination. The value in source is not affected. " OR " instruction logically OR the
bits of source and destination.
ORL A,#DATA ORL A, Rn
ORL A,DIRECT ORL A,@Ri
ORL DIRECT,A ORL DIRECT, #DATA
Logical Ex-OR
XRL destination, source:
XRL does a bitwise "EX-OR" operation between source and destination, leaving the
resulting value in destination. The value in source is not affected. " XRL " instruction
logically EX-OR the bits of source and destination.
XRL A, #DATA XRL A,Rn
XRL A, DIRECT XRL A,@Ri
XRL DIRECT, A XRL DIRECT, #DATA
Logical NOT
CPL complements operand, leaving the result in operand. If operand is a single bit
then the state of the bit will be reversed. If operand is the Accumulator then all the bits in the
Accumulator will be reversed.
CPL A,
SWAP A – Swap the upper nibble and lower nibble of A.
Rotate Instructions
RR A
This instruction is rotate right the accumulator. Its operation is illustrated below. Each bit is
shifted one location to the right, with bit 0 going to bit 7.
RL A
Rotate left the accumulator. Each bit is shifted one location to the left, with bit 7 going to bit0
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.
Examples
1. What will be the effect of RRC A, assuming A = 02H and C = 1?
6. Write a program to subtract a 16- bit number stored at locations 51H-52H from 55H-
56H and store the result in locations 40H and 41H. Assume that the least significant
byte of data or the result is stored in low address. If the result is positive, then store
00H, else store 01H in 42H.
ORG 0000H;
MOV A, 55H;
CLR C;
SUBB A, 51H;
MOV 40H, A;
MOV A, 56H;
SUBB A, 52H;
MOV 41H, A;
MOV A, #00;
ADDC A, #00;
MOV 42H, A;
END
7. Write a program to add two Binary Coded Decimal (BCD) numbers stored at
locations 60H and 61H and store the result in BCD at memory locations 52H and
53H. Assume that the least significant byte of the result is stored in low address.
ORG 0000H;
MOV A, 60H;
ADD A, 61H;
DA A;
MOV 52H, A;
MOV A, #00;
ADDC A,#00H;
MOV 53H,A;
END
Branch (JUMP) Instructions
Jump and Call Program Range There are 3 types of jump instructions.
1. Relative Jump
2. Short Absolute Jump
3. Long Absolute Jump
Examples
1. Jump to a program memory location after 8kB locations (2000H
addresses) from the location of the next instruction, which is 1000H.
2. Call to a program memory location after 1026 kB locations from the current
instruction.
Loop will execute 250 times and time taken will be 250 ( 1+2) µs when the delay loop
is using DJNZ instruction as it takes 2 cycles of 2 µs and NOP one cycle of 1µs.
Delay = 750 µs within loop.
2. How can we toggle bits at Port 2 after a delay of 4000 µs? Make the bits = 1s as 0s
and 0s as 1s.
3. Write a program to store data FFH into RAM memory locations 50H to 58H using
indirect addressing mode.
Compare and then conditional jump after comparison in 8051.
1. CJNE A, #data, rel
Compare A and immediate data and jump if both not equal.
2. CJNE Rn, #data, rel
3. CJNE A, direct, rel
4. CJNE @Rn, data, rel
4. Write a program to compute 1 + 2 + 3 + N (say N=15) and save the sum at70H.
Branch (JUMP) Instructions
Jump and Call Program Range There are 3 types of jump instructions.
1. Relative Jump
2. Short Absolute Jump
3. Long Absolute Jump
Examples
1. Jump to a program memory location after 8kB locations (2000H addresses) from the
location of the next instruction, which is 1000H.
Jump is above 2kB code space. So we can use the jump short.
The memory location to jump is 1000 + 2000 = 3000H.
LJMP 3000H
2. Jump to a program location after 16 locations backwards from the location of the next
instruction, which is 1000H.
Jump is below 256 locations and so we can use short jump.
-16 decimal = F0H as two’s complement number.
SJMP F0H
Examples
1. Call to a program memory location after 3kB locations (after 0C00 addresses) from
the location of the next instruction, which is 1000H.
Jump is above 2kB code space. We can use jump long.
1000H + 0C00H = 1C00H.
LCALL 1C00H;
Conditional short-relative jump instructions in 8051.
Instruction for decrement and then jump in program-loops in 8051.
1. DJNZ Rn, Rel
Decrement Rn , and jump if Rn is still not zero.
2. DJNZ direct, Rel
Decrement byte at the direct and jump if byte is still not zero.
Examples
1. MOV R1, #FA;
NOP;
DJNZ R1, -3;
Loop will execute 250 times and time taken will be 250 ( 1+2) µs when the delay loop
is using DJNZ instruction as it takes 2 cycles of 2 µs and NOP one cycle of 1µs.
Delay = 750 µs within loop.
2. How can we toggle bits at Port 2 after a delay of 4000 µs? Make the bits = 1s as 0s
and 0s as 1s.
MOV A, #FFH;
MOV R0, #04H;
MOV R1, #FAH;
NOP;
DJNZ R1, -5;
DJNZ R0, -7;
XRL A, P2;
MOV P2, A
3. Write a program to store data FFH into RAM memory locations 50H to 58H using
indirect addressing mode.
ORG 0000H ;
MOV A, #0FFH ;
MOV RO, #50H ;
MOV R5, #08H ;
Start: MOV @RO, A ;
INC RO ;
DJNZ R5, start ;
END
Compare and then conditional jump after comparison in 8051.
1. CJNE A, #data, rel
Compare A and immediate data and jump if both not equal.
2. CJNE Rn, #data, rel
3. CJNE A, direct, rel
4. CJNE @Rn, data, rel
5. Write a program to compute 1 + 2 + 3 + N (say N=15) and save the sum at70H.
ORG 0000H;
N EQU 15;
MOV R0, #00;
CLR A;
start: INC R0;
ADD A, R0;
CJNE R0, #N, start;
MOV 70H, A;
ASSEMBLER DIRECTIVES.
Assembler directives tell the assembler to do something other than creating the machine code
for an instruction. In assembly language programming, the assembler directives instruct the
assembler to
1. Process subsequent assembly language instructions
2. Define program constants
3. Reserve space for variables
The following are the widely used 8051 assembler directives.
ORG (origin)
The ORG directive is used to indicate the starting address. It can be used only when the program
counter needs to be changed. The number that comes after ORG can be either in hex or in
decimal. Eg: ORG 0000H ; Set PC to 0000.
EQU and SET
EQU and SET directives assign numerical value or register name to the specified symbol name.
EQU is used to define a constant without storing information in the memory. The symbol
defined with EQU should not be redefined. SET directive allows redefinition of symbols at a
later stage.
DB (DEFINE BYTE)
The DB directive is used to define an 8 bit data. DB directive initializes memory with 8 bit
values. The numbers can be in decimal, binary, hex or in ASCII formats. For decimal, the 'D'
after the decimal number is optional, but for binary and hexadecimal, 'B' and ‘H’ are required.
For ASCII, the number is written in quotation marks (‘LIKE This).
DATA1: : DB 40H ; hex
DATA2: DB 01011100B ; binary
DATA3: DB 48 ; decimal
DATA4: DB 'HELLOW’ ; ASCII
END
The END directive signals the end of the assembly module. It indicates the end of the program
to the assembler. Any text in the assembly file that appears after the END directive is ignored.
If the END statement is missing, the assembler will generate an error message.
BASICS OF INTERRUPTS.
During program execution if peripheral devices needs service from microcontroller, device will
generate interrupt and gets the service from microcontroller. When peripheral device activates
the interrupt signal, the processor branches to a program called interrupt service routine. After
executing the interrupt service routine, the processor returns to the main program.
Steps taken by processor while processing an interrupt:
1. It completes the execution of the current instruction.
2. PSW is pushed to stack.
3. PC content is pushed to stack.
4. Interrupt flag is reset.
5. PC is loaded with ISR address.
ISR will always ends with RETI instruction. The execution of RETI instruction results in the
following.
1. POP the current stack top to the PC.
2. POP the current stack top to PSW.
Actions on an interrupt of a program by a source.
Interrupt Check if
interrupts
Check which
enabled (IE.7
Instructions source priority
= 1) and other
highest from
IE bit
IP register
2. IP Register
This is an 8 bit register used for setting the priority of the interrupts.
If the bit is 0, the corresponding interrupt has a lower priority. If bit is 1, the
corresponding interrupt has a higher priority.
Interrupt Priority Register
TMOD Register
TMOD: Timer/Counter Mode Control Register (Not Bit addressable)
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1
Timer 0
GATE When TRx (in TCON) is set and GATE = 1 , TIMER / COUNTERx will run
only when
INTx pin is high (hardware control). When GATE = 0, TIMER / COUNTERx
will run only when TRx = 1 (software control).
C/T Timer or Counter selector. Cleared for timer operation (input from internal
system
clock. Set for counter operation. (input from Tx input pin).
M1 Mode Selector bit.
M2 Mode Selector bit.
M1 M0 OPERATING MODE
0 0 Mode 0 – 13 bit timer
0 1 Mode 1 – 16 bit timer
1 0 Mode 2 – 8 bit auto reload timer
1 1 Mode 3 – (Timer 0), TL0 is an 8-bit timer / counter controlled by the
standard Timer 0 control bits. THO is an 8-bit timer and is controlled
by Timer 1 control bits.
1 1 (Timer 1) Timer / Counter stops.
TF1 TCON. Timer 1 overflow flag. Set by hardware when the Timer / Counter 1
7 overflows. Cleared by hardware.
TR TCON. Timer 1 run control bit. Set / Cleared by software to turn Timer / Counter
1 6 1 ON/ OFF.
TF0 TCON. Timer 0 overflow flag. Set by hardware when the Timer / Counter 0
5 overflows. Cleared by hardware.
TR TCON. Timer 0 run control bit. Set / Cleared by software to turn Timer / Counter
0 4 0 ON/ OFF.
IE1 TCON. External interrupt 1 edge flag. Set by hardware when external interrupt
7 edge is detected. Cleared by hardware when interrupt is processed.
IT1 TCON. Interrupt 1 type control bit. Set/Cleared by software to specify falling
7 edge/flow level triggered External interrupt.
IE0 TCON. External interrupt 0 edge flag. Set by hardware when external interrupt
7 edge is detected. Cleared by hardware when interrupt is processed.
IT0 TCON. Interrupt 0 type control bit. Set/Cleared by software to specify falling
7 edge/flow level triggered External interrupt.
Examples
1. 8 LEDs are connected to port 0.Write a program to blink them with 1 sec
delay.
back: mov a,#0ffh
mov p0,a
acall delay
mov a,#00h
mov p0,a
acall delay
sjmp back
delay: mov r7,#64h
back1: mov tmod,#01h
mov th0,#0dbh
mov tl0,#0ffh
setb TR0
l1:jnb TF0,l1
clr TR0
clr TF0
djnz r7,back1
ret
End
2. 8 LEDs are connected to port 0. Write a program to blink them alternatively.
back: mov a,#0aah
mov p2,a
acall delay
mov a,#55h
mov p2,a
acall delay
sjmp back
delay:mov r7,#64h
back1: mov tmod,#01h
mov th0,#093h
mov tl0,#0ffh
setb TR0
l1: jnb TF0,l1
clr TR0
clr TF0
djnz r7,back1
ret
end
PROGRAMMING 8051 TIMERS IN ASSEMBLY
In order to program 8051 timers, it is important to know the calculation of initial count value to be
stored in the timer register. The calculations are as follows.
In any mode,
Timer Clock period = 1/Timer Clock Frequency. = 1/ (Master Clock Frequency/12), here 1 micro
second.
Convert the answer into hexadecimal and load onto THx and TLx register. (65536D = FFFFH+1) b.
b. Mode 0 (13 bit timer/counter)
Convert the answer into hexadecimal and load onto THx and TLx register. (8192D = 1FFFH+1) c.
c. Mode 2 (8 bit auto reload)
Convert the answer into hexadecimal and load onto THx register. Upon starting the timer this
value from THx will be reloaded to TLx register. (256D = FFH+1).
Mode 1:
Load the TMOD value register indicating which timer (0 or 1) is to be used and which
timer mode is selected.
Load registers TL and TH with initial count values.
Start the timer by the instruction “SETB TR0” for timer 0 and “SETB TR1” for timer.
Keep monitoring the timer flag (TF) with the “JNB TFx,target” instruction to see if it is
raised. Get out of the loop when TF becomes high.
Stop the timer with the instructions “CLR TR0” or “CLR TR1”, for timer 0 and timer 1,
respectively.
Clear the TF flag for the next round with the instruction “CLR TF0” or “CLR TF1”, for
timer 0 and timer 1, respectively.
Go back to step 2 to load TH and TL again.
1. 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 µs. Each half pulse = 250 µs.
SERIAL COMMUNICATION.
The 8051 microcontroller is parallel device that transfers eight bits of data simultaneously
over eight data lines to parallel I/O devices. Parallel data transfer over a long is very expensive.
Hence, a serial communication is widely used in long distance communication. In serial data
communication, 8-bit data is converted to serial bits using a parallel in serial out shift register and
then it is transmitted over a single data line. The data byte is always transmitted with least
significant bit first.
Transmitter Receiver
Transmitter Receiver
Transmitter Receiver
Transmitter Receiver
Baud rate:
The rate at which the data is transmitted is called baud or transfer rate. The baud rate is the
reciprocal of the time to send one bit. In asynchronous transmission, baud rate is not equal to
number of bits per second. This is because; each byte is preceded by a start bit and followed by
parity and stop bit. For example, in synchronous transmission, if data is transmitted with 9600 baud,
it means that 9600 bits are transmitted in one second. For bit transmission time = 1 second/ 9600 =
0.104 ms.
The 8051 supports a full duplex serial port. Three special function registers support serial
communication.
1. SBUF Register: Serial Buffer (SBUF) register is an 8-bit register. It has separate SBUF registers for
data transmission and for data reception. For a byte of data to be transferred via the TXD line, it
must be placed in SBUF register. Similarly, SBUF holds the 8-bit data received by the RXD pin and
read to accept the received data.
2. SCON register: The contents of the Serial Port Control (SCON) register are shown below. This
register contains mode selection bits, serial port interrupt bit (TI and RI) and also the ninth data bit
for transmission and reception (TB8 and RB8).
D7 D6 D5 D4 D3 D2 D1 D0
SM0 SM1 SM2 REN TB8 RB8 TI RI
SM0: SCON.7 – Serial Communication mode selection bit
SM2: SCON.5 – Multiprocessor communication bit. In modes 2 and 3, if set this will enable
multiprocessor communication.
TB8: SCON.3 – This is the 9th bit that is transmitted in mode 2 and 3.
RB8: SCON.2 - This is the 9th bit that is transmitted in mode 2 and 3.
TI: SCON.1 – Transmit Interrupt flag. Set by hardware must be cleared by software.
RI: SCON.0 – Receive Interrupt flag. Set by hardware must be cleared by software.
The SMOD bit (bit 7) of PCON register controls the baud rate in asynchronous mode transmission.
D7 D6 D5 D4 D3 D2 D1 D0
SMOD - - - GF1 GF0 PD IDL
SMOD: PCON.7 – Serial rate modify bit. Set to 1 by program to double baud rate using timer 1 for
modes 1 ,2 and 3. Cleared by program to use timer 1 baud rate.
PD: PCON.1 – Power down bit. Set to 1 by program to enter power down configuration for CHMOS
processors.
IDL: PCON.0 – Idle mode bit. Set to 1 by program to enter idle mode configuration for CHMOS
processors.
SERIAL COMMUNICATION MODES
4. Mode 3
Similar to mode 2 except baud rate is calculated as in mode 1.
1. Write a program for the 8051 to transfer letter ‘A’ serially at 4800- baud rate, 8- bit data, 1
stop bit continuously.
ORG 0000H
LJMP START
ORG 0030H
MOV SCON, #50H; //initialize UART in mode 2 ; 8 bit data and 1 stop bit
SJMP AGAIN
END