本帖最后由 andeyqi 于 2022-10-3 15:07 编辑
RoMeo 开发板上是支持I2C 功能的,原理图上有三个I2C外设接口,手头正好I2C 接口的KX224 加速度传感器,接上去验证下I2C 功能。
关于RoMeo 开发板的I2C 接口使用说明可以参照如下链接说明在此就不做介绍了,接口封装的还是比较简洁的,了解I2C协议应该就理解起来也比较容易。
以下是代码是实现初始化的时候设置加速度传感器的配置寄存器之后每500ms去赌气寄存器信息。 - /*
- DigitalReadSerial
- Reads a digital input on pin 2, prints the result to the Serial Monitor
- This example code is in the public domain.
- https://www.arduino.cc/en/Tutorial/BuiltInExamples/DigitalReadSerial
- */
- #include <Wire.h>
- #define KX224_DEVICE_ADDRESS_1E (0x1E) // 7bit Addrss
- #define KX224_DEVICE_ADDRESS_1F (0x1F) // 7bit Address
- #define KX224_WAI_VAL (0x2B)
- #define KX224_XOUT_L (0x06)
- #define KX224_WHO_AM_I (0x0F)
- #define KX224_CNTL1 (0x18)
- #define KX224_ODCNTL (0x1B)
- #define KX224_CNTL1_TPE (1 << 0)
- #define KX224_CNTL1_WUFE (1 << 1)
- #define KX224_CNTL1_TDTE (1 << 2)
- #define KX224_CNTL1_GSELMASK (0x18)
- #define KX224_CNTL1_GSEL_8G (0x00)
- #define KX224_CNTL1_GSEL_16G (0x08)
- #define KX224_CNTL1_GSEL_32G (0x10)
- #define KX224_CNTL1_DRDYE (1 << 5)
- #define KX224_CNTL1_RES (1 << 6)
- #define KX224_CNTL1_PC1 (1 << 7)
- #define KX224_ODCNTL_OSA_50HZ (2)
- #define KX224_ODCNTL_LPRO (1 << 6)
- #define KX224_IIR_BYPASS (1 << 7)
- #define KX224_CNTL1_VAL (KX224_CNTL1_RES | KX224_CNTL1_GSEL_8G)
- #define KX224_ODCNTL_VAL (KX224_ODCNTL_OSA_50HZ)
- unsigned short _g_sens = 4096;
- byte kx224_read(unsigned char memory_address, unsigned char *data, int size)
- {
- byte rc;
- unsigned char cnt;
- Wire.beginTransmission(KX224_DEVICE_ADDRESS_1E);
- Wire.write(memory_address);
- rc = Wire.endTransmission(false);
- if (rc != 0) {
- return (rc);
- }
- Wire.requestFrom(KX224_DEVICE_ADDRESS_1E, size, true);
- cnt = 0;
- while(Wire.available()) {
- data[cnt] = Wire.read();
- cnt++;
- }
- return (0);
- }
- byte kx224_write(unsigned char memory_address, unsigned char *data, unsigned char size)
- {
- byte rc;
- Wire.beginTransmission(KX224_DEVICE_ADDRESS_1E);
- Wire.write(memory_address);
- Wire.write(data, size);
- rc = Wire.endTransmission(true);
- return (rc);
- }
- byte kx224_get_rawval(unsigned char *data)
- {
- byte rc;
- rc = kx224_read(KX224_XOUT_L, data, 6);
- if (rc != 0) {
- Serial.println(F("Can't get KX224 accel value"));
- }
- return (rc);
- }
- byte kx224_get_val(float *data)
- {
- byte rc;
- unsigned char val[6];
- signed short acc[3];
- rc = kx224_get_rawval(val);
- if (rc != 0) {
- return (rc);
- }
- acc[0] = ((signed short)val[1] << 8) | (val[0]);
- acc[1] = ((signed short)val[3] << 8) | (val[2]);
- acc[2] = ((signed short)val[5] << 8) | (val[4]);
- // Convert LSB to g
- data[0] = (float)acc[0] / _g_sens;
- data[1] = (float)acc[1] / _g_sens;
- data[2] = (float)acc[2] / _g_sens;
- return (rc);
- }
- // the setup routine runs once when you press reset:
- void setup() {
- unsigned char reg = 0;
- unsigned char rc = 0;
- unsigned char gsel;
- // initialize serial communication at 9600 bits per second:
- Serial.begin(115200);
- Wire.begin();
- rc = kx224_read(KX224_WHO_AM_I, ®, sizeof(reg));
- if (rc != 0) {
- Serial.println(F("Can't access KX224"));
- Serial.println(rc);
- }
- Serial.print(F("KX224_WHO_AMI Register Value = 0x"));
- Serial.println(reg, HEX);
- if (reg != KX224_WAI_VAL) {
- Serial.println(F("Can't find KX224"));
- return;
- }
- reg = KX224_ODCNTL_VAL;
- rc = kx224_write(KX224_ODCNTL, ®, sizeof(reg));
- if (rc != 0) {
- Serial.println("Can't write KX224 ODCNTL register");
- return;
- }
- rc = kx224_read(KX224_CNTL1, ®, sizeof(reg));
- if (rc != 0) {
- Serial.println(F("Can't read KX224 CNTL1 register"));
- return;
- }
- gsel = reg & KX224_CNTL1_GSELMASK;
- reg |= KX224_CNTL1_PC1;
- rc = kx224_write(KX224_CNTL1, ®, sizeof(reg));
- if (rc != 0) {
- Serial.println(F("Can't write KX224 CNTL1 register at second"));
- return;
- }
- switch(gsel) {
- case KX224_CNTL1_GSEL_8G :
- // (Equivalent counts) / (Range) = (32768 / 8)
- _g_sens = 4096;
- break;
- case KX224_CNTL1_GSEL_16G :
- // (Equivalent counts) / (Range) = (32768 / 16)
- _g_sens = 2048;
- break;
- case KX224_CNTL1_GSEL_32G :
- // (Equivalent counts) / (Range) = (32768 / 32)
- _g_sens = 1024;
- break;
- default:
- break;
- }
- }
- // the loop routine runs over and over again forever:
- void loop() {
- byte rc;
- float acc[3];
- rc = kx224_get_val(acc);
- if (rc == 0) {
- Serial.write("KX224 (X) = ");
- Serial.print(acc[0]);
- Serial.println(" [g]");
- Serial.write("KX224 (Y) = ");
- Serial.print(acc[1]);
- Serial.println(" [g]");
- Serial.write("KX224 (Z) = ");
- Serial.print(acc[2]);
- Serial.println(" [g]");
- Serial.println();
- }
-
- delay(500);
- }
复制代码以下是测试数据翻转传感器可以读取到传感器的信息根据状态在变化。
==================资料分割线================== |