Main Content

Using the I2C Bus to Access Sensors

This example shows how to use the I2C blocks to communicate with I2C based devices.

Introduction

In this example you will learn how to configure and use I2C blocks to:

  • Access the EEPROM

  • Read the accelerometer and gyroscope data from I2C based sensor

Required Hardware

This example requires different hardware configurations for different tasks.

To access the EEPROM

Spectrum Digital F28335/F2808 eZdsp board

Note: Any C2000 (except c281x) controlSTICK or ControlCARD with docking station is not equipped with I2C EEPROM. You must add I2C EEPROM to this board to run the model.

Available models:

To read accelerometer and gyroscope data from I2C based sensor

  • Texas Instruments™ F28069/F28377S/F28379D/F28004x LaunchPad

  • Sensors BoosterPack (BOOSTXL-SENSORS)

Available models:

c28x_i2c_sensor

Read and Write Data to EEPROM

The model writes four bytes to EEPROM and reads back the data from the corresponding EEPROM address to show successful communication.

The model consists of these subsystems:

  • EEPROM Data: Contains the data that is written to the EEPROM.

  • EEPROM Address: Contains the EEPROM address where the data is written to.

  • Trigger Subsystem: Triggers the EEPROM Data Write and EEPROM Data Read subsystems alternatively. The EEPROM Data Write Subsystem is triggered when there is a change in the EEPROM data. The EEPROM Data Read is triggered when there is no EEPROM write.

  • EEPROM Data Write Subsystem: Consists of the I2C Transmit block, which sends the EEPROM address and the data along with a stop condition to the EEPROM at the end of the data write.

  • EEPROM Data Read Subsystem: Consists of the I2C Transmit block, which sends the EEPROM address from which data is read. A wait logic using a while loop ensures that the address is sent to the EEPROM from the Tx FIFO. The I2C Receive block reads the data of specified length along with a stop condition at the end of the data write.

You can run the model in External mode, monitor the transmitted and received data using display blocks. You can change the data in "EEPROM Data" subsystem and see the changes in the display blocks.

  1. Open the c28x_i2c_eeprom model. This model is configured for TI F28335 hardware board. To configure the model to run on other TI C2000 processors, you can change the 'Hardware board' parameter in the Configuration Parameters > Hardware Implementation pane.

  2. In the Configuration Parameters window, click Hardware Implementation and navigate to Target hardware resources > External mode and set the Serial port parameter to the COM port at Device Manager > Ports (COM & LTP) in Windows. For more information, see Parameter Tuning and Signal Logging with Serial Communication

  3. Run the model and observe the "Receive data" and "Transmit data" display blocks.

  4. Change the four byte data in "EEPROM Data" subsystem and observe the change getting reflected in "Receive data" block.

Use interrupts to Read and Write Data to EEPROM

The model writes four bytes to EEPROM and reads back the data from the corresponding EEPROM address to show successful communication.

The model consists of these subsystems:

  • EEPROM Data: Contains the free-running counter data that is written to the EEPROM.

  • EEPROM Address: Contains the EEPROM address where the data is written to.

  • Trigger Logic: Triggers EEPROM Data Write and EEPROM Data Read subsystems alternatively. The trigger logic consists of the STATVAR data store variable, which is updated to start a write/read operation in the I2C ISR block when the SCD interrupt is triggered.

  • EEPROM Data Write Subsystem: Consists of the I2C Transmit block, which sends the EEPROM address and the data along with a stop condition to the EEPROM at the end of the data write.

  • EEPROM Data Read Subsystem: Consists of the I2C Transmit block, which sends the EEPROM address to the EEPROM to initiate the EEPROM read operation. An ARDY interrupt is generated when the address is sent to the EEPROM from the TX FIFO. The I2C Receive block is then used in the interrupt service routine (ISR) to read the data of specified length along with a stop condition at the end of the data write.

You can run the model in External mode and monitor the transmitted and received data using display blocks.

  1. Open the c28x_i2c_eeprom_interrupt model. This model is configured for TI F28335 hardware board. To configure the model to run on other TI C2000 processors, you can change the target hardware in the Configuration Parameters > Hardware Implementation pane.

  2. In the Configuration Parameters window, click Hardware Implementation > Target hardware resources > External mode and set the Serial port parameter to the COM port at Device Manager > Ports (COM & LTP) in Windows. For more information, see Parameter Tuning and Signal Logging with Serial Communication

  3. Ensure that system interrupt, SCD interrupt, and ARDY interrupt are selected in Configuration Parameters > Hardware Implementation > I2C.

  4. Run the model and observe the free-running counter data in "Receive data" and "Transmit data" display blocks.

Read the Accelerometer and Gyroscope Data from I2C-based Sensor

The model configures the registers in Sensor Boosterpack reads the data from the accelerometer and gyroscope.

The model consists of these subsystems:

  • Initialization Subsystem: The Initialization Subsystem is triggered only once at the beginning. The Initialization Subsystem performs the required sensor initialization by sending a series of commands with 1 ms delay between each command to the sensor. For each command, the I2C transmit block sends the address and the data along with the stop condition to the sensor. At the end of the initialization, the read cycle is initiated by sending the address of the register to be read to the sensor using the I2C Transmit block.

  • Sensor Read: The Sensor Read Subsystem consists of the I2C Receive block, which reads the sensor data along with the stop condition that stops the current read cycle. Then, the I2C Transmit block sends the address to the sensor to initiate the data read for the next cycle. To ensure that the I2C Receive block is executed before the I2C Transmit block, set the block priorities for both the blocks by right-clicking and selecting Properties > Priority.

  • Data Realignment Subsystem: Aligns the data as required before displaying it using a display block.

You can run the model in External mode and monitor the accelerometer and gyroscope data in the scope.

1. Open the c28x_i2c_sensor model. This model is configured for TI Delfino F2837xS hardware board. To configure the model for other TI C2000 processors, you can change the hardware board in the Configuration Parameters > Hardware Implementation pane.

2. In the Configuration Parameters window, click Hardware Implementation and navigate to Target hardware resources > External mode and set the Serial port parameter to the COM port at Device Manager > Ports (COM & LTP) in Windows. For more information, see Parameter Tuning and Signal Logging with Serial Communication.

3. Ensure that the Sensor Boosterpack is connected to the Launchpad, and select the corresponding I2C module (A/B) on Tx and Rx blocks. I2C_B module exists only for F2807xs/F2837xS and F2837xD processors.

4. Select appropriate GPIO pins for SDA and SCL in Hardware Implementation > I2C_A/I2C_B to communicate with Sensor Boosterpack.

5. Run the model and observe the accelerometer and gyroscope sensor data in the scope.

More About