This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

SENSOR-CONTROLLER-STUDIO: I2C reading temperature sensor

Part Number: SENSOR-CONTROLLER-STUDIO

I am trying to read the temperature data from the temperature sensor: MPL115A2

But if I do "Task Testing" the variable "i2cStatus" from the state Data structure shows constantly 1, meaning there is an error or NAK.

And the output values "tempMSB" and "tempLSB" are constantly 1, too.

Below is the execution code.

 


// Configure and start the next measurement

i2cStart();

i2cTx((MPL115A2_ADDRESS<<1) | I2C_OP_WRITE);      // Device address write command

i2cTx(MPL115A2_REGISTER_STARTCONVERSION);     // Start conversion command

i2cTx(0x00); // Complete the passing of Start Conversion

i2cStop();

fwDelayUs(100000, FW_DELAY_RANGE_100_MS);        // Delay for A/D conversion

// If successful ...

if (state.i2cStatus == 0x0000) {

i2cStart();

i2cTx((MPL115A2_ADDRESS<<1) | I2C_OP_READ);      // Device address read command

i2cTx(MPL115A2_REGISTER_TEMP_MSB);                   // Access register

i2cRxNack(output.tempMSB);                                           // Read Temperature MSB

i2cRxNack(output.tempLSB);                                            // Read Temperature LSB

i2cStop();

}

// Schedule the next execution

fwScheduleTask(1);

  • I note that you shift the address but in the code snips in the SCE help the address is not shifted.

    Have you checked which on the I2C commands that lead to an error?
  • I figured it out:

    I ended the receive communication with of the first byte already with a NAK and even then tried to receive another byte. Also, I needed to first write to the first temperature data register. The sensor has an auto-address increment feature.

    TER said:
    I note that you shift the address but in the code snips in the SCE help the address is not shifted.

    Have you checked which on the I2C commands that lead to an error?

    The address given for my sensor was 7 bits long. That's why it had to be shifted once to the left.

    The following is the working code:


    // Configure and start the next measurement
    i2cStart();
    i2cTx((MPL115A2_ADDRESS<<1) | I2C_OP_WRITE);    // Device address write command
    i2cTx(MPL115A2_REGISTER_STARTCONVERSION);       // Start conversion command
    i2cTx(0x00);                                    // Complete the passing of Start Conversion
    
    i2cStop();
    
    fwDelayUs(100000, FW_DELAY_RANGE_100_MS);       // Delay for A/D conversion
    
    i2cStart();
    
    i2cTx((MPL115A2_ADDRESS<<1) | I2C_OP_WRITE);
    i2cTx(MPL115A2_REGISTER_TEMP_MSB);
    
    
    // If successful ...
    if (state.i2cStatus == 0x0000) {
    
        i2cRepeatedStart();
        i2cTx((MPL115A2_ADDRESS<<1) | I2C_OP_READ); // Device address read command
        
        i2cRxAck(output.tempMSB);                   // Read Temperature MSB
        i2cRxNack(output.tempLSB);                  // Read Temperature LSB
    
    }
    
    i2cStop();
    
    // Schedule the next execution
    fwScheduleTask(1);