|
| 1 | +/*************************************************************************** |
| 2 | + This is a library for the IO_ADS1118 precision ADC |
| 3 | +
|
| 4 | + Designed to work with all kinds of IO_ADS1118 Breakout |
| 5 | +
|
| 6 | + These sensors use I2C to communicate, 2 pins are required |
| 7 | + to interface. |
| 8 | +
|
| 9 | + Written by Adrien Chapelet for Iotech |
| 10 | + ***************************************************************************/ |
| 11 | + |
| 12 | +#include "Arduino.h" |
| 13 | +#include "IO_ADS1118.h" |
| 14 | + |
| 15 | +//#define DEBUG |
| 16 | + |
| 17 | +#ifdef DEBUG |
| 18 | + #define DEBUG_PRINT(x) Serial.println(x) |
| 19 | +#else |
| 20 | + #define DEBUG_PRINT(x) |
| 21 | +#endif |
| 22 | + |
| 23 | +IO_ADS1118::IO_ADS1118(int CS_pin) |
| 24 | +{ |
| 25 | + /* Constructor for IO_ADS1118 class. */ |
| 26 | + pinMode(CS_pin, OUTPUT); |
| 27 | + _cs = CS_pin; |
| 28 | +} |
| 29 | + |
| 30 | +void IO_ADS1118::begin() |
| 31 | +{ |
| 32 | + /* Initializes SPI parameters. |
| 33 | + Clock Polarity 0, Clock Phase 1 -> SPI mode 1. MSB first. |
| 34 | + Page 25 on datasheet |
| 35 | + */ |
| 36 | + SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE1)); |
| 37 | + SPI.begin(); |
| 38 | + digitalWrite(_cs, HIGH); // Do not begin transactions yet |
| 39 | + if(self_test()){ |
| 40 | + //Error |
| 41 | + DEBUG_PRINT("An error has occurred during self test"); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +byte IO_ADS1118::self_test() |
| 46 | +{ |
| 47 | + /* This self test returns 1 if there is an error communicating with |
| 48 | + the IO_ADS1118 chip. This may be due to a wiring problem or other. |
| 49 | + */ |
| 50 | + CURRENT_CONFIG = CONFIG_DEFAULT; |
| 51 | + if(CONFIG_DEFAULT != update_config(CONFIG_DEFAULT)){ |
| 52 | + return 1; |
| 53 | + } |
| 54 | + return 0; |
| 55 | +} |
| 56 | + |
| 57 | +word IO_ADS1118::update_config(uint16_t new_config) |
| 58 | +{ |
| 59 | + /* This function executes a 32-bit transaction. |
| 60 | +
|
| 61 | + Input: a word new_config (see datasheet) |
| 62 | + Output: a word specifying the configuration register. Should be |
| 63 | + identical to new_config if config updated correctly. |
| 64 | + */ |
| 65 | + word readConfig; //this is the return config |
| 66 | + |
| 67 | + digitalWrite(_cs, LOW); |
| 68 | + delayMicroseconds(1); |
| 69 | + // The following transfer results in garbage data: |
| 70 | + SPI.transfer16(new_config); |
| 71 | + // We capture the following transfer to read back the configuration |
| 72 | + readConfig = SPI.transfer16(new_config); |
| 73 | + digitalWrite(_cs, HIGH); // Done writing |
| 74 | + |
| 75 | + |
| 76 | + CURRENT_CONFIG = new_config; // Update global configuration variable |
| 77 | + Serial.print("Current_config is:"); |
| 78 | + Serial.println(CURRENT_CONFIG, HEX); |
| 79 | + return readConfig; |
| 80 | +} |
| 81 | + |
| 82 | +word IO_ADS1118::adsReadRaw(word port) |
| 83 | +{ |
| 84 | + /* This method executes a 16 bit transaction if the current config |
| 85 | + is already correct, or 2 32-bit transactions otherwise, updating |
| 86 | + the config as necessary. |
| 87 | +
|
| 88 | + Input: A port specifying the pin selection (see header file) |
| 89 | + Output: Raw requested data from sensor |
| 90 | + */ |
| 91 | + word read; |
| 92 | + byte dummy, MSB, LSB; |
| 93 | + |
| 94 | +#ifdef DEBUG |
| 95 | + Serial.print("Current config is: "); |
| 96 | + Serial.println(CURRENT_CONFIG, HEX); |
| 97 | + Serial.print("Updating configuration to: "); |
| 98 | + Serial.println(port | (CURRENT_CONFIG & ~(PIN_BITMASK)), HEX); |
| 99 | + Serial.print("CURRENT_CONFIG & ~PIN_BITMASK = "); |
| 100 | + Serial.println((CURRENT_CONFIG & ~PIN_BITMASK), HEX); |
| 101 | +#endif |
| 102 | + |
| 103 | + //Only update the configuration *if necessary* |
| 104 | + if((CURRENT_CONFIG & PIN_BITMASK) != port){ |
| 105 | + //Updates the configuration to the proper pin: |
| 106 | + update_config((port | (CURRENT_CONFIG & ~(PIN_BITMASK)))); |
| 107 | + } |
| 108 | + |
| 109 | +#ifdef DEBUG |
| 110 | + Serial.print("Input mux: 0x"); |
| 111 | + Serial.println(PIN_BITMASK & CURRENT_CONFIG, HEX); |
| 112 | + Serial.print("MOSI: "); |
| 113 | + Serial.println(CURRENT_CONFIG, HEX); |
| 114 | +#endif |
| 115 | + |
| 116 | + digitalWrite(_cs, LOW); |
| 117 | + delayMicroseconds(1); |
| 118 | + read = SPI.transfer16(CURRENT_CONFIG); |
| 119 | + digitalWrite(_cs, HIGH); |
| 120 | + return read; |
| 121 | +} |
| 122 | + |
| 123 | +double IO_ADS1118::convToFloat(word read) |
| 124 | +{ |
| 125 | + /* This method returns a double (float) signifying the input signal |
| 126 | + Vin for the proper FS (scaling). |
| 127 | +
|
| 128 | + Input: a binary word from the sensor |
| 129 | + Output: the floating point representation of the sensor value. |
| 130 | + */ |
| 131 | + |
| 132 | + // See page 26 of datasheet detailing the 6 gain modes. |
| 133 | + float gain[6] = {6.144, 4.096, 2.048, 1.024, 0.512, 0.256}; |
| 134 | + |
| 135 | + // Find the correct gain index using bitshift and bitmask |
| 136 | + float myGain = gain[(((PGA_BITMASK & CURRENT_CONFIG) >> 8) / 2)]; |
| 137 | + |
| 138 | +#ifdef DEBUG |
| 139 | + Serial.print("Gain = "); |
| 140 | + Serial.println(myGain); |
| 141 | +#endif |
| 142 | + |
| 143 | + return (float)((int)read)*((float)myGain)/ ((float)32768); |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | +bool IO_ADS1118::setGain(uint16_t GainSet){ |
| 148 | + //Temp variable |
| 149 | + uint16_t newConfig = CURRENT_CONFIG; |
| 150 | + |
| 151 | + //Clear the corresponding bits in current config |
| 152 | + newConfig &= ~(0b111 << 9); |
| 153 | + |
| 154 | + //Set the corresponding pins in current config |
| 155 | + newConfig |= (GainSet << 9); |
| 156 | + |
| 157 | + //Send update to the ADC |
| 158 | + if (update_config(newConfig) == newConfig){ |
| 159 | + return true; |
| 160 | + } |
| 161 | + else { |
| 162 | + return false; |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +double IO_ADS1118::adsRead(word port) |
| 167 | +{ |
| 168 | + /* This method returns the floating point representation of the desired |
| 169 | + sensor operation. |
| 170 | +
|
| 171 | + Input: a port selection (see header file) |
| 172 | + Output: the floating point representation of desired operation. |
| 173 | + */ |
| 174 | + return convToFloat(adsReadRaw(port)); // Reads from port; converts to float |
| 175 | +} |
| 176 | + |
| 177 | +double IO_ADS1118::readTemp() |
| 178 | +{ |
| 179 | + /* This function is still being debug and may or may not work. |
| 180 | + */ |
| 181 | + |
| 182 | + /* This method loads the temperature configuration, and returns the |
| 183 | + floating point representation of the temperature in degrees Celcius. |
| 184 | + */ |
| 185 | + if(update_config(CONFIG_TEMPERATURE) != CONFIG_TEMPERATURE){ |
| 186 | + DEBUG_PRINT("Error: not updating to temp config"); |
| 187 | + } |
| 188 | + word read; |
| 189 | + byte MSB, LSB; |
| 190 | + digitalWrite(_cs, LOW); |
| 191 | + delayMicroseconds(500); |
| 192 | + /* |
| 193 | + MSB = SPI.transfer((CURRENT_CONFIG >> 8) & 0xFF); |
| 194 | + LSB = SPI.transfer(CURRENT_CONFIG & 0xFF); |
| 195 | + digitalWrite(_cs, HIGH); |
| 196 | + delayMicroseconds(500); |
| 197 | + digitalWrite(_cs, LOW); |
| 198 | + delayMicroseconds(500); |
| 199 | + */ |
| 200 | +#ifdef DEBUG |
| 201 | + Serial.print("Sending: "); |
| 202 | + Serial.println((CURRENT_CONFIG >> 8), HEX); |
| 203 | +#endif |
| 204 | + MSB = SPI.transfer((CURRENT_CONFIG >> 8) & 0xFF); |
| 205 | +#ifdef DEBUG |
| 206 | + Serial.print("Sending: "); |
| 207 | + Serial.println((CURRENT_CONFIG & 0xFF), HEX); |
| 208 | +#endif |
| 209 | + LSB = SPI.transfer(CURRENT_CONFIG & 0xFF); |
| 210 | + delayMicroseconds(50); |
| 211 | + digitalWrite(_cs, HIGH); |
| 212 | + read = (MSB << 6) | (LSB >> 2); |
| 213 | +#ifdef DEBUG |
| 214 | + Serial.print("Raw MSB ="); |
| 215 | + Serial.println(MSB, BIN); |
| 216 | + Serial.print("Raw LSB ="); |
| 217 | + Serial.println(LSB, BIN); |
| 218 | + Serial.print("Raw read ="); |
| 219 | + Serial.println(read, BIN); |
| 220 | +#endif |
| 221 | + if((0x8000 & read) == 0){ |
| 222 | + return (double)read*0.03125; // datasheet page 18 |
| 223 | + } |
| 224 | + read = ~(read - 1); |
| 225 | + return (double)read*-0.03125; |
| 226 | +} |
| 227 | + |
0 commit comments