Mark
I wrote
"..and the values printed out may be about what are expected".
I would print the values out as hexadecimal since as decimal it is difficult to see exactly what is happening - see my original example.
Since this detail is more general coding I'll leave it to you to sort out because the accelerometer seems to be doing its job and also the I2C operation looks realistic.
If you want to do some general improvements I would suggest reading the status register as well as just the result values since this will also show whether there were errors and that the conversion has completed, etc. It also means that the reads can be made without always first commanding the address to be read from since the accelerometer's internal, address pointer wraps around to the status register after the conversion results have been read.
As reference I have shown the code to do this in the uTasker project below. It allows simulation of the KL02, I2C and the accelerometer so also makes it much easier to test and debug (using VisualStudio).
Regards
Mark
static const unsigned char ucReadAccelerometerState[] = {RESULT_LENGTH, MMA8451Q_READ_ADDRESS, OWN_TASK}; // command to start a read the defined amount of bytes with the task scheduled when the read has completed
static const unsigned char ucSetAccelerometerRead[] = {MMA8451Q_WRITE_ADDRESS, 0}; // command to set address to read to the first register address (status)
static const unsigned char ucSetAccelerometerMode[] = {MMA8451Q_WRITE_ADDRESS, ACC_CONTROL_REGISTER, (ACC_CONTROL_REGISTER_ACTIVE | ACC_CONTROL_REGISTER_DATA_RATE_50Hz | ACC_CONTROL_REGISTER_SLEEP_RATE_6_25Hz | ACC_CONTROL_REGISTER_LNOISE)}; // command to set the 14-bit resolution mode
#define RESULT_LENGTH 7 // 14 bit mode 1 status byte and 6 result bytes
// Initialisation
//
fnWrite(IICPortID, (unsigned char *)ucSetAccelerometerMode, sizeof(ucSetAccelerometerMode)); // write the operating mode
// Followed by the first status read
//
fnWrite(IICPortID, (unsigned char *)ucSetAccelerometerRead, sizeof(ucSetAccelerometerRead)); // write the register address to read
fnRead(IICPortID, (unsigned char *)ucReadAccelerometerState, 0); // start the read process of the status
...
// I2C reception
//
if (fnRead(IICPortID, ucInputMessage, RESULT_LENGTH) != 0) { // if the result read has completed
int i = 0;
fnSetColor((signed char)ucInputMessage[1], (signed char)ucInputMessage[3]); // set the colour to the LED
fnDebugMsg("3-axis state:"); // display the status on a regular basis
while (i < RESULT_LENGTH) { // display 4 values
if (i == 0) {
fnDebugHex(ucInputMessage[i], (sizeof(ucInputMessage[i]) | WITH_LEADIN | WITH_SPACE)); // display the received register contents
}
else {
unsigned short usValue = ucInputMessage[i++];
usValue <<= 8;
usValue |= ucInputMessage[i];
fnDebugHex(usValue, (sizeof(usValue) | WITH_LEADIN | WITH_SPACE)); // display the received register contents
}
i++;
}
fnDebugMsg("\r\n");
fnRead(IICPortID, (unsigned char *)ucReadAccelerometerState, 0); // start the read process of the next value
}