Code: Select all
private int readADC_SingleEnded(int channel)
{
string CQUE_NONE = "0x0000";
string CLAT_NONLAT = "0x0000";
string CPOL_ACTVLOW = "0x0003";
string CMODE_TRAD = "0x0000";
string DR_1600SPS = "0x0080";
string MODE_SINGLE = "0x0100";
string PGA_6_144V = "0x0000";
string MUX_SINGLE_0 = "0x4000";
string OS_SINGLE = "0x8000";
string POINTER_CONFIG = "0x01";
string POINTER_CONVERT = "0x00";
string MODE_CONTIN = "0x0000";
if (channel > 3)
{
return 0;
}
//Start with default values
int ADS1015_REG_CONFIG_CQUE_NONE = this.GetHexFromString(ref CQUE_NONE); // Disable the comparator (default val)
int ADS1015_REG_CONFIG_CLAT_NONLAT = this.GetHexFromString(ref CLAT_NONLAT); // Non-latching (default val)
int ADS1015_REG_CONFIG_CPOL_ACTVLOW = this.GetHexFromString(ref CPOL_ACTVLOW); // Alert/Rdy active low (default val)
int ADS1015_REG_CONFIG_CMODE_TRAD = this.GetHexFromString(ref CMODE_TRAD); // Traditional comparator (default val)
int ADS1015_REG_CONFIG_DR_1600SPS = this.GetHexFromString(ref DR_1600SPS); // 1600 samples per second (default)
int ADS1015_REG_CONFIG_MODE_SINGLE = this.GetHexFromString(ref MODE_SINGLE);
//other settings
int ADS1015_REG_CONFIG_PGA_6_144V = this.GetHexFromString(ref PGA_6_144V);
int ADS1015_REG_CONFIG_MUX_SINGLE_0 = this.GetHexFromString(ref MUX_SINGLE_0);
int ADS1015_REG_CONFIG_OS_SINGLE = this.GetHexFromString(ref OS_SINGLE);
//continous read mode ?
int ADS1015_REG_CONFIG_MODE_CONTIN = this.GetHexFromString(ref MODE_CONTIN);
//reg pointer
int ADS1015_REG_POINTER_CONFIG = this.GetHexFromString(ref POINTER_CONFIG);
//reg pointer convert
int ADS1015_REG_POINTER_CONVERT = this.GetHexFromString(ref POINTER_CONVERT);
//Start with default values
int config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
// Set PGA/voltage range
config |= ADS1015_REG_CONFIG_PGA_6_144V;
//// Set single-ended input channel
switch (channel)
{
case (0):
config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
break;
case (1):
//config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
break;
case (2):
//config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
break;
case (3):
//config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
break;
}
// Set 'start single-conversion' bit
config |= ADS1015_REG_CONFIG_OS_SINGLE;
//assign all
this.write_data = config;
// Write config register to the ADC
int iReturnVal = I2CWriteData(this.g_i2chandle, ADS1015_REG_POINTER_CONFIG, ref this.write_data, 2);
Trace.Write("\n Write Return Value: " + Convert.ToBoolean(iReturnVal));
//delay
Thread.Sleep(100);
int iReadReturn = I2CReadData(this.g_i2chandle, ADS1015_REG_POINTER_CONVERT, ref this.read_data, 2);
Trace.Write("\n Read Return Value: " + Convert.ToBoolean(iReadReturn));
//display in a textbox on the form
this.ShowData2(this.read_data.ToString());
//return value
return this.read_data;
}
Here is the structure for the library to read and write
WRITE:
UINT I2CWriteData (
HANDLE hContext,
UINT32 subAddr,
VOID *pBuffer,
UINT32 count
)
Description
This function writes the particular number of bytes to the I2C device.
Parameters
hContext – Handle returned by I2CDrvrOpen function.
subAddr – Slave Address of the I2C Device to which data has to be written
pBuffer – Pointer to the buffer holding the data.
count – Number of Bytes to be written
READ:
UINT I2CReadData(
HANDLE hContext,
UINT32 subAddr,
VOID *pBuffer,
UINT32 count
)
Description
This function reads the particular number of bytes from the I2C device.
Parameters
hContext – Handle returned by I2CDrvrOpen function.
subAddr – Slave Address of the I2C Device from which the data has to be read
*pBuffer – Pointer to the buffer to hold the data.
count – Number of Bytes to be read