|
| 1 | +#include "M5_ADS1100.h" |
| 2 | + |
| 3 | + |
| 4 | +#if ARDUINO >= 100 |
| 5 | +#include "Arduino.h" |
| 6 | +#else |
| 7 | +#include "WProgram.h" |
| 8 | +#endif |
| 9 | + |
| 10 | +#include <Wire.h> |
| 11 | + |
| 12 | +/**************************************************************************/ |
| 13 | +/* |
| 14 | + Abstract away platform differences in Arduino wire library |
| 15 | +*/ |
| 16 | +/**************************************************************************/ |
| 17 | +static uint8_t i2cread(void) |
| 18 | +{ |
| 19 | + #if ARDUINO >= 100 |
| 20 | + return Wire.read(); |
| 21 | + #else |
| 22 | + return Wire.receive(); |
| 23 | + #endif |
| 24 | +} |
| 25 | + |
| 26 | +/**************************************************************************/ |
| 27 | +/* |
| 28 | + Abstract away platform differences in Arduino wire library |
| 29 | +*/ |
| 30 | +/**************************************************************************/ |
| 31 | +static void i2cwrite(uint8_t x) |
| 32 | +{ |
| 33 | + #if ARDUINO >= 100 |
| 34 | + Wire.write((uint8_t)x); |
| 35 | + #else |
| 36 | + Wire.send(x); |
| 37 | + #endif |
| 38 | +} |
| 39 | + |
| 40 | +/**************************************************************************/ |
| 41 | +/* |
| 42 | + Writes 8-bits to the destination register |
| 43 | +*/ |
| 44 | +/**************************************************************************/ |
| 45 | +static void writeRegister(uint8_t i2cAddress, uint8_t value) |
| 46 | +{ |
| 47 | + Wire.beginTransmission(i2cAddress); |
| 48 | + i2cwrite((uint8_t)value); |
| 49 | + Wire.endTransmission(); |
| 50 | +} |
| 51 | + |
| 52 | +/**************************************************************************/ |
| 53 | +/* |
| 54 | + Reads 16-bits from the destination register |
| 55 | +*/ |
| 56 | +/**************************************************************************/ |
| 57 | +static uint16_t readRegister(uint8_t i2cAddress) |
| 58 | +{ |
| 59 | + Wire.beginTransmission(i2cAddress); |
| 60 | + Wire.endTransmission(); |
| 61 | + Wire.requestFrom(i2cAddress, (uint8_t)2); |
| 62 | + return (int16_t)((i2cread() << 8) | i2cread()); |
| 63 | +} |
| 64 | + |
| 65 | +/**************************************************************************/ |
| 66 | +/* |
| 67 | + Instantiates a new ADS1100 class with appropriate properties |
| 68 | +*/ |
| 69 | +/**************************************************************************/ |
| 70 | +void ADS1100::getAddr_ADS1100(uint8_t i2cAddress) |
| 71 | +{ |
| 72 | + ads_i2cAddress = i2cAddress; |
| 73 | + ads_conversionDelay = ADS1100_CONVERSIONDELAY; |
| 74 | +} |
| 75 | + |
| 76 | +/**************************************************************************/ |
| 77 | +/* |
| 78 | + Sets up the Hardware |
| 79 | +*/ |
| 80 | +/**************************************************************************/ |
| 81 | +void ADS1100::begin() |
| 82 | +{ |
| 83 | + Wire.begin(); |
| 84 | +} |
| 85 | + |
| 86 | +/**************************************************************************/ |
| 87 | +/* |
| 88 | + Sets the Operational status/single-shot conversion start |
| 89 | + This determines the operational status of the device |
| 90 | +*/ |
| 91 | +/**************************************************************************/ |
| 92 | +void ADS1100::setOSMode(adsOSMode_t osmode) |
| 93 | +{ |
| 94 | + ads_osmode = osmode; |
| 95 | +} |
| 96 | + |
| 97 | +/**************************************************************************/ |
| 98 | +/* |
| 99 | + Gets the Operational status/single-shot conversion start |
| 100 | +*/ |
| 101 | +/**************************************************************************/ |
| 102 | +adsOSMode_t ADS1100::getOSMode() |
| 103 | +{ |
| 104 | + return ads_osmode; |
| 105 | +} |
| 106 | + |
| 107 | +/**************************************************************************/ |
| 108 | +/* |
| 109 | + Sets the Device operating mode |
| 110 | + This controls the current operational mode of the ADS1100 |
| 111 | +*/ |
| 112 | +/**************************************************************************/ |
| 113 | +void ADS1100::setMode(adsMode_t mode) |
| 114 | +{ |
| 115 | + ads_mode = mode; |
| 116 | +} |
| 117 | + |
| 118 | +/**************************************************************************/ |
| 119 | +/* |
| 120 | + Gets the Device operating mode |
| 121 | +*/ |
| 122 | +/**************************************************************************/ |
| 123 | +adsMode_t ADS1100::getMode() |
| 124 | +{ |
| 125 | + return ads_mode; |
| 126 | +} |
| 127 | + |
| 128 | +/**************************************************************************/ |
| 129 | +/* |
| 130 | + Sets the Date Rate |
| 131 | + This controls the data rate setting |
| 132 | +*/ |
| 133 | +/**************************************************************************/ |
| 134 | +void ADS1100::setRate(adsRate_t rate) |
| 135 | +{ |
| 136 | + ads_rate = rate; |
| 137 | +} |
| 138 | + |
| 139 | +/**************************************************************************/ |
| 140 | +/* |
| 141 | + Gets the Date Rate |
| 142 | +*/ |
| 143 | +/**************************************************************************/ |
| 144 | +adsRate_t ADS1100::getRate() |
| 145 | +{ |
| 146 | + return ads_rate; |
| 147 | +} |
| 148 | + |
| 149 | +/**************************************************************************/ |
| 150 | +/* |
| 151 | + Sets the gain and input voltage range |
| 152 | + This configures the programmable gain amplifier |
| 153 | +*/ |
| 154 | +/**************************************************************************/ |
| 155 | +void ADS1100::setGain(adsGain_t gain) |
| 156 | +{ |
| 157 | + ads_gain = gain; |
| 158 | +} |
| 159 | + |
| 160 | +/**************************************************************************/ |
| 161 | +/* |
| 162 | + Gets a gain and input voltage range |
| 163 | +*/ |
| 164 | +/**************************************************************************/ |
| 165 | +adsGain_t ADS1100::getGain() |
| 166 | +{ |
| 167 | + return ads_gain; |
| 168 | +} |
| 169 | + |
| 170 | +/**************************************************************************/ |
| 171 | +/* |
| 172 | + Reads the conversion results, measuring the voltage |
| 173 | + difference between the P and N input |
| 174 | + Generates a signed value since the difference can be either |
| 175 | + positive or negative |
| 176 | +*/ |
| 177 | +/**************************************************************************/ |
| 178 | +int16_t ADS1100::Measure_Differential() |
| 179 | +{ |
| 180 | + // Start with default values |
| 181 | + uint16_t config; |
| 182 | + |
| 183 | + // Set Operational status/single-shot conversion start |
| 184 | + config |= ads_osmode; |
| 185 | + |
| 186 | + // Set Device operating mode |
| 187 | + config |= ads_mode; |
| 188 | + |
| 189 | + // Set Data rate |
| 190 | + config |= ads_rate; |
| 191 | + |
| 192 | + // Set PGA/voltage range |
| 193 | + config |= ads_gain; |
| 194 | + |
| 195 | + // Write config register to the ADC |
| 196 | + writeRegister(ads_i2cAddress, config); |
| 197 | + |
| 198 | + // Wait for the conversion to complete |
| 199 | + delay(ads_conversionDelay); |
| 200 | + |
| 201 | + // Read the conversion results |
| 202 | + uint16_t raw_adc = readRegister(ads_i2cAddress); |
| 203 | + return (int16_t)raw_adc; |
| 204 | +} |
0 commit comments