This article series is developed to teach you 8051 micro controller programming. I have divided this programming tutorial into a series of chapters as shown below. So you can start with Chapter 1 and then move to chapter 2 and chapter 3 and so on. So let’s begin the journey right now!
Note: Next chapters 1,2,3.. are under development phase. Please visit this page again for updates.
Note: To test any of these program or to write one your own and test, you dont need to buy a microcontroller board now. You can test your program using an 8051 simulator. Here is a big list of 8051 simulators available. In the beginning try the first one given in the list, Edsim51. Its an easy to use tool.
To program any microcontroller available in this world, first you need to learn and understand it’s instruction sets. Instruction set contains a set of instructions that is available for the programmer to create any kind of program he likes. Or in another way, using the instruction set a programmer can create the program required for the specific application he is making. So first of all one needs to master all available instructions, how an instruction works, how the execution of an instruction affects the microcontroller (affecting the registers, psw, stack etc) and the way it is used in a program. Once the instruction set is mastered, you can start playing with programs. Before getting into programming, there are some prerequisites. If you are really new to micro controller and if 8051 is the first one you are playing with, please read the following articles first.
3. Addressing modes of 8051 – You must read this article before writing any program for 8051 as this documents the root of instruction handling.
Now lets come to instruction sets of 8051 micro controller. The 8051 instruction set can be classified as shown below.
- Instructions for data transfer/ data move
- Instructions for arithmetic operations
- Instructions for branching a program
- Instruction for creating subroutines
- Instructions for logical operations
- Instructions for boolean operations
- Special purpose instructions
Follow the given link, where you can access the complete list of instructions for 8051 micro controller – 8051 Instruction Set (see the heading Alphabetical order below first table).
Note:- 8051 micro controller belongs to the MCS-51 family of micro controllers. This basically means,any 8051 variant micro controller (that comes under the MCS-51 family) made by any other manufacturer must use the same set of instructions made for MCS-51. So the “instruction decoder” part of all micro controllers under MCS-51 family is same. Example: Atmels AT89c2051 is one such micro controller that falls under MCS-51 family. So a program written for Intel 8051 can be used to run AT89C2051 too (you may have to make slight modifications to match hardware disparities).
1. Instructions for data transfer includes – MOV, MOVC, MOVX, PUSH, POP, XCH, XCHD
2.Instructions for arithmetic operations are – ADD, ADDC, SUBB, MUL, DIV, INC, DEC, DA A
3. Instructions for branching and subroutines – LJMP, AJMP, SJMP, LCALL, ACALL, JZ, JNZ, CJNE, DJNZ, JMP, NOP, RET, RETI
4. Instructions for logical operations – ANL, ORL, XRL, CLR, CPL, RL, RLC, RR, RRC, SWAP
5. Instructions for boolean variable operations – SETB, MOV, CLR, JB, JNB, JBC, ANL, ORL, CPL, JC, JNC
6. Special purpose instructions involves – MOVC, MOVX, SWAP, XCH, XCHD, JBC, RETI, DA A
You may learn about all instructions in detail by following that link given above. There are 44 instructions in 8051 or MCS-51 instruction set.
I assume you have gone through data transfer/arithmetic/branching and subroutine instructions by now. Now lets write a very simple program.
A program to find sum of N natural numbers and store the sum
Program description:- The number “N” is stored in location 35H. Natural numbers generated from 0 to N must be stored from location 55H. The sum of natural numbers must be stored in location 36H.
Analyzing the program description, we need 3 registers. R0 to store the value of “N” (given in location 35H)and to act as a counter for generating natural numbers upto N. R5 is used to save the value of first storage location of natural numbers and then R5 is incremented by one each to store each newly generated natural number. R7 is initiated as 0 and is incremented by 1 to generate natural numbers.
MOV PSW, #00H // Register bank '0' is selected by executing this instruction.
MOV R0, 35H // The value of 'N' stored in location 35H is transfered to R0.
MOV R5, #55H// The starting location for storing natural numbers '#55H' is transfered to R5
MOV A, #00H// Accumulator is initiated with value 0 for adding natural numbers cumulatively.
MOV R7, #00H// R7 is initialized to '0' to generate natural numbers. Note: '0' is not a natural number.
LOOP: INC R7// R7 is incremented by 1 to generate next natural number.
MOV @R5, 07H// This is indirect addressing mode used here.It is not possible to transfer data from one register to another register directly. So an instruction like MOV R5, R7 is invalid.Instead we use the direct address (07) of register R7 of register bank #00 to transfer the generated natural number to it's storage location in register R5.Indirect addressing is used as we need to save the generated natural number directly to memory address. R5 holds the starting location address (of the storage area) as its value i.e #55H.By indirectly addressing, we can save what ever value in R7 directly to location #55H.
INC R5// The storage location is incremented by 1 from #55H to #56H to store the next generated natural number
ADD A, R7// The generated natural number is added to contents in accumulator.
DJNZ R0, LOOP// The value of register Ro (value of 'N') is decremented by 1. It is checked against stopping condition zero. If its R0 is not equal to zero, the program control will move to label LOOP again and the steps from INC R7 will be executed again until R0 is equal to zero. When R0 is equal to zero, program control will exit the loop and move to next instruction given below.
MOV 36H,A// The sum of natural numbers in accumulator is moved to storage location 36H.
STOP: SJMP STOP// An infinite loop written at the end of the program. When this instruction is reached program control will get stuck at this instruction as it's an infinite loop.To get out of this infinite loop system reset must be applied.
A simple program to copy a block of data from one location to another
Program Description:- 10 bytes of data stored from 30H is to be copied to another location staring from 50H.
Analyzing the program, we see, we need two registers to store starting locations of source and destination. Lets say we take R0 as the source and R2 as the destination registers. Now we need a counter to count 10 bytes of data transfered from source to destination. Lets take R3 for that. It is not possible to transfer data from one register to another register directly by using any kind of addressing mode. So we need accumulator to stand in between as a temporary register. So here is it:
MOV R0,#30H // Address of the starting location of source data is moved to R0.
MOV R1,#50H // Address of the starting location of destination is moved to R1
MOV R3,#OAH// Set the counter R3 with 10. You can also use decimal number as MOV R3,#10d.
LOOP: MOV A, @R0// Indirect addressing mode is used. Contents at the location of Ro (30H) is copied to accumulator.
MOV @R1, A// Contents in accumulator is copied to location pointed by Ra (that is 50H).
INC R0 // Ro is incremented by 1 to point to next location.
INC R1// R1 is incremented by 1 to point to next location.
DJNZ R3, LOOP // Counter register R3 is decremented by 1 and checked against zero. See the explanation DJNZ in the first program "sum of natural numbers"
STOP: SJMP STOP // Infinite loop to terminate programh
To generate a Fibonacci series
Note:- Follow this link, If you are interested in learning about the Origin of Fibonacci series and the story behind
Program description:- We all know, a fibonacci series is an infinite series in mathematics that goes like 0,1,1,2,3,5,8,13,21….
So here we are going to write a program which generates fibonacci series upto N terms. The value of N is available in location 30H and we are going to save the series from location 40H.
Note: If you are hearing the term Fibonacci series for the first time, here have a good read – Fibonacci series generation
BEGIN: MOV R1,30H // Getting the value of "N"
MOV R7,#40H // The first number '0' of series is stored here.
MOV @R7,#00H // Loading value 'o' to address 40H using indirect addressing
INC R7 // Incrementing value of R7 from 40H to 41H to store next number '1'
MOV @R7, #01H // Storing value '1' to location 41H. Note that 'o' and '1' are seed values of a fibonacci series and has to be generated manually.
MOV R5,#42H // New register R5 is loaded with location address 42H to store next values of series, generated by adding the 2 previously generated numbers.
DEC R1 // The count value "N" is decremented by 2, as we have already generated and stored 1st two numbers.
DEC R7 // R7 is now reduced from 41H to 40H. We need to add contents of 40H and 41 H to get the number that is to be stored in 42H.
LOOP: MOV A, @R7 // Contents in R7 is moved to accumulator.
INC R7 // R7 is incremented to get the next value.
ADD A,@R7 // The two values are added and stored in Acc.
MOV @R5,A // The newly generated value of the series is stored in the address held by R5.
INC R5 // R5 is incremented to store next value.
DJNZ R1,LOOP // The count "N" is checked to zero (to know if all the numbers upto N are generated).
STOP: SJMP STOP // Run infinitely here or end of program execution.
These 3 programs will be enough for a “kick start” in 8051 programming. More programs and concepts will be explained in upcoming articles. I recommend you to download Edsim51 simulator (or any other one you prefer) and try out these programs. Make changes to these codes and write simple codes based on your ideas – like Multiplying 2 numbers, Generating a particular series, Arithmetic calculator etc. Only by doing yourself, you can master the art of programming!