semicolon
17.01.2007, 16:11
Hallo
Mein I2C Bus tut nicht so wie er sollte. Hardwaremässig sollte alles I.o. sein. Habe die beiden Leitungen SDA/SCL via je 10kOhm Widerstände gegen 1% Volt gezogen. Meinen I2C Bus benötige ich um meine RTC PCF8583 zu steuern.
Aber da will überhaupt nichts gehen.
Hier mein Code:
/************************************************** ********
Function: Initialisiert die I2C Schnittstelle:
- TWI enable
- Baud-Rate: 25kHz
Input: NOP
Output: NOP
************************************************** ********/
void init_I2C(void)
{
TWBR = 12; //Baud-Rate
TWSR = 0; /* no prescaler */
}
/************************************************** ********
Function: wait for TWINT Flag set. This indicates that
the START condition has been transmitted
Input: NOP
Output: NOP
************************************************** ********/
void wait_for_ACK(void)
{
while (!(TWCR & (1<<TWINT)));
}
/************************************************** ********
Function: Send one byte to I2C device
Input: byte to be transfered
Output: 0 write successful
1 write failed
************************************************** *********/
unsigned char i2c_write( unsigned char data )
{
uint8_t twst;
// send data to the previously addressed device
TWDR = data;
TWCR = (1<<TWINT) | (1<<TWEN);
// wait until transmission completed
wait_for_ACK();
// check value of TWI Status Register. Mask prescaler bits
twst = TW_STATUS & 0xF8;
if( twst != TW_MT_DATA_ACK) return 1;
return 0;
}
/************************************************** ***********************
Function: Terminates the data transfer and releases the I2C bus
Input: NOP
Output: NOP
************************************************** ***********************/
void i2c_stop(void)
{
/* send stop condition */
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
// wait until stop condition is executed and bus released
wait_for_ACK();
}/* i2c_stop */
/************************************************** **************************************
Function: Issues a start condition and sends address and transfer direction.
Input: Device Address + Direction
Output: return 0 = device accessible,
1= failed to access device
************************************************** **************************************/
unsigned char i2c_start(unsigned char address)
{
uint8_t twst;
// send START condition
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
// wait until transmission completed
wait_for_ACK();
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;
// send device address
TWDR = address;
TWCR = (1<<TWINT) | (1<<TWEN);
// wail until transmission completed and ACK/NACK has been received
wait_for_ACK();
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
return 0;
}/* i2c_start */
Was ist an meiner Software falsch? Wie kann ich mit dem Osziloskop überprüfen ob mein I2C Bus funktioniert? Als vielleicht über den SCL Pin
Wie muss ich die Datenrichtungsregister für den Atmega8 konfigurieren wenn ich den I2C benutze?
Danke.
Mein I2C Bus tut nicht so wie er sollte. Hardwaremässig sollte alles I.o. sein. Habe die beiden Leitungen SDA/SCL via je 10kOhm Widerstände gegen 1% Volt gezogen. Meinen I2C Bus benötige ich um meine RTC PCF8583 zu steuern.
Aber da will überhaupt nichts gehen.
Hier mein Code:
/************************************************** ********
Function: Initialisiert die I2C Schnittstelle:
- TWI enable
- Baud-Rate: 25kHz
Input: NOP
Output: NOP
************************************************** ********/
void init_I2C(void)
{
TWBR = 12; //Baud-Rate
TWSR = 0; /* no prescaler */
}
/************************************************** ********
Function: wait for TWINT Flag set. This indicates that
the START condition has been transmitted
Input: NOP
Output: NOP
************************************************** ********/
void wait_for_ACK(void)
{
while (!(TWCR & (1<<TWINT)));
}
/************************************************** ********
Function: Send one byte to I2C device
Input: byte to be transfered
Output: 0 write successful
1 write failed
************************************************** *********/
unsigned char i2c_write( unsigned char data )
{
uint8_t twst;
// send data to the previously addressed device
TWDR = data;
TWCR = (1<<TWINT) | (1<<TWEN);
// wait until transmission completed
wait_for_ACK();
// check value of TWI Status Register. Mask prescaler bits
twst = TW_STATUS & 0xF8;
if( twst != TW_MT_DATA_ACK) return 1;
return 0;
}
/************************************************** ***********************
Function: Terminates the data transfer and releases the I2C bus
Input: NOP
Output: NOP
************************************************** ***********************/
void i2c_stop(void)
{
/* send stop condition */
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
// wait until stop condition is executed and bus released
wait_for_ACK();
}/* i2c_stop */
/************************************************** **************************************
Function: Issues a start condition and sends address and transfer direction.
Input: Device Address + Direction
Output: return 0 = device accessible,
1= failed to access device
************************************************** **************************************/
unsigned char i2c_start(unsigned char address)
{
uint8_t twst;
// send START condition
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
// wait until transmission completed
wait_for_ACK();
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;
// send device address
TWDR = address;
TWCR = (1<<TWINT) | (1<<TWEN);
// wail until transmission completed and ACK/NACK has been received
wait_for_ACK();
// check value of TWI Status Register. Mask prescaler bits.
twst = TW_STATUS & 0xF8;
if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
return 0;
}/* i2c_start */
Was ist an meiner Software falsch? Wie kann ich mit dem Osziloskop überprüfen ob mein I2C Bus funktioniert? Als vielleicht über den SCL Pin
Wie muss ich die Datenrichtungsregister für den Atmega8 konfigurieren wenn ich den I2C benutze?
Danke.