Go Down

Topic: 4 bit LCD (Read 3822 times) previous topic - next topic

tjerk

Hi there,

Recently I got my Arduino board (which I am using on a mac) and I was wondering if anybody managed to hook up and LCD using 4 instead of 8 bits..and if so, if that person is willing to share this with me,

KR,

Tjerk Timan

mrbbp

Hi Tjerk.

I didn't tried but i suppose you send info to the lcd in 2 salves, the 4 left's bits and the fourth's right's bits.
i suppose you have to add a delay between the 2 "salves".

if you try it, give us feedback...
i don't find any tuto to communicate to lcd in 4bits, only instructions in the lcd's datasheets.

regards

eric

Massimo Banzi

Here is some code

I didn't test it... please try it out

if it works then write a nice article in the playground and document it

massimo


Code: [Select]

/*
*      LCD 4bit interface example
* 2006 Massimo Banzi
* Based on code written by Craig Lee 1998
*      
*/



#define LCD_RS      8      // Register select
#define LCD_EN      9      // Enable
#define LCD_D4 10      // Data bits
#define LCD_D5 11      // Data bits
#define LCD_D6 12      // Data bits
#define LCD_D7 13      // Data bits


     

void lcd_strobe()
{
     digitalWrite(LCD_EN,HIGH);
     digitalWrite(LCD_EN,LOW);
}

/* write a byte to the LCD in 4 bit mode */

void lcd_write(byte c)
{
     if(c & 0x80) digitalWrite(LCD_D7,HIGH); else  digitalWrite(LCD_D7,LOW);
     if(c & 0x40) digitalWrite(LCD_D6,HIGH); else  digitalWrite(LCD_D6,LOW);  
     if(c & 0x20) digitalWrite(LCD_D5,HIGH); else  digitalWrite(LCD_D5,LOW);  
     if(c & 0x10) digitalWrite(LCD_D4,HIGH); else  digitalWrite(LCD_D4,LOW);  
     lcd_strobe();
     if(c & 0x08) digitalWrite(LCD_D7,HIGH); else  digitalWrite(LCD_D7,LOW);
     if(c & 0x04) digitalWrite(LCD_D6,HIGH); else  digitalWrite(LCD_D6,LOW);  
     if(c & 0x02) digitalWrite(LCD_D5,HIGH); else  digitalWrite(LCD_D5,LOW);  
     if(c & 0x01) digitalWrite(LCD_D4,HIGH); else  digitalWrite(LCD_D4,LOW);  
     lcd_strobe();
     delayMicroseconds(40);
}

/*
*       Clear and home the LCD
*/

void
lcd_clear(void)
{
     digitalWrite(LCD_RS,LOW);

     lcd_write(0x1);
     delay(2);
}

/* write a string of chars to the LCD */

void
lcd_puts(const char * s)
{
   digitalWrite(LCD_RS,HIGH);      // write characters

     while(*s) lcd_write(*s++);
}

/* write one character to the LCD */

void
lcd_putch(byte c)
{
   digitalWrite(LCD_RS,HIGH); // write characters

     lcd_write(c);
}


/*
* Go to the specified position
*/

void
lcd_goto(byte pos)
{
   digitalWrite(LCD_RS,0);

     lcd_write(0x80 + pos);
}
     
/* initialise the LCD - put into 4 bit mode */

void
lcd_init(void)
{              
     pinMode(LCD_D7,OUTPUT);          
     pinMode(LCD_D6,OUTPUT);
     pinMode(LCD_D5,OUTPUT);
     pinMode(LCD_D4,OUTPUT);
     pinMode(LCD_EN,OUTPUT);        
     pinMode(LCD_RS,OUTPUT);        
     
     digitalWrite(LCD_RS, LOW);      // write control bytes

     delay(15);// power on delay

     digitalWrite(LCD_D4, HIGH);      // init!      
     digitalWrite(LCD_D5, HIGH); //
     lcd_strobe();
     delay(5);

     lcd_strobe();// init!      
     delayMicroseconds(100);

     lcd_strobe();// init!      
     delay(5);

     digitalWrite(LCD_D4, LOW);      // set 4 bit mode
     lcd_strobe();
     delayMicroseconds(40);
     
     lcd_write(0x28);// 4 bit mode, 1/16 duty, 5x8 font, 2lines
     lcd_write(0x0C);// display on
     lcd_write(0x06);// entry mode advance cursor
     lcd_write(0x01);// clear display and reset cursor
}


void setup() {
 lcd_init();
}

void loop() {
 lcd_puts("hello world");

}

tjerk

Dear MAssimo,

Thanx for the effort! I'll work on it today and will definitely post the result!

kr

tjerk

tomek

hej,

here is a codeexample for an lcd-display with 4 datapins, if you want i have also a nice pdf with some pictures, schematics and so on ....

tomek

Code: [Select]

/* 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
*
*/
int led = 13;
int DI = 12;
int RW = 11;
int DB[] = { 7, 8, 9, 10};
int Enable = 6;
int count = 0;

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 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);

// Write the message

//like this
LcdDataWrite('L');
LcdDataWrite('c');
LcdDataWrite('d');

//or like this
for ( count = 0; count<=7; count++) {
 int wrote [] = { 'D', 'i', 's', 'p', 'l', 'a', 'y',  ' ', 'w', 'i', 't', 'h',' ' };
LcdDataWrite(wrote[count]);
}

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

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

delay(3000);
}

mrbbp

thanks tomek,

i'll try it next week.

regards

eric :D

beltran

hey tomek,
sounds great! why don't you post it into the www.arduino.cc/playground wiki??
that could help everybody!

thanks in advance
b.

mrbbp

beltran... :'(
i'm enable to login to playground...
would you remove my account ?
i'll try to register an other one :)

regards

eric

beltran

voilà!
you have been deleted!
you and all others can subscribe at the following page
http://www.arduino.cc/playground/Main/LoginForm

there are many contributors but very little content...this thing needs to get working :)

cheers
b.

Go Up