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.
ADS1232.cpp
00001 /* 00002 ADS1232.cpp - Library for reading from a ADS1232 24-bit ADC. 00003 Created by Jeffrey M. Kubascik, June 28, 2016. 00004 Released into the public domain. 00005 */ 00006 00007 // https://os.mbed.com/teams/SiliconLabs/code/MemoryLCD/file/tip/LS013B7DH03.cpp/ 00008 // https://os.mbed.com/teams/SiliconLabs/code/MemoryLCD/file/0f8ae10b308d/LS013B7DH03.h/ 00009 00010 #include "ADS1232.h" 00011 #include "softSPI.h" 00012 00013 //#include "../SEGGER_RTT/SEGGER_RTT.h" 00014 //#define DEBUGL(X) SEGGER_RTT_printf(0, X) 00015 //#define DEBUGF(X,Y) SEGGER_RTT_printf(0, X, Y) 00016 00017 #define LOW 0 00018 #define HIGH 1 00019 00020 #define SPI_FREQUENCY_HZ 1000000 // SCLK frequency in hz 00021 #define SPI_BITS 8 // Number of bits per SPI frame (4 - 16) 00022 #define SPI_MODE 1 // Clock polarity and phase mode (0 - 3): 00023 // mode | POL PHA 00024 // -----+-------- 00025 // 0 | 0 0 00026 // 1 | 0 1 00027 // 2 | 1 0 00028 // 3 | 1 1 00029 00030 00031 //#include "../SEGGER_RTT/SEGGER_RTT.h" 00032 //#define DEBUGL(X) SEGGER_RTT_printf(0, X) 00033 //#define DEBUGF(X,Y) SEGGER_RTT_printf(0, X, Y) 00034 00035 //DigitalInOut _cs; 00036 DigitalOut* _pdwn; 00037 //DigitalOut* _gain0; 00038 //DigitalOut* _gain1; 00039 //DigitalOut* _speed; 00040 //DigitalOut* _a0; 00041 //DigitalOut* _temp; 00042 00043 DigitalIn *_din_rdy; 00044 00045 SoftSPI* spi; 00046 00047 //namespace twtg { 00048 00049 //ADS1232::ADS1232(PinName pin_sck, PinName pin_din_rdy, PinName pin_pdwn, PinName pin_gain0, PinName pin_gain1, PinName pin_speed, PinName pin_a0, PinName pin_temp) : 00050 ADS1232::ADS1232(PinName pin_sck, PinName pin_din_rdy, PinName pin_pdwn) : 00051 _pin_sck(pin_sck), 00052 _pin_din_rdy(pin_din_rdy), 00053 _pin_pdwn(pin_pdwn) //, 00054 // _pin_gain0(pin_gain0), 00055 // _pin_gain1(pin_gain1), 00056 // _pin_speed(pin_speed), 00057 // _pin_a0(pin_a0), 00058 // _pin_temp(pin_temp) 00059 { 00060 spi = new SoftSPI(_pin_sck, NC, _pin_din_rdy); 00061 _pdwn = new DigitalOut(_pin_pdwn, LOW); 00062 // _gain0 = new DigitalOut(_pin_gain0, LOW); 00063 // _gain1 = new DigitalOut(_pin_gain1, LOW); 00064 // _speed = new DigitalOut(_pin_speed, LOW); 00065 // _a0 = new DigitalOut(_pin_a0, LOW); 00066 // _temp = new DigitalOut(_pin_temp, LOW); 00067 _din_rdy = new DigitalIn(_pin_din_rdy, PullNone); 00068 configChanged = false; 00069 00070 currentChannel = AIN1; 00071 currentGain = GAIN128; // according to schematics 00072 currentSpeed = SLOW; 00073 00074 return; 00075 } 00076 00077 void ADS1232::attach(void) 00078 { 00079 if (_pin_pdwn != NC) _pdwn->write(HIGH); 00080 spi->attach(); 00081 configChanged = true; 00082 00083 // setChannel(currentChannel); 00084 // setGain(currentGain); 00085 // setSpeed(currentSpeed); 00086 return; 00087 } 00088 00089 void ADS1232::detach(void) 00090 { 00091 if (_pin_pdwn != NC) _pdwn->write(LOW); 00092 // if (_pin_gain0 != NC) _gain0->write(LOW); 00093 // if (_pin_gain1 != NC) _gain1->write(LOW); 00094 // if (_pin_speed != NC) _speed->write(LOW); 00095 // if (_pin_a0 != NC) _a0->write(LOW); 00096 // if (_pin_temp != NC) _temp->write(LOW); 00097 spi->detach(); 00098 return; 00099 } 00100 00101 // void ADS1232::setGain(Gain gain) 00102 // { 00103 // if (gain == currentGain) 00104 // { 00105 // return; 00106 // } 00107 00108 // switch(gain) 00109 // { 00110 // case GAIN1: 00111 // { 00112 // if(_pin_gain1 != NC) 00113 // { 00114 // _gain1->write(LOW); 00115 // } 00116 // if(_pin_gain0 != NC) 00117 // { 00118 // _gain0->write(LOW); 00119 // } 00120 // break; 00121 // } 00122 // case GAIN2: 00123 // { 00124 // if(_pin_gain1 != NC) 00125 // { 00126 // _gain1->write(LOW); 00127 // } 00128 // if(_pin_gain0 != NC) 00129 // { 00130 // _gain0->write(HIGH); 00131 // } 00132 // break; 00133 // } 00134 // case GAIN64: 00135 // { 00136 // if(_pin_gain1 != NC) 00137 // { 00138 // _gain1->write(HIGH); 00139 // } 00140 // if(_pin_gain0 != NC) 00141 // { 00142 // _gain0->write(LOW); 00143 // } 00144 // break; 00145 // } 00146 // case GAIN128: 00147 // { 00148 // if(_pin_gain1 != NC) 00149 // { 00150 // _gain1->write(HIGH); 00151 // } 00152 // if(_pin_gain0 != NC) 00153 // { 00154 // _gain0->write(HIGH); 00155 // } 00156 // break; 00157 // } 00158 // } 00159 // currentGain = gain; 00160 // configChanged = true; 00161 // return; 00162 // } 00163 00164 // void ADS1232::setSpeed(Speed speed) 00165 // { 00166 // if (speed == currentSpeed) 00167 // { 00168 // return; 00169 // } 00170 00171 // if(_pin_speed != NC) 00172 // { 00173 // switch(speed) 00174 // { 00175 // case SLOW: 00176 // { 00177 // _speed->write(LOW); 00178 // break; 00179 // } 00180 // case FAST: 00181 // { 00182 // _speed->write(HIGH); 00183 // break; 00184 // } 00185 // } 00186 // } 00187 00188 // currentSpeed = speed; 00189 // configChanged = true; 00190 // return; 00191 // } 00192 00193 // void ADS1232::setChannel(Channel channel) 00194 // { 00195 // if (channel == currentChannel) 00196 // { 00197 // return; 00198 // } 00199 00200 // switch(channel) 00201 // { 00202 // case AIN1: 00203 // { 00204 // if(_pin_temp != NC) 00205 // { 00206 // _temp->write(LOW); 00207 // } 00208 // if(_pin_a0 != NC) 00209 // { 00210 // _a0->write(LOW); 00211 // } 00212 // break; 00213 // } 00214 // case AIN2: 00215 // { 00216 // if(_pin_temp != NC) 00217 // { 00218 // _temp->write(LOW); 00219 // } 00220 // if(_pin_a0 != NC) 00221 // { 00222 // _a0->write(HIGH); 00223 // } 00224 // break; 00225 // } 00226 // case TEMP: 00227 // { 00228 // if(_pin_temp != NC) 00229 // { 00230 // _temp->write(HIGH); 00231 // } 00232 // if(_pin_a0 != NC) 00233 // { 00234 // _a0->write(LOW); 00235 // } 00236 // break; 00237 // } 00238 // } 00239 00240 // currentChannel = channel; 00241 // configChanged = true; 00242 // return; 00243 // } 00244 00245 int32_t ADS1232::read(void) 00246 { 00247 int32_t data = 0; 00248 uint8_t rx[3] = {0}; 00249 00250 waitDataReady(); 00251 spi->readBulk(rx, 3); 00252 spi->shiftInOutBit(0x01); // fix last clock 00253 00254 data = (rx[0] & 0x80) ? (0xFF << 24) : 0; // Extend signed bit 00255 data |= (int32_t)rx[0] << 16; 00256 data |= (int32_t)rx[1] << 8; 00257 data |= (int32_t)rx[2] << 0; 00258 00259 return data; 00260 } 00261 00262 void ADS1232::waitDataReady(void) 00263 { 00264 // According to the datasheet it doese NOT requires five dummy reads after a channel switch, but from practice it does 00265 uint8_t fallingEdges = configChanged ? 5 : 1; 00266 00267 while (fallingEdges > 0) 00268 { 00269 // Wait for falling edge 00270 while (_din_rdy->read() != HIGH); 00271 while (_din_rdy->read() != LOW); 00272 fallingEdges--; 00273 } 00274 } 00275 00276 //} // namespace twtg
Generated on Tue Jul 12 2022 20:25:42 by
