Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ADS1015.cpp
- Committer:
- xorjoep
- Date:
- 2018-06-27
- Revision:
- 1:c1ddf7c2d65b
- Parent:
- 0:94493b8b0fb0
- Child:
- 2:2bf91fd23a1a
File content as of revision 1:c1ddf7c2d65b:
#include "mbed.h"
#include "ADS1015.h"
#define CONFIG 0x81E3 | AD1015_CONFIG_FSR_4096
double getSample(I2C* i2c_plate, uint8_t ADC_address, uint16_t sensor_address)
{
sensor_address = CONFIG | sensor_address;
startSingleShotConversion(i2c_plate, ADC_address, sensor_address);
wait_ms(0.1);
startSingleShotConversion(i2c_plate, ADC_address, sensor_address);
wait_ms(0.3);
return readConversionResult(i2c_plate, ADC_address, sensor_address);
}
void startSingleShotConversion(I2C* i2c_plate, uint8_t address, uint16_t config)
{
char txbuf[10];
// set mux to correct input
txbuf[0] = AD1015_REG_CONFIG;
txbuf[1] = config >> 8;
txbuf[2] = config & 0xFF;
i2c_plate->write(address, txbuf, 3);
}
double readConversionResult(I2C* i2c_plate, uint8_t address, uint16_t config)
{
int16_t raw_data;
char rxbuf[10];
char txbuf[10];
txbuf[0] = AD1015_REG_CONVERSION_RESULT;
i2c_plate->write(address, txbuf, 1, true);
i2c_plate->read(address, rxbuf, 2);
raw_data = (rxbuf[0] << 8) | rxbuf[1];
return data_to_voltage(raw_data, config);
}
double data_to_voltage(int16_t data, uint16_t config)
{
double voltage;
data = data >> 4;
switch(config & 0x0E00) {
case AD1015_CONFIG_FSR_6144:
voltage = 0.003 * (double)data;
break;
case AD1015_CONFIG_FSR_4096:
voltage = 0.002 * (double)data;
break;
case AD1015_CONFIG_FSR_2048:
voltage = 0.001 * (double)data;
break;
case AD1015_CONFIG_FSR_1024:
voltage = 0.0005 * (double)data;
break;
case AD1015_CONFIG_FSR_0512:
voltage = 0.00025 * (double)data;
break;
case AD1015_CONFIG_FSR_0256:
voltage = 0.000125 * (double)data;
break;
default:
break;
}
return voltage;
}