Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Lcd display does not show any text.

mouse22

New Member
The lcd display doesn't show up. not sure what the problem is.
also the dc motor and bulb automatically turns on when i simulate
Kindly assist me
C:
#include <reg51.h>
#include <string.h>

#define LCD_DATA_PORT P0
sbit rs = P3^7;
sbit rw = P3^6;
sbit e = P3^5;

sbit fan = P2^0;
sbit bulb = P2^1;

void delay(unsigned int time) {
    unsigned int i, j;
    for (i = 0; i < time; i++)
        for (j = 0; j < 1275; j++);
}

void lcd_command(char cmd) {
 
    rs = 0;
    rw = 0;
        LCD_DATA_PORT = cmd;  // Send command
    e = 1;
    delay(1); // Adjust delay as needed
    e = 0;
}

void lcd_data(char store) {
 
    rs = 1;
    rw = 0;
        LCD_DATA_PORT = store;
    e = 1;
    delay(1); // Adjust delay as needed
    e = 0;
}

void lcd_init() {
 
    lcd_command(0x38); // 2 lines, 5x7 matrix
    delay(1);
    lcd_command(0x0C); // Turn on display (no cursor, no blink)
    delay(1);
    lcd_command(0x01); // Clear screen
    delay(1);
    lcd_command(0x80); // Set cursor to line 1
    delay(1);
}

void lcd_print(char *str) {
    while (*str) {
        lcd_data(*str++);
    }
}

void receive_data(char *buffer, int length) {
    int i = 0;
    while (i < length - 1) { // Ensure buffer doesn't overflow
        while (!RI); // Wait until receive flag is set
        if (SBUF == '\r') { // Ignore carriage return characters
            continue;
        }
        buffer[i++] = SBUF; // Read data from UART buffer
        RI = 0; // Clear receive flag
    }
    buffer[I] = '\0'; // Null-terminate the string[/I]
}

void process_command(char *command) {
    if (strcmp(command, "fan on") == 0) {
        fan = 1;
        lcd_command(0xC0); // Move cursor to line 2
        lcd_print("Fan on");
        delay(1000);
    } else if (strcmp(command, "fan off") == 0) {
        fan = 0;
        lcd_command(0xC0); // Move cursor to line 2
        lcd_print("Fan off");
        delay(1000);
    } else if (strcmp(command, "bulb on") == 0) {
        bulb = 1;
        lcd_command(0xC0); // Move cursor to line 2
        lcd_print("Bulb on");
        delay(1000);
    } else if (strcmp(command, "bulb off") == 0) {
        bulb = 0;
        lcd_command(0xC0); // Move cursor to line 2
        lcd_print("Bulb off");
        delay(1000);
    } else {
        lcd_command(0xC0); // Move cursor to line 2
        lcd_print("Wrong command");
        delay(1000);
    }
    lcd_command(0xC0); // Move cursor to line 2
    lcd_print("Waiting for cmd");
}

void main() {
    char receivedCommand[10];

    lcd_init();
    lcd_print("Home Automation");
    delay(2000);
    lcd_command(0xC0); // Move cursor to line 2
    lcd_print("Waiting for cmd");

    while (1) {
        receive_data(receivedCommand, 9); // Receive up to 9 characters (plus null terminator)
        process_command(receivedCommand);
    }
}
[I]
[/I]
 

Attachments

  • forum.jpg
    forum.jpg
    282.5 KB · Views: 42
  • fff.jpg
    fff.jpg
    269 KB · Views: 41
Last edited:
I put the code in code brackets (easier to read)

proteus is actually less forgiving than the real thing...

You need to put a 15s delay before writing to the screen
Most of the time you need to specify the three inits 33 then 33 then 33, before even starting.
I always set command 06 aswell cursor movement
 
Please i've change the port to seem like it working now but the display error is still the same.
#include <reg51.h>
#include <string.h>

#define LCD_DATA_PORT P2
sbit rs = P1^0;
sbit rw = P1^1;
sbit e = P1^2;

sbit fan = P1^6;
sbit bulb = P1^7;

void delay(unsigned int time) {
unsigned int i, j;
for (i = 0; i < time; i++)
for (j = 0; j < 1275; j++);
}

void lcd_command(char cmd) {

rs = 0;
rw = 0;
LCD_DATA_PORT = cmd; // Send command
e = 1;
delay(15); // Adjust delay as needed
e = 0;
}

void lcd_data(char store) {

rs = 1;
rw = 0;
LCD_DATA_PORT = store;
e = 1;
delay(1); // Adjust delay as needed
e = 0;
}

void lcd_init() {

lcd_command(0x38); // 2 lines, 5x7 matrix
delay(15);
lcd_command(0x0C); // Turn on display (no cursor, no blink)
delay(15);
lcd_command(0x01); // Clear screen
delay(15);
lcd_command(0x80); // Set cursor to line 1
delay(15);
}

void lcd_print(char *str) {
while (*str) {
lcd_data(*str++);
}
}

void receive_data(char *buffer, int length) {
int i = 0;
while (i < length - 1) { // Ensure buffer doesn't overflow
while (!RI); // Wait until receive flag is set
if (SBUF == '\r') { // Ignore carriage return characters
continue;
}
buffer[i++] = SBUF; // Read data from UART buffer
RI = 0; // Clear receive flag
}
buffer = '\0'; // Null-terminate the string
}

void process_command(char *command) {
if (strcmp(command, "fan on") == 0) {
fan = 1;
lcd_command(0xC0); // Move cursor to line 2
lcd_print("Fan on");
delay(1000);
} else if (strcmp(command, "fan off") == 0) {
fan = 0;
lcd_command(0xC0); // Move cursor to line 2
lcd_print("Fan off");
delay(1000);
} else if (strcmp(command, "bulb on") == 0) {
bulb = 1;
lcd_command(0xC0); // Move cursor to line 2
lcd_print("Bulb on");
delay(1000);
} else if (strcmp(command, "bulb off") == 0) {
bulb = 0;
lcd_command(0xC0); // Move cursor to line 2
lcd_print("Bulb off");
delay(1000);
} else {
lcd_command(0xC0); // Move cursor to line 2
lcd_print("Wrong command");
delay(1000);
}
lcd_command(0xC0); // Move cursor to line 2
lcd_print("Waiting for cmd");
}

void main() {
char receivedCommand[10];

lcd_init();
lcd_print("Home Automation");
delay(2000);
lcd_command(0xC0); // Move cursor to line 2
lcd_print("Waiting for cmd");

while (1) {
receive_data(receivedCommand, 9); // Receive up to 9 characters (plus null terminator)
process_command(receivedCommand);
}
}
 

Attachments

  • f2.jpg
    f2.jpg
    269.8 KB · Views: 27
Please I don't know which of the codes is about contrast settings as I'm new to this.
but I've changed the port but the display doesn't show anything though the port are working now.
You may be new to all of this which means you should have read the datasheet for the display carefully, at least three times – right? The datasheet for the LCD display may have a potentiometer connection that is used for contrast. It may nothing to do with codes and programming. Why would that be your default assumption?
 
I don't have the datasheet for the display, so I'm not in a good position to answer that.
 
You may be new to all of this which means you should have read the datasheet for the display carefully, at least three times – right? The datasheet for the LCD display may have a potentiometer connection that is used for contrast. It may nothing to do with codes and programming. Why would that be your default assumption?
I'm doing a course for simulation.... and we were had to work around this.
 
I'm doing a course for simulation.... and we were had to work around this.
I thought you were talking about actual hardware. I was one of the things that happened to me on my first LCD display. I did not know that a potentiometer was required to adjust the contrast to make the pixels visible.
 

Latest threads

New Articles From Microcontroller Tips

Back
Top