mbed library to interface a Texas Instruments' ADS7828, 12-bits, 8-channels, I2C interfaced ADC
ADS7828.cpp@2:2ff328d8e4dd, 2014-12-30 (annotated)
- Committer:
- frada
- Date:
- Tue Dec 30 14:41:54 2014 +0000
- Revision:
- 2:2ff328d8e4dd
- Parent:
- 1:19b0f0ba4d12
- Child:
- 3:abbfd9c7f30c
-
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 | 2:2ff328d8e4dd | 10 | #include "ADS7828.h" |
frada | 1:19b0f0ba4d12 | 11 | |
frada | 2:2ff328d8e4dd | 12 | // Constructor |
frada | 1:19b0f0ba4d12 | 13 | ADS7828::ADS7828(PinName sda, PinName scl) : _i2c(sda, scl) { |
frada | 1:19b0f0ba4d12 | 14 | _i2c.frequency(100000); /* Default I2C clock frequency = 100 kHz, Normal Mode */ |
frada | 1:19b0f0ba4d12 | 15 | address = ADS7828_BASE_ADDRESS; |
frada | 2:2ff328d8e4dd | 16 | VFSR = 2.5F; |
frada | 1:19b0f0ba4d12 | 17 | } |
frada | 1:19b0f0ba4d12 | 18 | |
frada | 2:2ff328d8e4dd | 19 | // Constructor overload |
frada | 2:2ff328d8e4dd | 20 | ADS7828::ADS7828(PinName sda, PinName scl, int freq) : _i2c(sda, scl) { |
frada | 2:2ff328d8e4dd | 21 | _i2c.frequency(freq); /* I2C clock frequency = set to input value */ |
frada | 2:2ff328d8e4dd | 22 | address = ADS7828_BASE_ADDRESS; |
frada | 2:2ff328d8e4dd | 23 | VFSR = 2.5F; |
frada | 2:2ff328d8e4dd | 24 | } |
frada | 2:2ff328d8e4dd | 25 | |
frada | 2:2ff328d8e4dd | 26 | // Constructor overload |
frada | 1:19b0f0ba4d12 | 27 | ADS7828::ADS7828(PinName sda, PinName scl, int freq, char subAddress) : _i2c(sda, scl) { |
frada | 1:19b0f0ba4d12 | 28 | _i2c.frequency(freq); /* I2C clock frequency = set to input value */ |
frada | 1:19b0f0ba4d12 | 29 | address = ADS7828_BASE_ADDRESS | subAddress; |
frada | 2:2ff328d8e4dd | 30 | VFSR = 2.5F; |
frada | 1:19b0f0ba4d12 | 31 | } |
frada | 1:19b0f0ba4d12 | 32 | |
frada | 1:19b0f0ba4d12 | 33 | |
frada | 2:2ff328d8e4dd | 34 | // Constructor overload |
frada | 2:2ff328d8e4dd | 35 | ADS7828::ADS7828(PinName sda, PinName scl, int freq, char subAddress, double VREF) : _i2c(sda, scl) { |
frada | 2:2ff328d8e4dd | 36 | _i2c.frequency(freq); /* I2C clock frequency = set to input value */ |
frada | 2:2ff328d8e4dd | 37 | address = ADS7828_BASE_ADDRESS | subAddress; |
frada | 2:2ff328d8e4dd | 38 | VFSR = VREF; |
frada | 2:2ff328d8e4dd | 39 | } |
frada | 2:2ff328d8e4dd | 40 | |
frada | 2:2ff328d8e4dd | 41 | // Read a raw value from a given channel |
frada | 1:19b0f0ba4d12 | 42 | int ADS7828::readRawValue(char mode, char channel) { |
frada | 1:19b0f0ba4d12 | 43 | char command, D[2], ack; |
frada | 1:19b0f0ba4d12 | 44 | |
frada | 1:19b0f0ba4d12 | 45 | command = mode | channel; |
frada | 1:19b0f0ba4d12 | 46 | ack = _i2c.write(address, &command, 1); |
frada | 1:19b0f0ba4d12 | 47 | if (ack) |
frada | 1:19b0f0ba4d12 | 48 | return -1; |
frada | 1:19b0f0ba4d12 | 49 | |
frada | 1:19b0f0ba4d12 | 50 | ack = _i2c.read(address + 1, D, 2); // Read conversion result |
frada | 1:19b0f0ba4d12 | 51 | if (ack) |
frada | 1:19b0f0ba4d12 | 52 | return -1; |
frada | 1:19b0f0ba4d12 | 53 | else |
frada | 1:19b0f0ba4d12 | 54 | return D[0] << 8 | D[1]; |
frada | 1:19b0f0ba4d12 | 55 | } |
frada | 1:19b0f0ba4d12 | 56 | |
frada | 2:2ff328d8e4dd | 57 | // Read a raw value from a given channel |
frada | 1:19b0f0ba4d12 | 58 | double ADS7828::readAnalogValue(char mode, char channel) { |
frada | 1:19b0f0ba4d12 | 59 | char command, D[2], ack; |
frada | 1:19b0f0ba4d12 | 60 | |
frada | 1:19b0f0ba4d12 | 61 | command = mode | channel; |
frada | 1:19b0f0ba4d12 | 62 | ack = _i2c.write(address, &command, 1); |
frada | 1:19b0f0ba4d12 | 63 | if (ack) |
frada | 1:19b0f0ba4d12 | 64 | return -1; |
frada | 1:19b0f0ba4d12 | 65 | |
frada | 1:19b0f0ba4d12 | 66 | ack = _i2c.read(address + 1, D, 2); // Read conversion result |
frada | 1:19b0f0ba4d12 | 67 | if (ack) |
frada | 1:19b0f0ba4d12 | 68 | return -1; |
frada | 1:19b0f0ba4d12 | 69 | else |
frada | 1:19b0f0ba4d12 | 70 | return VFSR/4096*(D[0] << 8 | D[1]); |
frada | 1:19b0f0ba4d12 | 71 | } |