Skip to content

Instantly share code, notes, and snippets.

@donghee
Last active October 9, 2015 04:47
Show Gist options
  • Save donghee/3440123 to your computer and use it in GitHub Desktop.
Save donghee/3440123 to your computer and use it in GitHub Desktop.
print temperature: TODO 20130228 코드 정리/수정필요.
// TODO: pull 필요 없음!
// +--------+
// | ADD0+-------- GND
// |TMP102 |
// |SCL SDA|
// |--+--+--|
// | |
// ----+--+----------- p27
// -------+----------- p28
//
// GND
#include "mbed.h"
int Tmp102_Test();
I2C i2c(p28, p27); // sda, scl
Serial pc(USBTX, USBRX);
int main() {
Tmp102_Test(); // TMP102 test program.
}
int Tmp102_Test() {
pc.printf("Tmp102 Test\n");
char writedata[] = {0x00}; // Temperature Register's Pointer Register Byte is 0x00(p0=0, p1=0), So data is 0x00
if(i2c.write(0x90, writedata, 1)) { // 0x90 is 8 bits representation of 0x48.
pc.printf("Write failed\n");
}
int poll = 0;
while(i2c.write(0x90, NULL, 0)) { // wait until we get an acknowledge
poll++;
}
pc.printf("Chip acknowledged after %d polls\n", poll);
pc.printf("Request temperature data:2bytes \n");
char readdata[2];
readdata[0] = 0;
readdata[1] = 0;
if(i2c.read(0x90, readdata, 2)) { // get temp data
pc.printf("Read failed\n");
}
unsigned short data0 = readdata[0];
unsigned short data1 = readdata[1];
unsigned short res = (data0 << 4) | (data1 >> 4);
// unsigned short res = (readdata[0] << 4) | (readdata[1] >> 4); // overflow!
float temp = (float) ((float)res * 0.0625);
pc.printf("Temperature: %f\n", temp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment