Using 128x64 LCD

I am trying to use 128x64 display. It works only if I remove and reinsert rs,rw,en,cs1 and cs2 frequently. Once its ON, it will work on all resets and no problem if I insert another program. If I disconnect the USB and insert it after 10-20 seconds I again need to insert and reinsert the pins.

Current program is shown below

const int rs =  9, rw = 10, en = 11, cs1 = 12, cs2 = 13,rst = 8;
const int d0 = 37, d1 = 36, d2 = 35, d3 = 34;
const int d4 = 33, d5 = 32, d6 = 31, d7 = 30;
const int switch_key = 2, switch_push = 3, music = 4;


void setup() 
{
  pinMode(rs, OUTPUT);
  pinMode(rw, OUTPUT);
  pinMode(en, OUTPUT);
  pinMode(cs1, OUTPUT);
  pinMode(cs2, OUTPUT);
  pinMode(rst, OUTPUT);
  pinMode(d0, OUTPUT);
  pinMode(d1, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);
  pinMode(d5, OUTPUT);
  pinMode(d6, OUTPUT);
  pinMode(d7, OUTPUT);
  pinMode(switch_push, INPUT);
  pinMode(switch_key, INPUT);
  pinMode(music, OUTPUT);
  Serial.begin(115200);
  Serial.print("began");
}

void loop() 
{

  byte i=0x00,j=0x40,k=0xF8;
  int c_time,flag;
  digitalWrite(rst,LOW);
  digitalWrite(cs2,HIGH);
  digitalWrite(cs1,HIGH);
  digitalWrite(rst,HIGH);
  digitalWrite(cs2,LOW);
  digitalWrite(cs1,LOW);
  lcdcmd(0x3F);
  lcdcmd(0x3F);

}

void lcdcmd(char cmd)
{
  PORTC = cmd;
  digitalWrite(rw,LOW);
  digitalWrite(rs,LOW);
  digitalWrite(en,LOW);
  delayMicroseconds(300);
  digitalWrite(en,HIGH);
  delayMicroseconds(300);
  digitalWrite(en,LOW);
}

First, why write your own code when there are several libraries out there already written?

next some additional questions.
We need to know what environment you are using like which arduino board and
which chip is on the glcd you are using.

The signal names look like it might be a ks0108.
If it is a ks0108 and the rst pin is wired up to the glcd reset signal, then why are you resetting the chip in your loop() code?

If you reset a ks0108 chip it can take several milliseconds before it is ready to receive a command.
You have to look at the reset status bit to determine when the reset is complete.

Also, after you send any command but before you send another command you must look at the ready status bit to see if the chip is ready to receive the next command.

Thanks for reply, As you told, I tried g8Ulib.
It works ok.
I am using arduino mega 2560. I need to manually write the code in order to know how it works and to be more familiar with this.
I am doing some problem with initialization. I dont know whats the problem. Thanks for any help. I tied RESET to vcc.

Everything solved. Just changed a few lines. Initialization bytes are{0xC0,0x40,0xB8,0x3F} and these are to be given when cs1 and cs2 are high. Just added below shown lines in setup.

digitalWrite(cs2,HIGH);
digitalWrite(cs1,HIGH);
lcdcmd(0xC0);
lcdcmd(0x40);
lcdcmd(0xB8);
lcdcmd(0x3F);

We still don't know which lcd chipset you have on you glcd since you have not told us, but those commands look like ks0108 commands.

  • 0xc0 set display start
  • 0x40 set address
  • 0xb8 set page
  • 0x3f display on.

I'd recommend reading the data sheet for the chipset (not the one for the module). It will show the proper initialization sequence as well as the necessary timing for reads/writes of data/commands. Once you start to send commands it is likely that you will have to monitor the "ready" status or add artificial delays to slow down the host code to keep from overrunning the lcd chipset with commands.

If it is a ks0108 based display, it will have more than a single chip on a 124x64 module since each chip can only control 64x64 pixels.
That means you have to initialize both of them.
Some modules allow you to talk to both chips at the same time, but many modules do not.
The safest thing to to is to initialize each chip separately.
Also, most ks0108 chips do not enable the pixels by default so in order to see pixels you have to send a display on command.

--- bill