Can deployed model run at MHz frequency on Arduino Mega 2560?

3 views (last 30 days)

While exploring the "Getting Started with Arduino Hardware" example, I would like to be able to generate a repeating pulse with a width of 5 us. When I attempt to do this with the "arduino_gettingstarted.SLX" file and my Arduino Mega 2560, I noticed on my oscilloscope that the pulse width was not what I defined it to be.

Is it possible to generate pulses at or around 5 us?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 13 Mar 2020
It is not possible to run the Arduino Mega 2560 at MHz frequency
The minimum sample time at which the Arduino Mega can run without overrunning is around 7.5 us. This time is derived from the time which the scheduler architecture needs to serve the Interrupt routine and call the model step function. Note that this time does not include the model running time. The model running time depends on the blocks are present in the model.
For the model "arduino_gettingstarted.SLX", this time is around 135 us. The total time for execution of one step function is 7.5 us + 135 us = 142.5 us. In the"arduino_gettingstarted.SLX" model, the pulse generator is configured to count till 10 samples and toggle the output on every 5 samples.
So the on/off time of output = 142.5 us *5 = 712.5 us (Observed is 750 us due to overrun overheads)
Having said that, to have better results, the pulse generator should be set to count till 2 and toggle at every count. This is done by setting the Period = 2 and Pulse width = 1. The sample time should then be adjusted accordingly. Another suggestion will be to use uint8(1) in the Amplitude parameter of Pulse Generator block. This will ensure that the output datatype of this blocks is uint8 which saves additional data validations.
Note that these minor instructions are effecting the result because we are trying to measure micro seconds. Since the Mega runs at 16 MHz, 1 machine cycle takes 0.0625 us. Each code statement can take multiple machine cycles to execute.
Given the above information, here is a list of potential workarounds that can be applied:
1) If using Pulse generator, use a uint8 datatype in Amplitude parameter of the block.
2) Use Counter Limited block and set the limit to 1. This way the block will output 0 and 1 in the alternate samples which will basically toggle the pin. Time taken to toggle pin with Counter Limited block is around 10 us for the Arduino Mega 2560.
3) Use a different Arduino board that runs at high frequency.

More Answers (0)

Categories

Find more on Arduino Hardware in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!