Skip to content

Commit f60b4a6

Browse files
committed
fixes for temp calculation
1 parent 07d5867 commit f60b4a6

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/SparkFun_TMAG5273_Arduino_Library.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2558,18 +2558,22 @@ int8_t TMAG5273::getError()
25582558
/// @return T-Channel data conversion results
25592559
float TMAG5273::getTemp()
25602560
{
2561-
// Variable to store full temperature value
2562-
int16_t temp = 0;
2563-
uint8_t databuffer[2];
2564-
size_t nRead;
2561+
2562+
// get the LSB and MSB values of the temperature data from the sensor
2563+
uint8_t tLSB = 0;
2564+
uint8_t tMSB = 0;
2565+
25652566
// Read in the MSB and LSB registers
2566-
if (_theI2CBus.readRegister(TMAG5273_REG_T_MSB_RESULT, databuffer, 2, nRead) != ksfTkErrOk)
2567+
if (_theI2CBus.readRegister(TMAG5273_REG_T_MSB_RESULT, tMSB) != ksfTkErrOk)
2568+
return 0;
2569+
if (_theI2CBus.readRegister(TMAG5273_REG_T_LSB_RESULT, tLSB) != ksfTkErrOk)
25672570
return 0;
25682571

25692572
// Combines the two in one register where the MSB is shifted to the correct location
2570-
temp = (databuffer[0] << 8) | (databuffer[1]);
2573+
int16_t tData = (tMSB << 8) | tLSB;
2574+
25712575
// Formula for correct output value
2572-
return TMAG5273_TSENSE_T0 + (((float)temp - TMAG5273_TADC_T0) / (TMAG5273_TADC_RES));
2576+
return TMAG5273_TSENSE_T0 + ((float)(tData - TMAG5273_TADC_T0) / (TMAG5273_TADC_RES));
25732577
}
25742578

25752579
/// @brief Readcs back the X-Channel data conversion results, the

src/SparkFun_TMAG5273_Arduino_Library_Defs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ Features as per datasheet
3131
/********************************** Constant Variable Definitions **********************************/
3232
#define TMAG5273_DEVICE_ID_VALUE 0x5449 // Value found in the device ID register
3333
#define TMAG5273_I2C_ADDRESS_INITIAL 0X22 // Initial I2C address value - can be changed using functions as seen below
34-
#define TMAG5273_TSENSE_T0 25 // Reference temperature for TADC_T0
34+
#define TMAG5273_TSENSE_T0 25.0f // Reference temperature for TADC_T0
3535
#define TMAG5273_TADC_T0 17508 // Temp result in decimal value (from 16-buit format)
36-
#define TMAG5273_TADC_RES 60.1 // Temperature sensing resolution (in 16-bit format)
36+
#define TMAG5273_TADC_RES 60.1f // Temperature sensing resolution (in 16-bit format)
3737

3838
#define TMAG5273_CRC_DISABLE 0X0 // Disables I2C CRC byte to be sent
3939
#define TMAG5273_CRC_ENABLE 0X1 // Enable I2C CRC byte to be sent

0 commit comments

Comments
 (0)