Main Content

使用ni USB 8451适配器使用ADXL345加速度计芯片进行TAP检测

此示例显示了如何使用Ni®USB8451 I2C适配器从ADXL345 I2C启用加速度计芯片中编写和读取数据。加速度计将配置为检测双击,MATLAB®将用于显示芯片已检测到的消息。

仪表控制工具箱™支持通过接口和驱动程序与仪器进行通信。金宝app

For a complete list of supported hardware, visit the Instrument Control Toolboxproduct page

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.

该示例使用AnalogDevices®ADXL345加速度计,该加速度计安装在SparkFun™的9DOF传感器棒上。加速度计具有可以连接到适配器I2C总线线输入的I2C总线线路。由于NI USB 8451没有可编程的内拉电阻器,因此需要用外部拔下电阻将公交线抬高。请注意,液位变速器用于分离V_S和V_DD。

Introduction

仪表控制工具箱™支持通过I2C适配器(例如Ni USB 金宝app8451)与I2C设备进行通信。该工具箱允许您创建一个I2C接口,该接口可用于配置适配器以与I2C外围芯片进行通信。

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.

验证NI845X安装

使用乐器命令检查是否正确安装了NI845X驱动程序,并且该仪器控制工具箱可以正确检测到它。

董事会序列号应帮助您识别设备

instrreset i2cinfo = instrhwinfo('i2c',,,,'ni845x');disp(i2cInfo);
adaptorDlllname:[1x94 char] adaptorDlllllllllversion:'版本3.4'adaptorName:'ni845x'boardIdsInuse:[1x0 double] installed hosted dobledboards:0检测boardserials:{'0180d47a);'vendordllname:'ni845x.dll'vendordriverDescription:“国家仪器NI USB 845X驱动程序”

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

accelerometerAddress = hex2dec('53');i2cinterface = i2c('ni845x',0,加速度计);fopen(i2Cinterface);i2cinterface.bitrate = 100;

设置TAP阈值,第二点延迟,第二点TAP窗口和TAP持续时间寄存器值

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值disp([[“写作价值:”num2str(valueTowrite)]);fwrite(i2Cinterface,[threshtapregisteraddress valuetOwrite]);fwrite(i2Cinterface,threshTapRegisterAddress);registerValue = fread(i2Cinterface,1,'uint8');disp([['Thresh_tap寄存器的值是:'num2str(registerValue)]);latentRegisterAddress = hex2dec('22');valueToWrite = hex2dec ('5');disp([[“写作价值:”num2str(valueTowrite)]);fwrite(i2cInterface, [latentRegisterAddress valueToWrite]);%确认价值返回到寄存器fwrite(i2cInterface, latentRegisterAddress); registerValue = fread(i2cInterface, 1,'uint8');disp([[“潜在寄存器的价值是:”num2str(registerValue)]);WindowRegisterAddress = hex2dec('23');valueToWrite = hex2dec ('FF');disp([[“写作价值:”num2str(valueTowrite)]);fwrite(i2cInterface, [windowRegisterAddress valueToWrite]);%确认价值返回到寄存器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([[“写作价值:”num2str(valueTowrite)]);fwrite(i2Cinterface,[durationRegisterAddress valuetOwrite]);fwrite(i2Cinterface,durationRegisterAddress);registerValue = fread(i2Cinterface,1,'uint8');disp([[“ DUR寄存器的价值为:”num2str(registerValue)]);tapaxesRegisterAddress = hex2dec('2a');valueToWrite = bin2dec('00000111');disp([[“写作价值:”num2str(valueTowrite)]);fwrite(i2Cinterface,[tapaxesRegisterAddress valuetWrite]);fwrite(i2 cinterface,tapaxesRegisterAddress);registerValue = fread(i2Cinterface,1,'uint8');disp([['The value of the TAP_AXES register is :'num2str(registerValue)]);InterruptEnableregisterAddress = hex2dec('2e');valueToWrite = bin2dec('01100000');disp([[“写作价值:”num2str(valueTowrite)]);fwrite(i2Cinterface,[InterruptEnableRegisterAddress valuetOwrite]);fwrite(i2 -cinterface,intrupteNablEregisterAddress);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

通过写入power_ctl寄存器来启用操作

根据数据表编写到power_ctl寄存器将导致芯片从待机模式转变为正常操作模式。

powerControlregisterAddress = hex2dec('2d');valueToWrite = bin2dec('00001000');disp([[“写作价值:”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)]);
写作值:8 power_ctl寄存器的值是:8

调查中断登记册

中断源寄存器将包含与特定源生成的中断标志相对应的位。检查是否生成双击中断

interruptSourceRegisterAddress = hex2dec('30');disp('Waiting for double tap...');while(1) fwrite(i2cInterface, interruptSourceRegisterAddress); InterruptValues = fread(i2cInterface, 1); TapInterrupt = bitand(InterruptValues, bin2dec('00100000'));ifTapInter Rupt Disp(“双击检测到!”);break;结尾结尾
Waiting for double tap... Double tap detected!