Also von der Befehlsfolge her siehts eigentlich ziemlich so aus wie bei mir.
Wo was anders ist, ist die Bus-Initialisierung.
Hier mal die Funktionen, die ich verwend. Evtl. hilft dir das weiter.
Code:
void I2C_Init(ui32_t scl_clock)
{
// Initializes the I2C/TWI Hardware as Master
TWSR = 0; // no prescaler
TWBR = ((F_CPU/scl_clock)-16)/2; // must be > 10 for stable operation
}
e_errors I2C_Start(ui8_t address)
{
ui8_t twst = 0;
ui8_t retval = ERR_OK;
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); // send START condition
while(!(TWCR & (1<<TWINT))); // wait until transmission completed
twst = TW_STATUS & 0xF8; // check value of TWI Status Register. Mask prescaler bits.
if ( (twst != TW_START) && (twst != TW_REP_START))
{
retval = ERR_TWI_NO_START;
}
if (retval == ERR_OK)
{
// send device address
TWDR = address;
TWCR = (1<<TWINT) | (1<<TWEN);
// wail until transmission completed and ACK/NACK has been received
while(!(TWCR & (1<<TWINT)));
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK))
{
retval = ERR_TWI_NO_ACK;
}
}
return retval;
}
void I2C_Stop(void)
{
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); // send stop condition
while(TWCR & (1<<TWSTO)); // wait until stop condition is executed and bus released
}
Damit sollte zumindest mal ein "Bin da!" des EEPROMs kommen.
Lesezeichen