arduino で I2C からデータを取得する方法について

10 ビュー (過去 30 日間)
Takafumi
Takafumi 2019 年 8 月 10 日
回答済み: Takafumi 2019 年 8 月 10 日
MATLAB の arduino のサポートパッケージはインストール済みです。
センサーはLIS3DHの加速度を希望します 

採用された回答

Takafumi
Takafumi 2019 年 8 月 10 日
以下がサンプルです。
timer関数で、0.5s 間隔で取得しています。
時間が、t 、z軸方向の重力加速度の数値が z_v で保存されます。
clear all,clc
global s d t z_v
h = @(~, ~) DIOTimerFcn;
h1 = @(~, ~) DIOStartFcn;
h2 = @(~, ~) DIOStopFcn;
obj1 = timer('StartFcn',h1,'TimerFcn', h,'StopFcn',h2, 'Period', 0.5, 'ExecutionMode', 'fixedRate');
obj1.TasksToExecute = 20;
%
%% start
start(obj1)
%%
function DIOStartFcn
global s d
s = arduino('COM4', 'Uno', 'Libraries', 'I2C')
d = device(s,'I2CAddress','0x19')
dec2hex(readRegister(d,hex2dec('0F')))
writeRegister(d,hex2dec('20'),hex2dec('7F'))
end
function DIOTimerFcn
global s d t z_v
persistent h
if isempty(h)
h = 1;
end
t(h) = datenum(now);
z_v(h)= u2i(readRegister(d,hex2dec('2D')));
h = h +1;
end
function DIOStopFcn
global s d
clear s d;
end
%%
function y = u2i(u)
if u< 127
y = u;
else
y = u -256;
end
y = 4*y/256; % range [-2 2]
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeArduino Hardware についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!