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
[/I]
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]
Attachments
Last edited: