Your First Matlab/STM32 Program

From RapidSTM32
Jump to: navigation, search


This example demonstrates how to create a Simulink model, for using FiO Std Evaluation Board (EVB) as a target, to acquire real-time data from two sources, including:

  1. A potentiometer
  2. An on-chip temperature sensor

Both analog inputs are sampled, and converted to digital values, by an on-chip 12-bits Analog to Digital Converter at 100Hz sampling rate. The converted data are sent to the host PC via USB Human Interface Device (HID) protocol. The host PC receives the data, converts the measured temperature from volts to Celsius, and finally, displays both data, in Simulink as both numerical values, as well as time series plots in real-time.

Contents

Watch the Step-by-Step Video

Part 1 of 2


Part 2 of 2

Step-by-Step Guide

Creating a system model for the FiO Target

1. Set FiO Std in USB In Application Programming (IAP) Mode and connect it to the host PC. This is required because, we will be using the Target USB Send Block and the Compile and Download Control Block in our example model, which requires that a FiO board is connected to the host PC to enable the full-feature mode of the RapidSTM32 Blockset.

2. Test connection between the target and the host PC with request_productinfo('usb','name') command as shown here.

3. Create a new model and set it to use RapidSTM32 target and insert Setup System Clocks & SysTick Block, Compile and Download Control Block, ADC Configuration Block, and Target HID Send Block as shown below. Note that:

Overall system model

4. Configure the ADC Configuration Block as shown below. Note that:

  • Use Custom Setting
  • Channel Selection [16 13] (Analog CH16 and CH13 are connected to the on-chip temperature sensor and Pin C13 respectively.)
  • Output Data Type Volts (single - 32bits)
  • Sample time (in seconds) 0.01
Configure the ADC Block

5. Configure the Target HID Send Block as shown below. Note that:

  • Use 'single single' for Binary data
  • Enable Blocking
  • Sample time (in seconds) -1 to inherit the sample time of 0.01 seconds from the previous block.
Configure Target HID Send Block

6. Connect all blocks as shown below.

Connected model

7. Save your model as adc_hid_target_demo, so we know clearly that this model is for the target (to run on a FiO board). Then, update the diagram twice. All blocks should display correct sample time of 10 milli second (or 0.01 second).

Updated Model

8. If you have not done so, set FiO Std in USB IAP Mode. Test connection between the target and the host PC with request_productinfo('usb','name') command as shown here.

9. Initiate the Build Process. The source code should be automatically generated, compiled, and downloaded to your target at the end of the process as shown below.

Build target complete

Creating a system model for the Host PC

1. Create a new model and set it to use RapidSTM32 target. Insert Host HID Receive Block, Convert Onboard AN16 Volts to Temp C, Display Block, Scope Block, and Data Type Conversion Block as shown below. Save this model as adc_hid_hostpc_demo.mdl. Note that:

Host PC system model

2. Configure the Host HID Receive Block as shown below. Note that:

  • Binary data format is 'single single'
  • Sample time is 0.01 second.
Configure Host HID Receive Block

3. Configure the Data Type Conversion Block as shown below. This is required since the Display Block requires double data type input but the Host HID Receive Block outputs single data type.

Configure Data Type Conversion Block

4. Connect all blocks as shown below.

Completed host PC model

5. Change the final simulation time to inf (infinity) so our host PC system model will run continuously until we press the stop button. Update the diagram twice or until all blocks display correct sample time. We are now ready to proceed with the simulation.

Updated diagram before simulation

Running the system

1. Connect FiO Std potentiometer output to pin C3 as shown below.

Connect FiO Std potentiometer output to pin C3

2. Set FiO Std in Run Custom User Program Mode.

3. Run the host PC Simulink model. Adjust the potentiometer on the FiO Std board and observe the output signals on the Simulink Display and Scope, and oscilloscope (if any).

ADC HID Demo system test

Download

  1. adc_hid_demo.7z
  2. adc_hid_demo video part 1 of 2
  3. adc_hid_demo video part 2 of 2

Related Examples

  1. Displaying ADC data on a character LCD (1 line)
  2. Displaying data on a character LCD (multiple lines)
  3. Open Loop Example: FiO ADC to Simulink Scope and Simulink Slide bar to PWM
  4. Closed Loop Example: FiO ADC to Simulink algorithm to FiO PWM

References

  1. How does the ADC Configuration Block works?
  2. What is a potentiometer?
  3. What is an Analog to Digital Converter (ADC)?



Personal tools
Namespaces

Variants
Actions
Main Menu
In other languages
Tools