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 – LEDLight 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
- Windows machine (Any Version)
- Keil IDE tool for 8051
- ProgISP v1.72
Required components and Programmer
- 1x AT89S51 Controller
- 1x 4Mhz Crystal
- 2x 22pf capacitor
- 1x LED 5v
- ISP AVR USB programmer
Circuit Diagram
C code
- See on GitHub AEW_Blink_LED.c
// 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. | Code | Explanation |
---|---|---|
1-5 | // http://esp8266iot.blogspot.in/ // http://aruneworld.blogspot.com/ // Tested By : Arun(20170227) // Example Name : AEW_Blink_LED.lua | Comments 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. |
11 | sbit 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-24 | void delay(void) ; | Function prototype declaration for delay() . |
26-35 | void main (void) | Main function declaration. |
28 | LED = 0 ; | Configures the LED pin as an output by setting it to 0. |
29-33 | while(1) | Initiates an infinite loop to ensure the LED continues blinking indefinitely. |
30 | LED = 0; | Turns off the LED. |
31 | delay(); | Calls the delay() function to introduce a delay. |
32 | LED = 1; | Turns on the LED. |
33 | delay(); | Calls the delay() function again to introduce another delay. |
37-47 | void delay(void) | Function definition for delay() . |
39-46 | int 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
- See on GitHub AEW_Blink_LED.asm
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
Address | Code | Mnemonic | Description |
---|---|---|---|
0000 | LJMP 082BH | LJMP | Long Jump to address 082Bh |
0800 | CLR A | CLR | Clear Accumulator (A) |
0801 | MOV R7,A | MOV | Move Accumulator (A) to Register 7 (R7) |
0802 | MOV R6,A | MOV | Move Accumulator (A) to Register 6 (R6) |
0803 | CLR A | CLR | Clear Accumulator (A) |
0804 | MOV R5,A | MOV | Move Accumulator (A) to Register 5 (R5) |
0805 | MOV R4,A | MOV | Move Accumulator (A) to Register 4 (R4) |
0806 | INC R5 | INC | Increment Register 5 (R5) |
0807 | CJNE R5,#00H,01H | CJNE | Compare and Jump if Not Equal; Compare R5 to 00H, if not equal, jump to address 01H |
080A | INC R4 | INC | Increment Register 4 (R4) |
080B | CJNE R4,#27H,0F8H | CJNE | Compare and Jump if Not Equal; Compare R4 to 27H, if not equal, jump to address 0F8H |
080E | CJNE R5,#10H,0F5H | CJNE | Compare and Jump if Not Equal; Compare R5 to 10H, if not equal, jump to address 0F5H |
0811 | INC R7 | INC | Increment Register 7 (R7) |
0812 | CJNE R7,#00H,01H | CJNE | Compare and Jump if Not Equal; Compare R7 to 00H, if not equal, jump to address 01H |
0815 | INC R6 | INC | Increment Register 6 (R6) |
0816 | MOV A,R7 | MOV | Move Register 7 (R7) to Accumulator (A) |
0817 | XRL A,#0AH | XRL | Exclusive OR with Immediate; Perform exclusive OR operation between A and 0AH |
0819 | ORL A,R6 | ORL | Logical OR between Accumulator (A) and Register 6 (R6) |
081A | JNZ 0E7H | JNZ | Jump if Not Zero; Jump to address 0E7H if the result of the previous operation is not zero |
081C | RET | RET | Return from Subroutine |
Table 2: Instructions from Address 081D to 0834
Address | Code | Mnemonic | Description |
---|---|---|---|
081D | CLR 91H | CLR | Clear the content of memory address 91H |
081F | CLR 91H | CLR | Clear the content of memory address 91H |
0821 | LCALL 0800H | LCALL | Long Call to subroutine at address 0800H |
0824 | SETB 91H | SETB | Set the content of memory address 91H |
0826 | LCALL 0800H | LCALL | Long Call to subroutine at address 0800H |
0829 | SJMP 0F4H | SJMP | Short Jump to address 0F4H |
082B | MOV R0,#7FH | MOV | Move Immediate to Register; Move the value 7FH to Register 0 (R0) |
082D | CLR A | CLR | Clear Accumulator (A) |
082E | MOV @R0,A | MOV | Move Accumulator (A) to the memory location pointed by Register 0 (R0) |
082F | DJNZ R0,0FDH | DJNZ | Decrement and Jump if Not Zero; Decrement Register 0 (R0) and if the result is not zero, jump to address 0FDH |
0831 | MOV 81H,#07H | MOV | Move Immediate to Register; Move the value 07H to the memory location pointed by Register 1 (81H) |
0834 | LJMP 081DH | LJMP | Long Jump to address 081DH |
NEXT
8051 – Introduction |
8051 – Program Methods |
8051 – Flash HEX into 8051 |
8051 – USB ISP Programmer |
8051 – Simulators |
|
8051 Interface – LED |
8051 Interface – LCD |
8051 Interface – 7 Segment |
8051 Interface – Keypad |
8051 Interface – Servo |
|
8051 – UART Bit banking |
8051 – I2C Bit banking (Add Soon) |
|
8051 – 10Khz Square Wave |
|
8051 – Interview Questions |
You must be logged in to post a comment.