0% found this document useful (0 votes)
11 views

Code

The document contains code for interfacing with an LCD display from an 8051 microcontroller. It defines functions for initializing the LCD, sending commands and data, clearing the display, and setting the cursor position. The main loop reads a switch input and sends different messages to the LCD based on the switch state.

Uploaded by

Malavika R Nair
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Code

The document contains code for interfacing with an LCD display from an 8051 microcontroller. It defines functions for initializing the LCD, sending commands and data, clearing the display, and setting the cursor position. The main loop reads a switch input and sends different messages to the LCD based on the switch state.

Uploaded by

Malavika R Nair
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <reg51.

h>

// LCD definitions

#define LCD_DATA P2

#define LCD_RS P3_0

#define LCD_EN P3_1

// SW pin

#define SW P1_0

// Function prototypes

void initLCD();

void sendCommand(unsigned char command);

void sendData(unsigned char data);

void sendString(const char* string);

void clearLCD();

void setCursor(unsigned char row, unsigned char col);

void main() {

initLCD();

while (1) {

// Read SW pin

unsigned char swStatus = SW;

// Send messages to LCD

clearLCD();

setCursor(0, 0);

if (swStatus) {

sendString("SW=1 Send Yes");


} else {

sendString("SW=0 Send NO");

for (unsigned int delayCount = 0; delayCount < 50000; delayCount++); // Delay

clearLCD();

void initLCD() {

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

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

sendCommand(0x06); // Increment cursor

sendCommand(0x01); // Clear display

void sendCommand(unsigned char command) {

LCD_RS = 0; // Select command register

LCD_DATA = command; // Send command to data pins

LCD_EN = 1; // Enable command execution

_nop_(); // Small delay

LCD_EN = 0;

_nop_(); // Small delay

void sendData(unsigned char data) {

LCD_RS = 1; // Select data register

LCD_DATA = data; // Send data to data pins

LCD_EN = 1; // Enable data execution

_nop_(); // Small delay


LCD_EN = 0;

_nop_(); // Small delay

void sendString(const char* string) {

unsigned char i = 0;

while (string[i] != '\0') {

sendData(string[i]);

i++;

void clearLCD() {

sendCommand(0x01); // Clear display

for (unsigned int delayCount = 0; delayCount < 5000; delayCount++); // Delay

void setCursor(unsigned char row, unsigned char col) {

unsigned char address;

if (row == 0)

address = 0x80 + col; // First line address

else

address = 0xC0 + col; // Second line address

sendCommand(address);

You might also like