Basics of 8051 Microcontroller Programming

8051 Microcontroller

8051 Microcontrollers are a basic, primitive controller which every embedded engineer should learn. Even though there are many modern microcontrollers the 8051 still holds its significance due to its capability to develop complex systems using it. This tutorial focuses on covering the basics of 8051 microcontroller and will give you clear idea about how to use ports, how to load values into and how to make controller work per your requirements.

This article will only cover the basics concepts needed to program the 8051 Microcontroller. It is advised to refer a book for detailed explanation of 8051 Microcontroller.


Bits and Bytes

Bits are the fundamental part of information or data based on which the Microcontroller operates. The bit can take value either as 1 or 0.

Bytes are defined as the collection of 8 bits. Generally in 8051 Microcontroller the data’s are fed as input or obtain output by means of byte values.



Registers are small memory element in a Microcontroller where a specific value can be loaded to perform a specific task. It can vary in size such as 8-bit or 16-bit registers, but in 8051 register we generally use only 8 bit registers. The LSB marks the initial or Least significant bit and MSB marks the last or Most significant bit.


hex values-table
Hex values

In Microcontrollers we use hex values to represent the data fed into it because of its simplicity in representation and easier understanding. For example if we have a 8bit data such as 0000 1111 it is easier to represent it as 0F (Refer the above table).

In 8051 Microcontrollers we used hex values to load values into any register or port. And it is represented as 0x0F. Where the “0x” stands for using data in the hex format.


There are several ways of using the ports of a Microcontroller and you can use any method which fits you with ease considering the flexibility of the code.

SBIT – Single bit which was used to define a specific pin or bit in the port of a 8051 microcontroller. Say for example if you need to access Pin 5 of the Port 0 in your 8051 Controller you must give it as

sbit led=P0^5

where led is the name assigned by the user which can be altered for your readability.

SFR- Special function registers are the registers which are accessed by means of its address. The address of the ports in the 8051 controller is given below.

PORT 0 – 0x80

PORT 1 – 0x90

PORT2 – 0xA0

PORT3 – 0xB0

To access  Port 1 using SFR we should give it as

sfr led=0x90

Where led is the name assigned by the user which can be altered for your readability.

The Ports of the Microcontroller can also be acessed by means of the Port numbers. For example you need to access Port 2, you can mention it as “P2”

Lets see a Sample code to give you a clear idea about the basic 8051 Programming.


In this below leds in the Port 2 and led in the Pin 0 of the Port 1 is toggled once using the simple statements.
sbit led=P1^0;      //Selecting first bit of the port 1 in 8051
sfr leds=0xA0;       //Accessing the whole Port 2 using address
void delay();
void main()
leds=0xF0;         //Making Lower four bits '0' & higher four as '1' of port 2
led=1;                //Giving high input to Pin 0 of Port 1
delay();              //A software delay
leds=0x0F;        //Making Lower four bits '1' & higher four as '0' of port 2
led=0;               //Giving low input to Pin 0 of Port 1
void delay()
unsigned int i;
for(i=0;i<=5000;i++);        //A delay by using for loop
The following two tabs change content below.
Frank Donald

Frank Donald

Junior Network Engineer at Tata Communications
Frank Donald is an Electronics and Communication Engineer who loves building stuff in his free time. Video games, blogging and programming are the things he loves most

Ask your query

2 Comments on "Basics of 8051 Microcontroller Programming"

Notify of
Sort by:   newest | oldest | most voted

There is a mistake in the description of the code:
leds=0x0F; //Making Lower four bits ‘1’ & higher four as ‘1’ of port 2
it should be:
leds=0x0F; //Making Lower four bits ‘1’ & higher four as ‘0’ of port 2