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.
Diff: ADS1015.cpp
- Revision:
- 0:94493b8b0fb0
- Child:
- 1:c1ddf7c2d65b
diff -r 000000000000 -r 94493b8b0fb0 ADS1015.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/ADS1015.cpp Tue Jun 26 13:30:53 2018 +0000
@@ -0,0 +1,53 @@
+#include "mbed.h"
+#include "ADS1015.h"
+
+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;
+}