clear all clc close all list = ni845xlist; controllerList = ni845xlist; ni845Obj = ni845x(controllerList.SerialNumber(1)); accelerometerAddress = scanI2CBus(ni845Obj); adxlDevice = device(ni845Obj, 'I2CAddress', accelerometerAddress); DeviceidRegisterAddress = 0x00; % Enable data-ready interrupt (bit 7) in INT_ENABLE register (0x2E) writeRegister(adxlDevice, 0x2E, 0x80); % Map data-ready interrupt to INT1 (bit 0) in INT_MAP register (0x2F) writeRegister(adxlDevice, 0x2F, 0x00); % setting ODR and low-power mode writeRegister(adxlDevice, 0x2C, 0x1B); % data format writeRegister(adxlDevice, 0x31, 0x8); dataRegs = hex2dec('32'):hex2dec('37'); % Set the ADXL345 in measuring mode writeRegister(adxlDevice, 0x2D, 8); % Specify the number of samples to acquire numSamples = 1000; % Change this to the desired number of samples % Read acceleration data from ADXL345 sampleCount = 0; while sampleCount < numSamples % Check if data-ready interrupt is active intSource = readRegister(adxlDevice, 0x30); dataReady = bitget(intSource, 8); % Check if new data is ready if dataReady==1 % Read acceleration data accelerationData = readRegister(adxlDevice, 0x32, length(dataRegs), 'uint8'); % Convert the raw data to signed 16-bit integers xAccraw = typecast(uint16(accelerationData(1) + bitshift(accelerationData(2), 8)), 'int16'); yAccraw = typecast(uint16(accelerationData(3) + bitshift(accelerationData(4), 8)), 'int16'); zAccraw = typecast(uint16(accelerationData(5) + bitshift(accelerationData(6), 8)), 'int16'); % Scale the acceleration values scaleFactor = 3.9; % scale factor xAcc = xAccraw * scaleFactor; yAcc = yAccraw * scaleFactor; zAcc = zAccraw * scaleFactor; % Display the acceleration values fprintf('Acceleration - X: %.2f g, Y: %.2f g, Z: %.2f g\n', xAcc, yAcc, zAcc); sampleCount = sampleCount + 1; pause off; end end