Marijn Billiet / AD5933

Files at this revision

API Documentation at this revision

Comitter:
dipi
Date:
Wed May 20 09:17:24 2015 +0000
Parent:
5:2dc8c3f02788
Commit message:
Beta version

Changed in this revision

ad5933.cpp Show annotated file Show diff for this revision Revisions of this file
ad5933.h Show annotated file Show diff for this revision Revisions of this file
--- a/ad5933.cpp	Wed May 13 14:07:40 2015 +0000
+++ b/ad5933.cpp	Wed May 20 09:17:24 2015 +0000
@@ -23,23 +23,22 @@
 #define INCR_FREQ  0x30     // increment frequency
 #define REPE_FREQ  0x40     // repeat frequency
 #define STANDBY    0xB0     // standby
+#define POWERDOWN  0xA0     // PowerDown modus
 #define MEAS_TEMP  0x90     // temperature
 
 #define WRITE_CMD  0x1A     // adress + write command
 #define READ_CMD   0x1B     // adress + read command
 
 #define CLOCK_FREQ 0x00F42400
+#define I2C_FREQ   400000
+
+#define WAITTIME   1800     // time to wait before polling for response
 
 AD5933::AD5933(PinName sda, PinName scl, bool extClk) : sCom(sda, scl)
 {
-    sCom.frequency(400000);
-    firstMeasurement = true;
+    sCom.frequency(I2C_FREQ);
     PGAandVoltout = 0x00;
     _extClk = extClk;
-    //if(_extClk)
-    //    setRegister(0x81, 0x08);
-    //else
-    //    setRegister(0x81, 0x00);
 }
 
 bool AD5933::gotoAdressPointer(uint8_t Adress)
@@ -114,13 +113,28 @@
     output &= setRegister(0x88,(nrOfSteps >> 8));
     output &= setRegister(0x89,nrOfSteps);
 
-    firstMeasurement = true;
+    return output;
+}
+
+bool AD5933::initFrequencySweepParam(unsigned int startFreq, unsigned int stepFreq, unsigned int nrOfSteps, unsigned int nrOfCycles, bool PGA, int RangeNr)
+{
+    bool output = setFrequencySweepParam(startFreq, stepFreq, nrOfSteps);
+    output &= setSettlingTime(nrOfCycles);
+    output &= standby();
+    output &= setAnalogCircuit(PGA, RangeNr);
+    output &= setControlReg(INIT_FREQ);
+    wait_ms(5);
+    output &= setControlReg(INIT_SWEEP);
+    wait_us(WAITTIME);
+    output &= getData();
+
     return output;
 }
 
 bool AD5933::setSettlingTime(unsigned int nrOfCycles)
 {
     bool output = true;
+
     if (nrOfCycles > 1022) {
         output &= setRegister(0x8A,((nrOfCycles/4) >> 8) | 0x06);
         output &= setRegister(0x8B,(nrOfCycles/4));
@@ -154,8 +168,15 @@
             PGAandVoltout |= 0x02;
             break;
     }
-    firstMeasurement = true;
-    return true;
+
+    uint8_t data = 0x00;
+    if(_extClk)
+        data |= 0x08;
+
+    bool output = setRegister(0x81, data);
+    output &= setRegister(0x80,PGAandVoltout);
+
+    return output;
 }
 
 bool AD5933::reset()
@@ -170,26 +191,23 @@
 bool AD5933::standby()
 {
     return setControlReg(STANDBY);
+}
 
+bool AD5933::powerdown()
+{
+    return setControlReg(POWERDOWN);
 }
 
 bool AD5933::Measure(bool increment)
 {
-    if(firstMeasurement) {
-        setControlReg(INIT_FREQ);
-        wait_ms(1000);
-
-        setControlReg(INIT_SWEEP);
-        wait_ms(5);
-        firstMeasurement = false;
-        return getData();
-    } else if(increment) {
+    if(increment) {
         setControlReg(INCR_FREQ);
-        wait_ms(5);
+        wait_us(WAITTIME);
         return getData();
     } else {
+        setControlReg(0x00);
         setControlReg(REPE_FREQ);
-        wait_ms(5);
+        wait_us(WAITTIME);
         return getData();
     }
 }
@@ -201,7 +219,7 @@
     bool output;
 
     while(((getRegister(0x8F) & 0x02)  != 0x02) && i < 10) {
-        wait_ms(50);
+        wait_us(500);
         i++;
     }
     if(i == 10)
@@ -218,12 +236,12 @@
 {
     int i = 0;
     uint8_t data[2];
-    
+
     setControlReg(MEAS_TEMP);
-    wait_ms(5);
+    wait_us(WAITTIME);
 
     while(((getRegister(0x8F) & 0x01) != 0x01) && i < 10) {
-        wait_ms(5);
+        wait_us(500);
         i++;
     }
     if(i == 10)
--- a/ad5933.h	Wed May 13 14:07:40 2015 +0000
+++ b/ad5933.h	Wed May 20 09:17:24 2015 +0000
@@ -12,6 +12,7 @@
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
+   
  --------------------------------------------------------------------------------- */
 
 #ifndef MBED_AD5933_H
@@ -21,6 +22,31 @@
 
 /** AD5933 class.
  *  Library to communicate with the AD5933 impedance meter chip.
+ *
+ * Example:
+ * @code
+ * #include "mbed.h"
+ * #include "ad5933.h"
+ *
+ * AD5933 Zmeter(p28, p27, false);
+ *
+ * int main() {
+ *     float temp = Zmeter.getTemperature();
+ *
+ *     Zmeter.initFrequencySweepParam(100000, 10, 2, 50, true, 1);
+ *
+ *     while(true) {
+ *          if(!Zmeter.Measure(true))
+ *              printf("error occured");
+ *          
+ *          int real = Zmeter.real;
+ *          int imag = Zmeter.imaginary;
+ *
+ *          wait(1);
+ *     }
+ *     
+ * }
+ * @endcode
  */
 class AD5933
 {
@@ -31,50 +57,64 @@
     @param extClk source of the Clock signal:  true = external;  false = internal
     */
     AD5933(PinName sda, PinName scl, bool extClk);
-    
-    /** Set all parameters concerning a frequency sweep.
-    @param startFreq initial frequency (max 100000Hz)
-    @param stepFreq stepsize of increment in frequency
-    @param nrOfSteps number of increments
-    */    
-    bool setFrequencySweepParam(unsigned int startFreq, unsigned int stepFreq, unsigned int nrOfSteps);
-    
+
     /** Time to wait before the ADC starts to sample
     @param nrOfCycles Number of cycles (outputfrequency) to wait for sampling can start  (max 2044)
-    */    
+    @return true on succes, false on failure
+    */
     bool setSettlingTime(unsigned int nrOfCycles);
-    
+
     /** Set the gain of the pre-amplification and the output voltage range
     @param PGA Gain of the pre-amplifier (true = x1; false = x5)
     @param RangeNr Set the output voltage (1 = 2V; 2 = 1V; 3 = 400mV; 4 = 200mV)
-    */ 
+    @return true on succes, false on failure
+    */
     bool setAnalogCircuit(bool PGA, int RangeNr);
-    
+
     /** Measure Impedance
     @param increment (true = goto next frequency in sweep; false = remeasure at current frequency)
-    */     
+    @return true on succes, false on failure
+    */
     bool Measure(bool increment);
-    
+
     /// Resets Device
+    ///@return true on succes, false on failure
     bool reset();
-    
+
     /// Get temperature of chip
+    ///@return temperature in degrees Celcius
     float getTemperature();
-    
+
+    /// Puts the Device in Standby Mode
+    ///@return true on succes, false on failure
     bool standby();
-    
+
+    /// Puts the Device in PowerDown Mode
+    ///@return true on succes, false on failure
+    bool powerdown();
+
+    /** Initialises a frequency sweep.
+    @param startFreq initial frequency (max 100000Hz)
+    @param stepFreq stepsize of increment in frequency
+    @param nrOfSteps number of increments
+    @param nrOfCycles Number of cycles (outputfrequency) to wait for sampling can start  (max 2044)
+    @param PGA Gain of the pre-amplifier (true = x1; false = x5)
+    @param RangeNr Set the output voltage (1 = 2V; 2 = 1V; 3 = 400mV; 4 = 200mV)
+    @return true on succes, false on failure
+    */
+    bool initFrequencySweepParam(unsigned int startFreq, unsigned int stepFreq, unsigned int nrOfSteps, unsigned int nrOfCycles, bool PGA, int RangeNr);
+
     /// real part of impedance (uncalibrated)
     unsigned int real;
-    
+
     /// imaginary part of impedance (uncalibrated)
     unsigned int imaginary;
-    
-    
+
 private:
     I2C sCom;
     uint8_t PGAandVoltout;
-    bool firstMeasurement;
 
+    bool setFrequencySweepParam(unsigned int startFreq, unsigned int stepFreq, unsigned int nrOfSteps);
     bool gotoAdressPointer(uint8_t Adress);
     bool setRegister(uint8_t RegisterAdress, uint8_t RegisterValue);
     bool writeBlock(uint8_t ByteArray[], uint8_t sizeArray);