Go Down

Topic: Enhanced LiquidCrystal (Read 16428 times) previous topic - next topic

jrraines

Modifications to LiquidCrystal for the Arduino

I made several modifications to the LiquidCrystal library module from Arduino17:

40x4 LCDs
I added support for an LCD of 4 LInes and 40 characters. I think that if 24x4, 32x4 LCDs exist, they would also work with the software as I have modified it although I have not had the opportunity to test that. The 40x4 LCD (and any HD44780 based LCD with between 81 and 160 characters) will have 2 enable lines. To use an LCD with 4 lines and 40 columns you would declare your LiquidCrystal object as:
LiquidCrystal lcd(RS,RW,Enable1,Enable2,  data3,data2,data1,data0); at this time I don't support 8 data lines nor do I support eliminating the RW line in this option.[media][/media]Then in the setup function you would call:
lcd.begin(40,4);


Linewrap
When you declare the dimensions of the LCD in your begin call, the LiquidCrystal library remembers how long the lines are. Now when it reaches the end of line 1, text wraps onto line 2 (not line 3 as previously).

16x4 LCDs
The begin statement also correctly positions text at the beginning of the line on 16x4 (and 40x4) LCDs, which were not correctly handled before.

setCursor
In the past setCursor selected a location in the HD44780's RAM not actually a screen location. If you use any of the commands that shift the display left or right with the previous routines, then setCursor and print, text appears in an unexpected location on the screen. With the new software,  if you call either scrollDisplayLeft() or scrollDisplayRight(), the LiquidCrystal package keeps track of the relationship between RAM and the LCD so that setCursor coordinates are pegged to a specific spot on the screen, rather than a spot in RAM. The sotware does not handle autoScroll, however. Call home() after autoScroll to restore the expected relationship between setCursor and the LCD screen.

Testing the LCD Busy Flag
Previous versions of LiquidCrystal always used timed delays on the Arduino side of the interface to give the LCD module enough time to complete its operation. This version still does that if you tie the RW pin to ground and do not tell LiquidCrystal what that pin number is. If you do specify RW now, however, the software will poll the busy flag on the LCD module. Arduino operations may thus overlap LCD operations and potentially things may go a little faster.

Crazy 8 Addressing
16x1 LCDs often have an unusual address layout; these modules often have two 8 character halves and work best with this software if you declare them as lcd.begin(8,2); if you do that, then you can print("abcdefghilklmno"); and have all the characters appear as you would like across the screen. If you use any of the scrolling commands, the bizarre addressing of these modules will manifest itself. For details follow the _LCD Addressing_ link at web.alfredstate.edu/weimandn

Disadvantages
The two real disadvantages I can see to the changes I have made are:

The code is longer than before. Much of the increase is in checkLcdBusyFlag() and this could be fairly easily replaced with delayMicroseconds(100);

2. The possibility that someone with a little 16x2 LCD is using the scrollDisplayLeft() or scrollDisplayRight() instructions to move data across the screen, but wants to write the data 40 characters at a time with a print statement. This version really does not let the user write data to the HD44780 DDRAM which is not visible. To accomplish a scrolling display with 40 characters per line, you would now need to write 16 characters, scroll the display, write a little more and so on.

There are going to be some incompatibilities between code that assumed that line 1 wrapped onto line 3, etc.

Directions for the future
I see no purpose to retaining the 8 pin interface options. In theory, by sending 8 bits at a time, things would proceed faster. However, even in the slowest situation output to the LCD goes faster than the eye can follow. The slowest situation would be using the 40x4 LCD, doing a scrollDisplayLeft() or scrollDisplayRight() and then printing 160 characters. When the display has been scrolled, the software actually calls setCursor internally with every character sent to the LCD, so you would be communicating 480 items to the LCD in that instance. As I have tested the various interfaces with the various shapes of LCDs, I have reflected many times on the likelihood that no one else has tried the 8 bit interfaces for some time. Don Weiman argues that inclusion of both 4 and 8 bit interfaces makes the code harder to read. I am more motivated by the idea that the 8 bit interface takes up Arduino RAM and pins but contributes essentially no additional function.

Thanks
Certainly my efforts would not have been possible without the help and prior efforts of David Mellis, Limor Friede, and Donald Weiman. Don was particularly patient in guiding me through the idiosyncracies of the HD44780 based LCDs and especially in supplying an example of how the busy flag could be tested.

available at    healthriskappraisal.org/LiquidCrystal440.zip

jrraines

http://healthriskappraisal.org/LiquidCrystal.zip

JaggedToaster

I would like to try this out, but the .zip is not found when I click the link!

Should it be available there now?

JaggedToaster

Nevermind, I noticed in the original post that the file is named LiquidCrystal440.zip.  I was able to download that one.

http://healthriskappraisal.org/LiquidCrystal440.zip

jrraines

whew. I was afraid the computer I use for a server in my basement had gone down. I'm on vacation and there would be no way to reboot for a week. It usually runs without problems for months.

The original link (in the first post) wasn't a bona fide link because of a restriction on first posts to the Arduino forums. for some reason, the first time you post you can't include a link. So lurk, ready, study and finally post when you have something to say is deprecated behavior.

jrraines

It does look like the server will be down until 2/7 when I get home.

mowcius

Arghh, damn, you couldn't send me the library could you? I am feeling impatient! PM me if you could.

I have just picked up a 20x4 LCD and would like to get it working.

Thanks,

Mowcius  ;)

floresta

John:

I will send him a copy.

Don

mowcius

#8
Feb 05, 2010, 09:34 am Last Edit: Feb 05, 2010, 09:49 pm by mowcius Reason: 1
Yeah to earlier post I meant 40x4 not 20x4... :P

Thanks.  ;)
 
Mowcius

mowcius

#9
Feb 05, 2010, 10:08 pm Last Edit: Feb 05, 2010, 10:10 pm by mowcius Reason: 1
Ok, I have got hold of a 40x4 LCD. It was apparently made by Nan Ya Plastics Corp (well it says that on it), but after a long google search I found nothing for any part numbers or other info.

Well I then looked around for displays that looked similar and came across this one: http://www.lumex.com/products/detail/190mm_x_54mm2 which looks very similar if not identical. Anyway I connected up the rw, rs, en1, en2 and four data lines (pins 1-4) and using the library, defining it as 4 rows and 40 columns I got it to display something. At first I just got a load of random characters (well 1/4, 1/2, ? etc) and two flashing cursors. After removing power and resetting I just got two flashing cursors in random positions, changing each time I reset.

These pictures will explain better:



Anyone got any idea of why this is happening/anything I can try?

Mowcius

floresta

Quote
... and four data lines (pins 1-4) ...

It looks like pin 1 = D7, Pin 2 = D6, Pin 3 = D5 and Pin 4 = D4.  Is that the way you interpreted (and connected) it?  Are you sure?  Did you doublecheck it?  Did you rip all the wires out and put them back?

Don

mowcius

I connected 1,2,3,4 on the display to 1,2,3,4 data on my arduino (pins 8,7,6,5 just to be confusing).

I then adjusted the code for 20x4 and set the pins to what I had connected. I will have a play around with different configurations today and I will see what I can come up with.

Mowcius

mowcius

Ok, I got it sorted. Yeah I had the 4 data lines the wrong way round. It is now working like a dream!

I have however tried to connect it up to my duino644 board and it will work on one 'port' but not the one I want to use it on. I have checked (about 4 times at least) to make sure that the connections are right and the pins are not being used for anything else but I can't get it to work. The library obviously does not need PWM pins (it worked on the other port with no PWM pins) but are there any specific types of pins it requires?

Or has anyone got any other ideas?  ;)

Mowcius

jrraines

The server is back up. I constructed a tiny benchmark to compare speeds of the different interfaces. I wrote 80 characters, then 80 blanks, looped through it 20 times. Of course it goes too fast to read, but here is what I measured:
8 data pins + RW 432 milliseconds
8 data pins - RW 641 milliseconds
4 data pins + RW 719 milliseconds
4 data pins - RW 1038 milliseconds
The options with RW are testing the LCD busy flag, of course, so testing the flag is about 1/3 faster. Again all of the options go faster than you can read. And if complex calculations were interspersed with sending the characters to the LCD you would see even bigger speed increases.

mowcius

Yeah thanks a load for the library. My 40x4 has been a great new (free) toy!

I haven't got an immediate use for it but I am thinking of a 4 line, big number clock with email alerts and other info...

Mowcius

Go Up