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
IP Logged
|