Sunday, July 20, 2025

Lcd working code

 Here’s a different version of your LCD code using image-style display logic — this example scrolls a message across a 16x2 LCD (JHD162A) using 8051 (AT89S52) in Keil C.


Connections (8051 to LCD JHD162A - 16x2)

LCD Pin Connection to 8051 Notes
VSS GND Ground
VDD +5V Power supply
V0 Potentiometer middle pin Contrast control
RS P2.0 Register Select
RW GND Write mode (always GND)
EN P2.1 Enable
D0-D7 P1.0 to P1.7 Data lines
A (LED+) +5V via 220Ω resistor LCD backlight (optional)
K (LED-) GND LCD backlight ground

Code for Scrolling Message (Image-style effect)

#include <reg51.h>
#define LCD P1

sbit RS = P2^0;
sbit EN = P2^1;

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

void lcd_cmd(unsigned char cmd) {
    RS = 0;
    LCD = cmd;
    EN = 1;
    delay(1);
    EN = 0;
    delay(5);
}

void lcd_data(unsigned char dat) {
    RS = 1;
    LCD = dat;
    EN = 1;
    delay(1);
    EN = 0;
    delay(5);
}

void lcd_init() {
    lcd_cmd(0x38);  // 8-bit, 2 line
    lcd_cmd(0x0C);  // Display on, cursor off
    lcd_cmd(0x06);  // Auto increment
    lcd_cmd(0x01);  // Clear display
    lcd_cmd(0x80);  // Cursor to first line
}

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

void scroll_message(char *msg) {
    int i = 0;
    while(msg[i] != '\0') {
        lcd_cmd(0x01);  // Clear LCD
        lcd_cmd(0x80);  // Start at beginning
        for(int j = 0; j < 16 && msg[i+j] != '\0'; j++) {
            lcd_data(msg[i+j]);
        }
        delay(300);
        i++;
    }
}

void main() {
    lcd_init();
    while(1) {
        scroll_message("  Welcome to 8051 Embedded World!   ");
    }
}

✅ Tips:

  • Use 10k potentiometer for contrast.

  • RW pin must be connected to GND, else LCD won’t work.

  • Use external 5V power supply if USB power is insufficient.


Want me to generate a scrolling dot-matrix style code or custom image/pattern on LCD next?

No comments:

Post a Comment

syllabus

  Group1 mains SYLLABUS FOR GROUP-I MAINS EXAMINATION Paper: General English (Qualifying Test) Written Examination (Main) 1.  Spotting Error...