I am using the Grove microphone and the Grove analog-to-digital converter to sample sound. The Grove platform documentation provides the following example C code to read data from the ADC that I am trying to port to Mathematica:
#include <Wire.h>
#define ADDR_ADC121 0x55
#define V_REF 3.00
#define REG_ADDR_RESULT 0x00
#define REG_ADDR_ALERT 0x01
#define REG_ADDR_CONFIG 0x02
#define REG_ADDR_LIMITL 0x03
#define REG_ADDR_LIMITH 0x04
#define REG_ADDR_HYST 0x05
#define REG_ADDR_CONVL 0x06
#define REG_ADDR_CONVH 0x07
unsigned int getData;
float analogVal=0; // convert
void init_adc()
{
Wire.beginTransmission(ADDR_ADC121); // transmit to device
Wire.write(REG_ADDR_CONFIG); // Configuration Register
Wire.write(0x20);
Wire.endTransmission();
}
void read_adc() //unsigned int *data
{
Wire.beginTransmission(ADDR_ADC121); // transmit to device
Wire.write(REG_ADDR_RESULT); // get result
Wire.endTransmission();
Wire.requestFrom(ADDR_ADC121, 2); // request 2byte from device
delay(1);
if(Wire.available()<=2)
{
getData = (Wire.read()&0x0f)<<8;
getData |= Wire.read();
}
Serial.print("getData:");
Serial.println(getData);
delay(5);
Serial.print("The analog value is:");
Serial.print(getData*V_REF*2/4096);
Serial.println("V");
}
The code writes 0x20 to 'register 2'. After some trial and error I found out that the Mathematica equivalent seems to be:
DeviceWrite[i2c, {0,0,32}]
because after that I can read data continuously using:
d = DeviceRead[i2c, 2]; n = d[[1]] * 256 + d[[2]]
Is this the way to set 'registers' on an ADC? Also, what does setting it achieve? The Grove documentation is rather vague on this point.
Thanks, Gijsbert