// For the Discord channel. A test.
// See also the same project with a Uno: https://wokwi.com/projects/456172810707305473
#define LCD_RS 1
#define LCD_E 2
#define LCD_D4 42
#define LCD_D5 41
#define LCD_D6 40
#define LCD_D7 39
void setup()
{
lcdInit();
lcdPrint("Hello");
}
void loop()
{
delay(20);
}
void lcdInit()
{
pinMode(LCD_RS, OUTPUT);
pinMode(LCD_E, OUTPUT);
pinMode(LCD_D4, OUTPUT);
pinMode(LCD_D5, OUTPUT);
pinMode(LCD_D6, OUTPUT);
pinMode(LCD_D7, OUTPUT);
delay(50);
sendNibble(0x03); delay(5);
sendNibble(0x03); delay(5);
sendNibble(0x03); delay(5);
sendNibble(0x02);
lcdCommand(0x28);
lcdCommand(0x0C);
lcdCommand(0x06);
lcdCommand(0x01);
}
void pulseEnable()
{
digitalWrite(LCD_E, HIGH);
delayMicroseconds(1);
digitalWrite(LCD_E, LOW);
delayMicroseconds(100);
}
void sendNibble(uint8_t nibble)
{
digitalWrite(LCD_D4, (nibble >> 0) & 1);
digitalWrite(LCD_D5, (nibble >> 1) & 1);
digitalWrite(LCD_D6, (nibble >> 2) & 1);
digitalWrite(LCD_D7, (nibble >> 3) & 1);
pulseEnable();
}
void lcdCommand(uint8_t cmd)
{
digitalWrite(LCD_RS, LOW);
sendNibble(cmd >> 4);
sendNibble(cmd & 0x0F);
delay(2);
}
void lcdWrite(uint8_t data)
{
digitalWrite(LCD_RS, HIGH);
sendNibble(data >> 4);
sendNibble(data & 0x0F);
}
void lcdPrint(const char* str)
{
while (*str) lcdWrite(*str++);
}
void lcdSetCursor(uint8_t col, uint8_t row)
{
uint8_t addr = (row == 0 ? 0x00 : 0x40) + col;
lcdCommand(0x80 | addr);
}