#define LCD_adr 0x20
//01234567890123456789
char text[ ][20] = { { "The answer 2 all??? " },
{ " 42!!! " },
{ "and the question??? " },
{ "6x9=? 42(13 base)?? " } } ;
// eigtl. #include "TWIown_LCD.h" // hier Code hinter loop..
void TWIbegin(); // Festlegen der Bitrate
int TWIstart( byte adr ); // Start der TWI-Kommunikation
int TWIendTransmit( ); // Beenden der TWI-Kommunikation
void TWIstop(); // Stopp der TWI-Kommunikation z.B. bei Fehler
int TWIwrite( byte data ); // Schicke 1 Byte per TWI ohne Ende
int TWIwrite1( byte adr, byte data ); // sends 1 Byte
int TWIwrite2( byte adr, byte data1,byte data2); // sends 2 Bytes
// #include "TWIown_LCD.h"
void TWIwrite1ByteLCD( byte adr, byte data, bool mode );
void TWIwrite1BcommandLCD( byte adr, byte command );
void TWIwrite1BdataLCD( byte adr, byte data );
void TWIsetCursorLCD( byte adr, byte col, byte row ); // x, y
// Quelle: datasheet page 183
#define START 0x08
// slave address + master write
#define SLA_W 0x4E
// slave address + master read
#define SLA_R 0x4F
#define MT_SLA_ACK 0x18
#define MT_DATA_ACK 0x28
// only for init() – to be replaced later…
#include <LiquidCrystal_I2C.h> // lib-version 1.1 (Schwartz)
LiquidCrystal_I2C lcd(0x20,20,4); // adress, signs per line, lines
void setup()
{ // TWIbegin( );
lcd.init(); lcd.backlight();
lcd.setCursor(0, 1); // 1.sign, 2. line
lcd.print("sh0rt Test!");
}
void loop()
{ for( byte j = 0; j < 4; j++ )
{ TWIsetCursorLCD( 0x20, 0, j ); // x, y
for( byte i = 0; i < 20; i++ )
TWIwrite1BdataLCD( 0x20, text[j][i] );
}
}
// #include "TWIown_LCD.h" "Bibliothek"
/* library to access LCDs HW-close */
void TWIbegin() { TWBR = (160 - 16)/2; }
int TWIstart( byte adr ) // STArt TWI-communication
{ int result = 0;
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); // Send START condition
while (!(TWCR & (1<<TWINT))); // Wait for TWINT flag set.
// indicates that the START condition has been transmitted.
if ((TWSR & 0xF8) != START) result = -1; // fault #1
else
{ TWDR = adr << 1; // PCF8574 0x4E <= 0x27
// Clear TWINT bit in TWCR to start transmission of address
TWCR = (1<<TWINT)|(1<<TWEN);
while (!(TWCR & (1<<TWINT))); // Wait for TWINT flag set.
// This indicates that the DATA has been transmitted,
// and ACK/NACK has been received.
if ((TWSR & 0xF8) != MT_SLA_ACK) result = -2; // fault #2
}
return( result ); // Transmit STOP condition
}
int TWIendTransmit( )
{ int result = 0;
if ((TWSR & 0xF8)!= MT_DATA_ACK) result = -3; // fault #3
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
return( result ); // Transmit STOP condition
}
void TWIstop()
{ TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
// unsigned long us=micros(); while ( micros() - us < 100 ) ;
delayMicroseconds(100);
}
int TWIwrite( byte data )
{ int result = 0;
TWDR = data; // Load DATA into TWDR register.
// clear TWINT bit in TWCR to
TWCR = (1<<TWINT) | (1<<TWEN); // start transmission of data
while (!(TWCR & (1<<TWINT))); // Wait for TWINT flag set.
// indicates that the DATA has been transmitted
// and ACK/NACK has been received.
if ((TWSR & 0xF8)!= MT_DATA_ACK) result = -3;
return( result );
}
int TWIwrite1( byte adr, byte data ) // sends 1 Byte
{ int result = TWIstart( adr ) ;
if( result == 0 )
result = TWIwrite( data );
TWIstop();
return result;
}
void TWIwrite1ByteLCD( byte adr, byte data, bool mode )
{ // für Daten: | 0x01 für Kommandos: | 0
byte HighNibble = data & 0xF0 ; // | RS
byte LowNibble = ( data & 0x0F ) << 4 ;
if( mode ) { HighNibble |= 0x01; LowNibble |= 0x01; }
HighNibble |= 0x08; LowNibble |= 0x08; // LCD_BACKLIGHT
TWIwrite1( adr, HighNibble ) ; // High-Nibble mit backlight
TWIwrite1( adr, HighNibble | 0x04 ) ; // High-Nibble | En | Rs
delayMicroseconds(1); // enable pulse must be >450ns
TWIwrite1( adr, HighNibble & 0xFB ) ; // High-Nibble | En
delayMicroseconds(50); // ena
TWIwrite1( adr, LowNibble ) ; // High-Nibble mit backlight
TWIwrite1( adr, LowNibble | 0x04 ) ; // High-Nibble | En
delayMicroseconds(1); // enable pulse must be >450ns
TWIwrite1( adr, LowNibble & 0xFB ) ; // High-Nibble | En
delayMicroseconds(50); // ena
}
void TWIwrite1BcommandLCD( byte adr, byte command )
{ TWIwrite1ByteLCD( adr, command, 0 ) ; }
void TWIwrite1BdataLCD( byte adr, byte data )
{ TWIwrite1ByteLCD( adr, data, 1 ) ; }
void TWIsetCursorLCD( byte adr, byte col, byte row ) // x, y
{ // 0...20 0..3 // 0x80 = LCD_SETDDRAMADDR
if( row > 3 ) row = 3; // 0, 1, 2, 3 = y = line
const byte row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
byte command = 0x80 | ( col + row_offsets[row] ) ;
TWIwrite1BcommandLCD( adr, command );
}
42!
6x9 = ???
base 13?