极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 33296|回复: 12

求助:恳求arduino中用IIC读取MMA8452三轴加速度模块数据的范例。

[复制链接]
发表于 2012-5-5 15:56:06 | 显示全部楼层 |阅读模式
    我购买了一块MMA8452三轴加速度模块,IIC界面的,没有想到搜遍整个网络均未发现基于arduino中的范例,英文的说明书又看不懂,想参考ADXL345等模块的写法,瞎忙活了三、四天都以失败告终,恳请站内哪位大哥见义勇为一下。如能在关键之处标出注释,更加感激不尽。
    现附上模块图片与英文说明书。
   




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2012-5-6 15:27:28 | 显示全部楼层
自言自语一下。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-5-8 14:39:13 | 显示全部楼层
没有人愿意帮忙吗?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-5-16 09:18:59 | 显示全部楼层
来个好心人帮个忙吧。
回复 支持 反对

使用道具 举报

发表于 2012-5-16 09:27:26 | 显示全部楼层
你可以试试这个mma7455的库,看看是否可用。。。目前我只找到7455的。或者你根据datasheet,在7455基础上改改。。。http://code.google.com/p/mma-7455-arduino-library/
回复 支持 反对

使用道具 举报

发表于 2013-2-15 23:05:59 | 显示全部楼层
呵呵,老兄,我也在用这个,刚找到了代码,也测试了下,没问题。贴出来,给大家共享。
接线顺序可以参考代码里面的注释。
  1. /*
  2. MMA8452Q Basic Example Code
  3. Nathan Seidle
  4. SparkFun Electronics
  5. November 5, 2012

  6. License: This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).

  7. This example code shows how to read the X/Y/Z accelerations and basic functions of the MMA5842. It leaves out
  8. all the neat features this IC is capable of (tap, orientation, and inerrupts) and just displays X/Y/Z. See
  9. the advanced example code to see more features.

  10. Hardware setup:
  11. MMA8452 Breakout ------------ Arduino
  12. 3.3V --------------------- 3.3V
  13. SDA -------^^(330)^^------- A4
  14. SCL -------^^(330)^^------- A5
  15. GND ---------------------- GND

  16. The MMA8452 is 3.3V so we recommend using 330 or 1k resistors between a 5V Arduino and the MMA8452 breakout.

  17. The MMA8452 has built in pull-up resistors for I2C so you do not need additional pull-ups.
  18. */

  19. #include <Wire.h> // Used for I2C

  20. // The SparkFun breakout board defaults to 1, set to 0 if SA0 jumper on the bottom of the board is set
  21. #define MMA8452_ADDRESS 0x1D  // 0x1D if SA0 is high, 0x1C if low

  22. //Define a few of the registers that we will be accessing on the MMA8452
  23. #define OUT_X_MSB 0x01
  24. #define XYZ_DATA_CFG  0x0E
  25. #define WHO_AM_I   0x0D
  26. #define CTRL_REG1  0x2A

  27. #define GSCALE 2 // Sets full-scale range to +/-2, 4, or 8g. Used to calc real g values.

  28. void setup()
  29. {
  30.   Serial.begin(9600);
  31.   Serial.println("MMA8452 Basic Example");

  32.   Wire.begin(); //Join the bus as a master

  33.   initMMA8452(); //Test and intialize the MMA8452
  34. }

  35. void loop()
  36. {  
  37.   int accelCount[3];  // Stores the 12-bit signed value
  38.   readAccelData(accelCount);  // Read the x/y/z adc values

  39.   // Now we'll calculate the accleration value into actual g's
  40.   float accelG[3];  // Stores the real accel value in g's
  41.   for (int i = 0 ; i < 3 ; i++)
  42.   {
  43.     accelG[i] = (float) accelCount[i] / ((1<<12)/(2*GSCALE));  // get actual g value, this depends on scale being set
  44.   }

  45.   // Print out values
  46.   for (int i = 0 ; i < 3 ; i++)
  47.   {
  48.     Serial.print(accelG[i], 4);  // Print g values
  49.     Serial.print("\t");  // tabs in between axes
  50.   }
  51.   Serial.println();

  52.   delay(10);  // Delay here for visibility
  53. }

  54. void readAccelData(int *destination)
  55. {
  56.   byte rawData[6];  // x/y/z accel register data stored here

  57.   readRegisters(OUT_X_MSB, 6, rawData);  // Read the six raw data registers into data array

  58.   // Loop to calculate 12-bit ADC and g value for each axis
  59.   for(int i = 0; i < 3 ; i++)
  60.   {
  61.     int gCount = (rawData[i*2] << 8) | rawData[(i*2)+1];  //Combine the two 8 bit registers into one 12-bit number
  62.     gCount >>= 4; //The registers are left align, here we right align the 12-bit integer

  63.     // If the number is negative, we have to make it so manually (no 12-bit data type)
  64.     if (rawData[i*2] > 0x7F)
  65.     {  
  66.       gCount = ~gCount + 1;
  67.       gCount *= -1;  // Transform into negative 2's complement #
  68.     }

  69.     destination[i] = gCount; //Record this gCount into the 3 int array
  70.   }
  71. }

  72. // Initialize the MMA8452 registers
  73. // See the many application notes for more info on setting all of these registers:
  74. // http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=MMA8452Q
  75. void initMMA8452()
  76. {
  77.   byte c = readRegister(WHO_AM_I);  // Read WHO_AM_I register
  78.   if (c == 0x2A) // WHO_AM_I should always be 0x2A
  79.   {  
  80.     Serial.println("MMA8452Q is online...");
  81.   }
  82.   else
  83.   {
  84.     Serial.print("Could not connect to MMA8452Q: 0x");
  85.     Serial.println(c, HEX);
  86.     while(1) ; // Loop forever if communication doesn't happen
  87.   }

  88.   MMA8452Standby();  // Must be in standby to change registers

  89.   // Set up the full scale range to 2, 4, or 8g.
  90.   byte fsr = GSCALE;
  91.   if(fsr > 8) fsr = 8; //Easy error check
  92.   fsr >>= 2; // Neat trick, see page 22. 00 = 2G, 01 = 4A, 10 = 8G
  93.   writeRegister(XYZ_DATA_CFG, fsr);

  94.   //The default data rate is 800Hz and we don't modify it in this example code

  95.   MMA8452Active();  // Set to active to start reading
  96. }

  97. // Sets the MMA8452 to standby mode. It must be in standby to change most register settings
  98. void MMA8452Standby()
  99. {
  100.   byte c = readRegister(CTRL_REG1);
  101.   writeRegister(CTRL_REG1, c & ~(0x01)); //Clear the active bit to go into standby
  102. }

  103. // Sets the MMA8452 to active mode. Needs to be in this mode to output data
  104. void MMA8452Active()
  105. {
  106.   byte c = readRegister(CTRL_REG1);
  107.   writeRegister(CTRL_REG1, c | 0x01); //Set the active bit to begin detection
  108. }

  109. // Read bytesToRead sequentially, starting at addressToRead into the dest byte array
  110. void readRegisters(byte addressToRead, int bytesToRead, byte * dest)
  111. {
  112.   Wire.beginTransmission(MMA8452_ADDRESS);
  113.   Wire.write(addressToRead);
  114.   Wire.endTransmission(false); //endTransmission but keep the connection active

  115.   Wire.requestFrom(MMA8452_ADDRESS, bytesToRead); //Ask for bytes, once done, bus is released by default

  116.   while(Wire.available() < bytesToRead); //Hang out until we get the # of bytes we expect

  117.   for(int x = 0 ; x < bytesToRead ; x++)
  118.     dest[x] = Wire.read();   
  119. }

  120. // Read a single byte from addressToRead and return it as a byte
  121. byte readRegister(byte addressToRead)
  122. {
  123.   Wire.beginTransmission(MMA8452_ADDRESS);
  124.   Wire.write(addressToRead);
  125.   Wire.endTransmission(false); //endTransmission but keep the connection active

  126.   Wire.requestFrom(MMA8452_ADDRESS, 1); //Ask for 1 byte, once done, bus is released by default

  127.   while(!Wire.available()) ; //Wait for the data to come back
  128.   return Wire.read(); //Return this one byte
  129. }

  130. // Writes a single byte (dataToWrite) into addressToWrite
  131. void writeRegister(byte addressToWrite, byte dataToWrite)
  132. {
  133.   Wire.beginTransmission(MMA8452_ADDRESS);
  134.   Wire.write(addressToWrite);
  135.   Wire.write(dataToWrite);
  136.   Wire.endTransmission(); //Stop transmitting
  137. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2013-3-14 21:17:00 | 显示全部楼层
我也被同样问题困扰多时!
回复 支持 反对

使用道具 举报

发表于 2014-5-8 20:08:15 | 显示全部楼层
wisepi 发表于 2013-2-15 23:05
呵呵,老兄,我也在用这个,刚找到了代码,也测试了下,没问题。贴出来,给大家共享。
接线顺序可以参考代 ...

这个看起来是可以的。好人!
回复 支持 反对

使用道具 举报

发表于 2014-7-26 22:02:16 | 显示全部楼层
wisepi 发表于 2013-2-15 23:05
呵呵,老兄,我也在用这个,刚找到了代码,也测试了下,没问题。贴出来,给大家共享。
接线顺序可以参考代 ...

本人菜鸟,刚好碰到有一个mma8452,sa0接了高电平,我今天试了那代码,怎么不行啊,通过串口,好像没有进入initMMA8452()函数吗,求大神解答。
回复 支持 反对

使用道具 举报

发表于 2014-7-29 15:42:40 | 显示全部楼层
这程序可以用,本人亲试,谢谢大神分享。
回复 支持 反对

使用道具 举报

发表于 2015-8-10 10:32:23 | 显示全部楼层
wisepi 发表于 2013-2-15 23:05
**** 作者被禁止或删除 内容自动屏蔽 ****

请问线怎么接的?
回复 支持 反对

使用道具 举报

发表于 2015-8-10 10:41:59 | 显示全部楼层
wisepi 发表于 2013-2-15 23:05
**** 作者被禁止或删除 内容自动屏蔽 ****

请问你的线是怎么接的》为什么我的不行
回复 支持 反对

使用道具 举报

发表于 2015-9-23 10:18:25 | 显示全部楼层
你好,之前在网上见你用arduino与MMA8452Q通讯,我用的那个例程卡在 读who am i 不知道您当时怎么调试的呢?望能解答,谢谢
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-6 00:20 , Processed in 0.042974 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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