문제

I have successfully connected MATLAB with my Arduino. So far, I have only sent simple tasks to the Arduino such as digitalWrite and such.

The code I have been using is as follows:

%-- connect to the board 
a = arduino('COM9')

%-- specify pin mode 
a.pinMode(9,'output');

%-- write 0 (off) to pin 9
a.digitalWrite(9,0);

%-- dummy variable
on = false;

%-- simple loop to make LED flash 5 times
for m in 1:5
    if on
        a.digitalWrite(9,0); % turn LED off
        on = false;
    else
        a.digitalWrite(9,1); % turn LED on
        on = true;
end

%-- close session 
delete(a)

Now that this basic test successfully passed, I wanted to get the SPI Arduino library to work with MATLAB. Is it possible to call a function from the Arduino SPI library in my MATLAB code? Specifically, I want to get SPI.begin(); and SPI.end(); to work from MATLAB, but a.SPI.begin() is not working. Is there some step I am missing?

To get the SPI library into an Arduino program, one must use #include <SPI.h>, but how can we make sure MATLAB knows all of the functions available in the SPI library? Hopefully it is not a problem that the Arduino SPI Library code is written in a different language than what MATLAB files are written in.

References:

  1. MATLAB Support Package for Arduino (aka ArduinoIO Package)
  2. Arduino SPI Library
도움이 되었습니까?

해결책

The library "ArduinoIO" does not support SPI. That library is just a serial port listener, and every matlab/arduino instruction send a code by serial, that is readed in the sketch on the arduino, translated in the corresponding arduino's instruction, and then executed.

You can create your own block that send some your-choise-spi-command, you'll also have to edit the arduino's sketch to execure the corrisponding SPI command. But you'll have to understand how the library works, change it's code, and so on.

It is way more faster (in execution speed, as serial comunication really is slow, and coding time) to code a "specialized" arduino sketch, that send back to Serial just the value you need, and then read serial and to pc-side computation.

다른 팁

To communicate with SPI device using Matlab support package you can use the following code:

a = arduino();
Spi_Device = spidev(a, 'D5'); % D5 is the pin number that you want to use for chip select
writeRead(Spi_Device,[hex2dec('00'), 100]); % 100 is the value that you want to send to the device

% When you done clear the spi object

clear Spi_Device 

The Legacy MATLAB and Simulink Support for Arduino is no longer supported. I would recommend using the MATLAB Support Package for Arduino Hardware as that has built-in support for basic SPI communication.

There are getting started type of examples that comes with the support package and one of them shows how to use SPI.

Disclaimer: Even though I work for MathWorks, these posts are based on my experience with the software as a user. For actual Technical Support, please contact Mathworks' TS.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top