I want to drive the LCD TC1602A with my own code without using the LiquidCrystal library. The lcd doesn't display any character and remains in its initial state (as shown in the image). I wrote the code by referring to the datasheet but couldn't find any errors. Please help me out here.
// My own driver to test the TC1602A LCD screen using Arduino Uno
// LCD register pins
const int lcd_rs = 3;
const int lcd_rw = 4;
const int lcd_en = 5;
const int lcd_d0 = 6;
const int lcd_d1 = 7;
const int lcd_d2 = 8;
const int lcd_d3 = 9;
const int lcd_d4 = 10;
const int lcd_d5 = 11;
const int lcd_d6 = 12;
const int lcd_d7 = 13;
void writeToLCD(unsigned char lcd_data) {
if (lcd_data & 0x01 == 0x01) {
digitalWrite(lcd_d0, HIGH);
}
else {
digitalWrite(lcd_d0, LOW);
}
if (lcd_data & 0x02 == 0x02) {
digitalWrite(lcd_d1, HIGH);
}
else {
digitalWrite(lcd_d1, LOW);
}
if (lcd_data & 0x04 == 0x04) {
digitalWrite(lcd_d2, HIGH);
}
else {
digitalWrite(lcd_d2, LOW);
}
if (lcd_data & 0x08 == 0x08) {
digitalWrite(lcd_d3, HIGH);
}
else {
digitalWrite(lcd_d3, LOW);
}
if (lcd_data & 0x10 == 0x10) {
digitalWrite(lcd_d4, HIGH);
}
else {
digitalWrite(lcd_d4, LOW);
}
if (lcd_data & 0x20 == 0x20) {
digitalWrite(lcd_d5, HIGH);
}
else {
digitalWrite(lcd_d5, LOW);
}
if (lcd_data & 0x40 == 0x40) {
digitalWrite(lcd_d6, HIGH);
}
else {
digitalWrite(lcd_d6, LOW);
}
if (lcd_data & 0x80 == 0x80) {
digitalWrite(lcd_d7, HIGH);
}
else {
digitalWrite(lcd_d7, LOW);
}
}
void sendToDataRegister (unsigned char lcd_data) {
writeToLCD(lcd_data);
// To write data to data register in lcd, RS is high and RW is low (check datasheet)
digitalWrite(lcd_rs, HIGH);
digitalWrite(lcd_rw, LOW);
digitalWrite(lcd_en, HIGH);
delay(2);
digitalWrite(lcd_en, LOW);
}
void sendToInstructionRegister (unsigned char lcd_cmd) {
// Function to execute various commands
writeToLCD(lcd_cmd);
digitalWrite(lcd_rs, LOW);
digitalWrite(lcd_rw, LOW);
digitalWrite(lcd_en, HIGH);
delay(2);
digitalWrite(lcd_en, LOW);
}
void writeString (unsigned char str[], int stringLength) {
for (int i = 0; i < stringLength; i++) {
sendToDataRegister(str[i]);
}
}
void initializeLCD() {
sendToInstructionRegister(0x0F);
sendToInstructionRegister(0x38); // Sets the display to 16 cols and 2 rows
sendToInstructionRegister(0x0C); // Turns on the display and turns off the cursor
sendToInstructionRegister(0x06); // Autoincrement the cursor
sendToInstructionRegister(0x01); // Clears the display
}
void setup() {
// put your setup code here, to run once:
pinMode(lcd_rs, OUTPUT);
pinMode(lcd_rw, OUTPUT);
pinMode(lcd_en, OUTPUT);
pinMode(lcd_d0, OUTPUT);
pinMode(lcd_d1, OUTPUT);
pinMode(lcd_d2, OUTPUT);
pinMode(lcd_d3, OUTPUT);
pinMode(lcd_d4, OUTPUT);
pinMode(lcd_d5, OUTPUT);
pinMode(lcd_d6, OUTPUT);
pinMode(lcd_d7, OUTPUT);
initializeLCD();
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
sendToInstructionRegister(0x80); // Sets the cursor at 1st row 1st col
writeString("Hello", 5);
sendToInstructionRegister(0xC0);
delay(500);
}
LCD-Driver.ino (2.9 KB)
