Interfacing Lcd with Arduino without using Liquid crystal Library

Hello!
I'm a beginner in Arduino&Embedded C.
I was given a task by my teacher ,which says I have to interface LCD sheild 16x02 using Arduino , but without using the standard liquid crystal library provided by arduino .I have to display any message on the lcd sheild without using the standard library.
Im kind of stuck on where to start from . Any sort of guidance or any links that i can refer to ,to get through this problem would be appreciated.Im currently using Arduino UNO board.

I have attached the code i had written below. It obviously didn't work & showed errors. Would really like to know if i was on the right track atleast.
Thankyou!!

sketch_jun21b.ino (1.78 KB)

I was given a task by my teacher ,which says I have to interface LCD sheild 16x02 using Arduino , but without using the standard liquid crystal library provided by arduino .I have to display any message on the lcd sheild with using the standard library.

how does blue and red text fit together?

For your task, see what's happening in the lcd library and do similar in your sketch.

And by the way, if you are using the Arduino-IDE - start with a simple basic sketch from the examples to get the right "Arduino like" structure for your program. a setup() and a loop()

Im sorry that was a mistake, fixed it.

Okay will try doing that!

You might want to have a look at the datasheet for the HD44780 that is used as a controller for most of the common LCD displays (if the display works with any of the libraries then its using that chip, or a clone that uses the same commands).

Thankyou!

Of course you could be rather devious. :roll_eyes:

After you read the HD44780 datasheet and the instructions for posting to the forum (point No. 7), you could read the actual library code and understand how it works. :grinning:

Oh! That's what noiasca said! :sunglasses:

tanvi256:
. . .
I have to display any message on the lcd sheild without using the standard library.
Im kind of stuck on where to start from
. . .

Try doing a search for "LCD Programming Examples". Any of the first several responses from a Google search (in the US at least) will give you all the information you need.

Don

tanvi256:
interface LCD sheild 16x02 using Arduino , but without using the standard liquid crystal library provided by arduino

You could be clever here. If that is the actual wording used for the instruction, you just need a non standard library not provided by Arduino. Problem solved! You will have complied with the instruction to the letter.

tanvi256,
It isn't clear what you are wanting/needing to do.
You mention Arduino and put your code into a .ino file but from looking at the code, it appears that you are used to using some other toolset other than Arduino and/or gcc or are not wanting to use any of the Arduino framework.

If you are really wanting to use the Arduino programming environment I think you probably need to spend a bit of time looking at how it works and how to use it.

Arduino created/re-invented its own little world that does not use standard C programming in terms of a main() function and directly touching hardware.
Arduino uses "sketches" which do not have a main() function as they have stolen that function for their environment.
They use the functions setup() for initial h/w setup functions and loop() which is called over and over by main.

Also this will not work with gcc or at least not for how you appear to be wanting it to.

/* Configure the data bus and Control pins as per the hardware connection 
   Databus is connected to P2_0:P2_7 and control bus P0_0:P0_2*/
#define LcdDataBus PORTD
#define LCD_RS PB0
#define LCD_EN PB1

The avr gcc package does include some libraries and header files for manipulating h/w but they don't work the way your code is assuming. There are port macros like PORTD which can be used to set the state of 8 pins at once but they are a pointer to the h/w and so they must be used as pointer.
The macros/defines like PB0 PB1 etc... are simple numbers that represent bit number.
IMO, these AVR macros are pretty silly as they don't really do anything or provide any benefit.
i.e. PB0 is the same as 0
Because of this, you cannot use these macros to assign values/states to what they "represent".
About their only benefit is that they match the datasheet and can concisely represent what h/w register pin & bit need to be modified when printed in documentation. But for actual coding, they don't server much purpose and, at least to me, are a bit misleading.

If you are using Arduino and sticking to the Arduino programming environment, Arduino does not provide the ability to address hardware like ports or pins directly.
While port manipulation is possible in Arduino sketches, it is done by stepping outside of the Arduino programming environment and using the capabilities provided by the particular gcc package.
Arduino only provides pin manipulation so to stay within the portable Arduino environment, you must do things pin at time using the Arduino provided function digitalWrite().

Also, a pin must be configured as an output pin in order to use it as an output pin.
If you want to drive an input (like a pin on the LCD) you must configure all the pins you use as output pins before you use them.

In Arduino you would use pinMode() to do this for each pin.
Outside of Arduino, you would need to write to the the appropriate DDRx register and appropriate bit for each pin.

--- bill

Hello everyone!
Thankyou for the help.
I could complete my task before time because of it.
Ive attached my final code below :slight_smile:

#define RS 8
#define E 9
#define D4 4
#define D5 5
#define D6 6
#define D7 7


void lcdwrite(int select, char data)
{
  
  digitalWrite(RS, select);                   //Change RS for command/data
  digitalWrite(D4, (data & 0b00010000) >> 4);
  digitalWrite(D5, (data & 0b00100000) >> 5);
  digitalWrite(D6, (data & 0b01000000) >> 6);
  digitalWrite(D7, (data & 0b10000000) >> 7);
  delay(15);
  digitalWrite(E, HIGH);
  delay(15);
  digitalWrite(E, LOW);
  delay(15);
  digitalWrite(D4, (data & 0b00000001) >> 0);
  digitalWrite(D5, (data & 0b00000010) >> 1);
  digitalWrite(D6, (data & 0b00000100) >> 2);
  digitalWrite(D7, (data & 0b00001000) >> 3);
  delay(15);
  digitalWrite(E, HIGH);
  delay(15);
  digitalWrite(E, LOW);
  delay(15);
}
void lcd_cmd(char cmd)
{
  lcdwrite(0, cmd);
}
void lcd_data(char dat)
{
  lcdwrite(1, dat);
}
void lcd_ini()
{
  
  lcdwrite(0, 0x33);
  delay(15);
  lcdwrite(0, 0x32);
  delay(15);
  lcdwrite(0, 0x28);
  delay(15);
  lcdwrite(0, 0x01);
  delay(15);
  lcdwrite(0, 0x0C);
  delay(15);
  lcdwrite(0, 0x06);
  delay(15);
  lcdwrite(0, 0x80);
  delay(15);

}



void setup() {
  // put your setup code here, to run once:
  
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  
  delay(150);
  lcd_ini();
  lcd_data('h');
  lcd_data('e');
  lcd_data('y');
  lcd_data('y');

}

void loop() {
  // put your main code here, to run repeatedly:

}
1 Like

Fully tested?

Looks good. :grinning:

//---------------------------------------------------------
// --------------------- LCD START ------------------------
//---------------------------------------------------------
const int RS = 12;
const int E = 11;
const int D4 = 5;
const int D5 = 4;
const int D6 = 3;
const int D7 = 2;

bool bFourBitMode = false;
char ReadSendState = -1;

unsigned char Battery_6[8] =
{
  0b00100,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111
};

unsigned char Battery_5[8] =
{
  0b00100,
  0b11111,
  0b10001,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111
};

unsigned char Battery_4[8] =
{
  0b00100,
  0b11111,
  0b10001,
  0b11111,
  0b11111,
  0b11111,
  0b11111,
  0b11111
};

unsigned char Battery_3[8] =
{
  0b00100,
  0b11111,
  0b10001,
  0b10001,
  0b11111,
  0b11111,
  0b11111,
  0b11111
};

unsigned char Battery_2[8] =
{
  0b00100,
  0b11111,
  0b10001,
  0b10001,
  0b10001,
  0b11111,
  0b11111,
  0b11111
};

unsigned char Battery_1[8] =
{
  0b00100,
  0b11111,
  0b10001,
  0b10001,
  0b10001,
  0b10001,
  0b11111,
  0b11111
};

unsigned char Battery_0[8] =
{
  0b00100,
  0b11111,
  0b10001,
  0b10001,
  0b10001,
  0b10001,
  0b10001,
  0b11111
};

void LcdSend(unsigned char Data)
{
  if (bFourBitMode)
  {
    digitalWrite(D4, (Data >> 4) & 0x01);
    digitalWrite(D5, (Data >> 5) & 0x01);
    digitalWrite(D6, (Data >> 6) & 0x01);
    digitalWrite(D7, (Data >> 7) & 0x01);
    
    delayMicroseconds(10);
    digitalWrite(E, HIGH);
    delayMicroseconds(10);
    digitalWrite(E, LOW);
    delayMicroseconds(100);
  }
  
  digitalWrite(D4, (Data >> 0) & 0x01);
  digitalWrite(D5, (Data >> 1) & 0x01);
  digitalWrite(D6, (Data >> 2) & 0x01);
  digitalWrite(D7, (Data >> 3) & 0x01);
  
  delayMicroseconds(10);
  digitalWrite(E, HIGH);
  delayMicroseconds(10);
  digitalWrite(E, LOW);
  delayMicroseconds(100);
}

void LcdCommand(unsigned char Command)
{
  if (ReadSendState != LOW)
  {
    ReadSendState = LOW;
    digitalWrite(RS, LOW);
  }
  
  LcdSend(Command);
  if (Command == 0x01) delayMicroseconds(2000);// Clear command takes more time
}

void LcdWrite(int Letter)
{
  if (ReadSendState != HIGH)
  {
    ReadSendState = HIGH;
    digitalWrite(RS, HIGH);
  }
  
  LcdSend(Letter);
}

void LcdWrite(const char* Text)
{
  if (ReadSendState != HIGH)
  {
    ReadSendState = HIGH;
    digitalWrite(RS, HIGH);
  }
  
  for (; *Text != 0; Text++)
  {
    char Letter = *Text;
    LcdSend(Letter);
  }
}

void LcdInit(bool bFirstInit)
{
  if (bFirstInit)
  {
    // Give it time to power up
    delayMicroseconds(15000);
    
    pinMode(RS, OUTPUT);
    pinMode(E, OUTPUT);
    pinMode(D4, OUTPUT);
    pinMode(D5, OUTPUT);
    pinMode(D6, OUTPUT);
    pinMode(D7, OUTPUT);
  }
  
  // Start
  bFourBitMode = false;
  
  LcdCommand(0x03);
  
  delayMicroseconds(4000);
  
  LcdCommand(0x03);
  LcdCommand(0x03);
  LcdCommand(0x02);
  
  bFourBitMode = true;
  
  LcdCommand(0x28);
  LcdCommand(0x0C);
  LcdCommand(0x01);// Clear
  LcdCommand(0x06);
  
  LcdCreateChar(0, Battery_0);
  LcdCreateChar(1, Battery_1);
  LcdCreateChar(2, Battery_2);
  LcdCreateChar(3, Battery_3);
  LcdCreateChar(4, Battery_4);
  LcdCreateChar(5, Battery_5);
  LcdCreateChar(6, Battery_6);
}

void LcdSetCursor(unsigned char Column, unsigned char Row)
{
  LcdCommand(0x80 | (Column + (Row != 0 ? 0x40 : 0x00)));
}

void LcdCreateChar(unsigned char Location, unsigned char SpecialChar[8])
{
  LcdCommand(0x40 | (Location << 3));
  
  for (unsigned int i = 0; i < 8; i++)
    LcdWrite(SpecialChar[i]);
}

//---------------------------------------------------------
// --------------------- LCD END --------------------------
//---------------------------------------------------------

void setup()
{
  LcdInit(true);
}

void loop()
{
  LcdInit(false);
  LcdSetCursor(0, 0);
  LcdWrite("print this");
}

Old code that I have from an old project. Might be useful for you