echo '' ;

Archives

8051 Interface – LCD


Interfacing an LCD (Liquid Crystal Display) with the 8051 microcontroller is a common practice in embedded systems for displaying information. Here’s a brief overview of 8051 Interface LCD

  • Hardware Connections: Connect the data lines (D0-D7) of the LCD to the GPIO pins of the 8051. Connect the control lines (RS, RW, E) to specific GPIO pins. Optionally, connect the backlight control pin and adjust the contrast using a potentiometer.
  • Initialization: Send initialization commands to the LCD to configure its operation mode, display settings, and other parameters. This typically involves sending specific command codes over the data lines.
  • Sending Data: To display characters or strings on the LCD, send the ASCII codes of the characters over the data lines while setting the RS (Register Select) line appropriately to indicate data transmission mode.
  • Sending Commands: To control the operation of the LCD (such as clearing the display, setting the cursor position, etc.), send command codes over the data lines while setting the RS line to indicate command transmission mode.
  • Timing Considerations: Ensure proper timing between data/command transmissions and the strobing of the E (Enable) line to ensure reliable communication with the LCD.
  • Busy Flag Checking: Check the busy flag of the LCD before sending new commands or data to ensure that the LCD is ready to receive new instructions.
  • Writing Custom Characters: Some LCDs allow you to define custom characters by programming specific patterns into the CGRAM (Character Generator RAM) of the LCD.
  • Example Code: Here’s a basic example of initializing and sending data to an LCD connected to an 8051 microcontroller:
Read more: 8051 Interface – LCD

Uses of LCD

  • Displaying Information: LCDs (Liquid Crystal Displays) are used to visually display information such as text, numbers, and symbols.
  • Interface Compatibility: They can be easily interfaced with 8051 microcontrollers through parallel or serial communication interfaces.
  • Textual Output: LCDs allow for the display of textual information, making them suitable for user interfaces or information panels.
  • Low Power Consumption: They typically consume less power compared to other display technologies, making them suitable for battery-powered devices.
  • Compact Size: LCD modules come in various sizes, allowing for flexibility in design and integration into compact devices.
  • Backlighting Options: Some LCD modules offer backlighting options, enabling visibility in low-light conditions.
  • Dynamic Display: LCDs can be dynamically updated to show changing information or real-time data.
  • Multipurpose Usage: They find applications in various domains such as digital clocks, thermometers, calculators, and industrial control panels.

Next will see about code of 8051 Interface LCD

Code Header file (LCD8.h)

#define LCD_First_Line     0x80
#define LCD_Second_Line    0xc0
#define LCD_Curser_On          0x0f
#define LCD_Curser_Off         0x0c
#define LCD_Clear_Display 0x01
#define Lcd8_Data_Port         P2

sbit Lcd8_RS = P0^0;
sbit Lcd8_RW = P0^1;
sbit Lcd8_EN = P0^2;

void Lcd8_Init();
void Lcd8_Command(unsigned char);
void Lcd8_Write(unsigned char,unsigned char);
void Lcd8_Display(unsigned char,const unsigned char*,unsigned int);
void Lcd8_Decimal2(unsigned char,unsigned char);
void Lcd8_Decimal3(unsigned char,unsigned char);
void Lcd8_Decimal4(unsigned char,unsigned int);
void Delay(unsigned int);

void Lcd8_Init()
{
    Lcd8_Command(0x38);     //to select function set
    Lcd8_Command(0x06);     //entry mode set
    Lcd8_Command(0x0c);     //display on
    Lcd8_Command(0x01);     //clear display
}

void Lcd8_Command(unsigned char com)
{
    Lcd8_Data_Port=com;
    Lcd8_EN=1;
    Lcd8_RS=Lcd8_RW=0;
    Delay(125);
    Lcd8_EN=0;
    Delay(125);
}

void Lcd8_Write(unsigned char com,unsigned char lr)
{
    Lcd8_Command(com);

    Lcd8_Data_Port=lr;          // Data 
    Lcd8_EN=Lcd8_RS=1;
    Lcd8_RW=0;
    Delay(125);
    Lcd8_EN=0;
    Delay(125);
}

void Lcd8_Display(unsigned char com,const unsigned char *word,unsigned int n)
{
    unsigned char Lcd_i;

    for(Lcd_i=0;Lcd_i<n;Lcd_i++)
    { 
        Lcd8_Write(com+Lcd_i,word[Lcd_i]);
    }
}

void Lcd8_Decimal2(unsigned char com,unsigned char val)
{
    unsigned int Lcd_hr,Lcd_t,Lcd_o;

    Lcd_hr=val%100;
    Lcd_t=Lcd_hr/10;
    Lcd_o=Lcd_hr%10;
    
    Lcd8_Write(com,Lcd_t+0x30);
    Lcd8_Write(com+1,Lcd_o+0x30);
}


void Lcd8_Decimal3(unsigned char com,unsigned char val)
{
    unsigned int Lcd_h,Lcd_hr,Lcd_t,Lcd_o;

    Lcd_h=val/100;
    Lcd_hr=val%100;
    Lcd_t=Lcd_hr/10;
    Lcd_o=Lcd_hr%10;
    
    Lcd8_Write(com,Lcd_h+0x30);
    Lcd8_Write(com+1,Lcd_t+0x30);
    Lcd8_Write(com+2,Lcd_o+0x30);
}

void Lcd8_Decimal4(unsigned char com,unsigned int val) 
{
    unsigned int Lcd_th,Lcd_thr,Lcd_h,Lcd_hr,Lcd_t,Lcd_o;

    val = val%10000;
    Lcd_th=val/1000;
    Lcd_thr=val%1000;
    Lcd_h=Lcd_thr/100;
    Lcd_hr=Lcd_thr%100;
    Lcd_t=Lcd_hr/10;
    Lcd_o=Lcd_hr%10;

    Lcd8_Write(com,Lcd_th+0x30);
    Lcd8_Write(com+1,Lcd_h+0x30);
    Lcd8_Write(com+2,Lcd_t+0x30);
    Lcd8_Write(com+3,Lcd_o+0x30);
}

void Delay(unsigned int del)
{
    while(del--);
}     

Code Main File (LCD8_Interface_Main.c)

#include<reg51.h>
#include "LCD8.h"

void timer_init(void);

static unsigned char i;
static unsigned int x=100,y=150,cnt; 

void main()
{
    Lcd8_Init();
    Lcd8_Display(LCD_First_Line,"   Hello World  ",16);
    while(1)
    {
        Lcd8_Display(LCD_Second_Line," ArunEworld.com ",16);
    }//while(1)
}//void main()

8051 Interface – Servo

This tutorial demonstrates how to interface a servo motor with an 8051 microcontroller, allowing precise control over the motor’s position and movement.

One of the most commonly used motors for precise angular movement is the stepper motor. Its advantage lies in its ability to control the angular position without requiring any feedback mechanism. It finds widespread use in industrial and commercial applications, moreover, it is commonly employed in drive systems such as robots and washing machines.

Read more… →

8051 Interface – Keypad

In this tutorial, we’ll explore the process of interface a 4×4 or 4×3 matrix keypad with the 8051 microcontroller. Its versatility and ease of interfacing with external peripherals make the 8051 microcontroller a popular choice for various embedded systems. One common peripheral is the keypad, which serves as an input device in numerous applications such as security systems, industrial control systems, and consumer electronics.

Interfacing a keypad with the 8051 microcontroller involves connecting the keypad’s rows and columns to specific pins on the microcontroller. The keypad typically consists of a matrix of switches arranged in rows and columns. When a key is pressed, it connects a particular row to a specific column, enabling the microcontroller to detect the pressed key by scanning the rows and columns.

We’ll discuss the hardware connections required and the software implementation to read the pressed keys. Additionally, we’ll demonstrate how to display the pressed keys on an LCD (Liquid Crystal Display) connected to the microcontroller, providing a user-friendly interface for input.

By the end of this tutorial, you’ll have a solid understanding of how to interface a keypad with the 8051 microcontroller, enabling you to incorporate user input functionality into your embedded systems projects.

A widely used input device, the keypad, features in various applications like telephones, computers, ATMs, electronic locks, etc., enabling users to input data for further processing. In this setup, we interface a 4×3 matrix keypad, consisting of switches arranged in rows and columns, with the microcontroller. Additionally, we interface a 16×2 LCD to display the output. The concept of interfacing the keypad is straightforward: each key on the keypad has two unique parameters, row and column (R, C). Whenever a key is pressed, the microcontroller identifies the pressed key by detecting its corresponding row and column numbers on the keypad.

Read more… →

8051 Protocol – UART Bit Banging Method

The “8051 Protocol UART Bit Banging Method” is a technique used to implement UART communication on 8051 microcontrollers without relying solely on dedicated hardware UART modules.

Instead, it involves manually toggling GPIO (General Purpose Input/Output) pins to transmit and receive serial data in accordance with the UART protocol. This approach provides flexibility and enables the implementation of UART communication on microcontrollers lacking dedicated UART hardware.

Uses

ApplicationDescription
Low-Cost ProjectsCost-effective solution for implementing UART communication without additional hardware components.
Prototyping and DevelopmentFacilitates quick implementation of UART functionality during the prototyping and development phase, enabling rapid iteration and testing.
Embedded SystemsProvides a compact and resource-efficient solution for adding UART communication capabilities to 8051-based devices in space-constrained environments.
Education and LearningOffers hands-on experience with microcontroller programming and UART implementation, suitable for educational settings to teach serial communication protocols.
Custom Communication ProtocolsEnables the implementation of custom or non-standard communication protocols tailored to specific project requirements, providing flexibility and customization options.
Legacy Systems IntegrationAllows integration with modern communication interfaces and protocols for legacy systems using 8051 microcontrollers lacking dedicated UART hardware support.

Advantages

  • Cost-effective solution, especially for projects with budget constraints.
  • Enables quick implementation of UART functionality during prototyping and development phases, facilitating rapid iteration.
  • Offers versatility and adaptability, suitable for various applications and scenarios.
  • Provides hands-on learning experience with microcontroller programming and UART implementation in educational settings.
  • Allows integration with modern communication interfaces and protocols for legacy systems using 8051 microcontrollers lacking dedicated UART hardware support.

Disadvantages

  • Requires precise timing control for reliable operation, which may be challenging to achieve, especially at higher baud rates.
  • Less efficient compared to hardware-based UART solutions, resulting in potentially slower communication speeds and increased CPU utilization.
  • Consumes more CPU resources and may limit the microcontroller’s ability to perform other tasks concurrently, especially in multitasking applications.
  • Developers with limited experience in low-level hardware interaction may encounter higher implementation complexity when compared to using dedicated hardware UART modules.

Code

code of 8051 Protocol UART Bit Banging Method

#define crystal_freq 11.0592
#include <reg51.h>
#include <intrins.h>

#define uart_ch1 0
#define uart_ch2 2

sbit tx = P2^0;
sbit rx = P2^1;

void delay() {
    int i;
    for(i = 6; i; i--);
    _nop_();
    _nop_();
}

void delay_ms(int i) {
    int j;
    for(; i; i--)
        for(j = 122; j; j--);
}

void tx_data(char data_, char val) {
    char i;
    P2 |= 0x03;
    tx = 0; // send start bit
    P2 &= ~(1 << val);
    delay();

    for(i = 0; i < 8; i++) {
        if(((data_ >> i) & (0x01)) == 0x01) {
            P2 |= (1 << val);
        } else {
            P2 &= ~(1 << val);
        }
        delay();
    }

    P2 |= (1 << val);
    delay();

    // delay_ms(1);
}

void init_uart() {
    tx = 1;
}

void str(char *ch, char uart0_tx) {
    while(*ch) {
        tx_data(*ch++, uart0_tx);
    }
}

void init() {
    SCON = 0x50;
    TMOD = 0x20;
    TH1 = TL1 = 253;
    TR1 = 1;
}

void tx1(char ch) {
    SBUF = ch;
    while(!TI);
    TI = 0;
}

void str1(char *ch) {
    while(*ch) {
        tx1(*ch++);
    }
}

void main() {
    init_uart(); 
    init();
    while(1) {
        str(".........P2^0 IO PIN UART....\n\r", uart_ch1);
        str(".........P2^1 IO PIN UART.....\n\r", uart_ch2);
        str1(".........INBUILT UART ....\n\r");
    }
}

The explanation for 8051 Protocol UART Bit Banging Method Code

#define crystal_freq 11.0592
#include <reg51.h>
#include <intrins.h>

#define uart_ch1 0
#define uart_ch2 2

sbit tx = P2^0;
sbit rx = P2^1;
  • Header Files: The code begins by including necessary header files. reg51.h is specific to the 8051 microcontroller, while intrins.h provides intrinsic functions.
  • Constants: The crystal frequency is defined as 11.0592 MHz. uart_ch1 and uart_ch2 represent UART channels 0 and 2, respectively.
  • Global Variables: Two SFR (Special Function Register) bits tx and rx are declared to represent the transmit and receive pins of the UART.
void delay() {
    int i;
    for(i = 6; i; i--);
    _nop_();
    _nop_();
}

void delay_ms(int i) {
    int j;
    for(; i; i--)
        for(j = 122; j; j--);
}
  • Delay Functions: delay() and delay_ms() are defined to create timing delays. delay() produces a small delay, and delay_ms() generates a delay in milliseconds.
void tx_data(char data_, char val) {
    char i;
    P2 |= 0x03;
    tx = 0; // send start bit
    P2 &= ~(1 << val);
    delay();

    for(i = 0; i < 8; i++) {
        if(((data_ >> i) & (0x01)) == 0x01) {
            P2 |= (1 << val);
        } else {
            P2 &= ~(1 << val);
        }
        delay();
    }

    P2 |= (1 << val);
    delay();

    // delay_ms(1);
}
  • UART Functions: tx_data() is defined to transmit a byte of data (data_) using the specified UART channel (val). It sends start and stop bits and transmits each data bit, waiting for appropriate delays between each bit.
void init_uart() {
    tx = 1;
}
  • UART Initialization: init_uart() initializes the UART communication by setting the transmit pin (tx) to logic high.
void str(char *ch, char uart0_tx) {
    while(*ch) {
        tx_data(*ch++, uart0_tx);
    }
}

void str1(char *ch) {
    while(*ch) {
        tx1(*ch++);
    }
}
  • String Transmission Functions: str() and str1() transmit strings over UART channels 0 and the inbuilt UART, respectively. They iterate over each character in the string and transmit it using the appropriate function (tx_data() or tx1()).
void init() {
    SCON = 0x50;
    TMOD = 0x20;
    TH1 = TL1 = 253;
    TR1 = 1;
}
  • Initialization: init() initializes the UART and timer modes. It sets the serial control register (SCON), timer mode register (TMOD), and timer 1 high and low bytes (TH1 and TL1). Finally, it starts timer 1 (TR1).
void main() {
    init_uart(); 
    init();
    while(1) {
        str(".........P2^0 IO PIN UART....\n\r", uart_ch1);
        str(".........P2^1 IO PIN UART.....\n\r", uart_ch2);
        str1(".........INBUILT UART ....\n\r");
    }
}
  • Main Function: The program’s entry point is the main() function. It first initializes UART communication and timer modes.

Need of Bit Banging

ReasonDescription
Hardware LimitationsIn scenarios where dedicated hardware modules for certain functionalities like UART communication are not available due to cost constraints or limited chip resources, bit banging offers a software-based alternative for implementing these functionalities.
FlexibilityBit banging provides flexibility in implementing custom communication protocols or interfaces that may not be supported by hardware peripherals. This allows developers to tailor the implementation to specific project requirements and integrate with various systems.
Legacy SystemsIn legacy systems or devices using older microcontrollers, hardware support for modern communication protocols may be lacking. Bit banging enables integration with modern communication interfaces without requiring hardware upgrades, extending the lifespan of existing systems.
Educational PurposesBit banging is frequently utilized in education to offer students practical experience with low-level hardware interaction and communication protocols, aiding in their comprehension of digital communication principles.
Debugging and TestingBit banging aids in debugging and testing by enabling manual control of communication signals, facilitating issue diagnosis and system functionality verification, particularly in the absence of hardware-based debugging tools.

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

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

8051 Interface – 7 Segment Display

This tutorial will discuss about 8051 Interface – 7 Segment Display. 7-Seg display is the most basic electronic display. It consists of eight LEDs which are associated in a sequence manner to display digits from 0 to 9 when proper combinations of LEDs are switched on. A 7-segment display uses seven LEDs to display digits from 0 to 9 and the 8th LED is used for dot. A typical seven-segment look likes as shown in the figure below. The 7-segment displays are used in several systems to display the numeric information. They can display one digit at a time. Thus the number of segments used depends on the number of digits to display. Here the digits 0 to 9 are displayed continuously at a predefined time delay.

Read more: 8051 Interface – 7 Segment Display
7-Segment Display

The 7-segment displays are available in two configurations which are common anode (V+) and common cathode (GND). Here, we use the common anode configuration because the output current of the microcontroller is not sufficient to drive the LEDs. The 7-segment display works on negative logic, we have to provide logic 0 to the corresponding pin to make on LED glow. In this tutorial, you can learn 8051 interfaces 7 segment display with 8051 controller. I chose the AT89S51 micro controller(You can select any other Keil support microcontroller) and demonstrated, that this is very simple and follow this below.

The following table shows the hex values used to display the different digits.

Use of 7 Segment Display

  • Numeric Display: Shows numbers (0-9) and some letters (A-F).
  • Segment Control: Each segment can individually controlled.
  • Connection: Requires connection to microcontroller GPIO pins.
  • Display Logic: Determine which segments to illuminate for each digit.
  • Multiplexing: Can display multiple digits by rapidly switching between them.
  • Example Usage: Digital clocks, timers, counters, and temperature displays.

Required software

Required components and Programmer

  • 1x AT89S51 Controller
  • 1x 4Mhz Crystal
  • 2x 22pf capacitor
  • ISP AVR USB programmer
  • 7 Seg Display

Circuit Diagram

C code

#include<reg51.h>

void delay(int k) //delay function
{
int i,j;
for(i=0;i<k;i++)
  for(j=0;j<1275;j++);
}
void main()
{
    unsigned char i;
    unsigned char arr[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
    P2=0x00;    
    while(1)
    {
        for(i=0;i<10;i++)
        {
            P2=arr[i];
            delay(100);
        }
        
    }
}

Assembly Code (ASM)

//See more on : http://www.ArunEworld.com

ORG 000H //initial starting address
START: MOV A,#00001001B // initial value of accumulator
MOV B,A
MOV R0,#0AH //Register R0 initialized as counter which counts from 10 to 0
LABEL: MOV A,B
INC A
MOV B,A
MOVC A,@A+PC // adds the byte in A to the program counters address
MOV P1,A
ACALL DELAY // calls the delay of the timer
DEC R0//Counter R0 decremented by 1
MOV A,R0 // R0 moved to accumulator to check if it is zero in next instruction.
JZ START //Checks accumulator for zero and jumps to START. Done to check if counting has been finished.
SJMP LABEL
DB 3FH // digit drive pattern for 0
DB 06H // digit drive pattern for 1
DB 5BH // digit drive pattern for 2
DB 4FH // digit drive pattern for 3
DB 66H // digit drive pattern for 4
DB 6DH // digit drive pattern for 5
DB 7DH // digit drive pattern for 6
DB 07H // digit drive pattern for 7
DB 7FH // digit drive pattern for 8
DB 6FH // digit drive pattern for 9
DELAY: MOV R4,#05H // subroutine for delay
WAIT1: MOV R3,#00H
WAIT2: MOV R2,#00H
WAIT3: DJNZ R2,WAIT3
DJNZ R3,WAIT2
DJNZ R4,WAIT1
RET
END

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

8051 Tutorials – 10hz Square wave generator using Timer

In this tutorial, you can learn how to generate a 10Hz square wave using a timer in an 8051 microcontroller. I chose the AT89S51 microcontroller (You can select any other Keil-supported microcontroller) and demonstrated it. Generating a 10Hz square wave using a timer in an 8051 microcontroller is a common task. Here’s a basic tutorial on how to do it:

 

Generating a square wave is a fundamental task in electronics, often required for various purposes such as clock signals, digital communication, and waveform testing. One common way to generate a square wave is by using an astable multivibrator circuit, which typically consists of two transistors or integrated circuits like the 555 timer.

Read more… →

8051 – Programming Technique

Programming for the 8051 microcontroller involves understanding its architecture, instruction set, and specific features. In this “8051 – Programming Technique” Tutorial are some key techniques and considerations for programming the 8051:

Read more: 8051 – Programming Technique

Key Technique and Considerations

Certainly! Here’s the information presented as a table:

Technique or ConsiderationDescription
Understanding the ArchitectureFamiliarize yourself with the basic architecture of the 8051 microcontroller, including its CPU, memory organization, I/O ports, timers/counters, and serial communication ports.
Instruction SetLearn the 8051’s instruction set, including data movement, arithmetic, logical, branching, and control instructions, to perform specific tasks and manipulate data.
Peripheral ProgrammingUtilize the 8051’s built-in peripherals, such as GPIO ports, timers/counters, and serial communication ports, to interface with external devices and perform specific functions.
Interrupt HandlingUnderstand interrupt handling in the 8051, including enabling/disabling interrupts, defining interrupt service routines (ISRs), and prioritizing interrupts based on application requirements.
Memory ManagementEfficiently manage memory resources, including RAM and ROM, to store program code, data variables, and constants, optimizing memory usage while ensuring efficient execution.
Timer ProgrammingUtilize the timers/counters available in the 8051 to generate precise timing delays, measure time intervals, and trigger events at specific intervals, configuring timer modes and parameters as needed.
Serial CommunicationImplement serial communication protocols, such as UART or SPI, using the 8051’s built-in serial ports, configuring communication parameters like baud rate, data format, and parity settings.
Optimization TechniquesEmploy optimization techniques to improve code efficiency and performance, such as loop unrolling, code reordering, and minimizing unnecessary memory accesses.
Testing and DebuggingThoroughly test 8051 programs using simulation tools or hardware debuggers, debug issues by analyzing program behavior, inspecting register values, and resolving errors.
Documentation and CommentsDocument code comprehensively with comments, especially for complex algorithms, interrupt service routines, and peripheral configurations, providing clear explanations for understanding and maintenance purposes.

By mastering these programming techniques and principles, you can develop efficient and reliable applications for the 8051 microcontroller, catering to a wide range of embedded system requirements.

C programming technique

  • How to Declare a Special Function Register(SFR)
    •  Definitions for P1 (8 bits), P1.0, and P1.1.  sfr P1 = 0x90; /* SFR for P1 */
  • How to Declare a Bit in Bit Addressable Spacial Function Register(SFR)
    • sbit P1_0 = P1^0; /* SFR for P1.0 */ 
    • sbit P1_1 = P1^1; /* SFR for P1.1 */
  • How to configure an I/O Port pin as an INPUT
    • P1_0 = 1; //configure as InPut
  • How to configure an I/O Port pin as an OUTPUT
    • P1_0 = 0; //configure as OutPut
  • How to read INPUT pin
  • How to read OUTPUT pin

8051 on ASM programming Technique

  • (file_name.asm) Assembly language is human programming language
  • (file_name.obj) Object file
  • (file_name.lst) List files
  • (file_name.abs) Absolute Object file
  • (file_name.hex) Hexadecimal file

Difference Between C code and equivalent Assembly code

  •  Empty Project code for 8051
  • LED define and initialize  Project code for 8051

8051 Hex Code Visualizer

Introducing the 8051 Hex Code Visualizer, brought to you by SpiceLogic Inc. This is a very simple tool targeted to solve only one problem. And that is visualizing the INTEL Hex Code that is generated targeting 8051 8 bit Micro-controllers. This tool will give you the complete understanding on how the Hex Code resides in the Memory of 8051 Micro-controllers. It is completely FREE and made for educational purpose. Get the app from

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

8051 – USB ISP Programmer

Using a USB In-System Programmer (ISP) for the 8051 microcontroller involves connecting the ISP programmer to your computer and the target 8051 microcontroller to load or program the HEX file into the microcontroller’s memory. Here’s a general guide about a USB ISP programmer with an 8051 microcontroller

Read more… →

8051 – Simulators

Simulators for the 8051 microcontroller are essential tools for embedded systems development and education. They allow programmers to test and debug their code without needing physical hardware, which can save time and resources. Some popular 8051 simulators include:

These simulators offer varying levels of features and complexity, catering to different needs and skill levels. Whether you’re a beginner learning 8051 programming or an experienced developer debugging complex embedded systems, there’s likely a simulator that fits your requirements.

Read more… →

8051- Loading a HEX file into the Microcontroller

           In this tutorial you can learn How to 8051 Loading a HEX file into the Microcontroller.  I chosen AT89S51 microcontroller (You can select any other Keil support microcontroller) and demonstrated, this is very simple, and follow the below steps,

  • 8051 Tutorials – How to load a HEX file into the AT89S51 MicroController?

Loading a HEX file into an 8051 microcontroller involves using a programmer to transfer the compiled HEX file into the microcontroller’s memory. Here are the general steps to load a HEX file into an 8051 microcontroller:

Read more… →

Interview Question – 8051

 

An interview question about the uses of the 8051 microcontroller could assess a candidate’s understanding of embedded systems and their applications.

“Can you discuss some of the common applications of the 8051 microcontroller?”

Read more: Interview Question – 8051

This question invites the candidate to demonstrate their knowledge of the versatility of the 8051 microcontroller and its relevance in various industries. They might mention its use in:

  1. Consumer Electronics: The 8051 has been extensively used in consumer electronics such as television sets, washing machines, microwave ovens, and remote controls due to its low cost and reliability.
  2. Industrial Automation: It plays a crucial role in industrial automation systems for tasks like process control, monitoring, and data logging.
  3. Automotive Electronics: In automotive applications, the 8051 is employed in engine control units (ECUs), airbag systems, anti-lock braking systems (ABS), and dashboard displays.
  4. Medical Devices: Many medical devices utilize the 8051 microcontroller for tasks like patient monitoring, diagnostic equipment, and portable medical devices due to its low power consumption and compact size.
  5. Telecommunications: It’s used in telecommunication equipment for tasks like modems, caller ID systems, and telephone exchange systems.
  6. Security Systems: The 8051 is employed in security systems such as access control systems, surveillance cameras, and alarm systems.
  7. Educational Purposes: It’s widely used in educational institutions for teaching purposes due to its simplicity and availability of learning resources.

By elaborating on these applications, the candidate can demonstrate their understanding of the 8051’s significance in various domains.

8051 Interview Questions

Which technology for develop 8051?

  • CMOS, NMOS



8051 follows which Architecture?

  • Harvard Architechture



Difference Between Harvard Architecture and Von Neumann Architecture?


What is width of the 8051 Data Bus?

  • 8 Bit



What is the width of the 8051 Address Bus

16 Bit address



 How many I/O ports are in 8051 Microcontroller?

Answere Will Add Soon



How many SFRs are in 8051 MicroController?

Answere Will Add Soon



What is DPTR

Answere Will Add Soon



Wat is the capacity of internal RAM and internal ROM?

  • Internal RAM – 256bytes
  • Internal ROM – 64Kbytes



What is the purpose of  PSW register in 8051?

Answere Will Add Soon



What is the purpose of SFR register in 8051?

Answere Will Add Soon



What is the use of EA pin in 8051?

Answere Will Add Soon



NEXT

C Quiz and Interview Questions
Embedded Interview Questions
I2C Interview Question
UART Interview Questions
SPI Interview Questions
ESP8266 Resource
MQTT Interview Questions

8051 – Getting Started with Keil IDE

Keil, a German-based software development company, offers various development tools such as IDE, Project Manager, Simulator, Debugger, C Cross Compiler, Cross Assembler, and Locator/Linker. Evaluation boards are also provided. In this tutorial will discuss “8051 – Getting Started with Keil IDE”

Keil supports various processors and controllers, including ARM Cortex-M, C166, 8051, and 251. It also offers Debug Adapters and Evaluation boards. The tools include C/C++ compilers, integrated development environments, RTOS, middleware, as well as debug adapters. Moreover, Keil provides a comprehensive suite of development resources for embedded system development.

Download Keil IDE Tool : Keil IDE  tool for 8051


How to Create a Project in Keil?

 I chooses AT89S51 micro controller(You can select any other keil support micro controller) and demonstrated, this is very simple and follow this below steps,

Steps to follow

  • Open a Keil Tool
  • In Project select New uVision Project.
  • Select the directory, and enter File_Name in anywhere in your machine, then Save project
  • “Select Device for Target” AT89S51 form Popup window and click OK
  • If popup windows then “Copy STARTUP.A51 to Project Folder and Add File to Project”, then Yes.
  •  Done. You created the project ! 

 How to write a Program in keil ?

Steps to follow

  • Select Source Group 1, Right click and click Add New item to Group ‘Source Group 1’.
  • Select C File(.c), then enter Name “LED_Blink” and Click Add
  • write program in text editor and save
  •  Done! 

 How to Create a Hex file?

Steps to follow

  • Select Options for Target from the Project menu in Target
  • Select the Output tab, Make sure the Create HEX File check box is checked,click OK
Note : Select the proper HEX file format to create (typically this will be HEX-80 for 8051 programs and HEX-386 for large C16x programs).
  • open specified folder and click Objects (after build only you can get this object folder files)
  •  Done!

How to Build a Project in Keil?

Steps to follow

  • Select Build Target(F7) from Project menu.

Go to specified project saved Folder directory

Open specified folder and click Objects

  •  Done!

How Debug program

  1. Start the debugger.
  2. Click on Debug – Go to run the program.
  3. Make sure that View – Periodic Window Update is checked.

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