如何使用粒子光子将MCP3428 ADC设置为16位模式

时间:2019-07-16 20:05:54

标签: adc particle-photon

我正试图将MCP3428 ADC与粒子光子一起使用,以读取16位分辨率的电压。我已经从GitHub上的ControlEverythingCommunity刷新了示例MCP3428.ino代码(以下代码),并且它以12位分辨率模式工作。我想知道要更改为以16位模式运行MCP3428 ADC所需的内容。

#include <application.h>
#include <spark_wiring_i2c.h>

// MCP3428 I2C address is 0x68(104)
#define Addr 0x68

int raw_adc = 0;
void setup() 
{
// Set variable
Particle.variable("i2cdevice", "MCP3428");
Particle.variable("rawADC", raw_adc);

// Initialise I2C communication as MASTER
Wire.begin();
// Start serial communication and set baud rate = 9600
Serial.begin(9600);

// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select configuration command
// Continuous conversion mode, Channel-1, 12-bit resolution
Wire.write(0x10);
// Stop I2C Transmission
Wire.endTransmission();
}

void loop()
{
unsigned int data[2];

// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select data register
Wire.write(0x00);
// Stop I2C Transmission
Wire.endTransmission();

// Request 2 bytes of data
Wire.requestFrom(Addr,2);

// Read 2 bytes of data
// raw_adc msb, raw_adc lsb
if(Wire.available() == 2)
{
    data[0] = Wire.read();
    data[1] = Wire.read();
}

// Convert the data to 12-bits
int raw_adc = (data[0] & 0x0F) * 256 + data[1];
if(raw_adc > 2047)
{
  raw_adc -= 4095;
}

// Output data to dashboard
Particle.publish("Digital value of Analog Input : ", String(raw_adc));
delay(1000);
}

0 个答案:

没有答案