49 TWSR &= ~((1<<TWPS1) | (1<<TWPS0));
59 int8_t _i2c_start(uint8_t addr, uint8_t rep)
63 TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
64 while(!(TWCR & (1<<TWINT)));
65 if((TWSR&0xF8) != ((!rep)?I2C_START:I2C_REP_START))
69 TWCR = (1<<TWINT) | (1<<TWEN);
71 while(!(TWCR & (1<<TWINT)));
72 if(!(((TWSR&0xF8) == I2C_MT_SLA_ACK) || ((TWSR&0xF8) == I2C_MR_SLA_ACK)))
78 int8_t i2c_start(uint8_t addr)
80 return _i2c_start(addr, 0);
83 int8_t i2c_rep_start(uint8_t addr)
85 return _i2c_start(addr, 1);
88 int8_t i2c_write(uint8_t data)
91 TWCR = (1<<TWINT) | (1<<TWEN);
93 while(!(TWCR & (1<<TWINT)));
94 if((TWSR&0xF8) != I2C_MT_DATA_ACK)
100 int8_t i2c_read(uint8_t *data, uint8_t ack)
103 TWCR = (1<<TWINT)|(1<<TWEN)| (ack?(1<<TWEA):0);
104 while(!(TWCR & (1<<TWINT))){
109 if((TWSR&0xF8) != (ack?I2C_MR_DATA_ACK:I2C_MR_DATA_NACK))
115 int8_t i2c_read_ack(uint8_t *data)
117 return i2c_read(data, 1);
120 int8_t i2c_read_nack(uint8_t *data)
122 return i2c_read(data, 0);
128 TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
129 while(TWCR & (1<<TWSTO));
134 DDRC &= ~( (1<<PC0) | (1<<PC1) );
135 PORTC |= ((1<<PC0) | (1<<PC1));