Your First Matlab/STM32 Program
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:
- A potentiometer
- 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.
Watch the Step-by-Step Video
Part 1 of 2
Part 2 of 2
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:
- Setup System Clocks & SysTick Block and Compile and Download Control Block are from Device Configuration Library
- ADC Configuration Block and Target HID Send Block are from On-Chip Peripherals Library
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
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.
6. Connect all blocks as shown below.
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).
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.
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 HID Receive Block is from Add-On Modules Library
- Convert Onboard AN16 Volts to Temp C is from On-Chip Peripherals Library
- Display Block, Scope Block are from Simulink Sinks Library
- Data Type Conversion Block is from Signal Attributes Library
2. Configure the Host HID Receive Block as shown below. Note that:
- Binary data format is 'single single'
- Sample time is 0.01 second.
4. Connect all blocks as shown below.
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.
Running the system
1. Connect FiO Std potentiometer output to pin C3 as shown below.
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).
- Displaying ADC data on a character LCD (1 line)
- Displaying data on a character LCD (multiple lines)
- Open Loop Example: FiO ADC to Simulink Scope and Simulink Slide bar to PWM
- Closed Loop Example: FiO ADC to Simulink algorithm to FiO PWM
- How does the ADC Configuration Block works?
- What is a potentiometer?
- What is an Analog to Digital Converter (ADC)?