8051 – GPIO Tutorial

Hi All… In this tutorial, we are going to see the 8051 GPIO Tutorial. Before that, if you are not installing Keil please install that. If you don’t know please go through them here. Please see here if you don’t know how to create the project. See the introduction of 8051 Here.

Let’s start!!!!!

Before we start, please read this article to get to know how the GPIO is working in any microcontroller.

8051 GPIO Tutorial

Introduction

Normally 8051 has four GPIO ports.

  1. Port 0 (P0)
  2. Port 1 (P1)
  3. Port 2 (P2)
  4. Port 3 (P3)

I have already discussed the Ports in the introduction. So now we can directly go into the programming.

WRITING ON A PORT PIN

Writing the data on a particular port is sending the data from the controller to any peripheral device which is connected with the controller. For example, here we are using the LED to indicate the data coming out from the parallel port.

READING FROM A PORT

Reading operation is getting the data from any peripheral device. Here we are using a simple push-button (switch) to give the input to the port pins.

LED Interfacing

A light-emitting diode (LED) is essentially a PN junction Opto-semiconductor that emits a monochromatic (single color) light when operated in a forward-biased direction. LEDs convert electrical energy into light energy. They are frequently used as “pilot” lights in electronic appliances to indicate whether the circuit is closed or not.

Pin Diagram

Here LEDs are connected to Port 2 (P2). If you are going to connect LEDs into Port 0 (P0) you should connect the pull-up resistor.

CODE

#include<reg51.h>
void main()
{ 
    unsigned int i; 
    while(1) 
    { 
        P2=0x00; 
        for(i=0;i<=30000;i++); 
        P2=0xff; 
        for(i=0;i<=30000;i++); 
    } 
}

OUTPUT

8051 GPIO Example

[ Image: LED Output ]

Switch Interfacing

Pin Diagram

Here we are connecting the switch into Port 1 . 0 (P1.0). Then LEDs are connected to P2. Whenever I press the switch these LEDs should glow.

CODE

#include<reg51.h>
 
sbit sw=P1^0;  
 
void main() 
{      while(1) 
    { 
        if( sw == 0)  
        { 
            P2 = 0xFF; 
        } 
        else  
        { 
            P2 = 0x00; 
         } 
     }
}

 

OUTPUT

8051 GPIO Tutorial

[ Image Output ]

 

Hope you enjoyed this tutorial. But this is the basic. You can do whatever you want. I will give you some exercises. Do practice it.

Tasks

  1. Blink alternate LEDs at P3 using software delay
  2. Blink P0 LEDs in cyclic fashion using software delay
  3. Count the number of times a switch at P1.1 is pressed and display the count in P2If you have any doubt please comment below.

In our next tutorial, we will see LCD interfacing.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
STM32F7 Bootloader TutorialsRaspberry PI Pico Tutorials
STM32F103 Bootloader TutorialsRT-Thread RTOS Tutorials
Zephyr RTOS Tutorials - STM32Zephyr RTOS Tutorials - ESP32
AUTOSAR TutorialsUDS Protocol Tutorials
Product ReviewsSTM32 MikroC Bootloader Tutorial
VHDL Tutorials
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Table of Contents