echo '' ;

8051 Interface – LED

In this 8051 Interface LED tutorial, you will learn how to implement a “Hello World” LED Blinking project in Keil for a microcontroller. Additionally, we have chosen the AT89S51 microcontroller (although you can select any other microcontroller supported by Keil) for demonstration purposes. This project is straightforward and can be easily followed by the steps outlined below.

Read more: 8051 Interface – LED

Light Emitting Diodes (LEDs) are fundamental and widely used electronic components for displaying digital signal states. When current flows through an LED, it emits light. However, excessive current can damage it; therefore, a current-limiting resistor is necessary. Commonly used resistors for this purpose include 220, 470, and 1K ohms. Depending on the desired brightness, any of these resistors can be utilized. Let’s begin by blinking LEDs; subsequently, we can proceed to generate various patterns using the available LEDs.

An essential aspect of any controller is the number of General Purpose Input/Output (GPIO) pins available for connecting peripherals. The 8051 microcontroller features 32 GPIOs organized into four ports, namely P0 to P3.

Required software

Required components and Programmer

  • 1x AT89S51 Controller
  • 1x 4Mhz Crystal
  • 2x 22pf capacitor
  • 1x LED 5v
  • ISP AVR USB programmer

Circuit Diagram

 C code

// http://esp8266iot.blogspot.in/
// http://aruneworld.blogspot.com/
// Tested By 	: Arun(20170227)
// Example Name : AEW_Blink_LED.lua

// Program to blink an LED at Port pin P1.0 (physical pin 1 of IC)

#include<reg52.h>  // special function register declarations for 89s52                

#include<stdio.h>  // prototype declarations for I/O functions

sbit LED = P1^1;    // defining pin P1^0 as LED

void delay(void) ;  //delay function prototype declaration

void main (void)
   
{
    LED = 0 ;              // Make LED pin as Output
    while(1)                //indefinite loop
    {
       LED = 0;           // LED Off
       delay();
       LED = 1;          // LED ON 
       delay();
    }
}

void delay(void)
{
    int j;
    int i;
    for(i=0;i<10;i++)
    {
        for(j=0;j<10000;j++)
        {
        }
    }
}

C Code Explanation

This table breaks down each line of the code along with its explanation to provide a clear understanding of the functionality of each part of the program.

Line No.CodeExplanation
1-5// http://esp8266iot.blogspot.in/ // http://aruneworld.blogspot.com/ // Tested By : Arun(20170227) // Example Name : AEW_Blink_LED.luaComments providing information about the code, including source references, testing details, and example name.
7#include<reg52.h>Includes the header file reg52.h, which contains special function register declarations for the AT89S52 microcontroller.
9#include<stdio.h>Includes the standard I/O header file stdio.h, although it seems unnecessary in this code since there are no standard I/O functions used.
11sbit LED = P1^1;Declares a bit-specific variable named LED, representing pin P1.1. This syntax is specific to the 8051 family of microcontrollers.
13-24void delay(void) ;Function prototype declaration for delay().
26-35void main (void)Main function declaration.
28LED = 0 ;Configures the LED pin as an output by setting it to 0.
29-33while(1)Initiates an infinite loop to ensure the LED continues blinking indefinitely.
30LED = 0;Turns off the LED.
31delay();Calls the delay() function to introduce a delay.
32LED = 1;Turns on the LED.
33delay();Calls the delay() function again to introduce another delay.
37-47void delay(void)Function definition for delay().
39-46int j; int i; for(i=0;i<10;i++) { for(j=0;j<10000;j++) { } }Nested loops to generate a time delay. The outer loop iterates 10 times, and the inner loop iterates 10,000 times, creating an approximate delay.

Assembly Code

      ORG 0000H
0000| 	LJMP 082BH 
      ORG 0800H
0800| 	CLR A
0801| 	MOV R7,A
0802| 	MOV R6,A
0803| 	CLR A
0804| 	MOV R5,A
0805| 	MOV R4,A
0806| 	INC R5
0807| 	CJNE R5,#00H,01H
080A| 	INC R4
080B| 	CJNE R4,#27H,0F8H
080E| 	CJNE R5,#10H,0F5H
0811| 	INC R7
0812| 	CJNE R7,#00H,01H
0815| 	INC R6
0816| 	MOV A,R7
0817| 	XRL A,#0AH
0819| 	ORL A,R6
081A| 	JNZ 0E7H
081C| 	RET
081D| 	CLR 91H
081F| 	CLR 91H
0821| 	LCALL 0800H
0824| 	SETB 91H
0826| 	LCALL 0800H
0829| 	SJMP 0F4H
082B| 	MOV R0,#7FH
082D| 	CLR A
082E| 	MOV @R0,A
082F| 	DJNZ R0,0FDH
0831| 	MOV 81H,#07H
0834| 	LJMP 081DH
      	END

Assembly Code Explanations

These tables provide a clear separation of the instructions, making it easier to read and understand each part of the code.

Table 1: Instructions from Address 0000 to 082B

AddressCodeMnemonicDescription
0000LJMP 082BHLJMPLong Jump to address 082Bh
0800CLR ACLRClear Accumulator (A)
0801MOV R7,AMOVMove Accumulator (A) to Register 7 (R7)
0802MOV R6,AMOVMove Accumulator (A) to Register 6 (R6)
0803CLR ACLRClear Accumulator (A)
0804MOV R5,AMOVMove Accumulator (A) to Register 5 (R5)
0805MOV R4,AMOVMove Accumulator (A) to Register 4 (R4)
0806INC R5INCIncrement Register 5 (R5)
0807CJNE R5,#00H,01HCJNECompare and Jump if Not Equal; Compare R5 to 00H, if not equal, jump to address 01H
080AINC R4INCIncrement Register 4 (R4)
080BCJNE R4,#27H,0F8HCJNECompare and Jump if Not Equal; Compare R4 to 27H, if not equal, jump to address 0F8H
080ECJNE R5,#10H,0F5HCJNECompare and Jump if Not Equal; Compare R5 to 10H, if not equal, jump to address 0F5H
0811INC R7INCIncrement Register 7 (R7)
0812CJNE R7,#00H,01HCJNECompare and Jump if Not Equal; Compare R7 to 00H, if not equal, jump to address 01H
0815INC R6INCIncrement Register 6 (R6)
0816MOV A,R7MOVMove Register 7 (R7) to Accumulator (A)
0817XRL A,#0AHXRLExclusive OR with Immediate; Perform exclusive OR operation between A and 0AH
0819ORL A,R6ORLLogical OR between Accumulator (A) and Register 6 (R6)
081AJNZ 0E7HJNZJump if Not Zero; Jump to address 0E7H if the result of the previous operation is not zero
081CRETRETReturn from Subroutine

Table 2: Instructions from Address 081D to 0834

AddressCodeMnemonicDescription
081DCLR 91HCLRClear the content of memory address 91H
081FCLR 91HCLRClear the content of memory address 91H
0821LCALL 0800HLCALLLong Call to subroutine at address 0800H
0824SETB 91HSETBSet the content of memory address 91H
0826LCALL 0800HLCALLLong Call to subroutine at address 0800H
0829SJMP 0F4HSJMPShort Jump to address 0F4H
082BMOV R0,#7FHMOVMove Immediate to Register; Move the value 7FH to Register 0 (R0)
082DCLR ACLRClear Accumulator (A)
082EMOV @R0,AMOVMove Accumulator (A) to the memory location pointed by Register 0 (R0)
082FDJNZ R0,0FDHDJNZDecrement and Jump if Not Zero; Decrement Register 0 (R0) and if the result is not zero, jump to address 0FDH
0831MOV 81H,#07HMOVMove Immediate to Register; Move the value 07H to the memory location pointed by Register 1 (81H)
0834LJMP 081DHLJMPLong Jump to address 081DH

NEXT

8051 – Introduction
8051 – Program Methods
8051 – Flash HEX into 8051
8051 – USB ISP Programmer
8051 – Simulators
8051 Interface
8051 Interface – LED
8051 Interface – LCD
8051 Interface – 7 Segment
8051 Interface – Keypad
8051 Interface – Servo
8051 Protocol Interface
8051 – UART Bit banking
8051 – I2C Bit banking (Add Soon)
8051 Tutorials
8051 – 10Khz Square Wave
Others
8051 – Interview Questions

Leave a Reply

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

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading