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 the 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 an 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.
#include<reg51.h> sbit ToggleLed = P2^1; //pin connected to toggle Led void Delay(const unsigned int uCount) //Function to provide delay { unsigned int uLoop1=0; unsigned int uLoop2=0; for(; uLoop1 < uCount; uLoop1++) { for(uLoop2=0; uLoop2 <65535; uLoop2++) {} } } int main() { ToggleLed=0; //configuring as output pin while(1) { ToggleLed=1; //Make pin high Delay(2); ToggleLed=0; // Make pin low Delay(2); } }
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.
#include<reg51.h> #define LED P2 //Port 2 connected to Led #define IN_BINARY(b7,b6,b5,b4,b3,b2,b1,b0) ((b7 << 7)|(b6 << 6)|(b5 << 5)|(b4 << 4)|(b3 << 3)|(b2 << 2)|(b1 << 1)|b0) void Delay(const unsigned int uCount) //Delay function { unsigned int uLoop1=0; unsigned int uLoop2=0; for(; uLoop1 < uCount; uLoop1++) { for(uLoop2=0; uLoop2 <65535; uLoop2++) { } } } int main() { LED=0xfd; //configuring as output port while(1) { LED=IN_BINARY(1,1,1,1,1,1,0,1); Delay(2); LED=IN_BINARY(1,1,1,1,1,1,1,1); Delay(2); } }
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.
#include<reg51.h> #define LED P2 //Port 2 connected to Led #define SET_LED(x) (1<<x) //Macro to set the Pin #define CLEAR_LED(x) (~(1<<x)) //Macro to clear the Pin void Delay(const unsigned int uCount) //Delay function { unsigned int uLoop1=0; unsigned int uLoop2=0; for(; uLoop1 < uCount; uLoop1++) { for(uLoop2=0; uLoop2 <65535; uLoop2++) { } } } int main() { LED=0xfd; //configuring as output port while(1) { LED |= SET_LED(1); //set the 2nd bit Delay(2); LED &= CLEAR_LED(1); //clear the 2nd bit Delay(2); } return 0; }
Recommended Post:
- Led blinking program in c for 8051.
- Interfacing of switch and led using the 8051
- Interfacing of Relay with 8051 microcontroller
- Moving message display on LCD using 8051
- LCD 4-bit mode c code for 8051.
- Create LCD custom characters for 16×2 alphanumeric LCD
- Interfacing of keypad with 8051
- Electronic digital lock using the 8051
- Interfacing of EEPROM with 8051 microcontrollers using I2C
- Embedded c interview questions.
- 8051 Microcontroller Pin Diagram and Pin Description.
- Can protocol interview questions.
- 8051 Architecture.