mbed library to interface a Texas Instruments' ADS7828, 12-bits, 8-channels, I2C interfaced ADC
ADS7828.cpp@1:19b0f0ba4d12, 2014-12-30 (annotated)
- Committer:
- frada
- Date:
- Tue Dec 30 14:22:31 2014 +0000
- Revision:
- 1:19b0f0ba4d12
- Child:
- 2:2ff328d8e4dd
-
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
frada | 1:19b0f0ba4d12 | 1 | /*************************************************************************** |
frada | 1:19b0f0ba4d12 | 2 | ADS7828.CPP |
frada | 1:19b0f0ba4d12 | 3 | Implementation file for library ADS7828 |
frada | 1:19b0f0ba4d12 | 4 | The ADS7828 is a 12-bits, 8-channels, I2C-interfaced from Texas Instruments |
frada | 1:19b0f0ba4d12 | 5 | |
frada | 1:19b0f0ba4d12 | 6 | (c) 2014 - By Francesco Adamo, Italy |
frada | 1:19b0f0ba4d12 | 7 | *****************************************************************************/ |
frada | 1:19b0f0ba4d12 | 8 | |
frada | 1:19b0f0ba4d12 | 9 | #include "mbed.h" |
frada | 1:19b0f0ba4d12 | 10 | #include "ads7828.h" |
frada | 1:19b0f0ba4d12 | 11 | |
frada | 1:19b0f0ba4d12 | 12 | ADS7828::ADS7828(PinName sda, PinName scl, int freq) : _i2c(sda, scl) { |
frada | 1:19b0f0ba4d12 | 13 | _i2c.frequency(freq); /* I2C clock frequency = set to input value */ |
frada | 1:19b0f0ba4d12 | 14 | address = ADS7828_BASE_ADDRESS; |
frada | 1:19b0f0ba4d12 | 15 | VFSR = 3.3F; |
frada | 1:19b0f0ba4d12 | 16 | } |
frada | 1:19b0f0ba4d12 | 17 | |
frada | 1:19b0f0ba4d12 | 18 | ADS7828::ADS7828(PinName sda, PinName scl) : _i2c(sda, scl) { |
frada | 1:19b0f0ba4d12 | 19 | _i2c.frequency(100000); /* Default I2C clock frequency = 100 kHz, Normal Mode */ |
frada | 1:19b0f0ba4d12 | 20 | address = ADS7828_BASE_ADDRESS; |
frada | 1:19b0f0ba4d12 | 21 | VFSR = 3.3F; |
frada | 1:19b0f0ba4d12 | 22 | } |
frada | 1:19b0f0ba4d12 | 23 | |
frada | 1:19b0f0ba4d12 | 24 | ADS7828::ADS7828(PinName sda, PinName scl, int freq, char subAddress) : _i2c(sda, scl) { |
frada | 1:19b0f0ba4d12 | 25 | _i2c.frequency(freq); /* I2C clock frequency = set to input value */ |
frada | 1:19b0f0ba4d12 | 26 | address = ADS7828_BASE_ADDRESS | subAddress; |
frada | 1:19b0f0ba4d12 | 27 | VFSR = 3.3F; |
frada | 1:19b0f0ba4d12 | 28 | } |
frada | 1:19b0f0ba4d12 | 29 | |
frada | 1:19b0f0ba4d12 | 30 | |
frada | 1:19b0f0ba4d12 | 31 | int ADS7828::readRawValue(char mode, char channel) { |
frada | 1:19b0f0ba4d12 | 32 | char command, D[2], ack; |
frada | 1:19b0f0ba4d12 | 33 | |
frada | 1:19b0f0ba4d12 | 34 | command = mode | channel; |
frada | 1:19b0f0ba4d12 | 35 | ack = _i2c.write(address, &command, 1); |
frada | 1:19b0f0ba4d12 | 36 | if (ack) |
frada | 1:19b0f0ba4d12 | 37 | return -1; |
frada | 1:19b0f0ba4d12 | 38 | |
frada | 1:19b0f0ba4d12 | 39 | ack = _i2c.read(address + 1, D, 2); // Read conversion result |
frada | 1:19b0f0ba4d12 | 40 | if (ack) |
frada | 1:19b0f0ba4d12 | 41 | return -1; |
frada | 1:19b0f0ba4d12 | 42 | else |
frada | 1:19b0f0ba4d12 | 43 | return D[0] << 8 | D[1]; |
frada | 1:19b0f0ba4d12 | 44 | } |
frada | 1:19b0f0ba4d12 | 45 | |
frada | 1:19b0f0ba4d12 | 46 | double ADS7828::readAnalogValue(char mode, char channel) { |
frada | 1:19b0f0ba4d12 | 47 | char command, D[2], ack; |
frada | 1:19b0f0ba4d12 | 48 | |
frada | 1:19b0f0ba4d12 | 49 | command = mode | channel; |
frada | 1:19b0f0ba4d12 | 50 | ack = _i2c.write(address, &command, 1); |
frada | 1:19b0f0ba4d12 | 51 | if (ack) |
frada | 1:19b0f0ba4d12 | 52 | return -1; |
frada | 1:19b0f0ba4d12 | 53 | |
frada | 1:19b0f0ba4d12 | 54 | ack = _i2c.read(address + 1, D, 2); // Read conversion result |
frada | 1:19b0f0ba4d12 | 55 | if (ack) |
frada | 1:19b0f0ba4d12 | 56 | return -1; |
frada | 1:19b0f0ba4d12 | 57 | else |
frada | 1:19b0f0ba4d12 | 58 | return VFSR/4096*(D[0] << 8 | D[1]); |
frada | 1:19b0f0ba4d12 | 59 | } |