Jump to content
Sign in to follow this  

Serial Print Issue

Recommended Posts



I am interfacing MSP430F5529 with sensor breakout board LSM5DS0. When I am trying to print the serial data in serial monitor, It is nicely printed. But when I try to convert the raw data to a unit value and try to print it, nothing is coming up in the monitor. I am using energia platform. the raw value that can be printed is in integer format and the unit value that can't be printed is in float format.Can anybody explain what is happening actually: I am sharing my code here:


// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.
#define LED RED_LED
#define  xmAddress                      0x1D
#define gAddress                        0x6B

// LSM9DS0 Gyro Registers //
#define WHO_AM_I_G            0x0F
#define CTRL_REG1_G            0x20
#define CTRL_REG2_G            0x21
#define CTRL_REG3_G            0x22
#define CTRL_REG4_G            0x23
#define CTRL_REG5_G            0x24
#define REFERENCE_G            0x25
#define STATUS_REG_G        0x27
#define OUT_X_L_G            0x28
#define OUT_X_H_G            0x29
#define OUT_Y_L_G            0x2A
#define OUT_Y_H_G            0x2B
#define OUT_Z_L_G            0x2C
#define OUT_Z_H_G            0x2D
#define FIFO_CTRL_REG_G        0x2E
#define FIFO_SRC_REG_G        0x2F
#define INT1_CFG_G            0x30
#define INT1_SRC_G            0x31
#define INT1_THS_XH_G        0x32
#define INT1_THS_XL_G        0x33
#define INT1_THS_YH_G        0x34
#define INT1_THS_YL_G        0x35
#define INT1_THS_ZH_G        0x36
#define INT1_THS_ZL_G        0x37
#define INT1_DURATION_G        0x38

// LSM9DS0 Accel/Magneto (XM) Registers //
#define OUT_TEMP_L_XM        0x05
#define OUT_TEMP_H_XM        0x06
#define STATUS_REG_M        0x07
#define OUT_X_L_M            0x08
#define OUT_X_H_M            0x09
#define OUT_Y_L_M            0x0A
#define OUT_Y_H_M            0x0B
#define OUT_Z_L_M            0x0C
#define OUT_Z_H_M            0x0D
#define WHO_AM_I_XM            0x0F
#define INT_CTRL_REG_M        0x12
#define INT_SRC_REG_M        0x13
#define INT_THS_L_M            0x14
#define INT_THS_H_M            0x15
#define OFFSET_X_L_M        0x16
#define OFFSET_X_H_M        0x17
#define OFFSET_Y_L_M        0x18
#define OFFSET_Y_H_M        0x19
#define OFFSET_Z_L_M        0x1A
#define OFFSET_Z_H_M        0x1B
#define REFERENCE_X            0x1C
#define REFERENCE_Y            0x1D
#define REFERENCE_Z            0x1E
#define CTRL_REG0_XM        0x1F
#define CTRL_REG1_XM        0x20
#define CTRL_REG2_XM        0x21
#define CTRL_REG3_XM        0x22
#define CTRL_REG4_XM        0x23
#define CTRL_REG5_XM        0x24
#define CTRL_REG6_XM        0x25
#define CTRL_REG7_XM        0x26
#define STATUS_REG_A        0x27
#define OUT_X_L_A            0x28
#define OUT_X_H_A            0x29
#define OUT_Y_L_A            0x2A
#define OUT_Y_H_A            0x2B
#define OUT_Z_L_A            0x2C
#define OUT_Z_H_A            0x2D
#define FIFO_CTRL_REG        0x2E
#define FIFO_SRC_REG        0x2F
#define INT_GEN_1_REG        0x30
#define INT_GEN_1_SRC        0x31
#define INT_GEN_1_THS        0x32
#define INT_GEN_1_DURATION    0x33
#define INT_GEN_2_REG        0x34
#define INT_GEN_2_SRC        0x35
#define INT_GEN_2_THS        0x36
#define INT_GEN_2_DURATION    0x37
#define CLICK_CFG            0x38
#define CLICK_SRC            0x39
#define CLICK_THS            0x3A
#define TIME_LIMIT            0x3B
#define TIME_LATENCY        0x3C
#define TIME_WINDOW            0x3D
#define ACT_THS                0x3E
#define ACT_DUR                0x3F

#define gODR                            0xF

#include <Wire.h>

float mRes;
void setup()

  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(115200);  // start serial for output

void loop()



int I2CreadByte(uint8_t address, uint8_t subAddress)
  uint8_t data;
  Wire.requestFrom(address, (uint8_t)1);
  while (Wire.available() < 1); // Wait until data becomes available           
  data = Wire.read(); // Read register data into `data` variable 
  Wire.endTransmission(); // End I2C transmission
  return data;

void I2CreadBytes(uint8_t address, uint8_t subAddress, uint8_t * dest,
                            uint8_t count)

    // Begin I2C transmission and send device address
    // Next send the register to be read. OR with 0x80 to indicate multi-read.
    Wire.write(subAddress | 0x80);         
    // End write, but send a restart to keep connection alive:
           // Request `count` bytes of data from the device
    Wire.requestFrom(address, count);
    // Wait until the data has been read in      
    while (Wire.available() < count);     
               // Store all `count` bytes into the given destination array.
    for (int i=0; i<count ;i++)
        dest[i] = Wire.read();
    // End I2C Transmission


void I2CwriteByte(uint8_t address, uint8_t subAddress, uint8_t data)
    Wire.write(subAddress); // Write register to be written to
    Wire.write(data); // Transmit byte to write      
    Wire.endTransmission(); // End I2C transmission

void dev_init()
  calcmRes(); // Calculate Gs / ADC tick, stored in mRes variable

void calcgRes()
    float gRes = 2000.0 / 32768.0;    

void calcmRes()
         float mRes = 2.0 / 32768.0 ;

float calcMag(float mag)
    // Return the mag raw reading times our pre-calculated Gs / (ADC tick):
    return mRes * mag;

void xmWriteByte(uint8_t subAddress, uint8_t data)
     I2CwriteByte(xmAddress, subAddress, data);

void readMag()
    uint8_t temp[6]; // We'll read six bytes from the mag into temp    
    int16_t mx,my,mz;
       // float q;
    xmReadBytes(OUT_X_L_M, temp, 6); // Read 6 bytes, beginning at OUT_X_L_M
    mx = (temp[1] << 8) | temp[0]; // Store x-axis values into mx
    my = (temp[3] << 8) | temp[2]; // Store y-axis values into my
    mz = (temp[5] << 8) | temp[4]; // Store z-axis values into mz


      Serial.print("M: ");
        Serial.print(" ");
        Serial.print(" ");
        Serial.print(" ");
        Serial.print("M: ");
        Serial.print(" ");
        Serial.print(" ");
        Serial.print(" ");

Share this post

Link to post
Share on other sites

Does Energia support printing of floating point numbers? (which are in general a bad idea to use on constrained platforms).

Maybe you're better off using the map() function:

//This is the core of what you're doing:
//mRes = 2.0 / 32768.0 ;
//Serial.print(mx * 2.0 / 32768.0);
Serial.print(map(mx, 0, 32767, 0, 2000));

This scales a value from the range [0..32767] to [0..2000].

Note that I changes 2.0 to 2000, so your result is in millithingies (is your reading in tesla or something the like? then it is millitesla)

The map() function still uses multiplication and division, but it does this in integer maths, not floating point.

Share this post

Link to post
Share on other sites

@@embeddedenthu as @@roadrunner84 in general it is discouraged to use floating point on the MSP430. The MSP430 does not have a floating point unit and all floating point instructions need to be emulated. This makes things slow and will significantly increase memory usage. If you are OK with that then go for it.


Below is a Sketch that demonstrates printing floating points. Not that by default, the print functions only print 2 decimal places. To increase this you need to pass in a second parameter indicating how many decimal places you want.

void setup()

float mRes = 2.0 / 32768.0;

float calcMag(float mag)
    // Return the mag raw reading times our pre-calculated Gs / (ADC tick):
    return mRes * mag;

void loop()
  Serial.println(mRes, 6);

  Serial.println(calcMag(300), 6);

Also note that in the function calcmRes() you the local variable mRes shadows the global one. mRes will remain 0 even after calling calcmRes().


I suggest changing it tot he following:

float mRes;

void calcmRes()
  mRes = 2.0 / 32768.0 ;         

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this