AVR Error Handling: I2C, SPI etc

avri2ctwi

How would one go about handling errors in AVR interfaces that do not specifically provide any acknowledgement scheme?

For example, I have an I2C temperature sensor which I poll every 1 hour. So every hour my AVR sends a TWI start condition to the sensor and gets the temperature at the end of it.Now
tet's say for some reason the sensor does not respond. Maybe it's been taken out, burned out etc.
So how can I check for such in my code and inform the user?

Right now I am using the polling method. Infinite loop till TWINT is set in the TWCR register

while ((TWCR & (1<<TWINT)) == 0){};
return TWDR;

Best Answer

The I2C module provides a way to identify erros, once you send, let's say a START condition or whatever you have to verify the TWSR register (TWI Status register) and it should match the state you are expecting (e.g. Aknowledge from slave). There is a flow diagram for each working mode showcasing the different status values.