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

조회 수: 8 (최근 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 CenterFile Exchange에서 Arduino Hardware에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!