4 bit bus mode for a LCD

floresta:
When I initially connected my LCD to run your software for some reason I thought that RW was driven from pin 9 and RS from pin 10, and naturally it didn't work. I redid Hello World to match how I interpreted the connections and it worked. When I went back to your sketch (with 9 and 10 still connected backwards) and made the cursor visible it showed up initially in position 00 and then it moved to row 2. When I changed the data to print a 'C' it wound up two spaces over on row 2. These addresses are in the low 40's (in hex) just like the ASCII codes for the capital letters. So now I knew that in trying to display 'A': (1) I wasn't getting RS high and, (2) I was actually sending a 'Set DDRAM Address' instruction. It didn't take much more work to find the switched wires.

Let me know when you give up and I'll send you some (old) Arduino code that doesn't use any libraries. I probably shouldn't post it here right now, your students may have heard of Google. I think the PM system here is acting up but you can find an Email address for me at the link in Reply #10.

Don

Hello Don.

Im very happy today cause I found the error and a nice A showed up on the leftmost part of the display at line 1, after correcting the error.

The cable between lcd DB7 and microcontroller was broken. The cable is one of those readymade which I bought in a store who sells Arduino components.

I suspect the wire might be broken as some pupils holds in the middle part of cable when removing cable from the lab.board.

Using a new cable, and the helloworld example application, both the hello worl at line1 and counter at line 2 showed up.

And so did the A letter using my program.

Now I can make functions that handle commands and textinformation for displays in general and the busy-check for LCD internal work.

I must say a big THANK YOU to all thats participating in my thread and especially to You.

The last version of my program is listed below

regards
Jan

/*
testing av lcd display
 */
byte t60=60;
int pin4=4; // pinnumber of datapin LSB
int pin5=5;
int pin6=6;
int pin7=7;
int enable = 8;
int registerselect= 9;
int readwrite = 10;  // or ground

int enablepulse();
int init4dpy();

void setup() 
{
  Serial.begin(9600); 
  pinMode(pin4, OUTPUT);
  pinMode(pin5, OUTPUT);
  pinMode(pin6, OUTPUT);
  pinMode(pin7, OUTPUT);
  pinMode(enable, OUTPUT);
  pinMode(registerselect, OUTPUT); 
  pinMode(readwrite, OUTPUT);

 
 digitalWrite(enable,LOW);
 digitalWrite(registerselect,LOW); 
 digitalWrite(readwrite,LOW);
  init4dpy();
 digitalWrite(enable,HIGH);
}

void loop() 
{

  
}

int enablepulse() //send a pulse to the enable output
{
  digitalWrite(enable,LOW);
  digitalWrite(enable,HIGH);
  delayMicroseconds(1);
  digitalWrite(enable,LOW);
 }
 
int init4dpy()
{

 
// delayMicroseconds(150000); //await start process to finish

// write 3times to lcd with content ox03
digitalWrite(pin4,HIGH);
digitalWrite(pin5,HIGH);
digitalWrite(pin6,LOW);
digitalWrite(pin7,LOW);
enablepulse();
delayMicroseconds(5000); 

digitalWrite(pin4,HIGH);
digitalWrite(pin5,HIGH);
digitalWrite(pin6,LOW);
digitalWrite(pin7,LOW);
enablepulse();
delayMicroseconds(150); 

digitalWrite(pin4,HIGH);
digitalWrite(pin5,HIGH);
digitalWrite(pin6,LOW);
digitalWrite(pin7,LOW);
enablepulse();
delayMicroseconds(150); 


//set 4 bit data mode
digitalWrite(pin4,LOW);
digitalWrite(pin5,HIGH);
digitalWrite(pin6,LOW);
digitalWrite(pin7,LOW);
enablepulse();
delayMicroseconds(150); 

// 4 bit mode start from here and downwards 

// function set: 4 bit bus mode, 2 line dpy, 5x8dots format

digitalWrite(pin4,LOW);
digitalWrite(pin5,HIGH);
digitalWrite(pin6,LOW);
digitalWrite(pin7,LOW);
enablepulse();
digitalWrite(pin4,LOW);
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);
digitalWrite(pin7,HIGH);
enablepulse();
delayMicroseconds(t60);

// turn off dpy, cursor and blink (ddram still exists)

digitalWrite(pin4,LOW);
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);
digitalWrite(pin7,LOW);
enablepulse();
digitalWrite(pin4,LOW);
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);
digitalWrite(pin7,HIGH);
enablepulse();
delayMicroseconds(t60);

// clear dpy
digitalWrite(pin4,LOW);
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);
digitalWrite(pin7,LOW);
enablepulse();
digitalWrite(pin4,HIGH);
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);
digitalWrite(pin7,LOW);
enablepulse();
delayMicroseconds(4000);

// entry mode: cursor/blink moves to right and ddram adress is increased by 1 and shift right. I dont fully understand SH parameter yet.

digitalWrite(pin4,LOW);
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);
digitalWrite(pin7,LOW);
enablepulse();
digitalWrite(pin4,LOW);
digitalWrite(pin5,HIGH);
digitalWrite(pin6,HIGH);
digitalWrite(pin7,LOW);
enablepulse();
delayMicroseconds(t60);

// end of initializing

// turn dpy, cursor on and blinking off

digitalWrite(pin4,LOW);
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);
digitalWrite(pin7,LOW);
enablepulse();
digitalWrite(pin4,LOW);
digitalWrite(pin5,LOW);
digitalWrite(pin6,HIGH);
digitalWrite(pin7,HIGH);
enablepulse();
delayMicroseconds(t60);

delay(1000);

  
  // write A to dpy
digitalWrite(registerselect,HIGH); 
delayMicroseconds(60);
digitalWrite(pin4,LOW);
digitalWrite(pin5,LOW);
digitalWrite(pin6,HIGH);
digitalWrite(pin7,LOW);
enablepulse();
digitalWrite(pin4,HIGH);
digitalWrite(pin5,LOW);
digitalWrite(pin6,LOW);
digitalWrite(pin7,LOW);
enablepulse();
delayMicroseconds(t60);
digitalWrite(registerselect,LOW);
}