C Programming and Interfacing with Intel 80C51 Microcontroller

| |
In this tutorial, we illustrate how to write C program that turns on and off a LED connected to the port of a Intel 80C51 microcontroller chip. Such simple project forms the basis of embedded software development with C.

Intel 80C51 is an 8 bit microcontroller used in many areas of embedded systems design. For our purpose here it should be noted that the 80C51 has 4 ports called Port0, Port1, Port2 and Port3. All of these are bidirectional port. Each of the port has 8 pins and each of them can be written to and read from. So there are 4x8=32 I/O ports.

Port 1 is different from others in that it is designed as an I/O port. Other ports additional to input/output function also provides other special functions which will not be discussed here.

Here we will look at how the individual pins of the Port 1 can be controlled. That is how to send a bit, read a bit and how to send a byte and read a byte. This is how you usually begin learning a microcontroller and C programming as well.

We will start by writing 1 bit to the port 1 pins.

Consider the following schematic:


In the above figure, a LED is connected to the Port 1, pin 0. So to turn on the LED we must have logic high at the port pin 0. The C program for doing this is as follows,

#include <reg51.h>

sbit mybit = P1^0;

void main(void)
     {
      mybit = 0;
      }

In the above code, the Port 1 bit 0 is assigned a variable called mybit is declared using the sbit data type. In the main function this variable is set to 0 meaning that port 1 bit 0 is set to logic 0 and therefore doing this will not turn on the LED.

But if we change the above code so that mybit = 1; then now the Port 1 bit 0 has logic 1 high and therefore due to the potential difference created between that pin and the ground current flows into the LED and the LED turns on.

So to turn on the LED we would write the C program as follows:

#include <reg51.h>

sbit mybit = P1^0;

void main(void)
     {
      mybit = 1;
      }

The result is shown,


Notice that the LED is turned into blue.

In the above schematics we did not limit the current through the LED. One should add a resistor to limit the current flowing into the LED so that it does not get excessive current otherwise it will burn and get damaged.

So lets add a resistor before we move on. The LED has 2.2V forward voltage and current limit of 10mA. If the Pin voltage is at 5V then we can calculate the resistor value to limit the current into the resistor to 10mA.

R = (5V-2.2V)/10mA = 380 ohm ~370ohm.

OK, see the diagram below



Above we showed you how to turn off and on a LED at the port 1 pin 0 of the microcontroller.

In next tutorial post we illustrate how to write program with delays to turn on and off the LED.

For learning what is Proteus and how it is useful for embedded system development see the blog post- Embedded Systems Programming with Proteus Software.

Related Posts by Categories

0 comments:

Post a Comment