查看: 1733|回复: 0

[原创] 【DFR0004/RoMeo控制器试用】KX224 加速度传感器 I2C 测试

[复制链接]
  • TA的每日心情
    奋斗
    前天 09:33
  • 签到天数: 208 天

    连续签到: 2 天

    [LV.7]常住居民III

    发表于 2022-10-3 10:58:03 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 andeyqi 于 2022-10-3 15:07 编辑

    RoMeo 开发板上是支持I2C 功能的,原理图上有三个I2C外设接口,手头正好I2C 接口的KX224 加速度传感器,接上去验证下I2C 功能。
    i2c.png

    kx224_i2c.jpg


    关于RoMeo 开发板的I2C 接口使用说明可以参照如下链接说明在此就不做介绍了,接口封装的还是比较简洁的,了解I2C协议应该就理解起来也比较容易。


    以下是代码是实现初始化的时候设置加速度传感器的配置寄存器之后每500ms去赌气寄存器信息。
    1. /*
    2.   DigitalReadSerial

    3.   Reads a digital input on pin 2, prints the result to the Serial Monitor

    4.   This example code is in the public domain.

    5.   https://www.arduino.cc/en/Tutorial/BuiltInExamples/DigitalReadSerial
    6. */
    7. #include <Wire.h>

    8. #define KX224_DEVICE_ADDRESS_1E   (0x1E)    // 7bit Addrss
    9. #define KX224_DEVICE_ADDRESS_1F   (0x1F)    // 7bit Address
    10. #define KX224_WAI_VAL             (0x2B)

    11. #define KX224_XOUT_L              (0x06)
    12. #define KX224_WHO_AM_I            (0x0F)
    13. #define KX224_CNTL1               (0x18)
    14. #define KX224_ODCNTL              (0x1B)

    15. #define KX224_CNTL1_TPE           (1 << 0)
    16. #define KX224_CNTL1_WUFE          (1 << 1)
    17. #define KX224_CNTL1_TDTE          (1 << 2)
    18. #define KX224_CNTL1_GSELMASK      (0x18)
    19. #define KX224_CNTL1_GSEL_8G       (0x00)
    20. #define KX224_CNTL1_GSEL_16G      (0x08)
    21. #define KX224_CNTL1_GSEL_32G      (0x10)
    22. #define KX224_CNTL1_DRDYE         (1 << 5)
    23. #define KX224_CNTL1_RES           (1 << 6)
    24. #define KX224_CNTL1_PC1           (1 << 7)

    25. #define KX224_ODCNTL_OSA_50HZ     (2)
    26. #define KX224_ODCNTL_LPRO         (1 << 6)
    27. #define KX224_IIR_BYPASS          (1 << 7)

    28. #define KX224_CNTL1_VAL           (KX224_CNTL1_RES | KX224_CNTL1_GSEL_8G)
    29. #define KX224_ODCNTL_VAL          (KX224_ODCNTL_OSA_50HZ)

    30. unsigned short _g_sens = 4096;



    31. byte kx224_read(unsigned char memory_address, unsigned char *data, int size)
    32. {
    33.   byte rc;
    34.   unsigned char cnt;

    35.   Wire.beginTransmission(KX224_DEVICE_ADDRESS_1E);
    36.   Wire.write(memory_address);
    37.   rc = Wire.endTransmission(false);
    38.   if (rc != 0) {
    39.     return (rc);
    40.   }

    41.   Wire.requestFrom(KX224_DEVICE_ADDRESS_1E, size, true);
    42.   cnt = 0;
    43.   while(Wire.available()) {
    44.     data[cnt] = Wire.read();
    45.     cnt++;
    46.   }

    47.   return (0);
    48. }

    49. byte kx224_write(unsigned char memory_address, unsigned char *data, unsigned char size)
    50. {
    51.   byte rc;

    52.   Wire.beginTransmission(KX224_DEVICE_ADDRESS_1E);
    53.   Wire.write(memory_address);
    54.   Wire.write(data, size);
    55.   rc = Wire.endTransmission(true);
    56.   return (rc);
    57. }

    58. byte kx224_get_rawval(unsigned char *data)
    59. {
    60.   byte rc;

    61.   rc = kx224_read(KX224_XOUT_L, data, 6);
    62.   if (rc != 0) {
    63.     Serial.println(F("Can't get KX224 accel value"));
    64.   }

    65.   return (rc);
    66. }

    67. byte kx224_get_val(float *data)
    68. {
    69.   byte rc;
    70.   unsigned char val[6];
    71.   signed short acc[3];

    72.   rc = kx224_get_rawval(val);
    73.   if (rc != 0) {
    74.     return (rc);
    75.   }

    76.   acc[0] = ((signed short)val[1] << 8) | (val[0]);
    77.   acc[1] = ((signed short)val[3] << 8) | (val[2]);
    78.   acc[2] = ((signed short)val[5] << 8) | (val[4]);

    79.   // Convert LSB to g
    80.   data[0] = (float)acc[0] / _g_sens;
    81.   data[1] = (float)acc[1] / _g_sens;
    82.   data[2] = (float)acc[2] / _g_sens;

    83.   return (rc);  
    84. }


    85. // the setup routine runs once when you press reset:
    86. void setup() {
    87.   unsigned char reg = 0;
    88.   unsigned char rc = 0;
    89.   unsigned char gsel;

    90.   // initialize serial communication at 9600 bits per second:
    91.   Serial.begin(115200);
    92.   Wire.begin();
    93.   rc = kx224_read(KX224_WHO_AM_I, ®, sizeof(reg));
    94.   if (rc != 0) {
    95.     Serial.println(F("Can't access KX224"));
    96.     Serial.println(rc);
    97.   }
    98.   Serial.print(F("KX224_WHO_AMI Register Value = 0x"));
    99.   Serial.println(reg, HEX);

    100.   if (reg != KX224_WAI_VAL) {
    101.     Serial.println(F("Can't find KX224"));
    102.     return;
    103.   }

    104.   reg = KX224_ODCNTL_VAL;
    105.   rc = kx224_write(KX224_ODCNTL, ®, sizeof(reg));
    106.   if (rc != 0) {
    107.     Serial.println("Can't write KX224 ODCNTL register");
    108.     return;
    109.   }

    110.   rc = kx224_read(KX224_CNTL1, ®, sizeof(reg));
    111.   if (rc != 0) {
    112.     Serial.println(F("Can't read KX224 CNTL1 register"));
    113.     return;
    114.   }

    115.   gsel = reg & KX224_CNTL1_GSELMASK;

    116.   reg |= KX224_CNTL1_PC1;
    117.   rc = kx224_write(KX224_CNTL1, ®, sizeof(reg));
    118.   if (rc != 0) {
    119.     Serial.println(F("Can't write KX224 CNTL1 register at second"));
    120.     return;
    121.   }
    122.   switch(gsel) {
    123.     case KX224_CNTL1_GSEL_8G :
    124.       // (Equivalent counts) / (Range) = (32768 / 8)
    125.       _g_sens = 4096;
    126.     break;

    127.     case KX224_CNTL1_GSEL_16G :
    128.       // (Equivalent counts) / (Range) = (32768 / 16)
    129.       _g_sens = 2048;
    130.     break;

    131.     case KX224_CNTL1_GSEL_32G :
    132.       // (Equivalent counts) / (Range) = (32768 / 32)
    133.       _g_sens = 1024;
    134.     break;

    135.     default:
    136.     break;
    137.   }
    138. }

    139. // the loop routine runs over and over again forever:
    140. void loop() {
    141.   byte rc;
    142.   float acc[3];

    143.   rc = kx224_get_val(acc);
    144.   if (rc == 0) {
    145.     Serial.write("KX224 (X) = ");
    146.     Serial.print(acc[0]);
    147.     Serial.println(" [g]");
    148.     Serial.write("KX224 (Y) = ");
    149.     Serial.print(acc[1]);
    150.     Serial.println(" [g]");
    151.     Serial.write("KX224 (Z) = ");
    152.     Serial.print(acc[2]);
    153.     Serial.println(" [g]");
    154.     Serial.println();
    155.   }

    156.   delay(500);
    157. }
    复制代码
    以下是测试数据翻转传感器可以读取到传感器的信息根据状态在变化。

    i2c_result.png

    ==================资料分割线==================
    KX224-1053-Specifications-Rev-2.0.pdf (2.47 MB, 下载次数: 1)
    回复

    使用道具 举报

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

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-11-23 18:48 , Processed in 0.116662 second(s), 16 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.