Hi,
After playing arround with some codes i found on the net (unfortunately non directly aimed at arduino or the wire library) i decided to put together a sketch to test arround some code and finally make some library of it.
Well.. the current state is... i can connect the LCD to the chip and i see a blinking cursor - yet i cant send any text messages onto it.. also it seemed that, no matter what i do, the cursor is always blinking, so i cannot really estimate if i am doing anything right at all...
is there someone who might help me with that code?
#define LCD_LINES 4 /**< Enter the number of lines of your display here */
#define LCD_ROWS 20 /**< Enter the number of rows of your display here */
#define LCD_LINE1 0x00 /**< This should be 0x00 on all displays */
#define LCD_LINE2 0x40 /**< Change this to the address for line 2 on your display */
#define LCD_LINE3 0x10 /**< Change this to the address for line 3 on your display */
#define LCD_LINE4 0x50 /**< Change this to the address for line 4 on your display */
#define LCD_D4_PIN 0 /**< LCD-Pin D4 is connected to P0 on the PCF8574 */
#define LCD_D5_PIN 1 /**< LCD-Pin D5 is connected to P1 on the PCF8574 */
#define LCD_D6_PIN 2 /**< LCD-Pin D6 is connected to P2 on the PCF8574 */
#define LCD_D7_PIN 3 /**< LCD-Pin D7 is connected to P3 on the PCF8574 */
#define LCD_RS_PIN 4 /**< LCD-Pin RS is connected to P4 on the PCF8574 */
#define LCD_RW_PIN 5 /**< LCD-Pin RW is connected to P5 on the PCF8574 */
#define LCD_EMPTY_PIN 6 /**< this pin is not connected */
#define LCD_E_PIN 7 /**< LCD-Pin E is connected to P7 on the PCF8574 */
#define LCD_D0 (1 << LCD_D4_PIN) /**< bit 0 in 1st lower nibble */
#define LCD_D1 (1 << LCD_D5_PIN) /**< bit 1 in 1st lower nibble */
#define LCD_D2 (1 << LCD_D6_PIN) /**< bit 2 in 1st lower nibble */
#define LCD_D3 (1 << LCD_D7_PIN) /**< bit 3 in 1st lower nibble */
#define LCD_D4 (1 << LCD_D4_PIN) /**< bit 4 in 2nd lower nibble */
#define LCD_D5 (1 << LCD_D5_PIN) /**< bit 5 in 2nd lower nibble */
#define LCD_D6 (1 << LCD_D6_PIN) /**< bit 6 in 2nd lower nibble */
#define LCD_D7 (1 << LCD_D7_PIN) /**< bit 7 in 2nd lower nibble */
#define LCD_RS (1 << LCD_RS_PIN) /**< RS-bit in 1st and 2nd higher nibble */
#define LCD_RW (1 << LCD_RW_PIN) /**< RW-bit in 1st and 2nd higher nibble */
#define LCD_EMPTY (1 << LCD_EMPTY_PIN) /**< empty-bit in 1st and 2nd higher nibble */
#define LCD_E (1 << LCD_E_PIN) /**< E-bit in 1st and 2nd higher nibble */
#define LCD_ADDRESS 0 /**< Used for reading the address-counter and busy-flag */
#define LCD_DATA 1 /**< Used for reading data */
#define LCD_CLEAR 0x01 /**< Clear screen */
#define LCD_HOME 0x02 /**< Cursor move to first digit */
#define LCD_ENTRYMODE 0x04 /**< Set entrymode */
#define LCD_INCREASE LCD_ENTRYMODE | 0x02 /**< Set cursor move direction -- Increase */
#define LCD_DECREASE LCD_ENTRYMODE | 0x00 /**< Set cursor move direction -- Decrease */
#define LCD_DISPLAYSHIFTON LCD_ENTRYMODE | 0x01 /**< Display is shifted */
#define LCD_DISPLAYSHIFTOFF LCD_ENTRYMODE | 0x00 /**< Display is not shifted */
#define LCD_DISPLAYMODE 0x08 /**< Set displaymode */
#define LCD_DISPLAYON LCD_DISPLAYMODE | 0x04 /**< Display on */
#define LCD_DISPLAYOFF LCD_DISPLAYMODE | 0x00 /**< Display off */
#define LCD_CURSORON LCD_DISPLAYMODE | 0x02 /**< Cursor on */
#define LCD_CURSOROFF LCD_DISPLAYMODE | 0x00 /**< Cursor off */
#define LCD_BLINKINGON LCD_DISPLAYMODE | 0x01 /**< Blinking on */
#define LCD_BLINKINGOFF LCD_DISPLAYMODE | 0x00 /**< Blinking off */
#define LCD_SHIFTMODE 0x10 /**< Set shiftmode */
#define LCD_DISPLAYSHIFT LCD_SHIFTMODE | 0x08 /**< Display shift */
#define LCD_CURSORMOVE LCD_SHIFTMODE | 0x00 /**< Cursor move */
#define LCD_RIGHT LCD_SHIFTMODE | 0x04 /**< Right shift */
#define LCD_LEFT LCD_SHIFTMODE | 0x00 /**< Left shift */
#define LCD_CONFIGURATION 0x20 /**< Set function */
#define LCD_8BIT LCD_CONFIGURATION | 0x10 /**< 8 bits interface */
#define LCD_4BIT LCD_CONFIGURATION | 0x00 /**< 4 bits interface */
#define LCD_2LINE LCD_CONFIGURATION | 0x08 /**< 2 line display */
#define LCD_1LINE LCD_CONFIGURATION | 0x00 /**< 1 line display */
#define LCD_5X10 LCD_CONFIGURATION | 0x04 /**< 5 X 10 dots */
#define LCD_5X7 LCD_CONFIGURATION | 0x00 /**< 5 X 7 dots */
#define PCFADDRESS 0x38
#include <WProgram.h>
#include <Wire.h>
void setup()
{
Serial.begin(9600);
Wire.begin();
/*Init*/
delay(15);
lcd_write(LCD_D5 | LCD_D4); //- Set interface to 8-bit
delay(5); //- Wait for more than 4.1ms
lcd_write(LCD_D5 | LCD_D4); //- Set interface to 8-bit
delay(101); //- Wait for more than 100us
lcd_write(LCD_D5 | LCD_D4); //- Set interface to 8-bit
lcd_write(LCD_D5); //- Set interface to 4-bit
//- From now on in 4-bit-Mode
//lcd_command(LCD_4BIT | LCD_2LINE | LCD_5X7); //- 2-Lines, 5x7-Matrix
lcd_command(LCD_DISPLAYOFF); //- Display off
lcd_command(LCD_CLEAR); //- Clear Screen
lcd_command(LCD_INCREASE | LCD_DISPLAYSHIFTOFF); //- Entrymode (Display Shift: off, Increment Address Counter)
lcd_command(LCD_DISPLAYON); //- Display on
lcd_command(LCD_BLINKINGOFF); //- Blinking off
lcd_command(LCD_CURSORON);
debug("DISPLAY INIT");
lcd_print("Hello world");
debug("PRINT");
}
void loop()
{
}
void debug(char * str)
{
Serial.println(str);
}
//- Write data to i2c
void lcd_write_i2c(unsigned char value)
{
Wire.beginTransmission(PCFADDRESS);
Wire.send(value);
Wire.endTransmission();
}
//- Write byte to display with toggle of enable-bit
void lcd_write(unsigned char value)
{
lcd_write_i2c(value | LCD_E); //- Set enable to high
lcd_write_i2c(value | LCD_E); //- Send data, keep enable high
lcd_write_i2c(value & (!LCD_E)); //- Set enable to low
}
//- Issue a command to the display (use the defined commands above)
void lcd_command(unsigned char command)
{
unsigned char lcddata;
lcddata = command;
lcddata >>= 4;
lcd_write(lcddata);
lcddata = command;
lcddata &= 0x0F;
lcd_write(lcddata);
}
//- Print string to cursor position
void lcd_print( char *string)
{
unsigned char i = 0;
Serial.print("Debug: ");
while(string[i] != 0x00)
{
lcd_putchar(string[i]);
Serial.print(string[i]);
i++;
}
debug("");
}
//- Put char to cursor position
void lcd_putchar(unsigned char value)
{
unsigned char lcddata;
lcddata = value;
lcddata >>= 4;
lcddata |= LCD_RS;
lcd_write(lcddata);
lcddata = value;
lcddata &= 0x0F;
lcddata |= LCD_RS;
lcd_write(lcddata);
delay(1500);
}