我已成功将MATLAB与我的Arduino连接。到目前为止,我只向Arduino发送了简单的任务,如digitalWrite等。
我一直在使用的代码如下:
%-- 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)
既然这个基本测试成功通过,我想让SPI Arduino库与MATLAB一起工作。是否可以在我的MATLAB代码中调用Arduino SPI库中的函数?具体来说,我想让SPI.begin();
和SPI.end();
从MATLAB开始工作,但a.SPI.begin()
无效。我缺少一些步骤吗?
要将SPI库转换为Arduino程序,必须使用#include <SPI.h>
,但我们如何确保MATLAB知道SPI库中可用的所有函数?希望Arduino SPI库代码是用不同于MATLAB文件编写的语言编写的,这不是问题。
参考文献:
“ArduinoIO”库不支持SPI。该库只是一个串口侦听器,每个matlab / arduino指令都按顺序发送一个代码,这个代码在arduino的草图中呈现,在相应的arduino指令中翻译,然后执行。
您可以创建自己的块来发送一些你的choise-spi-command,你还必须编辑arduino的草图以执行相应的SPI命令。但是您必须了解库的工作原理,更改代码等等。
它更快(执行速度,因为串行通信真的很慢,编码时间)编写一个“专门的”arduino草图,将所需的值发送回Serial,然后读取串行和pc端计算。
要使用Matlab支持包与SPI设备通信,您可以使用以下代码:
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
不再支持Legacy MATLAB和Simulink对Arduino的支持。我建议使用适用于Arduino硬件的MATLAB支持包,因为它内置了对基本SPI通信的支持。
支持包附带了一些示例,其中一个示例显示了如何使用SPI。
免责声明:即使我在MathWorks工作,这些帖子也是基于我作为用户使用该软件的经验。如需实际技术支持,请联系Mathworks的TS。