MATLAB Examples

Using the I2C Bus to Access Sensors

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

Contents

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 board is not equipped with I2C EEPROM. You must add I2C EEPROM to this board to run the example.

Available versions of this example:

To read accelerometer and gyroscope data from I2C based sensor

  • Texas Instruments™ F28069/F28377S/F28379D LaunchPad.
  • Sensors BoosterPack (BOOSTXL-SENSORS).

Available versions of this example:

Task 1 - Read and write data to EEPROM

This example shows how to access the EEPROM connected to the I2C bus at I2C slave address 0x50 on an F28335/F2808 eZdsp board.

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

You can run the example 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 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. Make sure correct 'Serial port' is selected in Configuration Parameters > Hardware Implementation > Target Hardware Resources > External mode as explained in Parameter Tuning and Signal Logging with Serial External mode.

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.

Task 2 - Use interrupts to read and write data to EEPROM

This example shows how to access the EEPROM connected to the I2C bus at I2C slave address 0x50 on an F28335/F2808 eZdsp board using interrupts.

This program writes four bytes to EEPROM and reads back the data from the corresponding EEPROM address to show successful communication. It uses the SCD interrupt to detect the stop condition after successful completion of write/read operation and ARDY interrupt to start the data read operation.

The I2C Transmit block in the "EEPROM Write" subsystem writes the free-running counter data from the "EEPROM Data" to the address specified by "EEPROM Address". When an I2C interrupt is detected, "I2C ISR - Data read" subsystem function is executed. As a result, the I2C Receive block reads the EEPROM data in "ARDY Subsystem - Data read".

You can run the example 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. Make sure correct 'Serial port' is selected in Configuration Parameters > Hardware Implementation > Target Hardware Resources > External mode as explained in Parameter Tuning and Signal Logging with Serial External mode.

3. Make sure '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.

Task 3 - Read the accelerometer and gyroscope data from I2C based sensor

This example shows how to access the Sensor BoosterPack connected to the I2C bus at I2C slave address 0x69 on a Launchpad.

This program configures the registers in Sensor Boosterpack reads the data from the accelerometer and gyroscope.

The sensors are configured for normal mode data read in the "Initialization" subsystem. The sensor data is read every 1ms from the address 0x0C continuously by triggering the subsystem "Sensor Read". The received data is buffered, realigned and is plotted separately for accelerometer and gyroscope in the subsystem "Data Realignment".

You can run the example 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. Make sure correct 'Serial port' is selected in Configuration Parameters > Hardware Implementation > Target Hardware Resources > External mode as explained in Parameter Tuning and Signal Logging with Serial External mode.

3. For F28379D LaunchPad, perform the following settings to run in External mode:

  • Set 'Oscillator clock (OSCCLK) frequency in MHz' in Hardware Implementation > Target Hardware Resources > Clocking to the value of 10.
  • Select Pin assignment 'Tx' as GPIO42 and 'Rx' as GPIO43 in Hardware Implementation pane > Target Hardware Resources > SCI_A.

4. Make sure the proper connection of Sensor Boosterpack on the Launchpad and select the corresponding I2C module (A/B) on Tx and Rx blocks. Note that I2C_B module exists only for F2807xs/F2837xS and F2837xD processors.

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

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

Summary

This example introduced I2C Bus to access a connected EEPROM and a Sensor BoosterPack.