Really nonblocking TWI/I2C master driver for Atmel AVR
Initializes the driver. Should be called once before calling any other TWI functions.
Writes data to the given address.
address
- TWI slave addressdata
- pointer to data bufferlength
- numer of bytes to write from the given data buffer
Returns TWI_STATUS
TWI_BUSY
- still sendingTWI_OK
- writing was okTWI_NOK
- writing was not ok
Writes one byte to the given address.
Reads data from the given address.
address
- TWI slave addresslength
- pointer to number of bytes to readdata
- address of pointer which points to the first data byte
Returns TWI_STATUS
, see above - only for reading
On TWI_OK
length
- contains received count of datadata
- points to first data byte
Resets the internal state. Normally a write is repeated until its TWI_OK
, after that we expect a read - which is repeated until its TWI_OK
. If this is not wanted, you can reset the internal state.
switch (operation) {
case 1:
if (twi_write(address, &data, 2)==TWI_OK) {
twi_resetstate();
operation++;
}
case 2:
if (twi_write(address, &data, 2)==TWI_OK) {
operation++;
}
case 3:
if (twi_read(address, &length, &data)==TWI_OK) {
operation++;
}
}
F_CPU
- you should define this before including this libraryTWI_FREQ
- defaults to 100kHz if left undefinedTWI_BUFFER_LENGTH
- defaults to 32 if left undefined
This example reads temperature and humidity from an SHT45 sensor. First we send the command 0xFD
to the address 0x44
, than we read 6 bytes from the sensor. The sensor replies with NACK if the conversion is not done. The read is performed until we get an ACK. After reading 6 Bytes temperature and humidity gets calculated.
if (readtemp==1) {
twi_write(0x44,0xFD);
uint8_t length=6;
uint8_t *buffer=NULL;
if (twi_read(0x44,&length,&buffer)==TWI_OK) {
if (length==6) {
uint16_t t=buffer[0]*256+buffer[1];
uint16_t h=buffer[3]*256+buffer[4];
reg.temperature=-45.0f+175.0f*((float) t/65535.0f);
reg.humidity=-6.0f+125.0f*((float) h/65535.0f);
if (reg.humidity<0) reg.humidity=0;
if (reg.humidity>100) reg.humidity=100;
}
readtemp++;
}
}