using AD5933 to measure impedances
Dependencies: 4DGL-uLCD-SE mbed
AD5933.h@71:91f99e81cd1c, 2016-12-13 (annotated)
- Committer:
- mvaca3
- Date:
- Tue Dec 13 15:50:29 2016 +0000
- Revision:
- 71:91f99e81cd1c
- Parent:
- 65:c3324ec84a48
fixed fmod issue
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nhardy6 | 17:ee9374e89c54 | 1 | #include <mbed.h> |
nhardy6 | 17:ee9374e89c54 | 2 | #include "uLCD_4DGL.h" |
nhardy6 | 17:ee9374e89c54 | 3 | |
nhardy6 | 17:ee9374e89c54 | 4 | #ifndef AD5933_H |
nhardy6 | 17:ee9374e89c54 | 5 | #define AD5933_H |
nhardy6 | 17:ee9374e89c54 | 6 | |
nhardy6 | 17:ee9374e89c54 | 7 | class AD5933 |
nhardy6 | 17:ee9374e89c54 | 8 | { |
nhardy6 | 17:ee9374e89c54 | 9 | public: |
nhardy6 | 17:ee9374e89c54 | 10 | //constructor |
Luke95 | 49:f9b34fc28449 | 11 | AD5933(I2C* i2c, Serial *pc, Serial *bt); |
nhardy6 | 17:ee9374e89c54 | 12 | |
nhardy6 | 17:ee9374e89c54 | 13 | //functions |
Luke95 | 63:a85af2f0f13b | 14 | void calibrate(double); //setup device |
nhardy6 | 48:3e9159532341 | 15 | void findZ(int); //finds impedance |
nhardy6 | 48:3e9159532341 | 16 | float getZ(); //returns impedance |
nhardy6 | 48:3e9159532341 | 17 | float getPhase(); //returns phase |
nhardy6 | 17:ee9374e89c54 | 18 | private: |
nhardy6 | 17:ee9374e89c54 | 19 | //devices |
nhardy6 | 17:ee9374e89c54 | 20 | I2C* _i2c; |
nhardy6 | 17:ee9374e89c54 | 21 | Serial* _pc; |
nhardy6 | 17:ee9374e89c54 | 22 | Serial* _bt; |
nhardy6 | 17:ee9374e89c54 | 23 | //variables |
nhardy6 | 17:ee9374e89c54 | 24 | //data |
Luke95 | 64:0dd5e0e2f8bf | 25 | int _real[10]; //real/imaginary values for a sweep |
Luke95 | 64:0dd5e0e2f8bf | 26 | int _imag[10]; |
Luke95 | 49:f9b34fc28449 | 27 | int _r; //real/imaginary values for a single frequency reading |
Luke95 | 40:8cad112eb8aa | 28 | int _i; |
nhardy6 | 23:458b9d572628 | 29 | int _temp[2]; //temperature (calibrated and current value) |
Luke95 | 65:c3324ec84a48 | 30 | long double _z; //impedance array |
Luke95 | 65:c3324ec84a48 | 31 | long double _phs; //real phase array |
nhardy6 | 23:458b9d572628 | 32 | //for calibration |
Luke95 | 65:c3324ec84a48 | 33 | long double _gain[10]; //gain |
Luke95 | 64:0dd5e0e2f8bf | 34 | float _phase[10]; //phase |
nhardy6 | 25:b74e22cfe52a | 35 | float _ref; //reference resistance |
nhardy6 | 17:ee9374e89c54 | 36 | //settings |
nhardy6 | 23:458b9d572628 | 37 | int _range; //output voltage range |
nhardy6 | 17:ee9374e89c54 | 38 | int _freq; //start frequency |
nhardy6 | 17:ee9374e89c54 | 39 | int _inc; //increment |
nhardy6 | 23:458b9d572628 | 40 | int _n; //number of increments |
Luke95 | 49:f9b34fc28449 | 41 | char _pga; |
nhardy6 | 17:ee9374e89c54 | 42 | //functions |
nhardy6 | 48:3e9159532341 | 43 | void sweep(); //gets the data |
Luke95 | 49:f9b34fc28449 | 44 | void sweep1(); |
nhardy6 | 48:3e9159532341 | 45 | void configure(); //ready the device for sweep |
nhardy6 | 48:3e9159532341 | 46 | void read(int); //get value |
nhardy6 | 48:3e9159532341 | 47 | void calculate(); //cleans value |
nhardy6 | 48:3e9159532341 | 48 | float getTemp(); //returns temperature |
nhardy6 | 17:ee9374e89c54 | 49 | }; |
nhardy6 | 17:ee9374e89c54 | 50 | |
nhardy6 | 17:ee9374e89c54 | 51 | //Register defines |
nhardy6 | 17:ee9374e89c54 | 52 | |
nhardy6 | 17:ee9374e89c54 | 53 | //control |
nhardy6 | 17:ee9374e89c54 | 54 | #define CTRL_HIGH 0x80 |
nhardy6 | 17:ee9374e89c54 | 55 | #define CTRL_LOW 0x81 |
nhardy6 | 17:ee9374e89c54 | 56 | //start frequency |
nhardy6 | 17:ee9374e89c54 | 57 | #define FREQ_HIGH 0x82 |
nhardy6 | 17:ee9374e89c54 | 58 | #define FREQ_MID 0x83 |
nhardy6 | 17:ee9374e89c54 | 59 | #define FREQ_LOW 0x84 |
nhardy6 | 17:ee9374e89c54 | 60 | //frequency increment |
nhardy6 | 17:ee9374e89c54 | 61 | #define FINC_HIGH 0x85 |
nhardy6 | 17:ee9374e89c54 | 62 | #define FINC_MID 0x86 |
nhardy6 | 17:ee9374e89c54 | 63 | #define FINC_LOW 0x87 |
nhardy6 | 17:ee9374e89c54 | 64 | //number of increments |
nhardy6 | 17:ee9374e89c54 | 65 | #define NINC_HIGH 0x88 |
nhardy6 | 17:ee9374e89c54 | 66 | #define NINC_LOW 0x89 |
nhardy6 | 17:ee9374e89c54 | 67 | //number of settling time cycles |
nhardy6 | 17:ee9374e89c54 | 68 | #define SET_CYCLE 0x8A |
nhardy6 | 17:ee9374e89c54 | 69 | #define CYCLES 0x8B |
nhardy6 | 17:ee9374e89c54 | 70 | //status |
nhardy6 | 17:ee9374e89c54 | 71 | #define STATUS 0x8F |
nhardy6 | 17:ee9374e89c54 | 72 | //temperature data |
nhardy6 | 17:ee9374e89c54 | 73 | #define TEMP_HIGH 0x92 |
nhardy6 | 17:ee9374e89c54 | 74 | #define TEMP_LOW 0x93 |
nhardy6 | 17:ee9374e89c54 | 75 | //real data |
nhardy6 | 17:ee9374e89c54 | 76 | #define REAL_HIGH 0x94 |
nhardy6 | 17:ee9374e89c54 | 77 | #define REAL_LOW 0x95 |
nhardy6 | 17:ee9374e89c54 | 78 | //imaginary data |
nhardy6 | 17:ee9374e89c54 | 79 | #define IMAG_HIGH 0x96 |
nhardy6 | 17:ee9374e89c54 | 80 | #define IMAG_LOW 0x97 |
nhardy6 | 17:ee9374e89c54 | 81 | |
nhardy6 | 23:458b9d572628 | 82 | //Constants |
nhardy6 | 23:458b9d572628 | 83 | |
nhardy6 | 17:ee9374e89c54 | 84 | // Temperature measuring |
nhardy6 | 17:ee9374e89c54 | 85 | #define TEMP_MEASURE (CTRL_TEMP_MEASURE) |
nhardy6 | 17:ee9374e89c54 | 86 | #define TEMP_NO_MEASURE (CTRL_NO_OPERATION) |
nhardy6 | 17:ee9374e89c54 | 87 | // Clock sources |
nhardy6 | 17:ee9374e89c54 | 88 | #define CLOCK_INTERNAL (CTRL_CLOCK_INTERNAL) |
nhardy6 | 17:ee9374e89c54 | 89 | #define CLOCK_EXTERNAL (CTRL_CLOCK_EXTERNAL) |
nhardy6 | 17:ee9374e89c54 | 90 | // PGA gain options |
nhardy6 | 17:ee9374e89c54 | 91 | #define PGA_GAIN_X1 (CTRL_PGA_GAIN_X1) |
nhardy6 | 17:ee9374e89c54 | 92 | #define PGA_GAIN_X5 (CTRL_PGA_GAIN_X5) |
nhardy6 | 17:ee9374e89c54 | 93 | // Power modes |
nhardy6 | 17:ee9374e89c54 | 94 | #define POWER_STANDBY (CTRL_STANDBY_MODE) |
nhardy6 | 17:ee9374e89c54 | 95 | #define POWER_DOWN (CTRL_POWER_DOWN_MODE) |
nhardy6 | 17:ee9374e89c54 | 96 | #define POWER_ON (CTRL_NO_OPERATION) |
nhardy6 | 17:ee9374e89c54 | 97 | // I2C result success/fail |
nhardy6 | 17:ee9374e89c54 | 98 | #define I2C_RESULT_SUCCESS (0) |
nhardy6 | 17:ee9374e89c54 | 99 | #define I2C_RESULT_DATA_TOO_LONG (1) |
nhardy6 | 17:ee9374e89c54 | 100 | #define I2C_RESULT_ADDR_NAK (2) |
nhardy6 | 17:ee9374e89c54 | 101 | #define I2C_RESULT_DATA_NAK (3) |
nhardy6 | 17:ee9374e89c54 | 102 | #define I2C_RESULT_OTHER_FAIL (4) |
nhardy6 | 17:ee9374e89c54 | 103 | // Control register options |
nhardy6 | 17:ee9374e89c54 | 104 | #define CTRL_NO_OPERATION (0b00000000) |
nhardy6 | 17:ee9374e89c54 | 105 | #define CTRL_INIT_START_FREQ (0b00010000) |
nhardy6 | 17:ee9374e89c54 | 106 | #define CTRL_START_FREQ_SWEEP (0b00100000) |
nhardy6 | 17:ee9374e89c54 | 107 | #define CTRL_INCREMENT_FREQ (0b00110000) |
nhardy6 | 17:ee9374e89c54 | 108 | #define CTRL_REPEAT_FREQ (0b01000000) |
nhardy6 | 17:ee9374e89c54 | 109 | #define CTRL_TEMP_MEASURE (0b10010000) |
nhardy6 | 17:ee9374e89c54 | 110 | #define CTRL_POWER_DOWN_MODE (0b10100000) |
nhardy6 | 17:ee9374e89c54 | 111 | #define CTRL_STANDBY_MODE (0b10110000) |
nhardy6 | 17:ee9374e89c54 | 112 | #define CTRL_RESET (0b00010000) |
nhardy6 | 17:ee9374e89c54 | 113 | #define CTRL_CLOCK_EXTERNAL (0b00001000) |
nhardy6 | 17:ee9374e89c54 | 114 | #define CTRL_CLOCK_INTERNAL (0b00000000) |
nhardy6 | 17:ee9374e89c54 | 115 | #define CTRL_PGA_GAIN_X1 (0b00000001) |
nhardy6 | 17:ee9374e89c54 | 116 | #define CTRL_PGA_GAIN_X5 (0b00000000) |
nhardy6 | 17:ee9374e89c54 | 117 | // Status register options |
nhardy6 | 17:ee9374e89c54 | 118 | #define STATUS_TEMP_VALID (0x01) |
nhardy6 | 17:ee9374e89c54 | 119 | #define STATUS_DATA_VALID (0x02) |
nhardy6 | 17:ee9374e89c54 | 120 | #define STATUS_SWEEP_DONE (0x04) |
nhardy6 | 17:ee9374e89c54 | 121 | #define STATUS_ERROR (0xFF) |
nhardy6 | 17:ee9374e89c54 | 122 | // Frequency sweep parameters |
nhardy6 | 17:ee9374e89c54 | 123 | #define SWEEP_DELAY (1) |
nhardy6 | 17:ee9374e89c54 | 124 | |
nhardy6 | 17:ee9374e89c54 | 125 | #endif |