如何在 RPI 3B+ 上使用 C++ 通过 i2c 从 MAX11613 芯片读取数据
How do I read data via i2c from a MAX11613 chip using C++ on a RPI 3B+
我正在尝试用 C++ 为 MAX11613 ADC 芯片 (MAX11613 Datasheet) 编写驱动程序。我想我已经为设置和配置编写了正确的代码,但是我在读取代码时遇到了一些问题。我将芯片设置为在单极模式下使用内部时钟和内部电压参考进行读取,然后写入配置以使用 AIN0 作为 + 信号并使用 AIN1 作为 - 信号通道进行扫描并报告差分读数。它似乎确实读取了数据,尽管数据看起来非常不稳定,而不是根据示波器结果预期的数据。
设置位://1111 0000=0xF0 SEL2=1,SEL1=1,SEL0=1,内部时钟,UNIPOLAR
配置位://0110 0000=0x60 SCAN1=1,SCAN0=1,AIN0-AIN1,差值
这是我阅读的代码,可能是问题的一部分:
static uint16_t readMAXRegister(uint8_t i2cAddress, uint8_t reg) {
unsigned char buff[16];
beginMAXTransmission(i2cAddress);
i2c_smbus_read_i2c_block_data(i2cMAXHandle, reg, 16, buff);
endMAXTransmission();
uint16_t res = (buff[1] << 8) + buff[0];
return res;
}
int16_t MAX11613::readMAXADC_Differential_0_1() {
// Write config register to the ADC
writeMAXRegister(m_i2cAddress, MAX_WRITE, MAX_CONFIG);
// Wait for the conversion to complete
usleep(m_conversionDelay);
// Read the conversion results
uint16_t res = readMAXRegister(m_i2cAddress, 1) >> m_bitShift;
// Shift 12-bit results right 4 bits
res = (res >> 11) == 0 ? res : -1 ^ 0xFFF | res;
std::bitset<12> y(res);
std::cout << "READ RESULT " << y << " " << res << std::endl;
return (int16_t)res;
}
经过更多检查,我现在似乎可以从设备读取数据了。对于可能感兴趣的任何其他人,这是有效的最终代码。
static void writeMAXRegister(uint8_t i2cAddress, uint8_t reg, uint8_t value) {
beginMAXTransmission(i2cAddress);
i2c_smbus_write_word_data(i2cMAXHandle, reg, payload);
endMAXTransmission();
uint8_t payload = value;
}
static uint16_t readMAXRegister(uint8_t i2cAddress, uint8_t reg) {
const uint8_t datalength = 2;
unsigned char data[datalength];
beginMAXTransmission(i2cAddress);
i2c_smbus_read_i2c_block_data(i2cMAXHandle, reg, datalength, data);
endMAXTransmission();
uint16_t res =((data[0]&0xF)<<8)+data[1];//<---THIS READS 16 BITS AND REMOVES FIRST 4 '1111' BITS OF DATA
return res;
}
我正在尝试用 C++ 为 MAX11613 ADC 芯片 (MAX11613 Datasheet) 编写驱动程序。我想我已经为设置和配置编写了正确的代码,但是我在读取代码时遇到了一些问题。我将芯片设置为在单极模式下使用内部时钟和内部电压参考进行读取,然后写入配置以使用 AIN0 作为 + 信号并使用 AIN1 作为 - 信号通道进行扫描并报告差分读数。它似乎确实读取了数据,尽管数据看起来非常不稳定,而不是根据示波器结果预期的数据。
设置位://1111 0000=0xF0 SEL2=1,SEL1=1,SEL0=1,内部时钟,UNIPOLAR
配置位://0110 0000=0x60 SCAN1=1,SCAN0=1,AIN0-AIN1,差值
这是我阅读的代码,可能是问题的一部分:
static uint16_t readMAXRegister(uint8_t i2cAddress, uint8_t reg) {
unsigned char buff[16];
beginMAXTransmission(i2cAddress);
i2c_smbus_read_i2c_block_data(i2cMAXHandle, reg, 16, buff);
endMAXTransmission();
uint16_t res = (buff[1] << 8) + buff[0];
return res;
}
int16_t MAX11613::readMAXADC_Differential_0_1() {
// Write config register to the ADC
writeMAXRegister(m_i2cAddress, MAX_WRITE, MAX_CONFIG);
// Wait for the conversion to complete
usleep(m_conversionDelay);
// Read the conversion results
uint16_t res = readMAXRegister(m_i2cAddress, 1) >> m_bitShift;
// Shift 12-bit results right 4 bits
res = (res >> 11) == 0 ? res : -1 ^ 0xFFF | res;
std::bitset<12> y(res);
std::cout << "READ RESULT " << y << " " << res << std::endl;
return (int16_t)res;
}
经过更多检查,我现在似乎可以从设备读取数据了。对于可能感兴趣的任何其他人,这是有效的最终代码。
static void writeMAXRegister(uint8_t i2cAddress, uint8_t reg, uint8_t value) {
beginMAXTransmission(i2cAddress);
i2c_smbus_write_word_data(i2cMAXHandle, reg, payload);
endMAXTransmission();
uint8_t payload = value;
}
static uint16_t readMAXRegister(uint8_t i2cAddress, uint8_t reg) {
const uint8_t datalength = 2;
unsigned char data[datalength];
beginMAXTransmission(i2cAddress);
i2c_smbus_read_i2c_block_data(i2cMAXHandle, reg, datalength, data);
endMAXTransmission();
uint16_t res =((data[0]&0xF)<<8)+data[1];//<---THIS READS 16 BITS AND REMOVES FIRST 4 '1111' BITS OF DATA
return res;
}