Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Programming Questions & Help >> Library writers >> 4 bit LCD library
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1231273220

Message started by showup on 01/06/09 at 20:20:20

Title: 4 bit LCD library
Post by showup on 01/06/09 at 20:20:20
Hi:

Is there a 4 bit LCD library that works with wiring board? I am just looking for something similar to the Arduino 4 bit library:

http://www.arduino.cc/playground/Code/LCD4BitLibrary

If we don't have one for Wiring dose the Arduino 4-bit LCD library will work in the Wiring environment with Wiring board?  ::)

Any comment is appreciated. Thanks.

Title: Re: 4 bit LCD library
Post by barragan on 01/07/09 at 06:09:23
at the very beginning there used to be one, but it was replaced by the actual one. Are you having trouble with the 8 bit one?

Title: Re: 4 bit LCD library
Post by showup on 01/07/09 at 11:43:41
I got a olimex AVR-MT-128 board. It comes with a LCD with 4 bit connection. I thought if we have a 4 bit library that runs under wiring environment it may be easy to use for this board. As the connections are different I need to de-solder and take out the LCD to make it work with 8 bit LCD library.

Does anybody still have a working copy of the 4 bit library that existed at the very beginning?

Thanks.

Title: Re: 4 bit LCD library
Post by showup on 01/07/09 at 21:56:17
I just tried using the Arduino 4Bit library in Wiring.
I am getting the following errors.

LCD4Bit.cpp: In member function 'void LCD4Bit::pulseEnablePin()':
LCD4Bit.cpp:58: error: 'digitalWrite' was not declared in this scope
LCD4Bit.cpp:59: error: 'delayMicroseconds' was not declared in this scope
LCD4Bit.cpp:64: error: 'delay' was not declared in this scope
LCD4Bit.cpp: In member function 'void LCD4Bit::pushNibble(int)':
LCD4Bit.cpp:73: error: 'digitalWrite' was not declared in this scope
LCD4Bit.cpp: In member function 'void LCD4Bit::commandWriteNibble(int)':
LCD4Bit.cpp:99: error: 'digitalWrite' was not declared in this scope
LCD4Bit.cpp: In member function 'void LCD4Bit::commandWrite(int)':
LCD4Bit.cpp:106: error: 'digitalWrite' was not declared in this scope
LCD4Bit.cpp: In member function 'void LCD4Bit::print(int)':
LCD4Bit.cpp:118: error: 'digitalWrite' was not declared in this scope
LCD4Bit.cpp: In member function 'void LCD4Bit::clear()':
LCD4Bit.cpp:139: error: 'delay' was not declared in this scope
LCD4Bit.cpp: In member function 'void LCD4Bit::init()':
LCD4Bit.cpp:146: error: 'pinMode' was not declared in this scope
LCD4Bit.cpp:154: error: 'delay' was not declared in this scope
LCD4Bit.cpp:160: error: 'delayMicroseconds' was not declared in this scope
LCD4Bit.cpp: In member function 'void LCD4Bit::leftScroll(int, int)':
LCD4Bit.cpp:225: error: 'delay' was not declared in this scope
o: In function `__static_initialization_and_destruction_0':
undefined reference to `LCD4Bit::LCD4Bit(int)'o: In function `loop':
o: In function `setup':


But isn't many of these are commands according to the Wiring syntax?
Any suggestions??

Title: Re: 4 bit LCD library
Post by barragan on 01/07/09 at 23:09:55
it is on the sourcecode repository, right here: http://wiring.uniandes.edu.co/source/tags/wiring-0006/wiring/core/WDisplay.c?revision=293&view=markup

Title: Re: 4 bit LCD library
Post by showup on 01/07/09 at 23:32:40
Thanks.

How can i get the pin connection info for this.

Title: Re: 4 bit LCD library
Post by showup on 01/08/09 at 16:15:33
I was able to make the Arduino LCD4Bit library work with Wiring.
This is the library i am talking about

http://www.arduino.cc/playground/Code/LCD4BitLibrary

I am posting it here so that some one can use it...

I am a noob in programming so the file may not be according to the professional standards of programming. But it does the job in  my LCD. Comments and corrections are appreciated.

extern "C" {
#include <stdio.h>  //not needed yet
#include <string.h> //needed for strlen()
#include <inttypes.h>
#include "WConstants.h"  //all things wiring / arduino
}

//command bytes for LCD
#define CMD_CLR 0x01
#define CMD_RIGHT 0x1C
#define CMD_LEFT 0x18
#define CMD_HOME 0x02

// --------- PINS -------------------------------------
//is the RW pin of the LCD under our control?  If we're only ever going to write to the LCD, we can use one less microcontroller pin,
//and just tie the LCD pin to the necessary signal, high or low.
//this stops us sending signals to the RW pin if it isn't being used.
boolean USING_RW = true;

//RS, RW and Enable can be set to whatever you like
int RS = 8;
int RW = 9;
int Enable = 10;
int DB[] = {12, 13, 14, 15};  //wire these to DB4~7 on LCD.

//--------------------------------------------------------

//some messages to display on the LCD
char msgs[6][15] = {
 "apple", "banana", "pineapple", "mango", "watermelon", "pear"};
int NUM_MSGS = 6;

int g_num_lines = 2;

void setup() {
 init();
 LCD4Bit(2);   //number of lines in LCD
}

void loop() {
 int pick = random(NUM_MSGS);
 char* msg = msgs[pick];
 
 clear();
 printIn(msg);
 delay(1000);

 //print some dots individually
 for (int i=0; i<3; i++){
 print('.');
 delay(100);
 }

 //print something on the display's second line.
 //uncomment this if your display HAS two lines!
 cursorTo(2, 0);  //line=2, x=0.
 printIn("Score: 6/7");
 delay(1000);
}


//This clocks whatever command or data is in DB4~7 into the LCD controller.
void pulseEnablePin(){
 digitalWrite(Enable,LOW);
 delayMicroseconds(1);
 // send a pulse to enable
 digitalWrite(Enable,HIGH);
 delayMicroseconds(1);
 digitalWrite(Enable,LOW);
 delay(1);  // pause 1 ms.  TODO: what delay, if any, is necessary here?
}

//push a nibble of data through the the LCD's DB4~7 pins, clocking with the Enable pin.
//We don't care what RS and RW are, here.
void pushNibble(int value){
 int val_nibble = value & 0x0F;  //clean the value.  (unnecessary)

 for (int i=DB[0]; i <= DB[3]; i++) {
   digitalWrite(i,val_nibble & 01);
   val_nibble >>= 1;
 }
 pulseEnablePin();
}

//push a byte of data through the LCD's DB4~7 pins, in two steps, clocking each with the enable pin.
void pushByte(int value){
 int val_lower = value & 0x0F;
 int val_upper = value >> 4;
 pushNibble(val_upper);
 pushNibble(val_lower);
}


//stuff the library user might call---------------------------------

void LCD4Bit (int num_lines) {
 g_num_lines = num_lines;
 if (g_num_lines < 1 || g_num_lines > 2) {
   g_num_lines = 1;
 }
}

void commandWriteNibble(int nibble) {
 digitalWrite(RS, LOW);
 if (USING_RW == true) {
 digitalWrite(RW, LOW);
 }
 pushNibble(nibble);
}


void commandWrite(int value) {
 digitalWrite(RS, LOW);
 if (USING_RW == true) {
   digitalWrite(RW, LOW);
 }
 pushByte(value);
 //TODO: perhaps better to add a delay after EVERY command, here.  many need a delay, apparently.
}




//print the given character at the current cursor position. overwrites, doesn't insert.
void print(int value) {
 //set the RS and RW pins to show we're writing data
 digitalWrite(RS, HIGH);
 if (USING_RW == true) {
   digitalWrite(RW, LOW);
 }

 //let pushByte worry about the intricacies of Enable, nibble order.
 pushByte(value);
}


//print the given string to the LCD at the current cursor position.  overwrites, doesn't insert.

void printIn(char msg[]) {
 uint8_t i;  //fancy int.  avoids compiler warning when comparing i with strlen()'s uint8_t
 for (i=0;i < strlen(msg);i++){
   print(msg[i]);
 }
}


//send the clear screen command to the LCD
void clear(){
 commandWrite(CMD_CLR);
 delay(1);
}

//post continues

Title: Re: 4 bit LCD library
Post by showup on 01/08/09 at 16:16:21
// initiatize lcd after a short pause
//while there are hard-coded details here of lines, cursor and blink settings, you can override these original settings after calling init()

void init () {
 pinMode(Enable,OUTPUT);
 pinMode(RS,OUTPUT);
 if (USING_RW == true) {
   pinMode(RW,OUTPUT);
 }
 pinMode(DB[0],OUTPUT);
 pinMode(DB[1],OUTPUT);
 pinMode(DB[2],OUTPUT);
 pinMode(DB[3],OUTPUT);

 delay(50);

 //The first 4 nibbles and timings are not in my DEM16217 SYH datasheet, but apparently are HD44780 standard...
 commandWriteNibble(0x03);
 delay(5);
 commandWriteNibble(0x03);
 delayMicroseconds(100);
 commandWriteNibble(0x03);
 delay(5);

 // needed by the LCDs controller
 //this being 2 sets up 4-bit mode.
 commandWriteNibble(0x02);
 commandWriteNibble(0x02);
 //todo: make configurable by the user of this library.
 //NFXX where
 //N = num lines (0=1 line or 1=2 lines).
 //F= format (number of dots (0=5x7 or 1=5x10)).
 //X=don't care

 int num_lines_ptn = g_num_lines - 1 << 3;
 int dot_format_ptn = 0x00;      //5x7 dots.  0x04 is 5x10

   commandWriteNibble(num_lines_ptn | dot_format_ptn);
 delayMicroseconds(60);

 //The rest of the init is not specific to 4-bit mode.
 //NOTE: we're writing full bytes now, not nibbles.

 // display control:
 // turn display on, cursor off, no blinking
 commandWrite(0x0C);
 delayMicroseconds(60);

 //clear display
 commandWrite(0x01);
 delay(3);

 // entry mode set: 06
 // increment automatically, display shift, entire shift off
 commandWrite(0x06);

 delay(1);//TODO: remove unnecessary delays
}


//non-core stuff --------------------------------------
//move the cursor to the given absolute position.  line numbers start at 1.
//if this is not a 2-line LCD4Bit instance, will always position on first line.
void cursorTo(int line_num, int x){
 //first, put cursor home
 commandWrite(CMD_HOME);

 //if we are on a 1-line display, set line_num to 1st line, regardless of given
 if (g_num_lines==1){
   line_num = 1;
 }
 //offset 40 chars in if second line requested
 if (line_num == 2){
   x += 40;
 }
 //advance the cursor to the right according to position. (second line starts at position 40).
 for (int i=0; i<x; i++) {
   commandWrite(0x14);
 }
}

Title: Re: 4 bit LCD library
Post by barragan on 01/09/09 at 21:13:44
thatīs great, Iīll add your contribution to the libraries.
best,

Title: Re: 4 bit LCD library
Post by Zap on 12/23/10 at 07:46:38
Was this 4 bit lib ever added?

Thanks Zap

Title: Re: 4 bit LCD library
Post by barragan on 12/24/10 at 05:33:35
hi Zap, it hasn't officially, but you can try the solution posted, it works well.

Title: Re: 4 bit LCD library
Post by Zap on 12/24/10 at 06:52:35
I did try that code but I must be doing some thing wrong as I get a list of errors below, (Same as you originally had.)

What did you do to get it to work?


LCD4Bit.cpp: In member function 'void LCD4Bit::pulseEnablePin()':
LCD4Bit.cpp:58: error: 'digitalWrite' was not declared in this scope
LCD4Bit.cpp:59: error: 'delayMicroseconds' was not declared in this scope
LCD4Bit.cpp:64: error: 'delay' was not declared in this scope
LCD4Bit.cpp: In member function 'void LCD4Bit::pushNibble(int)':
LCD4Bit.cpp:73: error: 'digitalWrite' was not declared in this scope
LCD4Bit.cpp: In member function 'void LCD4Bit::commandWriteNibble(int)':
LCD4Bit.cpp:99: error: 'digitalWrite' was not declared in this scope
LCD4Bit.cpp: In member function 'void LCD4Bit::commandWrite(int)':
LCD4Bit.cpp:106: error: 'digitalWrite' was not declared in this scope
LCD4Bit.cpp: In member function 'void LCD4Bit::print(int)':
LCD4Bit.cpp:118: error: 'digitalWrite' was not declared in this scope
LCD4Bit.cpp: In member function 'void LCD4Bit::clear()':
LCD4Bit.cpp:139: error: 'delay' was not declared in this scope
LCD4Bit.cpp: In member function 'void LCD4Bit::init()':
LCD4Bit.cpp:146: error: 'pinMode' was not declared in this scope
LCD4Bit.cpp:154: error: 'delay' was not declared in this scope
LCD4Bit.cpp:160: error: 'delayMicroseconds' was not declared in this scope
LCD4Bit.cpp: In member function 'void LCD4Bit::leftScroll(int, int)':
LCD4Bit.cpp:225: error: 'delay' was not declared in this scope
o: In function `__static_initialization_and_destruction_0':
undefined reference to `LCD4Bit::LCD4Bit(int)'o: In function `loop':
o: In function `setup':

Thanks Zap.

Title: Re: 4 bit LCD library
Post by barragan on 12/24/10 at 14:33:08
You must put the library inside your sketchbook directory My documents/Wiring/libraries (windows) or Documents/Wiring/libraries (osx), then restart Wiring, make sure to include the library in Sketch -> Add library, the above error seem because the library itself is not including WProgram.h, see you're getting an error somebody else had before and fixed it.

Title: Re: 4 bit LCD library
Post by Zap on 12/25/10 at 00:42:09
Barragan, I have the 4bit lib in the wiring program's libraries folder ,( I thought that's where it should be)
C:\Users\Ray\My progs\wiring-0027\libraries\LCD4Bit

There is no libaries folder in my sketchbook directory
C:\Users\Ray\Documents\Wiring
I tried making a Libraries folder there with the 4 bit lib in it but that also failed.

Thanks Zap

Title: Re: 4 bit LCD library
Post by barragan on 12/25/10 at 05:20:39
yes, no problem, you can create manually the libraries folder in your sketch folder and drop the LCD4Bit folder in there. Please see how somebody has the same error you're getting, to fix the error messages, just add this line:

#include <WProgram.h> after

#include <inttypes.h>

in the LCD4Bit.h file that is inside the distributed library.
Let me know.

Title: Re: 4 bit LCD library
Post by Zap on 12/25/10 at 12:30:37
Barragan, That certainly fixed it . Compiles, uploads & runs fine.
Thanks very much for your help.

Zap.

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.