Led blinking program in c for 8051.

toggle the led blinking

A Led blinking is a basic program. In this article, we will learn how to interface led with 8051 micro-controller. The 8051 is basic micro-controller and it’s the best for a beginner.In 8051 when we have turned the power on, all I/O pins have the high value. Means that initially all I/O pins work as input except PORT 0 because in 8051 all ports have the inbuilt pull-up resistor except PORT 0.

So when we want to make port pin as output we have to write 0 first time on that pin.In this Article, we will learn, how to write a Led blinking program for 8051.

Suppose a scenario where we want to toggle a led which attached with 2nd Pin of PORT 2.

Steps to write the Program

  • Firstly you have to select the port pin for led.
  • You have to configure the pin as output to write the first value 0.
  • Now pin configured as the output, if you have passed 1 or 0 on that pin it will directly reflect on it.

Here I am describing some method to write a led toggling program

Method 1

In this method, we use the sbit (single bit) keyword to select the port pin. It allows accessing of a single bit of an SFR register.

Syntax to use sbit

sbit   Any_Name  =   Px^y;

x is port 0,1,2 or 3.
y is pin o,1,..7.



If you want to learn more about the c language, here 10 Free days (up to 200 minutes) C video course for you.

Your free trial is waiting


Method 2

In this method, I have created a macro IN_BINARY, which takes input in 1 and 0 format. It set and reset the PORT Pin corresponding the input binary value.



Method 3

In this method, I am using the bit-wise operator to set and clear the bit. In below example, I have created two macro SET_BIT and CLEAR_BIT which are using to set and clear the PIN of PORT 2.

Leave a Reply