Unit-3
8051 Programming
Directives
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.
EQU (equate)
- This is used to define a constant without occupying a memory location.
- The following uses EQU for the counter constant and then the constant is used to load the R3 register.
CONST EQU 25H
MOV A,#CONST ;COPY 25 INTO A
END
- 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.
Key Takeaways:
Assembler directives are instructions to the assembler to perform various bookkeeping tasks, storage reservation, and other control functions.
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.
8051 Programming in Assembly Language
- Assembly programming language is developed by various compilers and the “keiluvison” is best suitable for microcontroller programming development.
- Microcontrollers or processors can understand only binary language in the form of ‘0s or 1s’; An assembler converts the assembly language to binary language, and then stores it in the microcontroller memory to perform the specific task.
8051 Microcontroller Programs in Assembly Language
The assembly language is made up of elements which all are used to write the program in a sequential manner. The rules should be followed to write programming in assembly language.
Rules of Assembly Language
- The assembly code must be written in upper case letters
- The labels must be followed by a colon .
- All symbols and labels must begin with a letter
- All comments are typed in lower case
- The last line of the program must be the END directive
The assembly language mnemonics are in the form of op-code, such as MOV, ADD, JMP, and so on, which are used to perform the operations.
Op-code: The op-code is a single instruction that can be executed by the CPU. Here the op-code is a MOV instruction.
Operands: The operands are a single piece of data that can be operated by the op-code. Example, multiplication operation is performed by the operands that are multiplied by the operand.
Syntax: MUL a,b;
Write 8085 Assembly language program to swap two 8-bit number stored at location 8000H and 8001H using direct addressing mode.
MOV B,M ; Load the first number to B
INX H ; Point to the next number
MOV A,M; Load the second number into A
MOV M,B; Store first number to second position
DCX H ; Point to previous location
MOV M,A; Store second number to first position.
HLT.
Write a program to transfer the block of data from 20h to 30h to external location 1020h to 1030h.
Mov r7, #0Ah ; initialize counter by 10d
Mov r0, #20h ; get initial source location
Mov dptr, #1020h ; get initial destination location
Nxt: Mov a, @r0 ; get first content in acc
Movx @dptr, a ; move it to external location
Inc r0 ; increment source location
Inc dptr ; increase destination location
Djnz r7, nxt ; decrease r7. if zero then over otherwise move next
Write a program to add two numbers
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
Write a program to multiply two numbers :
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
Write a program to subtract two numbers :
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
Write a program to divide two numbers :
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
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
Key Takeaways:
A Register is the main part in the processors and microcontrollers which is contained in the memory that provides a faster way of collecting and storing the data. The 8051 assembly language programming is based on the memory registers.
WAP to calculate the 500us time delay.
MOV TMOD, #10H //select the timer mode by the registers//
MOV TH1, #0FEH // store the delay time in higher bit//
MOV TL1, #32H // store the delay time in low bit//
JNB TF1, $ //decrement the value of the timer till it is zero//
CLR TF1 //clear the timer flag bit//
CLR TR1 //OFF the timer//
2. WAP to toggle the LEDs withthe 5 sec time delay
ORG 0000H
RETURN: MOV PO, #00H
ACALL DELAY
MOV P0, #0FFH
ACALL DELAY
SJUMP RETURN
DELAY: MOV R5, #50H //load register R5 with 50//
DELAY1: MOV R6, #200 //load register R6 with 200//
DELAY2: MOV R7, #229 //load register R7 with 200//
DJNZ R7, $ //decrement R7 till it is zero//
DJNZ R6, DELAY2//decrement R6 till it is zero//
DJNZ R5, DELAY1//decrement R5 till it is zero//
RET //go back to the main program //
END
3. WAP to count the 250 pulses using mode0 count0
Syntax:
ORG 0000H
MOV TMOD, #50H //select the counter//
MOV TH0, #15 //move the counting pulses higher bit//
MOV TH1, #9FH //move the counting pulses, lower bit//
SET TR0 //ON the timer//
JNB $ //decrement the count value till zero//
CLR TF0 //clear the counter, flag bit//
CLR TR0 //stop the timer//
END
Key Takeaways:
The 8051 microcontroller has two independent 16 bit up counting timers named Timer 0 and Timer 1 and this article is about generating time delays using the 8051 timers
References:
- The 8051 Microcontroller and Embedded Systems using Assembly and C by Muhammad Ali Mazidi.
- The 8051 Microcontroller by I. Scott Mackenzie, Raphael C.W Phan
3. 8051 Microcontrollers: Internals, Instructions, Programming, and Interfacing Book by Subrata Ghoshal