Last active
August 29, 2015 14:07
-
-
Save zerog2k/4798aa212ee1c3b00d49 to your computer and use it in GitHub Desktop.
read TI TMP513 (all-in-one current, voltage, temp sensors) via I2C on raspberry pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// read TI TMP512/TMP513 over I2C | |
// Jens Jensen AF5MI 2014 | |
#include <wiringPi.h> | |
#include <wiringPiI2C.h> | |
#include <stdio.h> | |
// address of device - see datasheet, assumes A0 = SCL. | |
#define ADDR 0x5F | |
// current calibration register, see datasheet for calc | |
// I = (V_shunt * CalReg) / 4096 | |
#define ICAL 40960 // R_shunt = 0.01 ohms, I in 0.1 mA | |
#define PR_mW 2 // PR multiplier per LSB to get mW result | |
#define CR_mA 0.1f // CR multiplier per LSB to get mA result | |
#define CONFREG1 0b0000100110011111 // config reg 1 - see datasheet | |
#define CFGREG1 0x00 // shunt config | |
#define CFGREG2 0x01 // temp config | |
#define STATUSREG 0x02 | |
#define SMBUSCTLREG 0x03 | |
#define SVR 0x04 // shunt voltage result, 10uV LSB | |
#define BVR 0x05 // bus voltage result, 4mV LSB | |
#define PR 0x06 // power measurement result | |
#define CR 0x07 // current measurement result | |
#define LTR 0x08 // local temp result register | |
#define RTR1 0x09 // remote temp result 1 register | |
#define RTR2 0x0A // remote temp result 2 register | |
#define RTR3 0x0B // remote temp result 3 register | |
#define SCR 0x15 // shunt calibration register | |
#define RTS1_NFACTOR 0x16 // n-Factor 1 Register 16h (r/w) | |
#define RTS2_NFACTOR 0x17 // n-Factor 2 Register 17h (r/w) | |
#define RTS3_NFACTOR 0x18 // n-Factor 3 Register 18h (r/w) | |
int fd; | |
short getRegRaw ( int fd, int reg) | |
{ | |
short data = wiringPiI2CReadReg16(fd, reg); | |
data = data >> 8 | ((data << 8) & 0xffff); // fix byte order | |
return data; | |
} | |
float getTempC( int fd, int reg ) { | |
// valid range: ±256C° | |
int raw = getRegRaw(fd, reg); | |
int diodeOpen = raw & 0x01; | |
float tempC = (raw >> 3) * 0.0625; | |
if (diodeOpen) | |
tempC = -1024; // invalid reading | |
return tempC; | |
} | |
float tempCtoF ( float tempC ) { | |
float tempF = (tempC * 9.0) / 5.0 + 32.0; | |
return tempF; | |
} | |
float busVoltageResult ( int bvraw ) { | |
float busVoltage = (bvraw >> 3) * 0.004; | |
return busVoltage; | |
} | |
int main (void) | |
{ | |
if((fd = wiringPiI2CSetup(ADDR)) < 0) { | |
printf("I2CSetup Failed, %i\n", fd); | |
} | |
// init current calibration reg | |
unsigned short caldata = ICAL >> 8 | ((ICAL << 8) & 0xffff); // fix byte order | |
wiringPiI2CWriteReg16(fd, SCR, caldata); | |
while (1) { | |
// get data | |
float ltrTempF = tempCtoF(getTempC(fd, LTR)); | |
float rtr1TempF = tempCtoF(getTempC(fd, RTR1)); | |
float rtr2TempF = tempCtoF(getTempC(fd, RTR2)); | |
float rtr3TempF = tempCtoF(getTempC(fd, RTR3)); | |
int bvrVal = getRegRaw(fd, BVR); | |
float busVoltage = busVoltageResult(bvrVal); | |
int svrVal = getRegRaw(fd, SVR); | |
int pwrVal = getRegRaw(fd, PR) * PR_mW; | |
int curVal = getRegRaw(fd, CR) * CR_mA; | |
printf("LTemp: %3.1f°F RT1: %3.1f°F RT2: %3.1f°F RT3: %3.1f°F\n", | |
ltrTempF, rtr1TempF, rtr2TempF, rtr3TempF); | |
printf("Vbus: %2.2f Vdc P: %3d mW I: %3d mA\n", | |
busVoltage, pwrVal, curVal); | |
printf("\n"); | |
sleep(2); | |
} | |
close(fd); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment