Module 2 Microcontroller
Module 2 Microcontroller
Structure
Objectives
To explain in detail the execution of 8051 Assembly language instructions and data
types
To explain loop, conditional and unconditional jump and call, handling and
manipulation of I/O instructions.
2.1 Introduction to 8051 assembly programming
1. First we use an editor to type a program, many excellent editors or word processors
are available that can be used to create and/or edit the program
Notice that the editor must be able to produce an ASCII file
For many assemblers, the file names follow the usual DOS conventions, but the
source file has the extension “asm“ or “src”, depending on which assembly you
are using.
2. The “asm” source file containing the program code created in step 1 is fed to an 8051
assembler ƒ The assembler converts the instructions into machine code. The
assembler will produce an object file and a list file .The extension for the object file
is “obj” while the extension for the list file is “lst”
3. Assembler require a third step called linking ƒ
The linker program takes one or more object code files and produce an absolute
object file with the extension “abs” ƒ
This abs file is used by 8051 trainers that have a monitor program
4. Next the “abs” file is fed into a program called “OH” (object to hex converter) which
creates a file with extension “hex” that is ready to burn into ROM
This program comes with all 8051 assemblers
ƒ Recent Windows-based assemblers combine step 2 through 4 into one step
1. It lists all the opcodes and addresses as well as errors that the assembler detected.
2. The programmer uses the lst file to find the syntax errors or debug
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. 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;
2. 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.
4. 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.
2.4 Arithmetic, logic instructions and programs
8051 Instructions The instructions of 8051 can be broadly classified under the following
headings.
2. Arithmetic instructions
3. Logical instructions
4. Branch instructions
5. Subroutine 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,R2
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
f. Move the contents of memory location to another memory location using direct and indirect
addressing
i. MOVC A, @A+PC
The 8051 can perform addition, subtraction. Multiplication and division operations on 8 bit numbers:
a) 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
ii. 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
Example: The Accumulator holds 0C3H (11000011B) and register 0 holds 0AAH (10101010B) with
the carry flag set. The following instruction, ADDC A,R0 leaves 6EH (01101110B) in the
Accumulator with AC cleared and both the Carry flag and OV set to 1.
Function: Divide
Description: DIV AB divides the unsigned eight-bit integer in the Accumulator by the unsigned
eight-bit integer in register B. The Accumulator receives the integer part of the quotient; register B
receives the integer remainder. The carry and OV flags are cleared.
Exception: if B had originally contained 00H, the values returned in the Accumulator and B register
are undefined and the overflow flag are set. The carry flag is cleared in any case.
Example: The Accumulator contains 251 (0FBH or 11111011B) and B contains 18 (12H or
00010010B). The following instruction, DIV AB leaves 13 in the Accumulator (0DH or 00001101B)
and the value 17 (11H or 00010001B) in B, since 251 = (13 x 18) + 17. Carry and OV are both
cleared.
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.
1. INC increments the value of source by 1.
2. If the initial value of register is FFh, incrementing the value will cause it to reset to 0.
3. 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.
Eg: INC A
INC Rn
INC DIRECT
INC @Ri
INC DPTR
a) 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
Example: If the Accumulator holds 0C3H (1100001lB), and register 0 holds 55H (01010101B), then
the following instruction, ANL A,R0 leaves 41H (01000001B) in the Accumulator
b) 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
Example: If the Accumulator holds 0C3H (11000011B) and R0 holds 55H (01010101B) then the
following instruction, ORL A,R0 leaves the Accumulator holding the value 0D7H (1101011lB).
c) 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
Example: If the Accumulator holds 0C3H (1100001lB) and register 0 holds 0AAH (10101010B) then
the instruction, XRL A,R0 leaves the Accumulator holding the value 69H (01101001B).
d)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
CPL C
CPL bit address
SWAP A
Swap the upper nibble and lower nibble of A
Function: Swap nibbles within the Accumulator
Description: SWAP A interchanges the low- and high-order nibbles (four-bit fields) of the
Accumulator (bits 3 through 0 and bits 7 through 4). The operation can also be thought of as a 4- bit
rotate instruction. No flags are affected. Example: The Accumulator holds the value 0C5H
(11000101B). The instruction, SWAP A leaves the Accumulator holding the value 5CH (01011100B)
XCH A,<byte>
Function: Exchange Accumulator with byte variable Description: XCH loads the Accumulator with
the contents of the indicated variable, at the same time writing the original Accumulator contents to
the indicated variable. The source/destination operand can use register, direct, or register-indirect
addressing.
Example: R0 contains the address 20H. The Accumulator holds the value 3FH (0011111lB). Internal
RAM location 20H holds the value 75H (01110101B). The following instruction, XCH A,@R0 leaves
RAM location 20H holding the values 3FH (00111111B) and 75H (01110101B) in the accumulator.
CPL A
Function: Complement Accumulator
Description: CPLA logically complements each bit of the Accumulator (one’s complement). Bits
which previously contained a 1 are changed to a 0 and vice-versa. No flags are affected.
Example: The Accumulator contains 5CH (01011100B). The following instruction, CPL A leaves
the Accumulator set to 0A3H (10100011B).
CPL bit
Function: Complement bit
Description: CPL bit complements the bit variable specified. A bit that had been a 1 is changed to 0
and vice-versa. No other flags are affected. CLR can operate on the carry or any directly addressable
bit.
Example: Port 1 has previously been written with 5BH (01011101B). The following instruction
sequence, CPL P1.1CPL P1.2 leaves the port set to 5BH (01011011B).
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 bit 0
Example: The Accumulator holds the value 0C5H (11000101B). The following instruction,
RL A leaves the Accumulator holding the value 8BH (10001011B) with the carry unaffected.
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
Example: The Accumulator holds the value 0C5H (11000101B), the carry is zero. The following
instruction, RRC A leaves the Accumulator holding the value 62 (01100010B) with the carry set.
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.
Example: The Accumulator holds the value 0C5H(11000101B), and the carry is zero. The following
instruction, RLC A leaves the Accumulator holding the value 8BH (10001010B) with the carry set.
2.5 Branch (JUMP) Instructions
Jump and Call Program Range There are 3 types of jump instructions.
They are:-
1. Relative Jump
2. Short Absolute Jump
3. Long Absolute Jump
1. Relative Jump
Jump that replaces the PC (program counter) content with a new address that is greater than (the
address following the jump instruction by 127 or less) or less than (the address following the jump by
128 or less) is called a relative jump. Schematically, the relative jump can be shown as follows: -
It can be seen that the upper 5bits of the program counter (PC) hold the page number and the
lower 11bits of the PC hold the address within that page.
Thus, an absolute address is formed by taking page numbers of the instruction (from the
program counter) following the jump and attaching the specified 11bits to it to form the 16-bit
address.
Advantage:
The instruction length becomes 2 bytes.
Example of short absolute jump: -
3. Long Absolute Jump/Call
Applications that need to access the entire program memory from 0000H to FFFFH use long
absolute jump.
Since the absolute address has to be specified in the op-code, the instruction length is 3 bytes
(except for JMP @ A+DPTR). This jump is not re-locatable.
Example: -
Operation: LJMP
Function: Long Jump
Syntax: LJMP code address.
Description: LJMP jumps unconditionally to the specified code address.
Operation: JNC
Function: Jump if Carry Not Set
Syntax: JNC reladdr
Description: JNC branches to the address indicated by reladdr if the carry bit is not set. If the carry bit
is set program execution continues with the instruction following the JNB instruction.
Operation: JC
Function: Jump if Carry Set
Syntax: JC reladdr
Description: JC will branch to the address indicated by reladdr if the Carry Bit is set. If the Carry Bit
is not set program execution continues with the instruction following the JC instruction.
Operation: JNB
Function: Jump if Bit Not Set
Syntax: JNB bit addr, reladdr
Description: JNB will branch to the address indicated by reladdress if the indicated bit is not set. If the
bit is set program execution continues with the instruction following the JNB instruction.
Operation: JB
Function: Jump if Bit Set
Syntax: JB bit addr, reladdr
Description: JB branches to the address indicated by reladdr if the bit indicated by bit addr is set. If
the bit is not set program execution continues with the instruction following the JB instruction.
Operation: JNZ
Function: Jump if Accumulator Not Zero
Syntax: JNZ reladdr
Description: JNZ will branch to the address indicated by reladdr if the Accumulator contains any
value except 0. If the value of the Accumulator is zero program execution continues with the
instruction following the JNZ instruction.
Operation: JZ
Function: Jump if Accumulator Zero
Syntax: JNZ reladdr
Description: JZ branches to the address indicated by reladdr if the Accumulator contains the value 0.
If the value of the Accumulator is non-zero program execution continues with the instruction
following the JNZ instruction.
Operation: DJNZ
Function: Decrement and Jump if Not Zero
Syntax: DJNZ register, reladdr
Description: DJNZ decrements the value of register by 1. If the initial value of register is 0,
decrementing the value will cause it to reset to 255 (0xFF Hex). If the new value of register is not 0
the program will branch to the address indicated by relative addr. If the new value of register is 0
program flow continues with the instruction following the DJNZ instruction.
Operation: CJNE
Function: Compare and Jump If Not Equal
Syntax: CJNE operand1,operand2,reladdr
Description: CJNE compares the value of operand1 and operand2 and branches to the indicated
relative address if operand1 and operand2 are not equal. If the two operands are equal program flow
continues with the instruction following the CJNE instruction. The Carry bit (C) is set if operand1 is
less than operand2, otherwise it is cleared.
Fig: 2.4: Jump Instruction Ranges
Bit level JUMP instructions will check the conditions of the bit and if condition is true, it
jumps to the address specified in the instruction.
All the bit jumps are relative jumps.
2.5.2 Subroutine CALL and RETURN Instructions
Subroutines are handled by CALL and RET instructions There are two types of CALL instructions
Call instructions may be included explicitly in the program as mnemonics or implicitly included using
hardware interrupts.
Subroutine: Subroutine is a standalone program or small program in a main program
“A Subroutine is a program that may be used many times in the execution of a larger [Link]
subroutine could be written into the body of the main program everywhere it is needed resulting in the
fastest possible code execution.”
1. A call, hardware or software when initiated, causes a jump to the address where the
subroutine is located.
2. At the end of the subroutine the program resumes operation at the opcode address
immediately following the call.
3. Call can be located anywhere in the program space and used many times.
4. The stack area of internal RAM is used to automatically store the address, called the return
address, of the instruction found immediately after the call.
5. Stack and stack pointer are often used to designate the top of the stack area in RAM that is
pointed to by the stack pointer
1. I/O Port pins, Ports and Circuits: One major feature of a microcontroller is versatility
built into the I/O circuits that connect the 8051 to the outside world.
2. Out of 40 pins 24 pins may each be used for one of two entirely different functions
yielding a total pin configuration of 64.
3. But the port pins have been multiplexed to perform different functions to make 8051
as 40 Pin IC
The port pin circuitry is as shown below
Port-0
Fig. 2. 6 : Port -0
1. Port -0 has 8 pins (P0.0-P0.7).The structure of a Port-0 pin is shown in Fig.2.6 ..Port-
0 can be configured as a normal bidirectional I/O port or it can be used for
address/data interfacing for accessing external memory.
2. When control is '1', the port is used for address/data interfacing. When the control is
'0', the port can be used as a normal bidirectional I/O port. Let us assume that control
is '0'.
3. When the port is used as an input port, '1' is written to the latch. In this situation both
the output MOSFETs are 'off'. Hence the output pin floats. This high impedance pin
can be pulled up or low by an external source.
4. When the port is used as an output port, a '1' written to the latch again turns 'off' both
the output MOSFETs and causes the output pin to float. An external pull-up is
required to output a '1'.
5. But when '0' is written to the latch, the pin is pulled down by the lower MOSFET.
Hence the output becomes zero. When the control is '1', address/data bus controls the
output driver MOSFETs. If the address/data bus (internal) is '0', the upper MOSFET is
'off' and the lower MOSFET is 'on'.
6. The output becomes '0'. If the address/data bus is '1', the upper transistor is 'on' and
the lower transistor is 'off'.
7. Hence the output is '1'. Hence for normal address/data interfacing (for external
memory access) no pull-up resistors are required. Port-0 latch is written to with 1's
when used for external memory access
Port-1
1. Port-2 has 8-pins (P2.0-P2.7) . The structure of a port-2 pin is shown in Fig 2.8
2. Port-2 is used for higher external address byte or a normal input/output port. The I/O
operation is similar to Port-1.
3. Port-2 latch remains stable when Port-2 pin are used for external memory access.
Here again due to internal pull-up there is limited current driving capability.
Port-3
1. Each pin of Port-3 can be individually programmed for I/O operation or for alternate
function. The alternate function can be activated only if the corresponding latch has
been written to '1'.
2. To use the port as input port, '1' should be written to the latch. This port also has
internal pull-up and limited current driving capability.
3. Alternate functions of Port-3 pins –
Note:
1. Port 1, 2, 3 each can drive 4 LS TTL inputs.
2. Port-0 can drive 8 LS TTL inputs in address /data mode. For digital output port, it needs
external pull-up resistors.
3. Ports-1,2and 3 pins can also be driven by open-collector or open-drain outputs.
Each Port 3 bit can be configured either as a normal I/O or as a special function bit.
Reading a port (port-pins) versus reading a latch.
There is a subtle difference between reading a latch and reading the output port pin.
The status of the output port pin is sometimes dependant on the connected load.
For instance if a port is configured as an output port and a '1' is written to the latch,
the output pin should also show '1'.
If the output is used to drive the base of a transistor, the transistor turns 'on'. If the
port pin is read, the value will be '0' which is corresponding to the base-emitter
voltage of the transistor.
Reading a latch: Usually the instructions that read the latch, read a value, possibly
change it, and then rewrite it to the latch. These are called "read-modify-write"
instructions.
2.6.1 Programs
1. Write a program to add the values of locations 50H and 51H and store the result in locations in 52h
and 53H.
ORG 0000H ; Set program counter 0000H
MOV A,50H ; Load the contents of Memory location 50H into A
ADD ADD A,51H ; Add the contents of memory 51H with CONTENTS A
MOV 52H,A ; Save the LS byte of the result in 52H
MOV A, #00 ; Load 00H into A
ADDC A, #00 ; Add the immediate data and carry to A
MOV 53H,A ; Save the MS byte of the result in location 53h
END
2. 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.
3. Write a program to add two 16 bit numbers stored at locations 51H-52H and 55H-56H and store the
result in locations 40H, 41H and 42H. Assume that the least significant byte of data and the result is
stored in low address and the most significant byte of data or the result is stored in high address.
6. Write a program to compute 1 + 2 + 3 + N (say N=15) and save the sum at70H
Outcomes