#define LCD_ADDRESS 0x27 // I2C Adresse des PCF8574T (mit R/W-Bit)
#define LCD_RS 0 // P0 - Register Select Pin
#define LCD_RW 1 // P1 - Read/Write Pin (immer auf GND)
#define LCD_EN 2 // P2 - Enable Pin
#define LCD_P3 3 // P3 - Muss auf 1 gesetzt werden
void i2c_start() {
TWCR = (1 << TWSTA) | (1 << TWEN) | (1 << TWINT);
while (!(TWCR & (1 << TWINT)));
}
void i2c_stop() {
TWCR = (1 << TWSTO) | (1 << TWINT) | (1 << TWEN);
}
void i2c_write(uint8_t data) {
TWDR = data;
TWCR = (1 << TWINT) | (1 << TWEN);
while (!(TWCR & (1 << TWINT)));
}
void lcd_command(uint8_t cmd) {
i2c_start();
i2c_write(LCD_ADDRESS << 1); // Schreibadresse
i2c_write((cmd & 0xF0) | (0 << LCD_EN) | (0 << LCD_RS) | (1 << LCD_P3));
i2c_write((cmd & 0xF0) | (1 << LCD_EN) | (0 << LCD_RS) | (1 << LCD_P3)); // high nibble
i2c_write((cmd & 0xF0) | (0 << LCD_EN) | (0 << LCD_RS) | (1 << LCD_P3));
i2c_write((cmd << 4) | (0 << LCD_EN) | (0 << LCD_RS) | (1 << LCD_P3)); // low nibble
i2c_write((cmd << 4) | (1 << LCD_EN) | (0 << LCD_RS) | (1 << LCD_P3));
i2c_write((cmd << 4) | (0 << LCD_EN) | (0 << LCD_RS) | (1 << LCD_P3));
i2c_stop();
_delay_us(1);
}
void lcd_write(uint8_t data) {
i2c_start();
i2c_write(LCD_ADDRESS << 1); // Schreibadresse
i2c_write((data & 0xF0) | (0 << LCD_EN) | (1 << LCD_RS) | (1 << LCD_P3)); // high nibble
i2c_write((data & 0xF0) | (1 << LCD_EN) | (1 << LCD_RS) | (1 << LCD_P3));
i2c_write((data & 0xF0) | (0 << LCD_EN) | (1 << LCD_RS) | (1 << LCD_P3));
i2c_write((data << 4) | (0 << LCD_EN) | (1 << LCD_RS) | (1 << LCD_P3)); // low nibble
i2c_write((data << 4) | (1 << LCD_EN) | (1 << LCD_RS) | (1 << LCD_P3));
i2c_write((data << 4) | (0 << LCD_EN) | (1 << LCD_RS) | (1 << LCD_P3));
i2c_stop();
_delay_ms(200);
}
void lcd_init() {
// Function Set (4-bit mode)
lcd_command(0x02); // 4-bit mode
lcd_command(0x28); // 2 lines, 5x8 matrix
lcd_command(0x0C); // Display on, cursor off, blink off
lcd_command(0x06); // Increment cursor
// Clear Display
lcd_command(0x01);
}
void lcd_print(char *str) {
while (*str) {
lcd_write(*str++);
}
}
int main(void) {
lcd_init();
lcd_print("Hallo AVR!");
while (1);
return 0;
}