@@ -2625,25 +2625,24 @@ float TMAG5273::getZData()
26252625// / @return Angle measurement result in degrees (float value)
26262626float TMAG5273::getAngleResult ()
26272627{
2628- uint8_t angleLSB = 0 ;
2629- uint8_t angleMSB = 0 ;
2630- if (_theI2CBus.readRegister (TMAG5273_REG_ANGLE_RESULT_LSB, angleLSB) != ksfTkErrOk)
2631- return 0 ;
2632- angleLSB = angleLSB & 0b11111111 ;
2633- if (_theI2CBus.readRegister (TMAG5273_REG_ANGLE_RESULT_MSB, angleMSB) != ksfTkErrOk)
2628+ // Read the angle data - returns the MSB and LSB in one read
2629+ uint8_t dataBuffer[2 ];
2630+ size_t nRead;
2631+ if (_theI2CBus.readRegister (TMAG5273_REG_ANGLE_RESULT_MSB, dataBuffer, 2 , nRead) != ksfTkErrOk)
26342632 return 0 ;
2635-
2636- // Combining the register value
2637- int16_t angleReg = angleLSB + (angleMSB << 8 );
2638-
2639- // Removing the uneeded bits for the fraction value
2640- float decValue = float (angleLSB & 0b1111 ) / 16 ;
2641-
2642- // Shift off the decimal value (last 4 bits)
2643- int16_t angleVal = angleReg >> 4 ;
2644-
2645- // Add the two values together now
2646- return angleVal + decValue;
2633+ // Combines the two in one register where the MSB is shifted to the correct location
2634+ // convert to uint16_t (the data is in 2's complement format)
2635+ uint16_t angleReg = (dataBuffer[0 ] << 8 ) | dataBuffer[1 ];
2636+
2637+ // the data is formatted in 13 bits, using 9.4 format (9 bits for integer, 4 bits for fraction (/16))
2638+ //
2639+ // fraction value - 4 LSB bits / 16
2640+ float fractionValue = (float )(angleReg & 0xF ) / 16 .f ;
2641+ // integer value - shift off the bottom 4 bits, use. the next 9 bits
2642+ float integerValue = (float )((angleReg >> 4 ) & 0x1FF );
2643+
2644+ // return the combined value
2645+ return integerValue + fractionValue;
26472646}
26482647
26492648// / @brief Returns the resultant vector magnitude (during angle
0 commit comments