Enhanced LiquidCrystal

It seems that the server is down again... is this library available anywhere else?

http://healthriskappraisal.org/LiquidCrystal440.zip

Thanks.

is this library available anywhere else?

Not that I know of. The link seems to work if you wait long enough.

[edit]Maybe it's not going to work today :([/edit]
I will email it to you. Word of advice though, I recommend you hide your email address in the user CP as it is very visible to spammers at the moment...

Mowcius

Well that was different; the GFI wall socket had tripped. usually its one of the routers (I think they both have memory leaks that bring them down about monthly).
Thanks, Mowcius, for emailing him the software.

I will email it to you. Word of advice though, I recommend you hide your email address in the user CP as it is very visible to spammers at the moment...

Mowcius

Thanks for the email, and thanks for the heads up on the contact info. Took me a while to figure out how to even turn it off. :-?

-Fandu

Yeah I am always wise to look through user settings on every forum to check for that kinda stuff...

Mowcius

I did add 2 more calling modes with allow a user-written test of the LCD busy flag. I tried to keep using it simple but it is necessarily more complex than the previous version, I put the new version on the server at:
http://healthriskappraisal.org/LiquidCrystalBusy.zip
The previous version is still available.

Modifications to LiquidCrystal with callback busy test

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. (You can pass 255 as the RW item, ground RW and save an Arduino pin.)
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).

println
Although print has worked properly in the past, println has not. Now the '\r' and '\n' characters are not sent to the screen as though they were visible characters and the '\r' resets the character position to the top of the next line.

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.

Speed testing
All of the interface modes go faster than the eye can follow. I compared the speeds of the different interfaces--writing 80 characters to the screen then 80 blanks and looping through that 20 times. The results are:
4 data pins 732 milliseconds
8 data pins 647 milliseconds
busy test 432 milliseconds
The 4 data pin option is significantly faster than the previous LIquidCrystal which takes 1076 milliseconds because of an unnecessary delay between sending the high and low data nibbles.

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

User callback busy test
Get LiquidCrystal running without this first; setting up this complicated option is error-prone and this should be left for last, if implemented at all.

The LCD has a busy flag which is intended to notify the processor when it is ready to accept a new command. Testing it involves manipulating almost all the pins in the interface and is not really practical inside the library routine because of the overhead of pinMode,digitalRead and digitalWrite. If the pin and ports were always the same, faster manipulation would be possible. In fact, the port to pin correspondence hasn't remained constant across boards, so even if everyone used the same pin numbers, code that manipulated ports would fail:
Arduino 8 Arduino 168/328 Arduino Mega
Digital Pin Port Port Port
0 PD0 PD 0 PE 0
1 PD1 PD 1 PE 1
2 PD2 PD 2 PE 4
3 PD3 PD 3 PE 5
4 PD4 PD 4 PG 5
5 PD5 PD 5 PE 3
6 PD6 PD 6 PH 3
7 PD7 PD 7 PH 4
8 PB0 PB 0 PH 5
9 PB1 PB 1 PH 6
10 PB2 PB 2 PB 4
11 PB3 PB 3 PB 5
12 PB4 PB 4 PB 6
13 PB5 PB 5 PB 7

The way to be able to test the busy flag and know the pin/port numbers is to use a callback function and let the testing be done in the user's code rather than inside the library.

if you've ever used attachInterrupt on the Arduino, you've used a callback function. You write a function and pass the address of the function to another routine in a function call. In this case, I've added a couple more forms of the (very overloaded) LiquidCrystal object. Now you can specify your callback function like this for the common case of displays up to 20x4:
LiquidCrystal lcd(49,45, 35,33,31,29,&checkBusyFlag);//RS,EN,D0,D2,D1,D3

Notice that in this case, RW must be attached to a digital output pin and the user code must set that pin LOW before calling LiquidCrystal. The busy routine will be manipulating the status of RW but the main LiquidCrystal routine doesn't manipulate it or know which pin it is. (These pin numbers will likely look unfamiliar to you; on my Mega, I like to plug the LCD into the socket on the board directly with pin 1 in a ground, pin 2 into socket 53, pin 3 in socket 51 and so on.)

You would declare your LiquidCrystal object like this for a 40x4 LCD :
LiquidCrystal lcd(48,47,46,52, 41,40,39,38,&checkBusyFlag);//RS,RW,EN,EN2,D0-D3

Then you can modify the macro definitions which are immediately below (busy is the data3 item in your call to LiquidCrystal):

//LiquidCrystal lcd(49,45, 35,33,31,29,&checkBusyFlag)  //RW is pin 47
#define set_data_pins_to_read DDRC&=~0b01010100,DDRA&=~0x80;//d0,d1,d2,d3 pins35,33,31,29
#define set_data_pins_to_write DDRC|=0b01010100,DDRA|=0x80;    // C2,C4,C6,A7
#define set_EN_high PORTL |= 0b00010000;    //port L4 pin45
#define set_EN_low PORTL &= ~(1<<4);
#define set_EN2_high PORTL |= (1<<4);    //portL4 the EN2 if you have a 40x4 LCD
#define set_EN2_low PORTL &= ~(1<<4);
#define set_RW_high PORTL |= (1<<2);
#define set_RW_low PORTL &= ~(1<<2);    //port L bit 2 pin47
#define set_RS_high PORTL |= (1<<0);    //port L bit 0  pin 49
#define set_RS_low PORTL &= ~(1<<0);
#define read_busy PINA & (1<<7);         //port A7  pin29   the last pin number spec'd to LiquidCrystal

//You DON'T have to modify the subroutine itself:
void checkBusyFlag(int8_t chip) {
 uint8_t busy; // = 0x04;
 set_data_pins_to_read;
 set_RW_high;             //RW to read
 set_RS_low;
 if (chip == 0) {  //the if and else can be eliminated if only one hd44780 chip eg 20x4
 do {
   set_EN_high;
   delayMicroseconds(1);
   busy = read_busy;    // read busy flag
   set_EN_low;
   delayMicroseconds(1);
   set_EN_high; 
   delayMicroseconds(1);  //pulse the second nibble--discard it;
   set_EN_low; 
 }while (busy);
} else {
   do {
   set_EN2_high;
   delayMicroseconds(1);
   busy = read_busy;    // read busy flag
   set_EN2_low;
   delayMicroseconds(1);
   set_EN2_high; 
   delayMicroseconds(1);  //pulse the second nibble--discard it;
   set_EN2_low;
 } while (busy);
}
 set_data_pins_to_write; // data pins to write
 set_RW_low;              //RW to write
 set_RS_high;
}

Again, this is complicated and you can expect to make some mistakes in the define statements at first. The payoff is about 37%-40% faster operation than with the 4 data pin option. It does cost you one Arduino pin for RW. I have not created 8 data pin versions of these. I am in favor of deprecating 8 pin mode.

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.

Appendix: A more common pin configuration for the busy test
The more common pin usage for interfacing an LCD is probably that given at

to use the busy test, you will have to connect RW, in this example, to pin 10. To achieve enough board independence for me to test this, download digitalWriteFast (see http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1267553811/0); it will take care of the pin to port issues without ALL of the speed disadvantages of digitalWrite, etc. I can test this code on my mega, whereas I can't test the equivalent port version.
Use this code, then:

#include <digitalWriteFast.h>
pinModeFast(10, OUTPUT);
digitalWriteFast(10,LOW);
LiquidCrystal lcd(12,11, 5,4,3,2,&checkBusyFlag); //pins from tutorial

#define set_data_pins_to_read pinModeFast(5,INPUT),pinModeFast(4,INPUT),pinModeFast(3,INPUT),pinModeFast                           (2,INPUT);   //data pins 5,4,3,2 
#define set_data_pins_to_write 
pinModeFast(5,OUTPUT), pinModeFast(4,OUTPUT), pinModeFast(3,OUTPUT), pinModeFast(2,OUTPUT); // digitalWritefast etc gives some board independence
#define set_EN_high digitalWriteFast(11,HIGH);    //port B3 pin 11
#define set_EN_low digitalWriteFast(11,LOW);
#define set_EN2_high digitalWriteFast(11,HIGH);    //port B3 pin 11
#define set_EN2_low digitalWriteFast(11,LOW);
#define set_RW_high digitalWriteFast(10,HIGH);
#define set_RW_low digitalWriteFast(10,LOW);    //port B bit 2 pin10
#define set_RS_high digitalWriteFast(12,HIGH);    //port B bit 4  pin 12
#define set_RS_low digitalWriteFast(12,LOW);
#define read_busy digitalReadFast(2);        //portD2, pin 2

//You DON'T have to modify the subroutine itself:
void checkBusyFlag(int8_t chip) {
 uint8_t busy; // = 0x04;
 set_data_pins_to_read;
 set_RW_high;             //RW to read
 set_RS_low;
 if (chip == 0) {  //the if and else can be eliminated if only one hd44780 chip eg 20x4
 do {
   set_EN_high;
   delayMicroseconds(1);
   busy = read_busy;    // read busy flag
   set_EN_low;
   delayMicroseconds(1);
   set_EN_high; 
   delayMicroseconds(1);  //pulse the second nibble--discard it;
   set_EN_low; 
 }while (busy);
} else {
   do {
   set_EN2_high;
   delayMicroseconds(1);
   busy = read_busy;    // read busy flag
   set_EN2_low;
   delayMicroseconds(1);
   set_EN2_high; 
   delayMicroseconds(1);  //pulse the second nibble--discard it;
   set_EN2_low;
 } while (busy);
}
 set_data_pins_to_write; // data pins to write
 set_RW_low;              //RW to write
 set_RS_high;
}

Finally here is my idea of the equivalent defines for the Mega8 or 168/328 based Arduinos using ports; I expect there are errors here:

//LiquidCrystal lcd(12,11, 5,4,3,2,&checkBusyFlag); The is the example configuration from the Arduino tutorial for LiquidCrystal BUT hook RW to pin 10
#define set_data_pins_to_read DDRD&=~0b00111100;   //data pins 5,4,3,2 on Arduino 168,328
#define set_data_pins_to_write DDRD|=0b00111100;    // D5,D4,D3,D2
#define set_EN_high PORTB |= 0b00001000;    //port B3 pin 11
#define set_EN_low PORTB &= ~(1<<3);
#define set_EN2_high PORTB |= (1<<3);    //port B3 pin 11
#define set_EN2_low PORTB &= ~(1<<3);
#define set_RW_high PORTB |= (1<<2);
#define set_RW_low PORTB &= ~(1<<2);    //port B bit 2 pin10
#define set_RS_high PORTB |= (1<<4);    //port B bit 4  pin 12
#define set_RS_low PORTB &= ~(1<<4);
#define read_busy PIND & (1<<2);        //portD2, pin 2

Okay, I'm confused. I downloaded the library zip, but I think to the wrong folder. When I open it, I have two of everything. I saved it in doc>arduino 18>libraries>liquidcrystal>examples.
What would be the correct folder to put it in and why do I have two two of of everything everything?? Also what do I need to "include" in the program i.e. LiquidCrystal440.h ?

At this point there are 2 possible downloads: if you downloaded LiquidCrystal440.zip, make a new folder for it at doc>arduino 18>libraries>LiquidCrystal440. Then include LiquidCrystal440.h.

LiquidCrystal.zip is just an alias for LiquidCrystal440.zip that I created because of typos earlier in this thread.

LiquidCrystalBusy.zip goes in the doc>arduino 18>libraries>LiquidCrystal folder and replaces the standard library routines there. include LiquidCrystal.h

All versions are called in your code with the name
LiquidCrystal lcd(....

sorry for the confusion

Just wanted to thank jrraines for his libary!!
It works fine on a 27x4 LCD (4Bit mode).

How about putting it to the Playground??? I've started to make modifications by myself before I found your lib.
If you don't know how to do that, I can do it for you :wink:

I don't know how to put it on the playground. feel free.. I'm glad to hear that it works on a 27x4; I didn't even know there was such a size but I was pretty sure it worked for all the 2 chip/4 line designs; their addressing will always be 0x0,0x40,0x0,0x40, I think.

My opinion, as of today would be that what would go on the playground would be the older version without the user busy flag stuff. Paul Stoffregen pointed out that just having that complex option available may scare off some users and similar thoughts on my part lead me to keep the older version available for download.

Done!
Arduino Playground - LCD <- second row
Arduino Playground - EnhancedLiquidCrystal

Anybody feel free to add more text!

bingo:

You've got a letter missing on the playground page.

EnhancedLiquidCrystal for lager LCDs with two Enable (Enable1, Enable2) LiquidCrystal440.zip

Don

http://code.google.com/p/liquidcrystal440/downloads/list is an alternate place to get the downloads

I found an LCD that needed longer delays than were in the previous versions of the code. Several of the delays are a lot longer. see:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1271515975/0

I have a new version of the code that works with this LCD. It is, of course, slower than the previous version. It is so much slower that it made sense to reinstate the busy flag testing when the rw pin number is specified.

http://www.healthriskappraisal.org/LiquidCrystalBulletproof.zip

Modifications to LiquidCrystal for the Arduino with callback busy test

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. It worked with a 27x4 LCD. 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. (You can pass 255 as the RW item, ground RW and save an Arduino pin.)
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).

println
Although print has worked properly in the past, println has not. Now the '\r' and '\n' characters are not sent to the screen as though they were visible characters and the '\r' resets the character position to the top of the next line.

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 or pass it the address of a user routine to test the busy flag. 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.

Syntactic Sugar
#include <Streaming.h> from New Streaming Library | Arduiniana
Then you can combine that with an overloading of the () operator in this code. This lets you specify screen location and chain print commands together by writing: lcd(column,line)<<“a=”<<a;
Streaming.h is so efficient you may actually save a few bytes of memory!

Speed testing
All of the interface modes go faster than the eye can follow. This version of the software is significantly slower than previous versions when using timed delays. I found an LCD (Axman) that needed longer delays and in the interests of making the code foolproof, I lengthened the delays to make than LCD work. I compared the speeds of the different interfaces--writing 80 characters to the screen then 80 blanks and looping through that 20 times. The results on a Mega are:
Axman 4 data pins no RW 1491 milliseconds | nonAxman 1491
Axman 4 data pins + RW 774 milliseconds | nonAxman 679
Axman 8 data pins no RW 1407 milliseconds | nonAxman 1407
Axman 8 data pins + RW 633 milliseconds | nonAxman 620
Axman 4 pins + user busy 510 milliseconds | nonAxman 441

I also have a Teensy++2.0 board. One of the interesting things about that board is that the software that comes with it includes considerable optimization of digitalRead, digitalWrite etc. The board runs at 16 megaHz, just like the Mega, but speeding up those commands results in an impressive change in the benchmarks:
Axman 4 data pins no RW 1289 milliseconds | nonAxman 1289
Axman 4 data pins + RW 369 milliseconds | nonAxman 331
Axman 8 data pins no RW 1251 milliseconds | nonAxman 1251
Axman 8 data pins + RW 423 milliseconds | nonAxman 394
Axman 4 pins + user busy 361 milliseconds | nonAxman 252

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

User callback busy test
Get LiquidCrystal running without this first; setting up this complicated option is error-prone and this should be left for last, if implemented at all.

Just a quick idea... would rwBusy() be faster if you unrolled the two loops for the pinMode. For example, instead of this:

    for (uint8_t i = 0; i < _data_pinNumber; i++) pinMode(_data_pins[i],INPUT);

maybe something like this could be faster?

    pinMode(_data_pins[0],INPUT);
    pinMode(_data_pins[1],INPUT);
    pinMode(_data_pins[2],INPUT);
    pinMode(_data_pins[3],INPUT);
    if (_data_pinNumber > 4) {
        pinMode(_data_pins[4],INPUT);
        pinMode(_data_pins[5],INPUT);
        pinMode(_data_pins[6],INPUT);
        pinMode(_data_pins[7],INPUT);
    }

If the _data_pins were static, the compiler would likely just insert direct memory addresses. But maybe it'll be smart and use the Y pointer with displacement instruction? Maybe not?

Or if the compiler doesn't generate efficient code for that, this might coax it into using the single LD with post inc instruction for the first arg, and a single instruction for the second, so each line should compile to only 2 instructions plus the call.

    const uint8_t *pinptr = _data_pins;
    pinMode(*pinptr++,INPUT);
    pinMode(*pinptr++,INPUT);
    pinMode(*pinptr++,INPUT);
    pinMode(*pinptr++,INPUT);
    if (_data_pinNumber > 4) {
        pinMode(*pinptr++,INPUT);
        pinMode(*pinptr++,INPUT);
        pinMode(*pinptr++,INPUT);
        pinMode(*pinptr++,INPUT);
    }

It really should allocate "pinptr" to Y (r28 & r29). If it doesn't, there an asm syntax that can be added to force allocating to Y, which isn't clobbered by the call to pinMode.

I will play with those ideas. I think the problem will be that the library might get any pin number passed to it. It would be great if the optimizer looked across the call from user code to library code and saw what the passed pin numbers were, but I have assumed that was way too much to expect of the optimizer. It would be GREAT if my assumption has been wrong!

You are right that there is something different going on in the case of the Teensy++ in the rw busy test and the issue is almost certainly the time consumed doing 8 pinModes on the way in and 8 pinModes on the way out.
This shows up as surprising relative speeds in the 4 bit and 8 bit versions with rw on the Teensy++.