Unit 4 8051 Assembly Language Programming and Interfacing
Unit 4 8051 Assembly Language Programming and Interfacing
The assembly language is a fully hardware related programming language. The embedded
designers must have sufficient knowledge on hardware of particular processor or controllers
before writing the program. The assembly language is developed by mnemonics; therefore,
users cannot understand it easily to modify the program.
The assembly language is made up of elements which all are used to write the program
in sequential manner. Follow the given rules to write programming in assembly language.
Rules of Assembly Language
DB(define byte): The define byte is used to allow a string of bytes. For example, print the
“EDGEFX” wherein each character is taken by the address and finally prints the “string” by
the DB directly with double quotes.
Syntax:
ORG 0000h
MOV a, #00h
————-
————-
DB”EDGEFX”
EQU (equivalent): The equivalent directive is used to equate address of the variable.
Syntax:
reg equ,09h
—————–
—————–
MOV reg,#2h
END:The END directive is used to indicate the end of the program.
Syntax:
reg equ,09h
—————–
—————–
MOV reg,#2h
END
Addressing Modes:
The way of accessing data is called addressing mode. The CPU can access the data in different
ways by using addressing modes. The 8051 microcontroller consists of five addressing modes
such as:
Immediate Addressing Mode
Register Addressing Mode
Direct Addressing Mode
Indirect Addressing Mode
Base Index Addressing Mode
Syntax:
MOV A,20h // 20h is an address; A is a register//
MOV 00h, 07h // both are addressed of the GPS registers//
Ex:
MOV 07h,#01h
MOV A, #08h
ADD A,07h //A<—A+07h the final value is stored in A//
Instruction Set:
The instruction set is the structure of the controller or processor that provides commands to the
controller to guide the controller for processing data. The instruction set consists of
instructions, native data types, addressing modes, interrupt registers, exceptional handling and
memory architecture. The 8051 microcontroller can follow CISC instructions with Harvard
architecture. In case of the 8051 programming different types of CISC instructions include:
Data Transfer Instruction set
Sequential Instruction Set
Arithmetic Instruction set
Branching Instruction set
Loop Instruction Set
Conditional Instruction set
Unconditional Instruction set
Logical Instruction set
Boolean Instruction set
Arithmetic Instruction Set:
The arithmetic instructions perform the basic operations such as:
Addition
Multiplication
Subtraction
Division
Addition:
ORG 0000h
MOV R0, #03H // move the value 3 to the register R0//
MOV A, #05H // move the value 5 to accumulator A//
Add A, 00H // addA value with R0 value and stores the result inA//
END
Multiplication:
ORG 0000h
MOV R0, #03H // move the value 3 to the register R0//
MOV A, #05H // move the value 5 to accumulator A//
MUL A, 03H // Multiplied result is stored in the Accumulator A //
END
Subtraction:
ORG 0000h
MOV R0, #03H // move the value 3 to register R0//
MOV A, #05H // move the value 5 to accumulator A//
SUBB A, 03H // Result value is stored in the Accumulator A //
END
Division:
ORG 0000h
MOV R0, #03H // move the value 3 to register R0//
MOV A, #15H // move the value 5 to accumulator A//
DIV A, 03H // final value is stored in the Accumulator A //
END
Conditional Instructions
The CPU executes the instructions based on the condition by checking the single bit status or
byte status. The 8051 microcontroller consists of various conditional instructions such as:
JB —>Jump below
JNB —> Jump if not below
JC —> Jump if Carry
JNC —>Jump if not Carry
JZ —>Jump if Zero
JNZ —> Jump if not Zero
Conditional Instructions
1. Syntax:
JB P1.0, label
––––––––
––––––––
Label: – – – – – – – –
––––––––
END
2. Syntax:
JNB P1.0, label
––––––––
––––––––
Label: – – – – – – – –
––––––––
END
3. Syntax:
JC,label
––––––––
––––––––
Label: – – – – – – – –
––––––––
END
4. Syntax:
JNC, label
––––––––
––––––––
Label: – – – – – – – –
––––––––
END
5. Syntax:
JZ, label
––––––––
––––––––
Label: – – – – – – – –
––––––––
END
6. Syntax:
JNZ, label
––––––––
––––––––
Label: – – – – – – – –
––––––––
END
The call and jump instructions are used to avoid the code replication of the program. When
some specific code used more than once in different places in the program, if we
mention specific name to code then we could use that name anywhere in the program without
entering a code for every time. This reduces the complexity of the program. The 8051
programming consists of call and jump instructions such as LCALL, SJMP.
LCALL
ACALL
SJMP
LJMP
1. Syntax:
ORG 0000h
––––––––
––––––––
ACALL, label
––––––––
––––––––
SJMP STOP
Label: – – – – – – – –
––––––––
––––––––
ret
STOP:NOP
2. Syntax:
ORG 0000h
––––––––
––––––––
LCALL, label
––––––––
––––––––
SJMP STOP
Label: – – – – – – – –
––––––––
––––––––
ret
STOP:NOP
Call and Jump Instructions
Loop Instructions:
The loop instructions are used to repeat the block each time while performing the increment
and decrement operations. The 8051 microcontroller consist two types of loop instructions:
CJNE —> compare and jump if not equal
DJNZ —> decrement and jump if not zero
1. Syntax:
of CJNE
MOV A, #00H
MOV B, #10H
Label:INC A
––––––
––––––
CJNE A, label
2. Syntax:
of DJNE
MOV R0, #10H
Label:– – – – – –
––––––
DJNE R0, label
––––––
––––––
END
Logical Instruction Set:
The 8051 microcontroller instruction set provides the AND, OR, XOR, TEST, NOT and
Boolean logic instructions for set and clears the bits based on the need in the program.
Logical Instruction Set
1. Syntax:
MOV A, #20H /00100000/
MOV R0, #03H /00000101/
ORL A, R0 //00100000/00000101=00000000//
2. Syntax:
MOV A, #20H /00100000/
MOV R0, #03H /00000101/
ANL A, R0
3. Syntax:
MOV A, #20H /00100000/
MOV R0, #03H /00000101/
XRL A, R0
Shifting Operators
The shift operators are used for sending and receiving the data efficiently. The
8051 microcontroller consist four shift operators:
RR —> Rotate Right
RRC —>Rotate Right through carry
RL —> Rotate Left
RLC —>Rotate Left through carry
Rotate Right (RR):
In this shifting operation, the MSB becomes LSB and all bits shift towards right side bit-by-
bit, serially.
Syntax:
MOV A, #25h
RR A
Rotate Left (RL):
In this shifting operation, the MSB becomes LSB and all bits shift towards Left side bit-by-
bit, serially.
Syntax:
MOV A, #25h
RL A
Syntax:
MOV A, #27h
RRC A
Syntax:
MOV A, #27h
RLC A
The microcontroller programming differs for each type of operating system. There are many
operating systems such as Linux, Windows, RTOS and so on. However, RTOS has several
advantages for embedded system development. Some of the Assembly level programming
examples are given below.
LED blinking using with 8051 microcontroller:
Number Displaying on 7-segment display using 8051 microcontroller
Timer/Counter calculations and program using 8051 microcontroller
Serial Communication calculations and program using 8051 microcontroller
LED programs with 8051 Microcontrller
1. WAP to toggle the PORT1 LEDs
ORG 0000H
TOGLE: MOV P1, #01 //move 00000001 to the p1 register//
CALL DELAY //execute the delay//
MOV A, P1 //move p1 value to the accumulator//
CPL A //complement A value //
MOV P1, A //move 11111110 to the port1 register//
CALL DELAY //execute the delay//
SJMP TOGLE
DELAY: MOV R5, #10H //load register R5 with 10//
TWO: MOV R6, #200 //load register R6 with 200//
ONE: MOV R7, #200 //load register R7 with 200//
DJNZ R7, $ //decrement R7 till it is zero//
DJNZ R6, ONE //decrement R7 till it is zero//
DJNZ R5, TWO //decrement R7 till it is zero//
RET //go back to the main program //
END
Timer/Counter Calculations and Program using 8051 Microcontroller:
The delay is the one of the important factors in the application software development.
The timers and counters are hardware components of the microcontroller, that are used in many
applications to provide the accurate time delay with count pulses. Both the tasks are
implemented by the software technique.
4x4 Keypad
Introduction
The keypad is used as an input device to read the key pressed by the user and to process it.
4x4 keypad consists of 4 rows and 4 columns. Switches are placed between the rows and
columns. A keypress establishes a connection between the corresponding row and column
between which the switch is placed.
To read the keypress, we need to configure the rows as outputs and columns as inputs.
Columns are read after applying signals to the rows in order to determine whether or not a key
is pressed and if pressed, which key is pressed.
For more information about the keypad and how to use it, refer to the topic 4x4 Keypad in the
sensors and modules section.
Example
Here, we are going to interface the 4x4 keypad with AT89S52 (8051) and will display the
pressed key on LCD16x2.
Interfacing Diagram