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@0:94493b8b0fb0, 2018-06-26 (annotated)
- Committer:
- xorjoep
- Date:
- Tue Jun 26 13:30:53 2018 +0000
- Revision:
- 0:94493b8b0fb0
- Child:
- 1:c1ddf7c2d65b
ADS van Jasper geimplementeerd
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| xorjoep | 0:94493b8b0fb0 | 1 | #include "mbed.h" |
| xorjoep | 0:94493b8b0fb0 | 2 | #include "ADS1015.h" |
| xorjoep | 0:94493b8b0fb0 | 3 | |
| xorjoep | 0:94493b8b0fb0 | 4 | void startSingleShotConversion(I2C* i2c_plate, uint8_t address, uint16_t config) |
| xorjoep | 0:94493b8b0fb0 | 5 | { |
| xorjoep | 0:94493b8b0fb0 | 6 | char txbuf[10]; |
| xorjoep | 0:94493b8b0fb0 | 7 | // set mux to correct input |
| xorjoep | 0:94493b8b0fb0 | 8 | txbuf[0] = AD1015_REG_CONFIG; |
| xorjoep | 0:94493b8b0fb0 | 9 | txbuf[1] = config >> 8; |
| xorjoep | 0:94493b8b0fb0 | 10 | txbuf[2] = config & 0xFF; |
| xorjoep | 0:94493b8b0fb0 | 11 | i2c_plate->write(address, txbuf, 3); |
| xorjoep | 0:94493b8b0fb0 | 12 | } |
| xorjoep | 0:94493b8b0fb0 | 13 | |
| xorjoep | 0:94493b8b0fb0 | 14 | double readConversionResult(I2C* i2c_plate, uint8_t address, uint16_t config) |
| xorjoep | 0:94493b8b0fb0 | 15 | { |
| xorjoep | 0:94493b8b0fb0 | 16 | int16_t raw_data; |
| xorjoep | 0:94493b8b0fb0 | 17 | char rxbuf[10]; |
| xorjoep | 0:94493b8b0fb0 | 18 | char txbuf[10]; |
| xorjoep | 0:94493b8b0fb0 | 19 | txbuf[0] = AD1015_REG_CONVERSION_RESULT; |
| xorjoep | 0:94493b8b0fb0 | 20 | i2c_plate->write(address, txbuf, 1, true); |
| xorjoep | 0:94493b8b0fb0 | 21 | i2c_plate->read(address, rxbuf, 2); |
| xorjoep | 0:94493b8b0fb0 | 22 | raw_data = (rxbuf[0] << 8) | rxbuf[1]; |
| xorjoep | 0:94493b8b0fb0 | 23 | return data_to_voltage(raw_data, config); |
| xorjoep | 0:94493b8b0fb0 | 24 | } |
| xorjoep | 0:94493b8b0fb0 | 25 | |
| xorjoep | 0:94493b8b0fb0 | 26 | double data_to_voltage(int16_t data, uint16_t config) |
| xorjoep | 0:94493b8b0fb0 | 27 | { |
| xorjoep | 0:94493b8b0fb0 | 28 | double voltage; |
| xorjoep | 0:94493b8b0fb0 | 29 | data = data >> 4; |
| xorjoep | 0:94493b8b0fb0 | 30 | switch(config & 0x0E00) { |
| xorjoep | 0:94493b8b0fb0 | 31 | case AD1015_CONFIG_FSR_6144: |
| xorjoep | 0:94493b8b0fb0 | 32 | voltage = 0.003 * (double)data; |
| xorjoep | 0:94493b8b0fb0 | 33 | break; |
| xorjoep | 0:94493b8b0fb0 | 34 | case AD1015_CONFIG_FSR_4096: |
| xorjoep | 0:94493b8b0fb0 | 35 | voltage = 0.002 * (double)data; |
| xorjoep | 0:94493b8b0fb0 | 36 | break; |
| xorjoep | 0:94493b8b0fb0 | 37 | case AD1015_CONFIG_FSR_2048: |
| xorjoep | 0:94493b8b0fb0 | 38 | voltage = 0.001 * (double)data; |
| xorjoep | 0:94493b8b0fb0 | 39 | break; |
| xorjoep | 0:94493b8b0fb0 | 40 | case AD1015_CONFIG_FSR_1024: |
| xorjoep | 0:94493b8b0fb0 | 41 | voltage = 0.0005 * (double)data; |
| xorjoep | 0:94493b8b0fb0 | 42 | break; |
| xorjoep | 0:94493b8b0fb0 | 43 | case AD1015_CONFIG_FSR_0512: |
| xorjoep | 0:94493b8b0fb0 | 44 | voltage = 0.00025 * (double)data; |
| xorjoep | 0:94493b8b0fb0 | 45 | break; |
| xorjoep | 0:94493b8b0fb0 | 46 | case AD1015_CONFIG_FSR_0256: |
| xorjoep | 0:94493b8b0fb0 | 47 | voltage = 0.000125 * (double)data; |
| xorjoep | 0:94493b8b0fb0 | 48 | break; |
| xorjoep | 0:94493b8b0fb0 | 49 | default: |
| xorjoep | 0:94493b8b0fb0 | 50 | break; |
| xorjoep | 0:94493b8b0fb0 | 51 | } |
| xorjoep | 0:94493b8b0fb0 | 52 | return voltage; |
| xorjoep | 0:94493b8b0fb0 | 53 | } |