MATLAB Examples

Tap detection with ADXL345 accelerometer chip using the NI USB 8451 adaptor

This example shows how to write and read data from the ADXL345 I2C enabled accelerometer chip using the NI USB 8451 I2C adaptor. The accelerometer will be configured to detect a double tap and MATLAB will be used to display a message that the chip has detected this.

Instrument Control Toolbox™ supports communication with instruments through interfaces and drivers.

For more details on the toolbox, visit the Instrument Control Toolbox product page.

Contents

Requirements

This example requires a Microsoft® Windows® system and NI845x driver 2.1.1 or higher installed. Make sure the Measurement & Automation Explorer recognizes the NI845x device before you use this example. In addition, ensure the Measurement & Automation Explorer™ is closed prior to accessing the device form MATLAB.

The example uses the Analog Devices® ADXL345 accelerometer that is mounted on a 9DOF Sensor Stick from SparkFun™. The accelerometer has I2C bus lines that can be connected to the adaptors I2C bus line inputs. The bus lines needs to be pulled up high with external pull up resistors ad the NI USB 8451 does not have programmable internal pullup resistors. Note that a level shifter is used to separate V_s and V_dd.

Introduction

Instrument Control Toolbox™ supports communication with I2C devices via I2C adaptors such as the NI USB 8451. The toolbox allows you to create an I2C interface that can be used to configure the adaptor to communicate with the I2C slave chips.

This example will demonstrate how to configure an I2C based accelerometer to respond to tapping the breadboard twice with your finger. When the double tap is detected, a message will be displayed in the MATLAB command window.

Verify NI845x Installation

Use the instrhwinfo command to check if the NI845x driver is installed correctly and that Instrument Control Toolbox can detect it correctly.

The board serial number should help you identify your device

clear all; close all; clc;
i2cInfo = instrhwinfo ('i2c', 'ni845x');
disp(i2cInfo);
             AdaptorDllName: [1x94 char]
          AdaptorDllVersion: 'Version 3.4'
                AdaptorName: 'ni845x'
              BoardIdsInUse: [1x0 double]
          InstalledBoardIDs: 0
       DetectedBoardSerials: {'0180D47A (BoardIndex: 0)'}
      ObjectConstructorName: 'i2c('ni845x', BoardIndex, RemoteAddress);'
              VendorDllName: 'Ni845x.dll'
    VendorDriverDescription: 'National Instruments NI USB 845x Driver'

Create the I2C Interface and set the bus speed (BitRate property)

accelerometerAddress = hex2dec('53');
i2cInterface = i2c('ni845x', 0, accelerometerAddress);
fopen(i2cInterface);
i2cInterface.BitRate = 100;

Set the tap threshold, second tap latency, second tap window and tap duration register values

Set the register values according to the datasheet of the device. Read the value back from the device to confirm that the value is indeed set.

threshTapRegisterAddress = hex2dec('1D');
valueToWrite = hex2dec('50'); %5g value
disp(['Writing Value: ' num2str(valueToWrite)]);
fwrite(i2cInterface, [threshTapRegisterAddress valueToWrite]);

fwrite(i2cInterface, threshTapRegisterAddress);
registerValue = fread(i2cInterface, 1, 'uint8');
disp(['The value of the THRESH_TAP register is :' num2str(registerValue)]);

latentRegisterAddress = hex2dec('22');
valueToWrite = hex2dec('5');
disp(['Writing Value: ' num2str(valueToWrite)]);
fwrite(i2cInterface, [latentRegisterAddress valueToWrite]);

% Confirm the value return to the register
fwrite(i2cInterface, latentRegisterAddress);
registerValue = fread(i2cInterface, 1, 'uint8');
disp(['The value of the LATENT register is :' num2str(registerValue)]);

windowRegisterAddress = hex2dec('23');
valueToWrite = hex2dec('FF');
disp(['Writing Value: ' num2str(valueToWrite)]);
fwrite(i2cInterface, [windowRegisterAddress valueToWrite]);

% Confirm the value return to the register
fwrite(i2cInterface, windowRegisterAddress);
registerValue = fread(i2cInterface, 1, 'uint8');
disp(['The value of the WINDOW register is :' num2str(registerValue)]);

durationRegisterAddress = hex2dec('21');
valueToWrite = hex2dec('10');
disp(['Writing Value: ' num2str(valueToWrite)]);
fwrite(i2cInterface, [durationRegisterAddress valueToWrite]);

fwrite(i2cInterface, durationRegisterAddress);
registerValue = fread(i2cInterface, 1, 'uint8');
disp(['The value of the DUR register is :' num2str(registerValue)]);

tapAxesRegisterAddress = hex2dec('2A');
valueToWrite = bin2dec('00000111');
disp(['Writing Value: ' num2str(valueToWrite)]);
fwrite(i2cInterface, [tapAxesRegisterAddress valueToWrite]);

fwrite(i2cInterface, tapAxesRegisterAddress);
registerValue = fread(i2cInterface, 1, 'uint8');
disp(['The value of the TAP_AXES register is :' num2str(registerValue)]);

interruptEnableRegisterAddress = hex2dec('2E');
valueToWrite = bin2dec('01100000');
disp(['Writing Value: ' num2str(valueToWrite)]);
fwrite(i2cInterface, [interruptEnableRegisterAddress valueToWrite]);

fwrite(i2cInterface, interruptEnableRegisterAddress);
registerValue = fread(i2cInterface, 1, 'uint8');
disp(['The value of the INT_ENABLE register is :' num2str(registerValue)]);
Writing Value: 80
The value of the THRESH_TAP register is :80
Writing Value: 5
The value of the LATENT register is :5
Writing Value: 255
The value of the WINDOW register is :255
Writing Value: 16
The value of the DUR register is :16
Writing Value: 7
The value of the TAP_AXES register is :7
Writing Value: 96
The value of the INT_ENABLE register is :96

Enable operation by writing to POWER_CTL register

Writing to the POWER_CTL register as per the datasheet will cause the chip to go from standby mode to normal operation mode.

powerControlRegisterAddress = hex2dec('2D');
valueToWrite = bin2dec('00001000');
disp(['Writing Value: ' num2str(valueToWrite)]);
fwrite(i2cInterface, [powerControlRegisterAddress valueToWrite]);

fwrite(i2cInterface, powerControlRegisterAddress);
registerValue = fread(i2cInterface, 1, 'uint8');
disp(['The value of the POWER_CTL register is :' num2str(registerValue)]);
Writing Value: 8
The value of the POWER_CTL register is :8

Poll the interrupt register

The interrupt source register will contain bits that correspond to interrupt flags being generated by specific sources. Check to see that the double tap interrupt is generated

interruptSourceRegisterAddress = hex2dec('30');

disp('Waiting for double tap...');
while(1)
   fwrite(i2cInterface, interruptSourceRegisterAddress);
   InterruptValues = fread(i2cInterface, 1);
   TapInterrupt = bitand(InterruptValues, bin2dec('00100000'));
   if TapInterrupt
       disp('Double tap detected!');
       break;
   end
end
Waiting for double tap...
Double tap detected!