MATLAB Examples

Read Accelerometer and Gyrometer Data from a STMicroelectronics Nucleo Board Using SCI Blocks

This example shows how to use Simulink® Coder Support Package for STMicroelectronics Nucleo Boards to send data to your computer from a board and read it using serial (SCI) blocks.

Copyright(C) 2016-2017 The MathWorks, Inc.

Contents

Introduction

This example can be divided into three sections:

1. In the first model, stmnucleo_SCI_communication, the SCI Write block sends serial data to your computer. The SCI Read block reads the received data and displays it in External mode.

2. In the second model, stmnucleo_AccelGyro_SCI, the SCI Write block sends serial data to USBTX, the default serial transmit port on the Nucleo board. Unlike the first model, this model is deployed on the Nucleo board.

3. In the third model, stmnucleo_AccelGyro_SCI_host, the Serial Read block from the Instrument Control Toolbox™ reads the data that is transmitted in the second model.

These models are pre-configured for STMicroelectronics Nucleo F401RE board. To configure these models for other supported Nucleo boards, browse to Simulation > Model Configuration Parameters > Hardware Implementation on the Simulink® model toolbar. From the Hardware board list, select any other Nucleo board, and click OK.

Prerequisites

Before you start with this example, we recommend you complete the Read data from an I2C based sensors using STMicroelectronics Nucleo board example.

Required Hardware

To run this example, you must have the following hardware:

STMicroelectronics Nucleo SCI Communication Model

In this model, the SCI Write block sends serial data to your computer. The SCI Read block reads the received data and displays it in External mode.

Task 1 — Connect Pin D2 to Pin D8

Connect the D8 (serial Tx) pin to the D2 (serial Rx) pin using a breadboard wire. The model writes the serial data to the D8 pin. This data is looped back to the D2 pin on the Nucleo board.

Note that you cannot use the D1 and D0 serial pins to send and receive data in External mode. External mode uses these pins when running the code on the model. Using the same pins to transmit data results in pin conflict.

Task 2 — Configure the SCI Write and SCI Read Blocks

This task explains how to configure the model to write and read serial data using the SCI Write and Read blocks.

1. Open the Read and write the serial data using SCI(serial) blocks on STMicroelectronics Nucleo board model.

2. In your Simulink® model, double-click the SCI Write block, and perform these steps:

a. Specify the SCI module parameter as 0.

b. Select the Output status option. When you select this option, the Status port becomes available.

This table lists the value at the Status port for different data transfer status.

    Output status(in decimal) |   Message
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
      0                       |  Succesful SCI operation
      1                       |  Receiver(Rx) busy
      2                       |  Transmiter(Tx) busy
      4                       |  SCI Parity error
      8                       |  SCI Frame error
     16                       |  SCI Bus error
     32                       |  SCI data not available
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

3 Double-click the SCI Read block, and perform these steps:

a. Specify the SCI module parameter as 0.

b. Select the Output status option. When you select this option, the Status port becomes available.The value at the Status port for different data transfer status is same as listed in the previous table.

Task 3 — Configure the Model for SCI

1. On the Simulink® model toolbar, select Simulation > Model Configuration Parameters.

2. In the Hardware Implementation pane, select the STMicroelectronics Nucleo board that you are using from the Hardware board list. Do not change any other settings.

3. Browse to Hardware board settings > Target hardware resources > SCI.

4. Set the Receive pin parameter to D2 and the Transmit pin parameter to D8. For more information on the serial pins available on your Nucleo board, refer to the datasheet of your board.

5. Click OK to save your changes.

Task 4 — Run the Model in External Mode

External mode allows you to tune parameters and monitor the serial data in the model while running the application on the Nucleo board.

1. On the Simulink® model toolbar, set the Simulation mode to External.

2. On the Simulink® model toolbar, select Simulation > Model Configuration Parameters.

3. In the Hardware Implementation pane, browse to Hardware board settings > Target hardware resources > External Mode.

3. For the Serial port parameter, specify the COM port number of the serial interface on your Windows® system.

To find the COM port on your Windows® system, open the Device Manager window, and expand the Ports (COM & LPT) list. In the expanded list, locate STMicroelectronics STLink Virtual COM Port (COMx), where x is the number of the port.

4. On the Simulink® model toolbar, click the Run button. After the model is successfully built, observe the following results in the model:

a. The Display block, Serial Write Status, displays the status as 0, indicating that the serial data write is successful.

b. The Display block, Serial Read Status, displays the status as 0, indicating that the serial data read is successful.

c. The Display block, Received data displays the data received by the SCI Read block. This value matches the data sent using the SCI Write block.

5. On the Simulink® model toolbar, click the Stop button to end the External mode execution.

STMicroelectronics Nucleo AccelGyro SCI Model

In this model, the SCI Write block sends the Sparkfun Digital 6DOF accelerometer and gyrometer sensor data along the X, Y, and Z axes to USBTX, the default serial transmit port on the Nucleo board. Unlike the first model, this model is deployed on the Nucleo board.

Task 1 — Configure and Run the Model to Write Sensor Data

This task explains how to configure the model to write sensor data using the SCI Write block.

1. Connect the ITG3200/ADXL345 sensor to the STMicroelectronics Nucleo board as described in the Read data from an I2C based sensors using STMicroelectronics Nucleo board example.

2. Open the Read the Accelerometer and Gyrometer data and send it to host using SCI(serial) blocks on STMicroelectronics Nucleo board model.

3. On the Simulink® toolbar, click Simulation > Model Configuration Parameters.

4. In the Hardware Implementation pane, select the STMicroelectronics Nucleo board that you are using from the Hardware board list. Do not change any other settings.

5. Click OK

6. In your Simulink® model, double-click the SCI Write block, and perform these steps.

a. Specify the SCI module parameter as 0.

b. Select the Output status option. When you select this option, the Status port becomes available.

This table lists the value at the Status port for different data transfer status.

    Output status(in decimal) |   Message
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
      0                       |  Succesful SCI operation
      1                       |  Receiver(Rx) busy
      2                       |  Transmiter(Tx) busy
      4                       |  SCI Parity error
      8                       |  SCI Frame error
     16                       |  SCI Bus error
     32                       |  SCI data not available
 _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

7. On the Simulink® model toolbar, click the Deploy To Hardware button. This action builds, downloads, and runs the model on the Nucleo board.

For more information, on how to configure I2C blocks in this model, see the Read data from an I2C based sensors using STMicroelectronics Nucleo board example.

Task 2 — Read the Values Using Serial Terminal

You can read the values from the board using any serial terminal or by performing the steps specified in the third model. The advantage of using the serial terminal is that you can read the values without any need to install the Instrument Control Toolbox™ on your computer.

To read values using the serial terminal:

1. Open the serial terminal available in your computer. In this example, Putty is used.

2. In Putty, select Connection > Serial. Specify the COM port number of the serial interface on your Windows® system, and the baud rate of data transfer.

To find the COM port on your Windows® system, open the Device Manager window, and expand the Ports (COM & LPT) list. In the expanded list, locate STMicroelectronics STLink Virtual COM Port (COMx), where x is the number of the port.

3. Click Open to see the serial data in the terminal.

4. Close the terminal to free the COM port after analyzing the data.

STMicroelectronics Nucleo AccelGyro SCI Host Model

In this model, the Serial Read block from the Instrument Control Toolbox™ reads the data that is transmitted in the second model. To use this model, you must have an Instrument Control Toolbox™ license.

Task 1 — Configure and Run the Model to Read Serial Data from the Sensor

Before opening the model, ensure that the Instrument Control Toolbox™ is installed in your computer. To check if the toolbox is available in your computer, use the ver command in the MATLAB® command window.

1. Open the Read the serial data using serial block on STMicroelectronics Nucleo board model.

2. Double-click the Serial Configuration block and set the Communication port parameter to the COM port number of the serial interface on your Windows® system.

To find the COM port on your Windows® system, open the Device Manager window, and expand the Ports (COM & LPT) list. In the expanded list, locate STMicroelectronics STLink Virtual COM Port (COMx), where x is the number of the port.

3. Double-click the Serial Receive block and set the Communication port and the Data size parameters.

4. On the Simulink® model toolbar, set the Simulation mode to Normal, and click the Run button.

5. After the model is successfully built, double-click the Scope block Accel and Gyro data. Observe the accelerometer and gyrometer data for each axis separately.

Other Things to Try

  • Perform the steps in this example to communicate with other UART-based sensors.

Summary

This example demonstrated how to send and receive serial data using the SCI Read and Write blocks.

In this example, you learned how to:

  • Transmit serial data and verify the output from various status messages.
  • Transmit serial sensor data and verify the output from various status messages.
  • Use any serial terminal software or the Serial Receive block from the Instrument Control Toolbox™ to read serial data in Normal mode.

You can use this example as a reference to access other UART-based sensors.