How to Send Command to LCD in Embedded C: Simple Guide
To send a command to an LCD in
embedded C, set the command value on the data pins, clear the register select pin (RS = 0), and then toggle the enable pin (EN) to latch the command. This process tells the LCD what action to perform, like clearing the screen or setting cursor position.Syntax
Sending a command to an LCD typically involves setting control pins and data pins correctly. The main steps are:
- RS (Register Select): Set to 0 for command mode.
- RW (Read/Write): Set to 0 for write mode.
- Data pins: Set the command byte on these pins.
- EN (Enable): Pulse this pin high then low to send the command.
This sequence ensures the LCD receives and executes the command.
c
void LCD_Command(unsigned char cmd) { PORTD = cmd; // Put command on data port PORTB &= ~(1 << 0); // RS = 0 for command PORTB &= ~(1 << 1); // RW = 0 for write PORTB |= (1 << 2); // EN = 1 to latch _delay_ms(1); // Short delay PORTB &= ~(1 << 2); // EN = 0 to finish _delay_ms(2); // Wait for command to execute }
Example
This example shows how to initialize the LCD and send a clear screen command (0x01) using the LCD_Command function.
c
#include <avr/io.h> #include <util/delay.h> void LCD_Command(unsigned char cmd) { PORTD = cmd; // Set command on data pins PORTB &= ~(1 << 0); // RS = 0 for command PORTB &= ~(1 << 1); // RW = 0 for write PORTB |= (1 << 2); // EN = 1 to latch _delay_ms(1); PORTB &= ~(1 << 2); // EN = 0 _delay_ms(2); } int main(void) { DDRD = 0xFF; // Data pins as output DDRB = 0x07; // Control pins (RS,RW,EN) as output _delay_ms(20); // Wait for LCD to power up LCD_Command(0x01); // Clear display command while(1) { // Main loop } return 0; }
Output
LCD screen clears
Common Pitfalls
Common mistakes when sending commands to an LCD include:
- Not setting RS to 0, which causes the LCD to treat the input as data, not a command.
- Forgetting to pulse the EN pin, so the LCD never latches the command.
- Not waiting enough time after sending a command, causing the LCD to miss or ignore it.
- Incorrect wiring or port configuration, leading to no visible effect.
c
/* Wrong: RS set to 1 (data mode) instead of 0 */ void Wrong_LCD_Command(unsigned char cmd) { PORTD = cmd; PORTB |= (1 << 0); // RS = 1 (wrong for command) PORTB &= ~(1 << 1); // RW = 0 PORTB |= (1 << 2); // EN = 1 _delay_ms(1); PORTB &= ~(1 << 2); // EN = 0 _delay_ms(2); } /* Right: RS set to 0 for command */ void Right_LCD_Command(unsigned char cmd) { PORTD = cmd; PORTB &= ~(1 << 0); // RS = 0 PORTB &= ~(1 << 1); // RW = 0 PORTB |= (1 << 2); // EN = 1 _delay_ms(1); PORTB &= ~(1 << 2); // EN = 0 _delay_ms(2); }
Quick Reference
| Pin | Purpose | Value for Command |
|---|---|---|
| RS | Register Select | 0 (command mode) |
| RW | Read/Write | 0 (write mode) |
| EN | Enable | Pulse high then low to send |
| Data Pins | Command bits | Set to command byte |
Key Takeaways
Set RS pin to 0 to send commands to the LCD.
Pulse the EN pin high then low to latch the command.
Wait briefly after sending commands for the LCD to process.
Ensure data pins carry the correct command byte.
Incorrect RS or missing EN pulse are common errors.