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