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
    282.5 KB · Views: 193
  • fff.jpg
    269 KB · Views: 186
Last edited:
Papabravo Proteus doesn't sim the contrast.. That code works well.

I had to put a serial port INIT so the code would read the serial input, The whole thing works as it should.

BUT!!! I'm afraid to post my code as I use SDCC compiler and it will throw salt into the mix..

This code is working using a 11,095Mhz frequency.

C:
#include <8052.h>
#include <string.h>

#define LCD_DATA_PORT P0
#define rs  P3_7
#define rw  P3_6
#define e  P3_5

#define fan  P2_0
#define bulb  P2_1

void delay(unsigned int time) {
    unsigned int i, j;
    for (i = 0; i < time; i++)
        for (j = 0; j < 127; 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
            RI = 0;
            return;
        }
        buffer[i++] = SBUF; // Read data from UART buffer
        
        RI = 0; // Clear receive flag
    }
    buffer[i] = '\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];
    P2_0 = 0; P2_1 = 0;

    SCON = 0x52;    // Mode 1...REN = on
    PCON = 0;        // SMOD = 0
    TH1 = 0xFD;        // 9600 baud
    TMOD = 0x20;    // 8 bit timer 1 with auto reload
    TR1 = 1;        // timer on

    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);
        receivedCommand[0]= 0;
    }
}
 

Thank you, for this code but still doesn't show the display .
how ever I have made some editing now the code shows on the display but I'm not able to turn on the appliances.
I'm not sure what is happening with the rx and tx pins. The above is my new code
 

Attachments

  • rer.jpg
    284.5 KB · Views: 159
Look at the receive data routine I had to make a small change as it didn't work for me either.
My code works okay now..

Also That serial init is for 11.0952Mz crystal... You still didn't tell me what yours is
 
Please the keil uviosn can loacte the #include <8052.h>
more over please could you screenshot the part of my code i need to work on.. The receiving part you are talking of

#include <reg51.h>
#include <string.h>

sbit rs = P3^2;
sbit rw = P3^6;
sbit e = P3^3;
sbit rx = P3^0; // UART receive pin (RX)
sbit tx = P3^1; // UART transmit pin (TX)

sbit fpin = P3^4;
sbit lpin = P3^5;


char gsm_read[20];

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;
P2 = cmd;
e = 1;
delay(5);
e = 0;
}

void lcd_data(char store) {
rs = 1;
rw = 0;
P2 = store;
e = 1;
delay(5);
e = 0;
}

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


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
RI = 0;
return;
}
buffer[i++] = SBUF; // Read data from UART buffer

RI = 0; // Clear receive flag
}
buffer = '\0'; // Null-terminate the string
}


void process_command(char *cmd) {
if (strcmp(cmd, "FAN_ON") == 0) {
fpin = 1;
lcd_command(0xC0);
lcd_print("Motor on");
delay(1000);
} else if (strcmp(cmd, "FAN_OFF") == 0) {
fpin = 0;
lcd_command(0xC0);
lcd_print("Motor off");
delay(1000);
} else if (strcmp(cmd, "BULB_ON") == 0) {
lpin = 1;
lcd_command(0xC0);
lcd_print("Bulb on");
delay(1000);
} else if (strcmp(cmd, "BULB_OFF") == 0) {
lpin = 0;
lcd_command(0xC0);
lcd_print("Bulb off");
delay(1000);
} else {
lcd_command(0xC0);
lcd_print("Wrong command");
delay(1000);
}
lcd_command(0xC0);
lcd_print("Waiting for cmd");
}


void main() {
char receivedCommand[10];
fpin = 0; lpin = 0;



SCON = 0x52; // Mode 1...REN = on
PCON = 0; // SMOD = 0
TH1 = 0xFD; // 9600 baud
TMOD = 0x20; // 8 bit timer 1 with auto reload
TR1 = 1; // timer on

lcd_init();
lcd_print("Home Automation");
delay(100);
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);
receivedCommand[0]= 0;
}
}
 
one more time... WHAT frequency speed are you using... The serial port has to be set up to suit the speed you are using. Your delay routine is counting 1275 so to get 1ms you will be running at a higher frequency than me.

Take the nominal 12Mhz, the tick cycle is 1us. Then "for(j=0;j<1275; j++)" will take a shed load longer than you think , each iteration clocks 15.14us ergo 10us * 1275 yeilds 19.3mS so much slower so you need to work out your delay.. I set mine at 127 because I'm running at 11.0592Mz ( baud friendly crystal )
I actually went the wrong way It should have been 27 the other way.. even 72 is too much, but for this it works, if each iteration takes 15,14mS you need 66 of them to get close to 1 mS.. less than this is better for the two overheads..

Then if you are using a 12Mhz frequency ( its in the edit properties ) your TH1 needs to be altered to suit.
12Mhz closest to 9600 baud is the same 0xFD but gives a baud of 10416 quite a bit off

I was never sure about baud error but a baud of 10416 will finish way before the stop bit
Note** the bit error after 8 bits will be over the 50%

The calculation is (10416-9600) / (9600) * 100 8.5% error the picture says it all

the sample is 50% of the bit each bit at 9600 is 1.04uS so the speed your getting ain't quite getting there
 
Last edited:
I'm using 11.0592Mhz....
Everything is working properly now. The delay was too big (1000). I had to reduce it.
Thank you very much.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…