LCD4Bit for 20x4 displays!

This could help you...
http://www.tekcrack.com/interfacing-arduino-with-a-purdy-lcd-4x40-modified-lcd4bit-library.html

let me know if it works!

Thx
Andy

This could help you...
http://www.tekcrack.com/interfacing-arduino-with-a-purdy-lcd-4x40-modified-lcd4bit-library.html

let me know if it works!

Thx
Andy

Yes the Zip files work fine.
But that code is for a 4x40 LCD with two enable signals, anybody got the original version code for only one enable signal?

In my 4x20 display, line 3 starts just after the 20th position of line 1 and line 4 starts just after the 20th position of line 2.

So this code in the cursor position method works for me on a 4x20 display, you will need to find the right values for a display with a different width

switch (line_num){ // lines start from 1, no offset needed for x if displaying on line 1
case 2:
x+= 40 ; break;
case 3:
x+= 20 ; break;
case 4:
x+= 60 ; break;
}

You will need to search the cpp file to modify any code that is aware of the total number of lines. In the 4bit library I think there is a global variable called g_num_lines, search on that and modify the code as necessary to respect values from 1 to 4 as valid.

So something like:
if (g_num_lines < 1 || g_num_lines > 2)
g_num_lines = 1;

becomes:
if (g_num_lines != 1 && g_num_lines != 2 && g_num_lines != 4)
g_num_lines = 1;

and
int num_lines_ptn = g_num_lines - 1 << 3;

becomes:
int num_lines_ptn ;
if (g_num_lines != 4)
num_lines_ptn = g_num_lines - 1 << 3;
else
num_lines_ptn = 2 - 1 << 3; // control the 4 lines display as if 2 lines

Mem,

Aha, did not figure that out, works fine on my 4x20 LCD.
No need to change the code in that case.

Thanks again...

I changed the origenal 4bit library code my self to let it work with my 4x20LCD, KS0066u Based.
I got it working perfectly.
Below the code for those of you who have the same LCD.

LCD4Bit.cpp:
changes i made
just replace the origenal ones with this ones.

The new constructor:

LCD4Bit::LCD4Bit (int num_lines) {
g_num_lines = num_lines;
if (g_num_lines < 1)
{
g_num_lines = 1;
}
if (g_num_lines > 4)
{
g_num_lines = 4;
}
}

function cursorTo

void LCD4Bit::cursorTo(int line_num, int x){
//first, put cursor home
commandWrite(CMD_HOME);
if (x>19)
{
x=19;
}
switch (line_num)
{
case 1:
x+=0x00 ; break;
case 2:
x+= 0x40 ; break;
case 3:
x+= 0x14 ; break; // In fact, line 3 is an extension of the line 1 (beyond the 20 first characters)
case 4:
x+= 0x54 ; break; // Line 4 is an extension of line 2
}
//
commandWrite(0x80+x);
}

Test program:

#include <LCD4Bit.h>
LCD4Bit lcd = LCD4Bit(4);

void setup() {
pinMode(13, OUTPUT); //we'll use the debug LED to output a heartbeat
lcd.init();
}

void loop() {
digitalWrite(13, HIGH); //light the debug LED
lcd.clear();
lcd.cursorTo(1,0);
lcd.printIn("Line 1");
lcd.cursorTo(2,0);
lcd.printIn("Line 2");
lcd.cursorTo(3,0);
lcd.printIn("Line 3");
lcd.cursorTo(4,0);
lcd.printIn("Line 4");
delay(1000);
digitalWrite(13, LOW); //clear the debug LED
}

Hope it works for you to!

hmm one question:
I always get this error after compiling:
Couldn't determine program size:

What's the matter?

cheers
Geko

you forgot to show your error, but maybe this helps. I had errors to when compiling. The reason was that i had more liberies to control the lcd in the libary directory that use the same files.
Try to tempery move other libs and try again.

I hope this hepls you.

hmm that worked!

Ok but now as I uploaded the sketch nothing
appears on the screen.

Hmm could you pls send me your library and sketch...
I think I messed it up...

PS: The Display shows:

Geko

hmm that worked!

Hmm could you pls send me your library and sketch...
I think I messed it up...
Geko

This evenig i wil send it to you when i'am home.

Hi,

I put my library online at:
http://www.ineonline.nl/download/LCD4Bit.zip

Good luck.

thx a lot.
I'm gonna try this...

Geko

ok great your code works for a 4x20 Display
It does work on my 4x40 Display but of course it
does show the second line on the first line just 20
characters to the right :slight_smile:

So I changed your cpp to:

void LCD4Bit::cursorTo(int line_num, int x){
  //first, put cursor home
  commandWrite(CMD_HOME);
  if (x>39)
  {
        x=39;
  }
switch (line_num)
  {
 case 1:
    x+=0x00 ; break;
  case 2: 
    x+= 0x80 ; break;
  case 3: 
    x+= 0x28 ; break; // In fact, line 3 is an extension of the line 1 (beyond the 20 first characters)
  case 4: 
    x+= 0xa8 ; break; // Line 4 is an extension of line 2
  }
  //
  commandWrite(0x80+x);
}

but it does not work. Could you help me pls....

Geko

My quistion to you is, don't you have 2 enable pins on you LCD?
in that case look at the repost in this trat Reply #7 from legba7

Else, tray to play with the x e.d. : x+= 0x80 ,0x14 0x54

I don't have your LCD so i can't try.

Good luck, and let us no if it works and with what values.

Yes I know I posted this link put it does not work for my LCD.
Nothing appears the LCD is just empty....

Phu that's a hard nut....

Geko

Here it is: http://bjonnh.free.fr/LCD4Bit.zip This is the old version, I'm using this one for now (an urgent project to do)

Here it is: http://bjonnh.free.fr/LCD4Bit.zip This is the old version, I'm using this one for now (an urgent project to do)

so do you have a 4x40 Display running with this code?
Hmm mine does not work. I got 2 Enable signals so I
could just connect one of them...

Thx
geko

Hi, I was looking for something like this and it works for me on an mtc-c204dply-1n 4x20 lcd display with 1 enable pin.

Gordon

I'm stuck! I can't get any text to display, and it seems to be hanging up somewhere since the debug LED never goes off.

I'm using a WH2004A 4x20 with the KS0066 driver.

I'm at a loss at what to do, any ideas?

edit

A bit of an update. I'm fairly sure that the Freeduino is talking to the LCD, since the lcd.clear command is working. Also, I inserted a small debug script for the LED, it blinks once for the first command, twice for the second ect. It's not just staying on anymore, it's blinking like it's supposed to (the LED) but I still don't have any text output on the display. Here's the code I've been using, any suggestions?

//example use of LCD4Bit library

#include <LCD4Bit.h>
//create object to control an LCD.
//number of lines in display=1
LCD4Bit lcd = LCD4Bit(4);

void setup() {
pinMode(13, OUTPUT); //we'll use the debug LED to output a heartbeat

lcd.init();
//optionally, now set up our application-specific display settings, overriding whatever the lcd did in lcd.init()
//lcd.commandWrite(0x0F);//cursor on, display on, blink on. (nasty!)

}

void loop() {
digitalWrite(13, HIGH); //light the debug LED
delay(200);
digitalWrite(13, LOW);
delay(1000);

//lcd.clear();
digitalWrite(13, HIGH); //debug lcd.clear
delay(200);
digitalWrite(13, LOW);
delay(1000);

lcd.cursorTo(1,0);
lcd.printIn("Line 1");
digitalWrite(13, HIGH); //debug first line of print
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(1000);

lcd.cursorTo(2,0);
lcd.printIn("Line 2");
digitalWrite(13, HIGH); // debug second line
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(1000);

lcd.cursorTo(3,0);
lcd.printIn("Line 3");
digitalWrite(13, HIGH); //debug third line
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(1000);

lcd.cursorTo(4,0);
lcd.printIn("Line 4");
digitalWrite(13, HIGH); //debug fourth line
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(200);
digitalWrite(13, HIGH);
delay(200);
digitalWrite(13, LOW);
delay(1000);

delay(1000);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW); //end debug LED

}

another edit

Yes I did comment out the lcd.clear command on purpose, just to see if it was clearing the 2 lines of blocks when I powered it up, and yes it does, if it's commented out, the blocks stay on the screen.

yet another edit

Ok, for some reason, when the cursor goes to line 3, the blocks dissapear. I'm guessing that there's no problem in communication, as things are happening, it's just that I'm getting no text output on the screen.

Hi,

I am using the code below to write user-defined code, but it is not working. I am using HD44780 4x20 LCD module.

void LCD4Bit::WriteCGRAM(int adress, char code[]) {
commandWrite(0x40+(adress<<3));

for (int i=0 ; i <= 7 ; i++)
{
print(code*);*

  • }*
  • commandWrite(CMD_HOME);*
    }[/quote]

Hi all!
I'm using to a 4x20 LCD a wintec 2004.

Now i have it working and what i note in it is this, to print the complete scren needs a little bit time :'(, so what i do is minimize all the delays() inside the lcd4bit lib.

Now works great, but what will be nice is send the complete screen and then turn on the display, i meen charge it first and then show the text. :o

Can be done? using what cmd ? ;D

Best Regads
Frank