ADS1220 Library with ChipSelect Support and Code to Voltage Conversion Program

Dependents:   WeightScale

Committer:
sandeepmalladi
Date:
Tue May 24 13:44:10 2016 +0000
Revision:
0:90cd7e5e24af
Child:
1:85ab9e2aab24
All update but error in serial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sandeepmalladi 0:90cd7e5e24af 1 #ifndef ADS1220_H_
sandeepmalladi 0:90cd7e5e24af 2 #define ADS1220_H_
sandeepmalladi 0:90cd7e5e24af 3 #include "mbed.h"
sandeepmalladi 0:90cd7e5e24af 4
sandeepmalladi 0:90cd7e5e24af 5 #define COUNTOF(__BUFFER__) (sizeof(__BUFFER__) / sizeof(*(__BUFFER__)))
sandeepmalladi 0:90cd7e5e24af 6 #define gain_error_correction 0.9107468123861566//0.9980039920159681//6218905 // 0.977517107
sandeepmalladi 0:90cd7e5e24af 7 #define weight_gram 408
sandeepmalladi 0:90cd7e5e24af 8
sandeepmalladi 0:90cd7e5e24af 9 /* Error Return Values */
sandeepmalladi 0:90cd7e5e24af 10 #define ADS1220_NO_ERROR 0
sandeepmalladi 0:90cd7e5e24af 11 #define ADS1220_ERROR -1
sandeepmalladi 0:90cd7e5e24af 12
sandeepmalladi 0:90cd7e5e24af 13 /* Command Definitions */
sandeepmalladi 0:90cd7e5e24af 14 #define ADS1220_CMD_RDATA 0x10
sandeepmalladi 0:90cd7e5e24af 15 #define ADS1220_CMD_RREG 0x20
sandeepmalladi 0:90cd7e5e24af 16 #define ADS1220_CMD_WREG 0x40
sandeepmalladi 0:90cd7e5e24af 17 #define ADS1220_CMD_SYNC 0x08
sandeepmalladi 0:90cd7e5e24af 18 #define ADS1220_CMD_SHUTDOWN 0x02
sandeepmalladi 0:90cd7e5e24af 19 #define ADS1220_CMD_RESET 0x06
sandeepmalladi 0:90cd7e5e24af 20
sandeepmalladi 0:90cd7e5e24af 21 /* ADS1220 Register Definitions */
sandeepmalladi 0:90cd7e5e24af 22 #define ADS1220_0_REGISTER 0x00
sandeepmalladi 0:90cd7e5e24af 23 #define ADS1220_1_REGISTER 0x01
sandeepmalladi 0:90cd7e5e24af 24 #define ADS1220_2_REGISTER 0x02
sandeepmalladi 0:90cd7e5e24af 25 #define ADS1220_3_REGISTER 0x03
sandeepmalladi 0:90cd7e5e24af 26
sandeepmalladi 0:90cd7e5e24af 27
sandeepmalladi 0:90cd7e5e24af 28 /* ADS1220 Register 0 Definition */
sandeepmalladi 0:90cd7e5e24af 29 // Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0
sandeepmalladi 0:90cd7e5e24af 30 //--------------------------------------------------------------------------------------------
sandeepmalladi 0:90cd7e5e24af 31 // MUX [3:0] | GAIN[2:0] | PGA_BYPASS
sandeepmalladi 0:90cd7e5e24af 32 //
sandeepmalladi 0:90cd7e5e24af 33 // Define MUX
sandeepmalladi 0:90cd7e5e24af 34 #define ADS1220_MUX_0_1 0x00
sandeepmalladi 0:90cd7e5e24af 35 #define ADS1220_MUX_0_2 0x10
sandeepmalladi 0:90cd7e5e24af 36 #define ADS1220_MUX_0_3 0x20
sandeepmalladi 0:90cd7e5e24af 37 #define ADS1220_MUX_1_2 0x30
sandeepmalladi 0:90cd7e5e24af 38 #define ADS1220_MUX_1_3 0x40
sandeepmalladi 0:90cd7e5e24af 39 #define ADS1220_MUX_2_3 0x50
sandeepmalladi 0:90cd7e5e24af 40 #define ADS1220_MUX_1_0 0x60
sandeepmalladi 0:90cd7e5e24af 41 #define ADS1220_MUX_3_2 0x70
sandeepmalladi 0:90cd7e5e24af 42 #define ADS1220_MUX_0_G 0x80
sandeepmalladi 0:90cd7e5e24af 43 #define ADS1220_MUX_1_G 0x90
sandeepmalladi 0:90cd7e5e24af 44 #define ADS1220_MUX_2_G 0xa0
sandeepmalladi 0:90cd7e5e24af 45 #define ADS1220_MUX_3_G 0xb0
sandeepmalladi 0:90cd7e5e24af 46 #define ADS1220_MUX_EX_VREF 0xc0
sandeepmalladi 0:90cd7e5e24af 47 #define ADS1220_MUX_AVDD 0xd0
sandeepmalladi 0:90cd7e5e24af 48 #define ADS1220_MUX_DIV2 0xe0
sandeepmalladi 0:90cd7e5e24af 49
sandeepmalladi 0:90cd7e5e24af 50 // Define GAIN
sandeepmalladi 0:90cd7e5e24af 51 #define ADS1220_GAIN_1 0x00
sandeepmalladi 0:90cd7e5e24af 52 #define ADS1220_GAIN_2 0x02
sandeepmalladi 0:90cd7e5e24af 53 #define ADS1220_GAIN_4 0x04
sandeepmalladi 0:90cd7e5e24af 54 #define ADS1220_GAIN_8 0x06
sandeepmalladi 0:90cd7e5e24af 55 #define ADS1220_GAIN_16 0x08
sandeepmalladi 0:90cd7e5e24af 56 #define ADS1220_GAIN_32 0x0a
sandeepmalladi 0:90cd7e5e24af 57 #define ADS1220_GAIN_64 0x0c
sandeepmalladi 0:90cd7e5e24af 58 #define ADS1220_GAIN_128 0x0e
sandeepmalladi 0:90cd7e5e24af 59
sandeepmalladi 0:90cd7e5e24af 60 // Define PGA_BYPASS
sandeepmalladi 0:90cd7e5e24af 61 #define ADS1220_PGA_BYPASS 0x01
sandeepmalladi 0:90cd7e5e24af 62
sandeepmalladi 0:90cd7e5e24af 63 /* ADS1220 Register 1 Definition */
sandeepmalladi 0:90cd7e5e24af 64 // Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0
sandeepmalladi 0:90cd7e5e24af 65 //--------------------------------------------------------------------------------------------
sandeepmalladi 0:90cd7e5e24af 66 // DR[2:0] | MODE[1:0] | CM | TS | BCS
sandeepmalladi 0:90cd7e5e24af 67 //
sandeepmalladi 0:90cd7e5e24af 68 // Define DR (data rate)
sandeepmalladi 0:90cd7e5e24af 69 #define ADS1220_DR_20 0x00
sandeepmalladi 0:90cd7e5e24af 70 #define ADS1220_DR_45 0x20
sandeepmalladi 0:90cd7e5e24af 71 #define ADS1220_DR_90 0x40
sandeepmalladi 0:90cd7e5e24af 72 #define ADS1220_DR_175 0x60
sandeepmalladi 0:90cd7e5e24af 73 #define ADS1220_DR_330 0x80
sandeepmalladi 0:90cd7e5e24af 74 #define ADS1220_DR_600 0xa0
sandeepmalladi 0:90cd7e5e24af 75 #define ADS1220_DR_1000 0xc0
sandeepmalladi 0:90cd7e5e24af 76
sandeepmalladi 0:90cd7e5e24af 77 // Define MODE of Operation
sandeepmalladi 0:90cd7e5e24af 78 #define ADS1220_MODE_NORMAL 0x00
sandeepmalladi 0:90cd7e5e24af 79 #define ADS1220_MODE_DUTY 0x08
sandeepmalladi 0:90cd7e5e24af 80 #define ADS1220_MODE_TURBO 0x10
sandeepmalladi 0:90cd7e5e24af 81 #define ADS1220_MODE_DCT 0x18
sandeepmalladi 0:90cd7e5e24af 82
sandeepmalladi 0:90cd7e5e24af 83 // Define CM (conversion mode)
sandeepmalladi 0:90cd7e5e24af 84 #define ADS1220_CC 0x04
sandeepmalladi 0:90cd7e5e24af 85
sandeepmalladi 0:90cd7e5e24af 86 // Define TS (temperature sensor)
sandeepmalladi 0:90cd7e5e24af 87 #define ADS1220_TEMP_SENSOR 0x02
sandeepmalladi 0:90cd7e5e24af 88
sandeepmalladi 0:90cd7e5e24af 89 // Define BCS (burnout current source)
sandeepmalladi 0:90cd7e5e24af 90 #define ADS1220_BCS 0x01
sandeepmalladi 0:90cd7e5e24af 91
sandeepmalladi 0:90cd7e5e24af 92 /* ADS1220 Register 2 Definition */
sandeepmalladi 0:90cd7e5e24af 93 // Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0
sandeepmalladi 0:90cd7e5e24af 94 //--------------------------------------------------------------------------------------------
sandeepmalladi 0:90cd7e5e24af 95 // VREF[1:0] | 50/60[1:0] | PSW | IDAC[2:0]
sandeepmalladi 0:90cd7e5e24af 96 //
sandeepmalladi 0:90cd7e5e24af 97 // Define VREF
sandeepmalladi 0:90cd7e5e24af 98 #define ADS1220_VREF_INT 0x00
sandeepmalladi 0:90cd7e5e24af 99 #define ADS1220_VREF_EX_DED 0x40
sandeepmalladi 0:90cd7e5e24af 100 #define ADS1220_VREF_EX_AIN 0x80
sandeepmalladi 0:90cd7e5e24af 101 #define ADS1220_VREF_SUPPLY 0xc0
sandeepmalladi 0:90cd7e5e24af 102
sandeepmalladi 0:90cd7e5e24af 103 // Define 50/60 (filter response)
sandeepmalladi 0:90cd7e5e24af 104 #define ADS1220_REJECT_OFF 0x00
sandeepmalladi 0:90cd7e5e24af 105 #define ADS1220_REJECT_BOTH 0x10
sandeepmalladi 0:90cd7e5e24af 106 #define ADS1220_REJECT_50 0x20
sandeepmalladi 0:90cd7e5e24af 107 #define ADS1220_REJECT_60 0x30
sandeepmalladi 0:90cd7e5e24af 108
sandeepmalladi 0:90cd7e5e24af 109 // Define PSW (low side power switch)
sandeepmalladi 0:90cd7e5e24af 110 #define ADS1220_PSW_SW 0x08
sandeepmalladi 0:90cd7e5e24af 111
sandeepmalladi 0:90cd7e5e24af 112 // Define IDAC (IDAC current)
sandeepmalladi 0:90cd7e5e24af 113 #define ADS1220_IDAC_OFF 0x00
sandeepmalladi 0:90cd7e5e24af 114 #define ADS1220_IDAC_10 0x01
sandeepmalladi 0:90cd7e5e24af 115 #define ADS1220_IDAC_50 0x02
sandeepmalladi 0:90cd7e5e24af 116 #define ADS1220_IDAC_100 0x03
sandeepmalladi 0:90cd7e5e24af 117 #define ADS1220_IDAC_250 0x04
sandeepmalladi 0:90cd7e5e24af 118 #define ADS1220_IDAC_500 0x05
sandeepmalladi 0:90cd7e5e24af 119 #define ADS1220_IDAC_1000 0x06
sandeepmalladi 0:90cd7e5e24af 120 #define ADS1220_IDAC_2000 0x07
sandeepmalladi 0:90cd7e5e24af 121
sandeepmalladi 0:90cd7e5e24af 122 /* ADS1220 Register 3 Definition */
sandeepmalladi 0:90cd7e5e24af 123 // Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0
sandeepmalladi 0:90cd7e5e24af 124 //--------------------------------------------------------------------------------------------
sandeepmalladi 0:90cd7e5e24af 125 // I1MUX[2:0] | I2MUX[2:0] | DRDYM | RESERVED
sandeepmalladi 0:90cd7e5e24af 126 //
sandeepmalladi 0:90cd7e5e24af 127 // Define I1MUX (current routing)
sandeepmalladi 0:90cd7e5e24af 128 #define ADS1220_IDAC1_OFF 0x00
sandeepmalladi 0:90cd7e5e24af 129 #define ADS1220_IDAC1_AIN0 0x20
sandeepmalladi 0:90cd7e5e24af 130 #define ADS1220_IDAC1_AIN1 0x40
sandeepmalladi 0:90cd7e5e24af 131 #define ADS1220_IDAC1_AIN2 0x60
sandeepmalladi 0:90cd7e5e24af 132 #define ADS1220_IDAC1_AIN3 0x80
sandeepmalladi 0:90cd7e5e24af 133 #define ADS1220_IDAC1_REFP0 0xa0
sandeepmalladi 0:90cd7e5e24af 134 #define ADS1220_IDAC1_REFN0 0xc0
sandeepmalladi 0:90cd7e5e24af 135
sandeepmalladi 0:90cd7e5e24af 136 // Define I2MUX (current routing)
sandeepmalladi 0:90cd7e5e24af 137 #define ADS1220_IDAC2_OFF 0x00
sandeepmalladi 0:90cd7e5e24af 138 #define ADS1220_IDAC2_AIN0 0x04
sandeepmalladi 0:90cd7e5e24af 139 #define ADS1220_IDAC2_AIN1 0x08
sandeepmalladi 0:90cd7e5e24af 140 #define ADS1220_IDAC2_AIN2 0x0c
sandeepmalladi 0:90cd7e5e24af 141 #define ADS1220_IDAC2_AIN3 0x10
sandeepmalladi 0:90cd7e5e24af 142 #define ADS1220_IDAC2_REFP0 0x14
sandeepmalladi 0:90cd7e5e24af 143 #define ADS1220_IDAC2_REFN0 0x18
sandeepmalladi 0:90cd7e5e24af 144
sandeepmalladi 0:90cd7e5e24af 145 // define DRDYM (DOUT/DRDY behaviour)
sandeepmalladi 0:90cd7e5e24af 146
sandeepmalladi 0:90cd7e5e24af 147 #define ADS1220_DRDY_MODE 0x02
sandeepmalladi 0:90cd7e5e24af 148
sandeepmalladi 0:90cd7e5e24af 149 class ADS1220 {
sandeepmalladi 0:90cd7e5e24af 150 public:
sandeepmalladi 0:90cd7e5e24af 151
sandeepmalladi 0:90cd7e5e24af 152 /** Create a new Digole Serial Display interface
sandeepmalladi 0:90cd7e5e24af 153 *
sandeepmalladi 0:90cd7e5e24af 154 * @param sda is the pin for I2C SDA
sandeepmalladi 0:90cd7e5e24af 155 * @param scl is the pin for I2C SCL
sandeepmalladi 0:90cd7e5e24af 156 * @param address is the 7-bit address (default is 0x27 for the device)
sandeepmalladi 0:90cd7e5e24af 157 */
sandeepmalladi 0:90cd7e5e24af 158 ADS1220(PinName mosi, PinName miso, PinName sclk);
sandeepmalladi 0:90cd7e5e24af 159
sandeepmalladi 0:90cd7e5e24af 160
sandeepmalladi 0:90cd7e5e24af 161 /* Low Level ADS1220 Device Functions */
sandeepmalladi 0:90cd7e5e24af 162 void Init(void); // Device intialization
sandeepmalladi 0:90cd7e5e24af 163 int WaitForDataReady(int Timeout); // DRDY polling
sandeepmalladi 0:90cd7e5e24af 164 void AssertCS(int fAssert); // Assert/deassert CS
sandeepmalladi 0:90cd7e5e24af 165 void SendByte(unsigned char cData ); // Send byte to the ADS1220
sandeepmalladi 0:90cd7e5e24af 166 unsigned int ReceiveByte(void); // Receive byte from the ADS1220
sandeepmalladi 0:90cd7e5e24af 167
sandeepmalladi 0:90cd7e5e24af 168 /* ADS1220 Higher Level Functions */
sandeepmalladi 0:90cd7e5e24af 169 unsigned int ReadData(void); // Read the data results
sandeepmalladi 0:90cd7e5e24af 170 void ReadRegister(int StartAddress, int NumRegs, unsigned * pData); // Read the register(s)
sandeepmalladi 0:90cd7e5e24af 171 void WriteRegister(int StartAddress, int NumRegs, unsigned * pData); // Write the register(s)
sandeepmalladi 0:90cd7e5e24af 172 void SendResetCommand(void); // Send a device Reset Command
sandeepmalladi 0:90cd7e5e24af 173 void SendStartCommand(void); // Send a Start/SYNC command
sandeepmalladi 0:90cd7e5e24af 174 void SendShutdownCommand(void); // Place the device in powerdown mode
sandeepmalladi 0:90cd7e5e24af 175
sandeepmalladi 0:90cd7e5e24af 176
sandeepmalladi 0:90cd7e5e24af 177 /* Register Set Value Commands */
sandeepmalladi 0:90cd7e5e24af 178 void Config(void);
sandeepmalladi 0:90cd7e5e24af 179 int SetChannel(int Mux);
sandeepmalladi 0:90cd7e5e24af 180 int SetGain(int Gain);
sandeepmalladi 0:90cd7e5e24af 181 int SetPGABypass(int Bypass);
sandeepmalladi 0:90cd7e5e24af 182 int SetDataRate(int DataRate);
sandeepmalladi 0:90cd7e5e24af 183 int SetClockMode(int ClockMode);
sandeepmalladi 0:90cd7e5e24af 184 int SetPowerDown(int PowerDown);
sandeepmalladi 0:90cd7e5e24af 185 int SetTemperatureMode(int TempMode);
sandeepmalladi 0:90cd7e5e24af 186 int SetBurnOutSource(int BurnOut);
sandeepmalladi 0:90cd7e5e24af 187 int SetVoltageReference(int VoltageRef);
sandeepmalladi 0:90cd7e5e24af 188 int Set50_60Rejection(int Rejection);
sandeepmalladi 0:90cd7e5e24af 189 int SetLowSidePowerSwitch(int PowerSwitch);
sandeepmalladi 0:90cd7e5e24af 190 int SetCurrentDACOutput(int CurrentOutput);
sandeepmalladi 0:90cd7e5e24af 191 int SetIDACRouting(int IDACRoute);
sandeepmalladi 0:90cd7e5e24af 192 int SetDRDYMode(int DRDYMode);
sandeepmalladi 0:90cd7e5e24af 193
sandeepmalladi 0:90cd7e5e24af 194 /* Register Get Value Commands */
sandeepmalladi 0:90cd7e5e24af 195 int GetChannel(void);
sandeepmalladi 0:90cd7e5e24af 196 int GetGain(void);
sandeepmalladi 0:90cd7e5e24af 197 int GetPGABypass(void);
sandeepmalladi 0:90cd7e5e24af 198 int GetDataRate(void);
sandeepmalladi 0:90cd7e5e24af 199 int GetClockMode(void);
sandeepmalladi 0:90cd7e5e24af 200 int GetPowerDown(void);
sandeepmalladi 0:90cd7e5e24af 201 int GetTemperatureMode(void);
sandeepmalladi 0:90cd7e5e24af 202 int GetBurnOutSource(void);
sandeepmalladi 0:90cd7e5e24af 203 int GetVoltageReference(void);
sandeepmalladi 0:90cd7e5e24af 204 int Get50_60Rejection(void);
sandeepmalladi 0:90cd7e5e24af 205 int GetLowSidePowerSwitch(void);
sandeepmalladi 0:90cd7e5e24af 206 int GetCurrentDACOutput(void);
sandeepmalladi 0:90cd7e5e24af 207 int GetIDACRouting(int WhichOne);
sandeepmalladi 0:90cd7e5e24af 208 int GetDRDYMode(void);
sandeepmalladi 0:90cd7e5e24af 209
sandeepmalladi 0:90cd7e5e24af 210 /* Useful Functions within Main Program for Setting Register Contents
sandeepmalladi 0:90cd7e5e24af 211 *
sandeepmalladi 0:90cd7e5e24af 212 * These functions show the programming flow based on the header definitions.
sandeepmalladi 0:90cd7e5e24af 213 * The calls are not made within the demo example, but could easily be used by calling the function
sandeepmalladi 0:90cd7e5e24af 214 * defined within the program to complete a fully useful program.
sandeepmalladi 0:90cd7e5e24af 215 * Similar function calls were made in the firwmare design for the ADS1220EVM.
sandeepmalladi 0:90cd7e5e24af 216 *
sandeepmalladi 0:90cd7e5e24af 217 * The following function calls use ASCII data sent from a COM port to control settings
sandeepmalladi 0:90cd7e5e24af 218 * on the ADS1220. The data is recontructed from ASCII and then combined with the
sandeepmalladi 0:90cd7e5e24af 219 * register contents to save as new configuration settings.
sandeepmalladi 0:90cd7e5e24af 220 *
sandeepmalladi 0:90cd7e5e24af 221 * Function names correspond to datasheet register definitions
sandeepmalladi 0:90cd7e5e24af 222 */
sandeepmalladi 0:90cd7e5e24af 223 void set_MUX(char c);
sandeepmalladi 0:90cd7e5e24af 224 void set_GAIN(char c);
sandeepmalladi 0:90cd7e5e24af 225 void set_PGA_BYPASS(char c);
sandeepmalladi 0:90cd7e5e24af 226 void set_DR(char c);
sandeepmalladi 0:90cd7e5e24af 227 void set_MODE(char c);
sandeepmalladi 0:90cd7e5e24af 228 void set_CM(char c);
sandeepmalladi 0:90cd7e5e24af 229 void set_TS(char c);
sandeepmalladi 0:90cd7e5e24af 230 void set_BCS(char c);
sandeepmalladi 0:90cd7e5e24af 231 void set_VREF(char c);
sandeepmalladi 0:90cd7e5e24af 232 void set_50_60(char c);
sandeepmalladi 0:90cd7e5e24af 233 void set_PSW(char c);
sandeepmalladi 0:90cd7e5e24af 234 void set_IDAC(char c);
sandeepmalladi 0:90cd7e5e24af 235 void set_IMUX(char c, int i);
sandeepmalladi 0:90cd7e5e24af 236 void set_DRDYM(char c);
sandeepmalladi 0:90cd7e5e24af 237 void set_ERROR(void);
sandeepmalladi 0:90cd7e5e24af 238 void set_ERROR_Transmit(void);
sandeepmalladi 0:90cd7e5e24af 239 void set_ERROR_Receive(void);
sandeepmalladi 0:90cd7e5e24af 240
sandeepmalladi 0:90cd7e5e24af 241 private:
sandeepmalladi 0:90cd7e5e24af 242 SPI _device;
sandeepmalladi 0:90cd7e5e24af 243 uint8_t _address;
sandeepmalladi 0:90cd7e5e24af 244 uint8_t _Comdelay;
sandeepmalladi 0:90cd7e5e24af 245
sandeepmalladi 0:90cd7e5e24af 246 size_t printNumber(unsigned long n, uint8_t base);
sandeepmalladi 0:90cd7e5e24af 247 size_t printFloat(double number, uint8_t digits);
sandeepmalladi 0:90cd7e5e24af 248 };
sandeepmalladi 0:90cd7e5e24af 249
sandeepmalladi 0:90cd7e5e24af 250 #endif /*ADS1220_H_*/