极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9215|回复: 3

提高三軸 ADXL345 速率

[复制链接]
发表于 2014-4-27 11:13:36 | 显示全部楼层 |阅读模式
我想要讓輸出資料達到每秒2000筆的資料
可是就算使用3200hz 的速率 每秒就只能輸出55筆資料
請問我要如何提高採集速率或是輸出速率 謝謝

  1. #include <Wire.h>

  2. #define DEVICE (0x53)    //ADXL345 device address
  3. #define TO_READ (6)        //num of bytes we are going to read each time (two bytes for each axis)

  4. byte buff[TO_READ] ;    //6 bytes buffer for saving data read from the device
  5. char str[512];                      //string buffer to transform data before sending it to the serial port
  6. int a=0;
  7. void setup()
  8. {
  9.   Wire.begin();        // join i2c bus (address optional for master)
  10.   Serial.begin(9600);  // start serial for output
  11.   
  12.   //Turning on the ADXL345
  13.   writeTo(DEVICE, 0x2D, 0);      
  14.   writeTo(DEVICE, 0x2D, 16);
  15.   writeTo(DEVICE, 0x2D, 8);
  16.   writeTo(DEVICE, 0x2C, 15);
  17. }

  18. void loop()
  19. {
  20.   
  21.   int regAddress = 0x32;    //first axis-acceleration-data register on the ADXL345
  22.   int x, y, z;

  23.   readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
  24.   
  25.    //each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
  26.    //thus we are converting both bytes in to one int
  27.   x = (((int)buff[1]) << 8) | buff[0];   
  28.   y = (((int)buff[3])<< 8) | buff[2];
  29.   z = (((int)buff[5]) << 8) | buff[4];
  30.   
  31.   //we send the x y z values as a string to the serial port
  32.   sprintf(str, "%d %d %d", x, y, z);
  33.   a=a+1;
  34.   Serial.println(a);
  35.   Serial.println(millis());
  36. //Serial.println(str);
  37. // Serial.write(10);
  38.   
  39.   //It appears that delay is needed in order not to clog the port
  40.   delay(15);
  41. }

  42. //---------------- Functions
  43. //Writes val to address register on device
  44. void writeTo(int device, byte address, byte val) {
  45.    Wire.beginTransmission(device); //start transmission to device
  46.    Wire.write(address);        // send register address
  47.    Wire.write(val);        // send value to write
  48.    Wire.endTransmission(); //end transmission
  49. }

  50. //reads num bytes starting from address register on device in to buff array
  51. void readFrom(int device, byte address, int num, byte buff[]) {
  52.   Wire.beginTransmission(device); //start transmission to device
  53.   Wire.write(address);        //sends address to read from
  54.   Wire.endTransmission(); //end transmission
  55.   
  56.   Wire.beginTransmission(device); //start transmission to device
  57.   Wire.requestFrom(device, num);    // request 6 bytes from device
  58.   
  59.   int i = 0;
  60.   while(Wire.available())    //device may send less than requested (abnormal)
  61.   {
  62.     buff[i] = Wire.read(); // receive a byte
  63.     i++;
  64.   }
  65.   Wire.endTransmission(); //end transmission
  66. }
复制代码
回复

使用道具 举报

发表于 2014-4-27 11:57:10 | 显示全部楼层
LZ你有点搞晕了!
把345设置的输出率很高3.2k,它有能力输出,但你的程序现在没有这么快的速度去读的:先不说读345寄存器要不要消耗时间,就光一个delay(15);,每次延时15ms,这个语句就控制你程序1秒钟只能读66.6666……次的,肯定不会有你想要的结果
还有,你设置波特率是9600,也就是1s传输1200byte,再按照你的程序设计,3轴数据每次6个字节,还有变量a,millis(),这样加起来每次要传10多个的字节,算下来1s也就能传输100次左右数据的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-4-27 12:39:41 | 显示全部楼层
I-robofan 发表于 2014-4-27 11:57
LZ你有点搞晕了!
把345设置的输出率很高3.2k,它有能力输出,但你的程序现在没有这么快的速度去读的:先不 ...

感謝解答 原來如此~~ 那我提高了最高波特率115200 雖然數據有明顯提升到500筆 但是與預想還有差距
會不會跟COM本身限制有關 還是有什麼方法能夠在提高呢


謝謝
回复 支持 反对

使用道具 举报

发表于 2014-4-27 16:35:13 | 显示全部楼层
andysleep 发表于 2014-4-27 12:39
感謝解答 原來如此~~ 那我提高了最高波特率115200 雖然數據有明顯提升到500筆 但是與預想還有差距
會不會 ...

首先你要明白你要1s读2000次的目的是什么?测试什么极限,还是真的有需求,否则完全没必要1s读2000次的!
还有IIC是也有速度限制(100kHz,400kHz,或者其他的),程序还要跑其他代码,这些都需要时间消耗的,
不要去纠结1s读2000次了,完全没多大意义的
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-14 21:24 , Processed in 0.036948 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表