Hi,
I'm successfully reading data from a TMP275 temp sensor over I2C. I'm visualizing data in a terminal and on some lines a '0' appears giving a false reading. I can't find the cause of this problem but I suspect it has something to do with the conversion to char.
terminal log where the temp is actually 26 degrees:
26,0
06,0
06,0
26,0
06,0
06,0
06,0
26,0
06,0
here is my code:
int main(void) { i2c_init(); // initialize I2C library i2c_start_wait(sensor+I2C_WRITE); // set device address and write mode i2c_write(0x1); // write pointer register 00000001 to select config register i2c_rep_start(sensor+I2C_READ); // set device address and read mode config = i2c_readNak(); // read config register config |= _BV(6); // set config register for 12 bit resolution config |= _BV(5); // Table 8, Page 7 from Datasheet i2c_rep_start(sensor+I2C_WRITE); // set device address and write mode i2c_write(0x1); // write pointer register 00000001 to select config register i2c_write(config); // write config back to config register i2c_stop(); // stop while(1) { i2c_start_wait(sensor+I2C_WRITE); // set device address and write mode i2c_write(0x0); // write pointer register 00000000 to select temp register i2c_rep_start(sensor+I2C_READ); // set device address and read mode temp_high=i2c_readAck(); // Read high byte of temperature temp_low=i2c_readNak(); // Read low byte of temperature i2c_stop(); // stop usart_init(); // init usart _delay_ms(100); // the Atmega8 will take a few seconds to get initialized and get stable clock pulses temp_low=(temp_low>>4)*625/1000; // we adjuts the LSB temperature byte to get the fraction part of the temperature //send temp over USART if (temp_high<0) { // first case if temp is < 0 temp_high = -temp_high-1; // negate temp_high and substract 1 temp_low = -temp_low-1; // negate temp_low and substract 1 sprintf(buf, "-percent d, percent d\r\n", temp_high, temp_low); //for some reason the forum prevents me from posting with percent sign usart_puts(buf); // and write the whole formatted string. _delay_ms(1000); } else if (temp_high>0) { // second case if temp is > 0 sprintf(buf, "percent d, percent d\r\n", temp_high, temp_low); //for some reason the forum prevents me from posting with percent sign usart_puts(buf); // and write the whole formatted string. _delay_ms(1000); } } }
Thanks
Florin