Read Temperature from an NXP FXOS8700CQ Sensor Available on NXP FRDM-K64F Hardware
This example shows you how to use Simulink® Coder™ Support Package for NXP™ FRDM-K64F Board to configure and read temperature from an NXP FXOS8700CQ sensor which is a I2C based sensor.
Introduction
Simulink Coder Support Package for NXP FRDM-K64F Board enables you to use the I2C Controller Read/ I2C Controller Write blocks to communicate with I2C based devices.
In this example, you will learn how to communicate to the NXP FXOS8700CQ sensor which is available on the NXP FRDM-K64F board. FXOS8700CQ is a small, low-power, 3-axis, linear accelerometer and 3-axis, magnetometer combined into a single package. This sensor is interfaced with the NXP FRDM-K64F board using the I2C bus.
For more details about the device, refer to the FXOS8700CQ datasheet.
This examples shows you how to use the I2C Controller Read and I2C Controller Write blocks to read the temperature values from the FXOS8700CQ. If you would like to read Accelerometer data or Magnetometer data from this sensor, you can use the FXOS8700 6-Axes Sensor block available in the NXP FRDM-K64F library.
Prerequisites
We recommend completing the Getting Started with Simulink Coder Support Package for NXP FRDM-K64F Board example and Code Verification and Validation with External Mode example.
Required Hardware
To run this example, you will need the following hardware:
NXP FRDM-K64F Board
USB type A to Micro-B cable
Model
Task 1 - Configure the Model for NXP FRDM-K64F Hardware
In this task, you will configure the model for the NXP FRDM-K64F Hardware.
1. Open the Initialize and Read temperature from I2C sensor using NXP FRDM-K64F Hardware model.
2. In your Simulink model, click Simulation > Model Configuration Parameters to open Configuration Parameters dialog.
3. Select the Hardware Implementation pane and select NXP FRDM-K64F hardware from the Hardware board parameter list.
4. Click OK.
Task 2 - Configure the Model to Read Temperature
This task shows how to initialize configuration registers of the NXP FXOS8700CQ sensor to read the temperature data.
1. Open the Initialize and Read temperature from I2C sensor using NXP FRDM-K64F Hardware model.
2. Notice the two subsystems in the model:
The One_time_initialization subsystem configures NXP FXOS8700CQ sensor.
The Execution_loop subsystem reads the 8-bit temperature.
The One_time_initialization subsystem executes only once at the first step of the execution of the model. For the rest of the execution time, the Execution_loop subsystem runs. This selective execution of subsystems is handled using the Unit Delay and the Enabled Subsystem blocks from the Simulink library.
The One_time_initialization subsystem performs 2 functions. It sets the mode of the sensor and activates the sensor.
4. Configure the I2C Controller Write
block to set the mode of the NXP FXOS8700CQ sensor:
According to the FXOS8700CQ datasheet, the TEMP register of the FXOS8700CQ sensor gives a valid temperature reading, when the sensor is set to magnetometer mode or hybrid mode. The mode of the sensor can be set by writing to the 'm_hms' bits of the M_CTRL_REG1 register.
Open the One_time_initialization subsystem.
Open the I2C Controller Write1 block.
The onboard FXOS8700CQ sensor is connected to the I2C Module of the processor via the pins PTE24 (SCL) and PTE25 (SDA). As seen in the FRDM K64F reference manual that these pins belong to I2C module 0. Hence, set the I2C module to 0.
Notice that the Peripheral address is set to '0x1D' and Peripheral register address is set to '0x5B'. It can be seen in the FXOS8700CQ datasheet that '0x1D' is the peripheral address of the device and '0x5B' is the address of the register M_CTRL_REG1.
Set the Peripheral byte order parameter to Big Endian as the Configuration Register has a size of 8-bit whose value needs to be sent over the I2C bus using Big Endian.
5. Configure the I2C Controller Write
block to activate the NXP FXOS8700CQ sensor:
Open the I2C Controller Write2 block in the One_time_initialization subsystem.
The I2C module and the Peripheral address are set to 0 and '0x1D' as explained in step 4 above.
Notice that the Peripheral register address is set to '0x2A'. It can be seen in the FXOS8700CQ datasheet that '0x2A' is the address of the register CTRL_REG1.
The sensor is in standby mode after reset. You can set the sensor to Active mode by setting the active bit in the CTRL_REG1 to 1 as per the FXOS8700CQ datasheet.
6. Notice the following in the One_time_initialization subsystem:
The Constant1 and Constant2 blocks holds the value of '0x01' to be written to the M_CTRL_REG1 and CTRL_REG1 registers. The sensor is set to 'magnetometer only' mode by writing '0b01' to the 'm_hms' bits of the M_CTRL_REG1 register.
The I2C Controller Read1 and I2C Controller Read2 blocks, which are connected to the Display blocks, helps to verify that the correct data is written to the Configuration Registers. The I2C Controller Read block uses the same settings as the respective I2C Controller Write block.
The priority of the I2C Controller Write1 block is set to be higher than the priority of the I2C Controller Write2 block. This is to ensure that the mode of the sensor is set first and then activated. The priority of the I2C Controller Write block is set to be higher than the priority of the respective I2C Controller Read block, to ensure that you read the value of the Configuration Register after it is set. To set the Priority of a block, right click on the block > Properties > General > Priority. To know more about block priorities and their impact on block execution order, refer to Set Block Properties.
7. Open and observe the following in the Execution_loop subsystem:
Open the I2C Controller Read block. As mentioned for the I2C Controller Write block, notice that the I2C Module is set to 0 and Peripheral address is set to '0x1D'.
It can be seen in the FXOS8700CQ datasheet that '0x51' is the address of the TEMP register, which contains the temperature value in eight-bit 2's complement form. Hence the Peripheral register address is set to '0x51', Data type and Data size (N) are set to 'int8' and '1' respectively.
The sensitivity of the sensor is 0.96 C/LSB. Therefore, the Gain block performs a multiplication by 0.96 to give the temperature value in degree Celsius (C).
Task 3 - Run the Model in External Mode
In this task, you run the model in External mode to monitor the temperature. See example Code Verification and Validation with External Mode for more understanding on External Mode.
1. Change the Simulation mode on the toolbar of the model to External.
2. Click the Run button on the toolbar of the model to run it in External mode.
3. Open the One_time_initialization subsystem.
4. Verify that the Display blocks shows a value of '0x01' in hexadecimal corresponding to the desired Configuration Register value.
5. In the Execution_loop subsystem, monitor the Display block to observe the FXOS8700CQ temperature reading in degree Celsius. The onboard LED glows when the Display block in the model shows a temperature above 27 degree Celsius.
6. Change the threshold value in the Switch block according to your ambient temperature and click Apply. See the onboard LED glow when the temperature exceeds the new threshold value.
7. Click the Stop button in the Simulink model to end the External mode execution.
Other Things to Try
Follow the steps in this example to communicate to other I2C based sensors.