DE | EN | CN

ESE Block 13 I2C

Use an I²C fieldbus. For this exercise you need a Temperature sensor add-on as an extension to your mySTM32 Board light.
Field bus systems play an important role in many microcontroller applications. On the one hand, such digital communication channels make it possible to use intelligent sensors and actuators and it allows the developer to use controllers with a relatively small number of pins and thus to keep the footprint small. The I²C Bus enables high data transfer rates and even in standard mode to address more than 100 devices in one bus. The I²C bus only needs two lines, i.e. two pins on the controller. There are thousands I²C devices which cover a wide range of applications. Virtually every modern microcontroller family supports this standard. I²C was originally developed by Philips for communication within a device, for example within a television. This means that this bus system is not intended for long transmission paths but was designed for a few centimeters to a few meters. In one of the following chapters you will get to know another field bus system, the CAN bus, which can reliably transmit at least 40 and, under certain conditions, up to 500 meters of data. You can also find this fieldbus on the small mySTM32 Board light.

The task

Develop a microcontroller application in which an I²C temperature sensor is read out and the current temperature is sent to the PC via UART.

Requirements for the solution:

  • Temperature sensor LM75, address configuration 0x90
  • Data transmission 8 bit, 19200 baud
  • Temperature values in full degrees Celsius

SRS I2C

Preparation

If you have still opened a class diagram, select the menu item “up” in the context menu (right mouse button) of the diagram. If the project is no longer open, open the SiSy UML project again. Perform the following preparatory work:

  • create a new class diagram
  • Target language ARM C ++
  • Target platform STM32F042 mySTM32 Board light HAL
  • Load diagram template application framework for PEC applications (XMC, STM32, AVR)
  • Assign driver package for STM32F0
  • optional template stm32F042_48Mhz assign

Solution idea

The task is to read out a digital temperature sensor via an I²C bus and to send the data to a PC terminal via UART. The technically motivated system components for the design of the system architecture can be derived from this.


For the Temperature sensor LM75 there is a corresponding library module in addition to other I²C devices. The following excerpt from the class model of the PEC library shows the relationship between the module PecLm75 and the corresponding I²C bus component PecI2cBus .

Two operations are important for us to use these building blocks. On the one hand, the temperature should be read out in whole degrees. The getTemp () operation is used for this. This already converts the temperature data from the sensor into whole degrees. The sensor itself provides the temperature in 0.125 ° C (8ths of a degree) steps. Furthermore, modern microcontrollers can have several I²C bus interfaces. It is therefore necessary to assign the device with its address to the specific bus. This is done using the connect (…) operation. There are two alternatives for this operation. The operation connect (…) in the device or in the bus.

The following rough draft for the required microcontroller solution can be derived from the knowledge gained so far:

In simple natural language, this draft can be read as follows:

  • the controller is a stm32F042_48MHz
  • the controller has a SensorBus
  • the SensorBus is a PecI2CBus
  • the controller has a terminal
  • the Terminal is a PecUart

The allocation of the specific resources must take place in the implementation.

REMEMBER: Assign BUS = DEVICE with ADDRESS

Realization

The implementation should contain the elements described in the above draft. In addition, we still have to allocate the specifically used resources of our controller. Complete your class model so that it corresponds to the following illustration. Arrange the elements clearly.

First, the connection to the bus must be established and the device address assigned. Up to eight LM75 can be addressed in one bus. There are jumpers on the add-on to configure the device address. Make sure that the address jumpers on the add-on are as follows (all on 0 = 0x90):

In the onStart () operation of the Controller class, make a note of the following code:

Controller::onStart():void
sensorBus.connect(sensorBus.temperaturSensor,0x90);

The reading of the current temperature of the digital sensor takes place on API-Level similar to the procedure for the analog sensor. Note that the temperature sensor is relatively sluggish. Use a small waiting routine so that the changes to the data on the terminal can be easily followed.
In the onWork () operation of the Controller class, write down the following code:

Controller::onWork():void
temperatur=sensorBus.temperaturSensor.getTemp();
terminal.writeByte(temperatur);
waitMs(10);

You are now ready to build and test the application.

Test

Compile the program. Correct typographical errors if necessary. Transfer the executable program to the controller's program memory.

  1. Build (compile and link)
  2. burning
  3. connect the temperature sensor add-on to the mySTM32 board light
  4. Pay attention to the correct position of the address jumpers
  5. Activate the pull-up resistors with the appropriate jumpers
  6. Note the settings in the SiSy ControlCenter

Change the application so that the raw sensor data (raw value) is sent to the PC. To increase readability, convert the 10-bit raw data into text. Change the code of the operation onWork () of the class Controller as follows:

Controller::onWork():void
uint16_t temp;
temp = sensorBus. TemperaturSensor.getRawValue ();
String txt;
txt.format("\nRawValue=%d", temp);
terminal.writeString(txt);
waitMs(100);

Build, broadcast and test the application. Compare the results of both variants with each other.

Video summary

Learned and established work steps:

  1. Create and open class diagram
  2. Select diagram template for PEC application
  3. Load the correct driver package
  4. Search for the desired modules in the Explorer and drag them into the diagram
  5. Aggregate classes, form components
  6. create the necessary source code in the operations
  7. Create and burn an ARM application in the class diagram

And because it was so beautiful, here the whole thing again as a video.

or the fast version with music

Exercise

Use the text LCD to display the sensor data!

Continue with:

Search keywords