const int rs = 12;
const int en = 11;
const int d4 = 10;
const int d5 = 9;
const int d6 = 8;
const int d7 = 7;
void pulseEnable() {
digitalWrite(en, HIGH);
delayMicroseconds(1);
digitalWrite(en, LOW);
delayMicroseconds(50);
}
void sendCommand(byte command) {
digitalWrite(rs, LOW);
digitalWrite(d4, (command >> 4) & 1);
digitalWrite(d5, (command >> 5) & 1);
digitalWrite(d6, (command >> 6) & 1);
digitalWrite(d7, (command >> 7) & 1);
pulseEnable();
digitalWrite(d4, command & 1);
digitalWrite(d5, (command >> 1) & 1);
digitalWrite(d6, (command >> 2) & 1);
digitalWrite(d7, (command >> 3) & 1);
pulseEnable();
}
void sendData(byte data) {
digitalWrite(rs, HIGH);
digitalWrite(d4, (data >> 4) & 1);
digitalWrite(d5, (data >> 5) & 1);
digitalWrite(d6, (data >> 6) & 1);
digitalWrite(d7, (data >> 7) & 1);
pulseEnable();
digitalWrite(d4, data & 1);
digitalWrite(d5, (data >> 1) & 1);
digitalWrite(d6, (data >> 2) & 1);
digitalWrite(d7, (data >> 3) & 1);
pulseEnable();
}
void printText(const char* text, int row, int col) {
int position = col + (row == 1 ? 0x40 : 0x00);
sendCommand(0x80 | position); // Set DDRAM address
for (int i = 0; text[i] != '\0'; ++i) {
sendData(text[i]);
}
}
void setup() {
pinMode(rs, OUTPUT);
pinMode(en, OUTPUT);
pinMode(d4, OUTPUT);
pinMode(d5, OUTPUT);
pinMode(d6, OUTPUT);
pinMode(d7, OUTPUT);
sendCommand(0x33);
sendCommand(0x32);
sendCommand(0x28);
sendCommand(0x0C);
sendCommand(0x06);
sendCommand(0x01);
printText("Hello", 0, 0);
printText("World", 1, 0);
}
void loop() {
}