使用XIAO esp32c3 利用 I2c 读取FDC2214的数据,希望可以给出完整的代码(arduino IDE)
9条回答 默认 最新
dadasddaaadasd 2023-10-01 12:55关注#include <Wire.h> #include "Arduino.h" #define FDC2214_ADDR 0x2B // FDC2214的地址 #define FDC2214_REG_MEAS_CONFIG 0x1A // 测量配置寄存器地址 #define FDC2214_REG_MEAS_MSB 0x00 // 测量结果MSB寄存器地址 #define FDC2214_REG_MEAS_LSB 0x01 // 测量结果LSB寄存器地址 float data; void setup() { Serial.begin(9600); // 初始化串口 Wire.begin(); // 初始化I2C byte config1 = 0x18; // 配置寄存器的值 byte config2 = 0x33; // 配置寄存器的值 writeRegister(FDC2214_REG_MEAS_CONFIG, config1, config2); // 写入配置寄存器 } void loop() { uint32_t data = readRegister(FDC2214_REG_MEAS_MSB, FDC2214_REG_MEAS_LSB); // 读取测量结果寄存器的值 Serial.print("Capacitance: "); Serial.print(data); Serial.println(" pF"); delay(1000); // 延时1秒 } void writeRegister(byte reg, byte config1, byte config2) { Wire.beginTransmission(FDC2214_ADDR); // 开始传输 Wire.write(reg); // 写入寄存器地址 Wire.write(config1); Wire.write(config2); Wire.endTransmission(); // 结束传输 } uint32_t readRegister(byte msbReg, byte lsbReg) { Wire.beginTransmission(FDC2214_ADDR); // 开始传输 Wire.write(msbReg); // 写入MSB寄存器地址 Wire.endTransmission(); // 结束传输 delay(10); Wire.requestFrom(FDC2214_ADDR, 4); // 请求读取2个字节的数据 uint32_t data = (Wire.read() << 24) |(Wire.read() << 16) |( Wire.read() << 8) | Wire.read();// 读取数据并合并成一个16位整数((Wire.read() << 16) | Wire.read()); return data; }已在arduino IDE运行成功
(这里SD接地)如果接高的话地址为0x2A本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报 编辑记录