How do we run inbuilt DAC of STM32 using MATLAB Simulink?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-01-30 5:12 AM
DAC block is not directly available on MATLAB Simulink.
We tried interfacing CubeIDE with Simulink using S-Function builder but were not able to run. We are facing problem in creating S-function.
Any idea on how to do so directly or indirectly would be appreciated.
Solved! Go to Solution.
- Labels:
-
ADC
-
DAC
-
STM32-MatTarget
-
STM32CubeIDE
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-02-07 1:04 AM
Hello @AAgra.5​
I would like to share with you an answer from our partner Mathworks about your issue.
There are multiple ways to add DAC driver in Simulink model to leverage DAC peripheral.
- You can create driver blocks following “DriverGuide�? from https://mathworks.com/matlabcentral/fileexchange/39354-device-drivers?s_tid=srchtitle_driver%20blocks_3
- Using Custom code blocks. For example, you can refer DC-DC Buck Converter Simulink Model using STM32 BG474EDPOW1 for detailed explanation.
Attached is an example model to use DAC and ADC with Embedded Coder Support Package for STMicroelectronics STM32 Processors that uses second way. Model is created with MATLAB version R2022b.
The attached example uses custom code way to trigger DAC and the voltage is read by ADC. The output port 1 in the model is mapped/configured to generate “DAC_Output�? global variable as shown below:
- Select in “Apps -> Embedded Coder -> C Code -> Code Interface -> Default Code Mappings�? to open “Code Mapping – Component Interface�? window at the bottom of model. Go to “Outputs�? tab and map output port 1 to generate “DAC_Output�? global variable.
- In the STM32CubeMX project, “DAC�? peripheral is enabled and configured. During the code generation of Simulink model, the code for STM32CubeMX project is also generated. STM32CubeMX generates DAC peripheral initialization function to configure DAC. This initialization function is automatically populated in “main()�? in generated code for model.
- Add “System Initialize�? and “System Outputs�? custom code blocks to enable DAC channels at init and trigger DAC to convert “DAC_Output�? respectively.
- You can run the model with “Monitor and Tune�? to view the results in Simulink in real-time following the example “Monitoring and Tuning Using STMicroelectronics STM32 Processor Based Boards�?.
Special thanks dedicated to Mathworks Team for the support.
Firas
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-01-31 1:28 AM
Hello @AAgra.5​
You need to Install Simulink Coder Support Package for STM32 Nucleo Boards
Also you may need to check Getting Started with STM32 Nucleo Boards Using Simulink - MATLAB & Simulink (mathworks.com)
Also make sure that you are using a Nucleo board that is supported in the support package.
Hope this helps
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-01-31 2:26 AM
Hii @F.Belaid​
Thank you so much for the insights.
But the thing is I am facing a particular problem with DAC convertors as no MATLAB block is available.
As referenced in the link given ADC, Digital Input and Digital Output are applied.
I have tried using S-function builder as the code was available in CubeIDE but was not able to do it.
Could you guide me in the same.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-01-31 6:35 AM
Well @AAgra.5​,
I guess you may need to check Level-2 MATLAB S-function API which allows you to create S-function block in Simulink not in STM32CubeIDE.
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2023-02-07 1:04 AM
Hello @AAgra.5​
I would like to share with you an answer from our partner Mathworks about your issue.
There are multiple ways to add DAC driver in Simulink model to leverage DAC peripheral.
- You can create driver blocks following “DriverGuide�? from https://mathworks.com/matlabcentral/fileexchange/39354-device-drivers?s_tid=srchtitle_driver%20blocks_3
- Using Custom code blocks. For example, you can refer DC-DC Buck Converter Simulink Model using STM32 BG474EDPOW1 for detailed explanation.
Attached is an example model to use DAC and ADC with Embedded Coder Support Package for STMicroelectronics STM32 Processors that uses second way. Model is created with MATLAB version R2022b.
The attached example uses custom code way to trigger DAC and the voltage is read by ADC. The output port 1 in the model is mapped/configured to generate “DAC_Output�? global variable as shown below:
- Select in “Apps -> Embedded Coder -> C Code -> Code Interface -> Default Code Mappings�? to open “Code Mapping – Component Interface�? window at the bottom of model. Go to “Outputs�? tab and map output port 1 to generate “DAC_Output�? global variable.
- In the STM32CubeMX project, “DAC�? peripheral is enabled and configured. During the code generation of Simulink model, the code for STM32CubeMX project is also generated. STM32CubeMX generates DAC peripheral initialization function to configure DAC. This initialization function is automatically populated in “main()�? in generated code for model.
- Add “System Initialize�? and “System Outputs�? custom code blocks to enable DAC channels at init and trigger DAC to convert “DAC_Output�? respectively.
- You can run the model with “Monitor and Tune�? to view the results in Simulink in real-time following the example “Monitoring and Tuning Using STMicroelectronics STM32 Processor Based Boards�?.
Special thanks dedicated to Mathworks Team for the support.
Firas
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
