Home » code snippets, projects

Arduino and HD44780 LCD v1.0 snippet

Submitted by Carl on Saturday, 20 September 2008One Comment
/* LCD display with 4 DataPins
* --------
*
* This is a example in how to use an LCD screen
* configured with data transfers over 2 x 4 bits. The example
* based on the "LCD Hola example" by DojoDave.
*
* There are the following pins to be considered:
*
* - DI, RW, DB0..DB3, Enable (7 in total)
*
* the pinout for LCD displays is standard and there is plenty
* of documentation to be found on the internet.
*
* (cleft) 2006 Tomek for K3 and fh-potsdam
* (cleft) 2008 Carl - Modifications
*
*/
int led = 13;
int DI = 12;
int RW = 11;
int DB[] = { 7, 8, 9, 10};
int Enable = 6;
int count = 1;
int count1 = 1;
int count2 = 1;

void LcdCommandWrite(int value) {
int i = 0;
int value1 = 0;
value1 = value;

value1 >>= 4;               //send the first 4 databits (from 8) + RW and DI
for (i=DB[0]; i <= DI; i++) {
  digitalWrite(i,value1 & 01);
  value1 >>= 1;
}
digitalWrite(Enable,LOW);   // send a pulse to enable
delayMicroseconds(1);
digitalWrite(Enable,HIGH);
delayMicroseconds(1);
digitalWrite(Enable,LOW);
delayMicroseconds(1); //    pause 1 ms according to datasheet
delay(1);

for (i=DB[0]; i <= DB[3]; i++) {  // secound part of the secound 4 bits (from 8)
  digitalWrite(i,value & 01);
  value >>= 1;
}
value >>= 4;                 // send the RW and DI of the secound 4 bits(from 8)
for (i=RW; i <= DI; i++) {
  digitalWrite(i,value & 01);
  value >>= 1;
}
digitalWrite(Enable,LOW); // send a pulse to enable
delayMicroseconds(1);
digitalWrite(Enable,HIGH);
delayMicroseconds(1);
digitalWrite(Enable,LOW);
delayMicroseconds(1); // pause 1 ms according to datasheet
}

void LcdDataWrite(int value) {
int i = 0;
int value1 = 0;
digitalWrite(DI, HIGH);
digitalWrite(RW, LOW);
value1 =value;
value1 >>= 4;                     //send the first 4 databits (from 8)
for (i=DB[0]; i <= DB[3]; i++) {
  digitalWrite(i,value1 & 01);
  value1 >>= 1;
}
digitalWrite(Enable,LOW);   // send a pulse to enable
delayMicroseconds(1);
digitalWrite(Enable,HIGH);
delayMicroseconds(1);
digitalWrite(Enable,LOW);
delayMicroseconds(1);   // pause 1 ms according to datasheet
delay(1);
digitalWrite(DI, HIGH);
digitalWrite(RW, LOW);
for (i=DB[0]; i <= DB[3]; i++) {
  digitalWrite(i,value & 01);
  value >>= 1;
}
digitalWrite(Enable,LOW);   // send a pulse to enable
delayMicroseconds(1);
digitalWrite(Enable,HIGH);
delayMicroseconds(1);
digitalWrite(Enable,LOW);
delayMicroseconds(1); // pause 1 ms according to datasheet
}

// this funktion help us to write number over 9, easyely in the lcd display

void LcdNumberWrite(int nr) {

int n1 = 0;
int n2 = 0;

n1 = n2 = nr;

n1 = n1 / 100;
LcdCommandWrite(560 + n1);  //512 used to wirte data (see commands for character modul)
n2 = (n2 - n1 * 100) / 10;
LcdCommandWrite(560 + n2);  //512 used to wirte data (see commands for character modul)
nr = nr - n1 *100 - n2 * 10;
LcdCommandWrite(560 + nr);  //512 used to wirte data (see commands for character modul)
}

void LcdNumberWrite2digits(long N) {
long display1 = 0;
long display2 = 0;
long display3 = 0;
long display4 = 0;
long display1a = 0;
long display1b = 0;
long display1c = 0;
//display1 = display2 = display 3 = display4 = N;

/*
display1c = (N/1000000);
N = N - (display1c * 1000000);
LcdCommandWrite(560 + display1c); //512 used to wirte data

display1b = (N/100000);
N = N - (display1b * 100000);
LcdCommandWrite(560 + display1b); //512 used to wirte data

display1a = (N/10000);
N = N - (display1a * 10000);
LcdCommandWrite(560 + display1a); //512 used to wirte data

display1 = (N/1000);
N = N - (display1 * 1000);
LcdCommandWrite(560 + display1); //512 used to wirte data

display2 = (N/100);
N = N - (display2 * 100);
LcdCommandWrite(560 + display2); //512 used to wirte data
*/
display3 = (N/10);
N = N - (display3 * 10);
LcdCommandWrite(560 + display3); //512 used to wirte data

display4 = N;
LcdCommandWrite(560 + display4); //512 used to wirte data

}

void setup (void) {

int i = 0;
for (i=Enable; i <= DI; i++) {
pinMode(i,OUTPUT);
}
delay(100);
// initiatize lcd after a short pause
// needed by the LCDs controller

///////////////////////////////////////////////////// 4 pin initialization
LcdCommandWrite(0x03); // function set:
// 4 pin initialization
delay(64);
LcdCommandWrite(0x03); // function set:
// 4 pin initialization
delay(50);
LcdCommandWrite(0x03); // function set:
// 4 pin initialization
delay(50);
LcdCommandWrite(0x02); // function set:
// 4 pin initialization
delay(50);
LcdCommandWrite(0x2C); // function set:
// 4-bit interface, 1 display lines, 5x7 font
///////////////////////////////////////////////////// end of 4 pin initialization 

delay(20);
LcdCommandWrite(0x06); // entry mode set:
// increment automatically, no display shift
delay(20);
LcdCommandWrite(0x0E); // display control:
// turn display on, cursor on, no blinking
delay(20);
LcdCommandWrite(0x01); // clear display, set cursor position to zero
delay(100);

LcdCommandWrite(0x80); // display control:
delay(20);

//////// unter this line are the special stuff you don't need for a initialitzation

LcdCommandWrite(0x0F); //  cursor blink
delay(10);
}

void loop (void) {

//>>>>>>>>>>>>>>>>>>>>>>>>>>>possible commands for the Lcd Display>>>>>>><< able to use for LcdDisplays with 4 or with 8 DataPins

//LcdCommandWrite(0x01); // clear display, set the cursor to home position
//LcdCommandWrite(0x02); // set cursor position to zero
//LcdCommandWrite(0x0A); // set the display off
//LcdCommandWrite(0x0E); // set the display on and with out cursor blink
//LcdCommandWrite(0x0F); // set the display on and  with cursor blink
//LcdCommandWrite(0x0F); //  cursor blink
LcdCommandWrite(0x0E); //  cursor not blink
//LcdCommandWrite(0x18); // shift display and cursor to the left
//LcdCommandWrite(0x1c); // shift display and cursor to the right
//LcdCommandWrite(0x14); // shift cursor to the right
//LcdCommandWrite(0x10); // shift cursor to the left

//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> example <<<<<<<<<

LcdCommandWrite(0x02); // set cursor position to zero
delay(10);
/*
for ( count = 1; count<=10; count++) {
LcdNumberWrite2digits(count);
}
for ( count = 1; count<=10; count++) {
LcdNumberWrite2digits(count);
}
for ( count = 1; count<=10; count++) {
LcdNumberWrite2digits(count);
}
for ( count = 1; count<=10; count++) {
LcdNumberWrite2digits(count);
}
for ( count = 1; count<=3; count++) {
  int wrote [] = { 'o', 'n', 'e'};
  LcdDataWrite(wrote[count]);
}
for ( count = 1; count<=16; count++) {
  LcdDataWrite(' ');
}

for ( count1 = 1; count1<=3; count1++) {
  int wrote1 [] = { 't', 'w', 'o' , };
  LcdDataWrite(wrote1[count1]);
}
for ( count = 1; count<=16; count++) {
  LcdDataWrite(' ');
}
for ( count2 = 1; count2<=5; count2++) {
  int wrote2 [] = { 't', 'h', 'r','e','e',};
  LcdDataWrite(wrote2[count2]);
}
for ( count = 1; count<=16; count++) {
  LcdDataWrite(' ');
}
for ( count = 1; count<=4; count++) {
  int wrote [] = { 'f', 'o', 'u','r'};
  LcdDataWrite(wrote[count]);
}
for ( count = 1; count<=16; count++) {
  LcdDataWrite(' ');
}
LcdDataWrite('o')
n
e
');
LcdDataWrite('two                 ');
LcdDataWrite('three               ');
LcdDataWrite('four                ');
for ( count = 1; count<=20; count++) {
LcdDataWrite(' ');
}

for ( count = 1; count<=10; count++) {
LcdNumberWrite2digits(count);
}
LcdDataWrite('w');
LcdDataWrite('i');
LcdDataWrite('t');
LcdDataWrite('h');
LcdDataWrite(' ');

// and Number over 9 easyely like this
LcdDataWrite('4');

LcdDataWrite('P');
LcdDataWrite('i');
LcdDataWrite('n');
LcdDataWrite('s');

*/
for ( count = 0; count<=2; count++) {
  int wrote [] = { 'O', 'n', 'e',};
  LcdDataWrite(wrote[count]);
}
for ( count = 0; count<=16; count++) {
  LcdDataWrite(' ');
}
for ( count = 0; count<=2; count++) {
  int wrote [] = { 'T','w','o',};
LcdDataWrite(wrote[count]);
}
for ( count = 0; count<=16; count++) {
  LcdDataWrite(' ');
}
for ( count = 0; count<=4; count++) {
  int wrote [] = { 'T','h','r','e','e',};
LcdDataWrite(wrote[count]);
}
for ( count = 0; count<=14; count++) {
  LcdDataWrite(' ');
}
for ( count = 0; count<=3; count++) {
  int wrote [] = { 'F','o','u','r',};
LcdDataWrite(wrote[count]);
} 

delay(1000);
}

One Comment »

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.