7.

8051 INTERFACING: LEDS
Introduction
Light Emitting Diodes as we know are PN Junction diodes which emit light when forward biased. Well, lets
say they simply emit light! They come in various packages through hole as well as SMD. They are basically
used to:

Indicate System status, ON/OFF etc.

LED lighting: Well, recent technology trends show a increasing usage of LEDs in domestic and
industrial lighting. And there are cool projects in DIY community too. Check the Lumapad for instance.

Also you could build some display projects like the persistence of vision[POV].

Well, let’s just start interfacing a simple LED with the microcontroller.

Objective
Turn ON LEDs for a second, and turn OFF LEDs for a second repeatedly. The LEDs are connected to
any of the four ports of the 8051 microcontroller.
Schematic Diagram

1

2 . The second section describes the LED interfacing section. LED Interface As we know. It could be using a battery or a simple regulated power supply.c' files depending on the peripheral or control features used. The microcontroller resets when RST pin has 5v. You may wish to connect to any of the other ports as well. the controller resets. keeping common ground. following components are required:  Clock Source: It is provided by a crystal oscillator and two capacitors as shown in the schematic. Bare Minimum To make a micrcontroller work. hence to limit the current to 10mA a resistance of 470Ω is used in series with the LED. which will be common for any interface. the main.  Reset Circuitry: The reset pin should be tied to ground. the minimum connections required are discussed in the first section.  Header files o The header files will contain function prototypes of the included c files. The microcontroller provides a 5V output on a port pin.  C files o The c files primarily contain.  Power supply: A +5V power source is required. 8 LEDs are interfaced to PORT2 of the schematic as shown.The schematic can be divided in two logical parts for the microcontroller. In the circuit shown above. Code The code is divided mainly in to two sections for all the interfaces.c and other '*. whenever a switch is pressed. The 5mm Led used in this tutorial has a forward voltage of 2v and maximum current rating of 20mA. glows the LED. providing a logic 1(5v) from microcontroller pin to the anode of the LED.

LedOn sets all the 8 bits and LedOff clears all the 8 bits. registers and Special function registers of the 8051 family. Lets look at the first two lines of the code #include <reg51.h"  #include <reg51. hence the loop never ends!  In the first set of instructions turns ON all leds connected the four ports. Whenever a interface.c The code below is included in Led_Blinking. It generates specified delay in microsecond. for a second. This helps the compiler to associate the name of ports and registers used in the c code to their addresses.This makes the code reusable.h> #include "delay.h" #define LedOn 0xFF //sending a logic one turns on the LED #define LedOff 0x00 //sending logic zero turns it off /* start the main program */ void main() { while(1) { /* Turn On all the leds and wait for one second */ P0=LedOn. milliseconds and seconds.c) and a corresponding header(. And this repeats forever! /* Reg51.h" This is the delay library file.h> contains definitions of all the ports.h contains the defnition of all ports and SFRs */ #include <reg51. 3 . P1=LedOn.  #include "delay.  The main function contains a while(1) function.g lcd.c file. This file contains the main() function with which execution of the code begins. Then we define LedOn and LedOff as two constants. The argument 1 to function which is always true. lets start with a simple example.h) file is included. Led_Blinking. for a second and next set turns them OFF. a source file(e. You could view a step by step procedure to configure the project in the 8051 tools set up tutorial. 3 So. The entire project can be downloaded from the link at the end of the page. or feature is required to be tested.h> #include "delay.

c contains functions to generate the required delay. P3=LedOn. P2=LedOff. P1=LedOff. P3=LedOff.P2=LedOn. the delay. 4 . } } delay.c Library file As discussed earlier. delay_sec(1). delay_sec(1). /* Turn off all the leds and wait for one second */ P0=LedOff.