LCD Project

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

#include <reg51.

h>

// Define LCD control signals and data pins

#define LCD_RS P1_0

#define LCD_RW P1_1

#define LCD_E P1_2

#define LCD_DATA P2

// Define switch input pin

#define SWITCH P3_0

// Function to initialize the LCD

void LCD_Init()

LCD_Command(0x38); // 2 lines, 5x7 matrix

LCD_Command(0x0E); // Display on, cursor blinking

LCD_Command(0x01); // Clear display

LCD_Command(0x80); // Set cursor position to line 1, column 1

// Function to send a command to the LCD

void LCD_Command(unsigned char cmd)

LCD_DATA = cmd; // Send command to data pins

LCD_RS = 0; // RS = 0 for command mode

LCD_RW = 0; // RW = 0 for write operation

LCD_E = 1; // Enable pulse

delay(); // Delay for a short period

LCD_E = 0; // Disable pulse

}
// Function to send a character to the LCD

void LCD_Char(unsigned char chr)

LCD_DATA = chr; // Send character to data pins

LCD_RS = 1; // RS = 1 for data mode

LCD_RW = 0; // RW = 0 for write operation

LCD_E = 1; // Enable pulse

delay(); // Delay for a short period

LCD_E = 0; // Disable pulse

// Function to send a string to the LCD

void LCD_String(char *str)

while (*str) {

LCD_Char(*str); // Send each character of the string

str++;

// Delay function for a short period

void delay()

unsigned int i;

for(i=0; i<1000; i++); // Adjust this value for the desired delay

void main()

// Initialize the LCD

LCD_Init();
while (1) {

if (SWITCH == 0) {

LCD_Command(0x01); // Clear display

LCD_Command(0x80); // Set cursor position to line 1, column 1

LCD_String("SW=0 NO"); // Display "NO" for switch status 0

} else {

LCD_Command(0x01); // Clear display

LCD_Command(0x80); // Set cursor position to line 1, column 1

LCD_String("SW=1 YES"); // Display "YES" for switch status 1

You might also like