Hi !
I tried to drive a 20x4 LCD using PCF8574 using I2C(TWI) with tiny2313.
In another topic I found that topic "I2C to LCD with PCF8574".I am using codevisionAVR compiler.The problem is that codevision reported about 20 errors during compilation.Mainly compiler can not find #include "headers.h" ...
Any held/ideas ?
The code of "I2C to LCD with PCF8574" (by Fricko) is the following:
Notice that author says that "The I2C routine expects an Atmel-Chip with TWI".
/*********************************************
Project : I2C to LCD Interface-Routinen
Version :
Date : 2005
Author : Andreas Schδfer
Chip type : ATMEGA8 / PCF8574
Clock frequency : 4 MHz
Port PCF8574: 7 6 5 4 3 2 1 0
D7 D6 D5 D4 * EN RW RS
**********************************************/
#include "headers.h"
#pragma warn-
char lcdErr;
// Daten fόr selbstdefinierte Zeichen
flash char charSet[8][8] = {0x0f, 0x01, 0x0f, 0x01, 0x0f, 0x00, 0x00, 0x00,
0x0f, 0x01, 0x0f, 0x01, 0x0f, 0x00, 0x00, 0x00, // "3"
0x18, 0x1c, 0x1e, 0x1f, 0x1e, 0x1c, 0x18, 0x00, // gefόllter Pfeil rechts
0x00, 0x00, 0x1f, 0x1f, 0x1f, 0x1f, 0x1f, 0x00, // gefόlltes Quadrat
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
//*****************************************************************
// LC-Display initialisieren
//*****************************************************************
void lcd_init(void)
{
char i;
char dummy = 0;
// High-Nibble von Byte 8 = Display Control:
// 1DCB**** D: Disp on/off; C: Cursor on/off B: blink on/off
char init_sequenz[]= {0x34, 0x34, 0x34, 0x24,
0x24, 0xc4, 0x04, 0b11000100,
0x04, 0x14, 0x04, 0x64};
delay_ms(30); // 30 ms Delay nach power-up
for (i = 0; i < 12; i++)
{
lcdErr = i2c_master_tx(0x40, &dummy, 1); // enable toggeln;
delay_ms(5); // 5 ms verzφgern
lcdErr = i2c_master_tx(0x40, &init_sequenz[i], 1); // write init-data
}
delay_ms(5); // 5 ms verzφgern
write_cg_ram();
}
//*****************************************************************
// Selbstdefinierte Zeichen in das CG-Ram schreiben
//*****************************************************************
void write_cg_ram(void)
{
char i, n;
n = 0;
do
{
lcd_instruction(0, 0x40 + n); //CG-Ram Adresse setzen
delay_us(50);
for (i = 0; i < 8; i++)
{
lcd_instruction(1, charSet[n/8][i]);
delay_us(50);
}
n += 8;
}
while (n < 64);
}
//*****************************************************************
// Stringlδnge ermitteln
//*****************************************************************
char str_len(char *string)
{
char i = 0;
while (*(string + i) != 0)
i++;
return i;
}
//*****************************************************************
// String an aktueller Curserposition ausgeben
//*****************************************************************
void lcd_write(char *strLcd)
{
char lNibble, hNibble;
char dummy = 1;
char length, i;
#asm("sei")
i = 0;
length = str_len(strLcd);
for (i = 0; i < length; i++)
{
lNibble = *(strLcd + i) & 0x0f;
lNibble <<= 4; // untere 4-Bit
hNibble = *(strLcd + i) & 0xf0; // obere 4-Bit
lNibble |= 0x05; // Enable & RS setzen
hNibble |= 0x05;
lcdErr = i2c_master_tx(0x40, &dummy, 1); // enable toggeln;
lcdErr = i2c_master_tx(0x40, &hNibble, 1); // obere 4-Bit
lcdErr = i2c_master_tx(0x40, &dummy, 1); // enable toggeln;
lcdErr = i2c_master_tx(0x40, &lNibble, 1); // untere 4-Bit
}
}
//*****************************************************************
// Befehl senden
//*****************************************************************
void lcd_instruction(char mode, char data)
{
char lNibble, hNibble;
char dummy = 0;
#asm("sei")
lNibble = data & 0x0f;
lNibble <<= 4; // untere 4-Bit
hNibble = data & 0xf0; // obere 4-Bit
switch (mode)
{
case 0:
lNibble |= 0x04; // Enable setzen
hNibble |= 0x04;
break;
case 1:
lNibble |= 0x05; // Enable & RS setzen
hNibble |= 0x05;
break;
case 2:
lNibble |= 0x07; // Enable, RS & Read-Data setzen
hNibble |= 0x07;
}
lcdErr = i2c_master_tx(0x40, &dummy, 1); // enable toggeln;
lcdErr = i2c_master_tx(0x40, &hNibble, 1); // obere 4-Bit
lcdErr = i2c_master_tx(0x40, &dummy, 1); // enable toggeln;
lcdErr = i2c_master_tx(0x40, &lNibble, 1); // untere 4-Bit
}
//*****************************************************************
// Cursor an Position xy
//*****************************************************************
void lcd_gotoxy(char x, char y)
{
if(y == 1)
x += 0x40; // Fόr 1. Pos. in zweiter Zeile
x |= 0x80;
lcd_instruction(0, x);
}
//*****************************************************************
// LCD lφschen
//*****************************************************************
void lcd_clear(void)
{
lcd_instruction(0, 0x01);
}
//*****************************************************************
// Cursor in Home-Position
//*****************************************************************
void lcd_home(void)
{
lcd_instruction(0, 0x02);
}
//*****************************************************************
// Datenbytes όber I2C an adressierten Baustein senden
//*****************************************************************
unsigned char i2c_master_tx(unsigned char addr, unsigned char *data, unsigned char length)
{
unsigned char i;
TWCR = 0xa4; // Start senden
while (!(TWCR & 0x80)); // warten bis gesendet
if (TWSR != 0x08)
return I2C_START_ERROR;
TWDR = addr & 0xfe; // Slave-Adresse + w in TW-Datenregister schreiben
TWCR = 0x84; // senden
while (!(TWCR & 0x80)); // warten bis gesendet
if (TWSR != 0x18) // Aknowledge ?
return I2C_ADDR_AKN_ERROR;
for (i = 0; i < length; i++)// Datenbytes senden
{
TWDR = *data; // Datenbyte in TW-Datenregister schreiben
TWCR = 0x84; // senden
while (!(TWCR & 0x80)); // warten bis gesendet
if (TWSR != 0x28) // Aknowledge ?
return I2C_DATA_AKN_ERROR;
data ++;
}
TWCR = 0x94; // Stop senden
return I2C_WRITE_OK;
}
#pragma warn+
Thank You !