0
0
Power-electronicsHow-ToBeginner · 4 min read

How to Display Text on LCD Using Embedded C: Simple Guide

To display text on an LCD using Embedded C, initialize the LCD by sending commands to set it up, then send characters one by one to the data register. Use functions to send commands and data, typically controlling pins like RS, RW, and EN for communication.
📐

Syntax

To display text on an LCD, you typically use two main functions: one to send commands (like clearing the screen or setting cursor position) and one to send data (characters to display). The basic syntax involves:

  • lcd_command(command): Sends a command byte to the LCD.
  • lcd_data(data): Sends a character byte to the LCD to display.
  • Initialization function to set LCD mode (4-bit or 8-bit).

Control pins like RS (Register Select), RW (Read/Write), and EN (Enable) are used to tell the LCD what you are sending.

c
void lcd_command(unsigned char cmd);
void lcd_data(unsigned char data);
void lcd_init(void);

// Example usage:
lcd_init();
lcd_command(0x80); // Set cursor to first line
lcd_data('H'); // Display character 'H'
💻

Example

This example shows how to initialize a 16x2 LCD and display the text "Hello" on the first line.

c
#include <xc.h>
#define RS LATBbits.LATB0
#define RW LATBbits.LATB1
#define EN LATBbits.LATB2
#define DATA_PORT LATD

void delay_ms(int ms) {
    for(int i=0; i<ms*1000; i++) {
        __asm("nop");
    }
}

void lcd_command(unsigned char cmd) {
    RS = 0; // Command mode
    RW = 0; // Write mode
    DATA_PORT = cmd; // Put command on data port
    EN = 1; // Enable pulse
    delay_ms(1);
    EN = 0;
    delay_ms(2);
}

void lcd_data(unsigned char data) {
    RS = 1; // Data mode
    RW = 0; // Write mode
    DATA_PORT = data; // Put data on data port
    EN = 1; // Enable pulse
    delay_ms(1);
    EN = 0;
    delay_ms(2);
}

void lcd_init(void) {
    lcd_command(0x38); // 8-bit mode, 2 lines, 5x7 font
    lcd_command(0x0C); // Display ON, cursor OFF
    lcd_command(0x06); // Entry mode set
    lcd_command(0x01); // Clear display
    delay_ms(2);
}

int main(void) {
    TRISB = 0x00; // Set PORTB as output for control pins
    TRISD = 0x00; // Set PORTD as output for data pins
    lcd_init();
    lcd_command(0x80); // Move cursor to first line
    char *text = "Hello";
    while(*text) {
        lcd_data(*text++);
    }
    while(1);
    return 0;
}
Output
Hello
⚠️

Common Pitfalls

Common mistakes when displaying text on LCD include:

  • Not initializing the LCD properly before sending data.
  • Incorrect timing delays causing commands or data to be missed.
  • Confusing command mode and data mode by wrong RS pin settings.
  • Not setting the data port direction as output.
  • Sending data before the LCD is ready.

Always ensure proper delays and pin configurations.

c
/* Wrong: No delay after command */
lcd_command(0x01); // Clear display
lcd_data('A'); // May not display correctly due to missing delay

/* Right: Add delay after command */
lcd_command(0x01); // Clear display
__delay_ms(2); // Wait for clear to finish
lcd_data('A'); // Displays correctly
📊

Quick Reference

CommandHex CodeDescription
Clear Display0x01Clears the LCD screen
Return Home0x02Moves cursor to home position
Entry Mode Set0x06Cursor moves right, no display shift
Display ON/OFF0x0CDisplay ON, cursor OFF
Function Set0x388-bit mode, 2 lines, 5x7 font
Set DDRAM Address0x80Set cursor position

Key Takeaways

Initialize the LCD with proper commands before sending text.
Use RS pin low for commands and high for data when communicating.
Add delays after commands to let the LCD process them.
Set data pins as output and control pins correctly.
Send characters one by one to display text on the LCD.