介绍

ADXL335加速度计模块
加速度计是一种机电设备,可以测量g重力引起的加速度。
它可用于需要倾斜传感的应用中。
ADXL335测量沿X,Y和Z轴的加速度,并提供与这3个轴的加速度成比例的模拟电压输出。
微控制器可以通过使用ADC将它们转换为数字信号来处理这些电压。
连接图

ADXL335加速度计模块与MSP-EXP430G2 TI Launchpad连接
例
利用加速度计的模拟电压求出装置的横向移动和纵向移动,并在Energia串行监视器上显示。
谨慎行事: MSP-EXP430G2 TI Launchpad主板具有512字节的RAM,可轻松使用,尤其是在使用不同库时。有时您需要串行缓冲区足够大以包含所需的数据,并且您必须修改串行库的缓冲区大小。在做这些事情时,我们必须确保代码不会使用超过70%的RAM。这可能导致代码以不稳定的方式工作,有时运行良好并且在其他方面失败。
有些时候RAM使用率可能会超过70%并且代码将完全正常工作,并且即使RAM使用率为65%,代码也无法工作。
在这种情况下,可能需要对缓冲区大小和/或变量进行一些试验和错误。
程序
- #include <math.h>
- const int x_out = A3; /* connect x_out of module to A3 of TI Launchpad board */
- const int y_out = A4; /* connect y_out of module to A4 of TI Launchpad board */
- const int z_out = A5; /* connect z_out of module to A5 of TI Launchpad board */
-
- void setup() {
- Serial.begin(9600); /* Define baud rate for serial communication */
- }
-
- void loop() {
- int x_adc_value, y_adc_value, z_adc_value;
- double x_g_value, y_g_value, z_g_value;
- double roll, pitch, yaw;
- x_adc_value = analogRead(x_out); /* Digital value of voltage on x_out pin */
- y_adc_value = analogRead(y_out); /* Digital value of voltage on y_out pin */
- z_adc_value = analogRead(z_out); /* Digital value of voltage on z_out pin */
- Serial.print("x = ");
- Serial.print(x_adc_value);
- Serial.print("\t\t");
- Serial.print("y = ");
- Serial.print(y_adc_value);
- Serial.print("\t\t");
- Serial.print("z = ");
- Serial.print(z_adc_value);
- Serial.print("\t\t");
- //delay(100);
- x_g_value = ( ( ( (double)(x_adc_value * 3.3)/1024) - 1.65 ) / 0.330 ); /* Acceleration in x-direction in g units */
- y_g_value = ( ( ( (double)(y_adc_value * 3.3)/1024) - 1.65 ) / 0.330 ); /* Acceleration in y-direction in g units */
- z_g_value = ( ( ( (double)(z_adc_value * 3.3)/1024) - 1.80 ) / 0.330 ); /* Acceleration in z-direction in g units */
余下程序:
本教程完整程序下载: