بخش Register Map(صفحه 23)
.
.
function[] = ADXL345_SPI()
a = arduino();
finishup = onCleanup(@() ExitProgram(a));
CS = '10';
acc = spidev(a,CS,'Mode',3,'BitOrder','msbfirst','Bitrate',2000000);
POWER_CTL = hex2dec('2D'); %value = 0x08 8
BW_RATE = hex2dec('2C'); %Value = 0x0E 14
DATA_FORMAT = hex2dec('31'); %value = 0x00 0
Device_ID = hex2dec('00');
DATAX0 = hex2dec('32');
DATAX1 = hex2dec('33');
WriteData(acc,DATA_FORMAT, '00');
pause(0.01);
WriteData(acc,BW_RATE, '0E');
pause(0.01);
WriteData(acc,POWER_CTL, '08');
pause(0.01);
while 1 %We Creat a loop function
value = int16(ReadData(acc,DATAX0,6));
X = bitsll(value(1,3),8) + value(1,2); %Calculate the X-Axis
Y = bitsll(value(1,5),8) + value(1,4); %Calculate the Y-Axis
Z = bitsll(value(1,7),8) + value(1,6); %Calculate the Y-Axis
output_str = sprintf('X=%1.0fg Y=%1.0fg Z=%1.0fg',X/256,Y/256,Z/256);
disp(output_str);
% Value For X Axis
% X0 = int16(ReadData(acc,DATAX0));
% X1 = int16(ReadData(acc,DATAX1));
% X = bitsll(X1(1,2),8) + X0(1,2);
% disp(X/256);
pause(0.3);
end
end
function WriteData(board, registerValue, value)
Data2Write = [registerValue, hex2dec(value)];
writeRead(board,Data2Write);
end
% function value = ReadData(board, registerValue)
function value = ReadData(board, registerValue,numBytes)
registerValue = registerValue + 192;
% registerValue = registerValue + 128;
Data2Read = [registerValue, zeros(1,numBytes,'uint8')];
value = writeRead(board, Data2Read);
end
function ExitProgram(board)
clear board;
clear all;
disp('Program has Exit');
end
دیدگاهها
هیچ نظری هنوز ثبت نشده است.