i2c Trouble Reading a tmp102 with C on the Propeller
DMCo
Posts: 2
I'm new to Propellers but not programming. I'm confident with programming Arduinos, and non-Arduino Atmel chips in C and C++. I'm exploring the propeller, and started with a simple simple temperature sensor, the TI tmp102, on a breakout board from Sparkfun.
I studied the Propeller C code in the Learn folder for the Parallax Compass Module HMC5883L. I bought the compass just to confirm the code. The sample program works fine and reads the device. I modified the code to read the tmp102 sensor, but I'm getting no readings as if the chip isn't there. When I tap into the wiring to the Arduino Uno, it confirms that the circuit is working successfully. On the propeller I have 2 220 pullup resisters, and the 4 lines from the breakout board SLC, SDA, VSS, and GND. What could be simpler, except it doesn't work.
Below is my code.
Any help would be appreciated.
/*
Read Sparkfun tmp-102 tempperature breakout
with TI's TMP102 i2c chip
*/
#include "simplei2c.h"
#include "simpletools.h"
#define TMP_ADDR 0x48
int16_t readTmp(i2c *bus);
int main() {
i2c *tmpBus = i2c_newbus(3, 2, 0); // New I2C bus SCL=P3, SDA=P2
while(1) {
int16_t temp = readTmp(&tmpBus);
float fahrenheit = (1.8 * temp) + 32;
print("C: %f%c\n", temp, CLREOL );
print("F: %f%c\n", fahrenheit, CLREOL);
pause(1000);
}
}
int16_t readTmp(i2c *bus) {
uint8_t data[2];
i2c_in(bus, 0x48, 0, 0, data, 2);
print("D0: %d\n%c", data[0], CLREOL);
print("D1: %d\n", data[1], CLREOL);
int16_t tmp = (data[0] << 8) | data[1];
int TemperatureSum = ((data[0] << 8) | data[1]) >> 4;
float celsius = TemperatureSum*0.0625;
return celsius;
}
I studied the Propeller C code in the Learn folder for the Parallax Compass Module HMC5883L. I bought the compass just to confirm the code. The sample program works fine and reads the device. I modified the code to read the tmp102 sensor, but I'm getting no readings as if the chip isn't there. When I tap into the wiring to the Arduino Uno, it confirms that the circuit is working successfully. On the propeller I have 2 220 pullup resisters, and the 4 lines from the breakout board SLC, SDA, VSS, and GND. What could be simpler, except it doesn't work.
Below is my code.
Any help would be appreciated.
/*
Read Sparkfun tmp-102 tempperature breakout
with TI's TMP102 i2c chip
*/
#include "simplei2c.h"
#include "simpletools.h"
#define TMP_ADDR 0x48
int16_t readTmp(i2c *bus);
int main() {
i2c *tmpBus = i2c_newbus(3, 2, 0); // New I2C bus SCL=P3, SDA=P2
while(1) {
int16_t temp = readTmp(&tmpBus);
float fahrenheit = (1.8 * temp) + 32;
print("C: %f%c\n", temp, CLREOL );
print("F: %f%c\n", fahrenheit, CLREOL);
pause(1000);
}
}
int16_t readTmp(i2c *bus) {
uint8_t data[2];
i2c_in(bus, 0x48, 0, 0, data, 2);
print("D0: %d\n%c", data[0], CLREOL);
print("D1: %d\n", data[1], CLREOL);
int16_t tmp = (data[0] << 8) | data[1];
int TemperatureSum = ((data[0] << 8) | data[1]) >> 4;
float celsius = TemperatureSum*0.0625;
return celsius;
}
Comments
Welcome to the forums.
This is wrong because tmpBus is already a pointer:
int16_t temp = readTmp(&tmpBus);
It should be:
int16_t temp = readTmp(tmpBus);
Also, you should consider using pull up resistors between 3.3K and 10K Ohms. 220 Ohms is "aggressive".
--Steve