10 #include <compat/twi.h>
17 #define F_CPU 16000000UL
21 #define SCL_CLOCK 100000L
46 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
49 while(!(TWCR & (1<<TWINT)));
52 twst = TW_STATUS & 0xF8;
53 if ( (twst != TW_START) && (twst != TW_REP_START))
return 1;
57 TWCR = (1<<TWINT) | (1<<TWEN);
60 while(!(TWCR & (1<<TWINT)));
63 twst = TW_STATUS & 0xF8;
64 if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) )
return 1;
85 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
88 while(!(TWCR & (1<<TWINT)));
91 twst = TW_STATUS & 0xF8;
92 if ( (twst != TW_START) && (twst != TW_REP_START))
continue;
96 TWCR = (1<<TWINT) | (1<<TWEN);
99 while(!(TWCR & (1<<TWINT)));
102 twst = TW_STATUS & 0xF8;
103 if ( (twst == TW_MT_SLA_NACK )||(twst ==TW_MR_DATA_NACK) )
106 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
109 while(TWCR & (1<<TWSTO));
141 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
144 while(TWCR & (1<<TWSTO));
162 TWCR = (1<<TWINT) | (1<<TWEN);
165 while(!(TWCR & (1<<TWINT)));
168 twst = TW_STATUS & 0xF8;
169 if( twst != TW_MT_DATA_ACK)
return 1;
182 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
183 while(!(TWCR & (1<<TWINT)));
197 TWCR = (1<<TWINT) | (1<<TWEN);
198 while(!(TWCR & (1<<TWINT)));
void i2c_init(void)
initialize the I2C master interace. Need to be called only once
unsigned char i2c_write(unsigned char data)
Send one byte to I2C device.
unsigned char i2c_start(unsigned char address)
Issues a start condition and sends address and transfer direction.
void i2c_stop(void)
Terminates the data transfer and releases the I2C bus.
void i2c_start_wait(unsigned char address)
Issues a start condition and sends address and transfer direction.
unsigned char i2c_readAck(void)
read one byte from the I2C device, request more data from device
unsigned char i2c_rep_start(unsigned char address)
Issues a repeated start condition and sends address and transfer direction.
unsigned char i2c_readNak(void)
read one byte from the I2C device, read is followed by a stop condition