@@ -2569,18 +2569,18 @@ float TMAG5273::getTemp()
25692569// / @return X-Channel data conversion results
25702570float TMAG5273::getXData ()
25712571{
2572- uint8_t tempRead;
2573- if (_theI2CBus.readRegister (TMAG5273_REG_X_LSB_RESULT, tempRead) != ksfTkErrOk)
2572+ uint8_t xLSB = 0 ;
2573+ uint8_t xMSB = 0 ;
2574+ if (_theI2CBus.readRegister (TMAG5273_REG_X_LSB_RESULT, xLSB) != ksfTkErrOk)
25742575 return 0 ;
25752576
2576- int8_t xLSB = *(int8_t *)&tempRead;
2577-
2578- if (_theI2CBus.readRegister (TMAG5273_REG_X_MSB_RESULT, tempRead) != ksfTkErrOk)
2577+ if (_theI2CBus.readRegister (TMAG5273_REG_X_MSB_RESULT, xMSB) != ksfTkErrOk)
25792578 return 0 ;
2580- int8_t xMSB = *(int8_t *)&tempRead;
25812579
25822580 // Combines the two in one register where the MSB is shifted to the correct location
2583- int32_t xData = xLSB + (xMSB << 8 );
2581+ // convert the uint16_t to int16_t
2582+ uint16_t tmpData = (xMSB << 8 ) + xLSB;
2583+ int16_t xData = *(int16_t *)&tmpData;
25842584
25852585 // Reads to see if the range is set to 40mT or 80mT
25862586 uint8_t rangeValXY = getXYAxisRange ();
@@ -2606,17 +2606,19 @@ float TMAG5273::getXData()
26062606// / @return Y-Channel data conversion results
26072607float TMAG5273::getYData ()
26082608{
2609- uint8_t tempRead;
26102609
2611- if (_theI2CBus.readRegister (TMAG5273_REG_Y_LSB_RESULT, tempRead) != ksfTkErrOk)
2610+ uint8_t yLSB = 0 ;
2611+ uint8_t yMSB = 0 ;
2612+ if (_theI2CBus.readRegister (TMAG5273_REG_Y_LSB_RESULT, yLSB) != ksfTkErrOk)
26122613 return 0 ;
2613- int8_t yLSB = *( int8_t *)&tempRead;
2614- if (_theI2CBus.readRegister (TMAG5273_REG_Y_MSB_RESULT, tempRead ) != ksfTkErrOk)
2614+
2615+ if (_theI2CBus.readRegister (TMAG5273_REG_Y_MSB_RESULT, yMSB ) != ksfTkErrOk)
26152616 return 0 ;
2616- int8_t yMSB = *(int8_t *)&tempRead;
26172617
26182618 // Combines the two in one register where the MSB is shifted to the correct location
2619- int32_t yData = yLSB + (yMSB << 8 );
2619+ // convert the uint16_t to int16_t
2620+ uint16_t tmpData = (yMSB << 8 ) + yLSB;
2621+ int16_t yData = *(int16_t *)&tmpData;
26202622
26212623 // Reads to see if the range is set to 40mT or 80mT
26222624 uint8_t rangeValXY = getXYAxisRange ();
@@ -2643,17 +2645,19 @@ float TMAG5273::getYData()
26432645// / @return Z-Channel data conversion results.
26442646float TMAG5273::getZData ()
26452647{
2646- uint8_t tempRead;
2648+ uint8_t zLSB = 0 ;
2649+ uint8_t zMSB = 0 ;
26472650
2648- if (_theI2CBus.readRegister (TMAG5273_REG_Z_LSB_RESULT, tempRead ) != ksfTkErrOk)
2651+ if (_theI2CBus.readRegister (TMAG5273_REG_Z_LSB_RESULT, zLSB ) != ksfTkErrOk)
26492652 return 0 ;
2650- int8_t zLSB = *( int8_t *)&tempRead;
2651- if (_theI2CBus.readRegister (TMAG5273_REG_Z_MSB_RESULT, tempRead ) != ksfTkErrOk)
2653+
2654+ if (_theI2CBus.readRegister (TMAG5273_REG_Z_MSB_RESULT, zMSB ) != ksfTkErrOk)
26522655 return 0 ;
2653- int8_t zMSB = *(int8_t *)&tempRead;
26542656
26552657 // Combines the two in one register where the MSB is shifted to the correct location
2656- int32_t zData = zLSB + (zMSB << 8 );
2658+ // convert the uint16_t to int16_t
2659+ uint16_t tmpData = (zMSB << 8 ) + zLSB;
2660+ int16_t zData = *(int16_t *)&tmpData;
26572661
26582662 // Reads to see if the range is set to 40mT or 80mT
26592663 uint8_t rangeValZ = getZAxisRange ();
0 commit comments