Line 71: Line 71:
 
======delay.c Library file======
 
======delay.c Library file======
 
{{Box|type = l_green_light|text=<br/>
 
{{Box|type = l_green_light|text=<br/>
As discussed earlier, the delay.c contains functions to generate the required delay.
+
As discussed earlier, the delay.c contains functions to generate the required delay. It can be accessed [[8051_Family_C_Library#Delay.c:_8051_delay_C-Library|here.]]
 
}}
 
}}
<syntaxhighlight>
 
/*----------------------------------------------------------------------------------
 
                      8051 delay library  with the crystal frequency 11.0592MHz
 
Filename: delay.c
 
Controller: P89V51RD2/89c51(8051 family)
 
Oscillator: 11.0592 MHz
 
Author: XploreLabz
 
website: www.xplorelabz.com
 
----------------------------------------------------------------------------------*/
 
#include<reg51.h>
 
 
/*----------------------------------------------------------------------------------
 
                        void delay_us(unsigned int n)
 
----------------------------------------------------------------------------------
 
* I/P Arguments: unsigned int.
 
* Return value : none
 
 
* description :
 
        This function is used generate delay in us.
 
        It genarates a approximate delay of 10us for each count,
 
        if 5000 is passed as the argument then it generates a delay of apprx 50ms.  
 
 
-----------------------------------------------------------------------------------*/
 
void delay_us(unsigned int us_count)
 
{
 
    while(us_count!=0)
 
      {
 
us_count--;
 
  }
 
  }
 
 
/*----------------------------------------------------------------------------------
 
                        void delay_ms(unsigned int n)
 
-----------------------------------------------------------------------------------
 
* I/P Arguments: unsigned int.
 
* Return value : none
 
 
* description:
 
    This function is used generate delay in ms.
 
    It genarates a approximate delay of 1ms for each count,
 
    if 1000 is passed as the argument then it generates delay of apprx 1000ms(1sec)
 
-----------------------------------------------------------------------------------*/
 
void delay_ms(unsigned int ms_count)
 
{
 
        while(ms_count!=0)
 
{
 
        delay_us(112); //delay_us is called to generate 1ms delay
 
ms_count--;
 
}
 
 
  }
 
 
 
/*----------------------------------------------------------------------------------
 
                        void delay_sec(unsigned char sec_count)
 
  -----------------------------------------------------------------------------------
 
  * I/P Arguments: unsigned char.
 
  * Return value : none
 
 
  * description:
 
      This function is used generate delay in sec .
 
      It genarates a approximate delay of 1sec for each count,
 
      if 10 is passed as the argument then it generates delay of apprx 10sec
 
 
    note: A max of 255 sec delay can be generated wsing this function.
 
-----------------------------------------------------------------------------------*/
 
void delay_sec(unsigned char sec_count)
 
{
 
 
 
while(sec_count!=0)
 
  {
 
    delay_ms(1000); //delay_ms is called to generate 1sec delay
 
sec_count--;
 
}
 
  }
 
</syntaxhighlight>
 

Revision as of 21:12, 15 December 2013

LED Interfacing with 8051


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, lets just start interfacing a simple LED with the micrcontroller.

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
Code


The code is divided mainly in to two sections for all the interfaces.

  • C files
    • The c files primarily contain, the main.c and other '*.c' files depending on the peripheral or control features used.
  • Header files
    • The header files will contain function prototypes of the included c files.

This makes the code reusable. Whenever a interface, or feature is required to be tested, a source file(e.g lcd.c) and a corresponding header(.h) file is included.

3

So, lets start with a simple example. The entire project can be downloaded from the link at the end of the page. You could view a step by step procedure to configure the project in the 8051 tools set up tutorial. The code below is included in Led_Blinking.c file. This file contains the main() function with which execution of the code begins. Lets look at the first two lines of the code

#include <reg51.h> 
#include "delay.h"
  • #include <reg51.h> contains definitions of all the ports, registers and Special function registers of the 8051 family. This helps the compiler to associate the name of ports and registers used in the c code to their addresses.
  • #include "delay.h" This is the delay library file. It generates specified delay in microsecond, milliseconds and seconds.

Then we define LedOn and LedOff as two constants. LedOn sets all the 8 bits and LedOff clears all the 8 bits.

  • The main function contains a while(1) function. The argument 1 to function which is always true, hence the loop never ends!
  • In the first set of instructions turns ON all leds connected the four ports, for a second and next set turns them OFF, for a second. And this repeats forever!
/* Reg51.h contains the defnition of all ports and SFRs */
#include <reg51.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;
	   P1=LedOn;
	   P2=LedOn;
	   P3=LedOn;
	   delay_sec(1);
	 /* Turn off all the leds and wait for one second */
	   P0=LedOff;
	   P1=LedOff;
	   P2=LedOff;
	   P3=LedOff;				   
	   delay_sec(1);
     }
}
delay.c Library file


As discussed earlier, the delay.c contains functions to generate the required delay. It can be accessed here.