Introduction to 8051 Microcontroller

Urvisha Shrivastava
Techloop
Published in
6 min readApr 8, 2020

A brief introduction to to the most popular microcontroller, Intel’s 8051

8051 microcontroller board with peripherals. (Source )

There is a very popular saying that goes like “Old is Gold”

Intel’s 8051 Microcontroller totally embodies this saying. It is a simple and hassle-free platform to get started with embedded systems and was developed in the 1980s.

Even after so many decades, the Microcontroller is relevant and used widely. Let’s find out what makes it immortal.

40 pin 8051 Microcontroller(Source)
  • 8051 is an 8-bit microcontroller.
  • It is built with 40 pins DIP (dual inline package).
  • 4kb of ROM storage, and 128 bytes of RAM storage.
  • It has two16-bit timers.
  • It consists of four parallel 8-bit ports, which are programmable as well as addressable as per the requirement.
  • An on-chip crystal oscillator is integrated with the microcontroller having a crystal frequency of 12 MHz.

Those are too many technical terms at once. Let us understand them one by one by going through the architecture first.

This blog will help develop a general idea about 8051 and can help you start coding one.

Once you get a hang of it you can create your own programs to perform any functionality.

ARCHITECTURE :

Architecture explaining the 8051 in detail.(Source)

This architecture helps us understand pictorially how various elements of the 8051 board are connected and functions.

Now let us talk about the pins of 8051.

PIN DIAGRAM:

The pin diagram of the 8051 microcontroller consists of 40 pins.

  • A total of 32 pins are set away into four Ports such as P0, P1, P2, and P3. Where each port contains 8 pins, for various functions such as i/o, external address, address latch, etc.

You can find detailed theoretical information of 8051 in the datasheet of 8051 available on google.

Now let us move to the fun segment: CODING an 8051 microcontroller.

The compiler we will use is KEIL, you can the software from the following link: https://www.keil.com/download/

PROGRAMMING 8051 :

8051 is programmed in Assembly language Programming (ALP ).

Here’s an example of ALP to give you an idea of programming of 8051.

Let us add two numbers in assembly language programming.

Addition of two 8-bit number

  • In assembly language, the instruction ‘mov’ is used to move the content . ‘a’ and ‘b’ are Registers ( you can think them as a variable where number is being stored and the mathematical operations are implemented upon.)
  • The first line of code “mov dptr,#4500h” is the instruction to assign the memory location.
  • And then the values are moved in the registers by the following instructions: “mov a,#05h” and “mov b,#08h”.
  • It’s the convention to first write the instruction then the register followed by the value ending with ‘h’ which implies it is a hexadecimal number.
  • Next, the “add” instruction is used to perform arithmetic addition operation.
  • Lastly “sjmp” which stands for Short jump is used which transfers execution to the specified address and is used to form a loop.
  • Instruction ‘end’ instruction is used to finish the execution.

The output is displayed on the LHS column beside the register name assigned to it.

The above’s code’s result can be obtained in the software itself, we need not connect the hardware, KEIL has inbuilt 8051 characteristics to execute the program.

Now let us do another assembly language program with hardware implementation, in which we will dump the code to 8051 after compiling.

LED and Switch interface

With the push of a button, we will be changing the pattern of the blinking of LEDs. Sounds interesting right?

Switch and LED interfacing

  • Here in this code Port0 is made the input port and Port 1 is made the output port by assigning them to registers. U must be wondering what is meant bt move the port pins to register, here it means whatever value(high or low) that the register contains, will be assigned to the port pins.
  • The “mov r3,#08h” is used to ensure rotation of LEDs blinking happens 8 number of times this can vary on how many times u want to rotate. A check is kept on this register for implementation of loop using “sjmp” instruction.
  • By “mov a, p0” instruction we fetch the value from the port0.
  • Next “cjne” which stands for compare and jump if not equal, checks whether register “a” has value “fe” or not which is the highest value a port can hold as per restrictions if it’s not equal then jump to line “lp” and execute it.
  • The “mov a,#055h” is making alternate ones as in 01010101, these few lines are the code for blinking in an alternate fashion.
  • If the content of register “a” is not “fe” then a single LED will glow, if it reaches “fe” which will be when we press the switch then alternate LEDs will glow.
  • Next port 1 is assigned to the register a, this is for the first pattern of blinking(single LED). The “lp: mov a,#01h” is the program for the blinking of one LED as only one pin is set high (01h converted to binary is 0000 0001), hence the last LED will be glowing.
  • Next, we instruct the microcontroller to rotate the blinking of LEDs in what fashion, this is done by the instruction “rl a”, it stands for rotate left.
  • The delay subroutine is what gets executed when “acall delay” instruction is executed. Here the program is in a loop within a loop “f” which is a hexadecimal number its value is 15.
  • Next “djnz” instruction is “decrement and jump if no zero”. so each time value in r0,r1,r2 registers are checked as they decrease from “f” and stops when they are equal to zero, this way a delay of “f x f x50h” is generated. This delay is called so that the changes are visible to human eyes.
  • Similarly “djnz r3,l” means r3 is checked for becoming zero if it’s not then going to l instruction.
  • The instruction “sjmp start” is used to create a loop as discussed earlier.
single LED blinking

After the switch is pressed the blinking pattern changes as-

LEDs blinking in an alternate fashion

The output can be checked by going to peripherals and then I-O ports after clicking on the run button. Select the ports to which you have assigned values in the program and u can see the LEDs which are high.

There are tons of mesmerizing codes we can write and execute them on different peripherals like LCD Screen, Motor, Speed Control, Keypad interfacing, etc.

APPLICATION :

8051 has a wide variety of applications -

  • Automobile applications
  • Defense applications
  • Industrial instrumentation devices
  • Process control devices
  • Voltmeter applications

And many more …….

To Conclude:

Microcontrollers were originally programmed only in assembly language, but various high-level programming languages, such as C, Python, and JavaScript, are now also in common use to target microcontrollers and embedded systems.

The use of electronic gadgets and instruments in almost every field of our life has led to the development of several significant microcontroller boards over time such as

  • Arduino Uno which uses Arduino IDEwhich has implied coding a microcontroller to a great extent,
  • Node MCU which has been revolutionary changes as it has an on-chip Wifi module etc.

We will be going through various programs that you can do in 8051 microcontrollers and others in the upcoming blogs.

Stay tuned.

--

--