echo '' ;

Category Archives: 8051 Interface

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 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