Work with an ADXL345 acelerometer and NI845x borad using I2C Protocol. i write down my script anybody can help me ? the value the i plot are strenge
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi
for my projet i have to work with an ADXL345 acelerometer and NI845x borad using I2C Protocol.
now i have to plot the aceleration of the three axis this is my script:
function [Xvalue,Yvalue,Zvalue]=aceleration
fs_I2C= 100; acq_time=10; g_range=16; resolucion='full';
array_fs_I2C =[3200,1000,400,100];
i2cInfo = instrhwinfo ('i2c', 'ni845x');%ensure tha NI is installed
% Direccion del registro accelerometerAddress = hex2dec('53');%ok i2cInterface = i2c('ni845x', 0, accelerometerAddress);%ok fopen(i2cInterface);%ok
i2cInterface.BitRate = fs_I2C;%I2C speed not the ADXL Bit Rate
%Inicialización
freqSelectRegisterAddress = hex2dec('2C'); % 2C is the addres of rate and power mode control (ADXL345) valueToWrite = bin2dec('00001010');
fwrite(i2cInterface, [freqSelectRegisterAddress valueToWrite]);
%stand by mode
powerControlRegisterAddress = hex2dec('2D');% 2D is the addres of power saving feastures control register valueToWrite = bin2dec('00001000'); %measure mode fwrite(i2cInterface, [powerControlRegisterAddress valueToWrite]);
%CONVERSION DIRECCIONES REGISTROS
XregLow=hex2dec('32'); XregHigh=hex2dec('33'); YregLow=hex2dec('34'); YregHigh=hex2dec('35'); ZregLow=hex2dec('36'); ZregHigh=hex2dec('37');
%DATA FORMAT
dataFormatRegisterAddress = hex2dec('31');
switch (g_range)
case 2
if strcmp(resolucion,'10bit')
valueToWrite = bin2dec('00000000');
fwrite(i2cInterface, [dataFormatRegisterAddress valueToWrite]);
factor_de_conversion = 0.0039;
factor_de_resolucion = 6;
else
valueToWrite = bin2dec('00001000');
fwrite(i2cInterface, [dataFormatRegisterAddress valueToWrite]);
factor_de_conversion = 0.0039;
factor_de_resolucion = 6;
end
case 4
if strcmp(resolucion,'10bit')
valueToWrite = bin2dec('00000001');
fwrite(i2cInterface, [dataFormatRegisterAddress valueToWrite]);
factor_de_conversion = 0.0078;
factor_de_resolucion = 6;
else
valueToWrite = bin2dec('00001001');
fwrite(i2cInterface, [dataFormatRegisterAddress valueToWrite]);
factor_de_conversion = 0.0039;
factor_de_resolucion = 5;
end
case 8
if strcmp(resolucion,'10bit')
valueToWrite = bin2dec('00000010');
fwrite(i2cInterface, [dataFormatRegisterAddress valueToWrite]);
factor_de_conversion = 0.0156;
factor_de_resolucion = 6;
else
valueToWrite = bin2dec('00001010');
fwrite(i2cInterface, [dataFormatRegisterAddress valueToWrite]);
factor_de_conversion = 0.0039;
factor_de_resolucion = 4;
end
case 16
if strcmp(resolucion,'10bit')
valueToWrite = bin2dec('00000011');
fwrite(i2cInterface, [dataFormatRegisterAddress valueToWrite]);
factor_de_conversion = 0.0312;
factor_de_resolucion = 6;
else
valueToWrite = bin2dec('00001011');
fwrite(i2cInterface, [dataFormatRegisterAddress valueToWrite]);
factor_de_conversion = 0.0039;
factor_de_resolucion = 5;
end
otherwise %default i put g_range 2g y resolucion 10bit
if strcmp(resolucion,'10bit')
valueToWrite = bin2dec('00000000');
fwrite(i2cInterface, [dataFormatRegisterAddress valueToWrite]);
factor_de_conversion = 0.0039;
factor_de_resolucion = 6;
end
end
%PLOT
nW=acq_time*fs_I2C; %number of samples
for i=1:nW
% X
fwrite(i2cInterface, XregLow);
XregisterValueLow = fread(i2cInterface, 1, 'uint8');
fwrite(i2cInterface, XregHigh);
XregisterValueHigh = fread(i2cInterface, 1, 'uint8');
Xlow = dec2bin(XregisterValueLow);
Xhigh = dec2bin(XregisterValueHigh);
XhighT = num2str(Xhigh);
nBitsKeep = 8-(factor_de_resolucion);
if length(XhighT)>nBitsKeep
XhighT = XhighT(end-nBitsKeep+1:end);
end
Xhigh = str2num(XhighT);
% Y
fwrite(i2cInterface, YregLow);
YregisterValueLow = fread(i2cInterface, 1, 'uint8');
fwrite(i2cInterface, YregHigh);
YregisterValueHigh = fread(i2cInterface, 1, 'uint8');
Ylow = dec2bin(XregisterValueLow);
Yhigh = dec2bin(XregisterValueHigh);
YhighT = num2str(Xhigh);
nBitsKeep = 8-(factor_de_resolucion); % Mirar mejor qué bits necesitamos del registro alto (depende resol??)
if length(YhighT)>nBitsKeep
YhighT = YhighT(end-nBitsKeep+1:end);
end
Yhigh = str2num(YhighT);
% Z
fwrite(i2cInterface, ZregLow);
ZregisterValueLow = fread(i2cInterface, 1, 'uint8');
fwrite(i2cInterface, ZregHigh);
ZregisterValueHigh = fread(i2cInterface, 1, 'uint8');
Zlow = dec2bin(XregisterValueLow);
Zhigh = dec2bin(XregisterValueHigh);
ZhighT = num2str(Xhigh);
nBitsKeep = 8-(factor_de_resolucion);
if length(ZhighT)>nBitsKeep
ZhighT = ZhighT(end-nBitsKeep+1:end);
end
Zhigh = str2num(ZhighT);
Xvalue(i)=XregisterValueHigh*(2^8)+XregisterValueLow;
Xvalueg(i)=Xvalue(i)*factor_de_conversion; % value in g
Xvalue(i)=Xvalueg(i)*9.81; % value in m/s^2 , 1g = 9.81m/s^2 ,
Yvalue(i)=YregisterValueHigh*(2^8)+ YregisterValueLow;
Yvalueg(i)=Yvalue(i)*factor_de_conversion; % value in g
Yvalue(i)=Yvalueg(i)*9.81; % value in m/s^2
Zvalue(i)=ZregisterValueHigh*(2^8)+ZregisterValueLow;
Zvalueg(i)=Zvalue(i)*factor_de_conversion;
Zvalue(i)=Zvalueg(i)*9.81; % value in m/s^2
% pause(1/fs);
end t=(0:length(Zvalue)-1)/fs_I2C;
%PLOT
figure
subplot(3,1,1); plot(t,Xvalueg); xlabel('tiempo (s)') ylabel('Acel x (g)')
subplot(3,1,2); plot(t,Yvalueg); xlabel('tiempo (s)') ylabel('Acel y (g)')
subplot(3,1,3); plot(t,Zvalueg); xlabel('tiempo (s)') ylabel('Acel z (g)')
delete(i2cInterface); clear('i2cInterface');
0 commentaires
Réponses (0)
Voir également
Catégories
En savoir plus sur Title dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!