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.
ad5933.cpp
00001 /* --------------------------------------------------------------------------------- 00002 Copyright 2015 Marijn Billiet 00003 00004 Licensed under the Apache License, Version 2.0 (the "License"); 00005 you may not use this file except in compliance with the License. 00006 You may obtain a copy of the License at 00007 00008 http://www.apache.org/licenses/LICENSE-2.0 00009 00010 Unless required by applicable law or agreed to in writing, software 00011 distributed under the License is distributed on an "AS IS" BASIS, 00012 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 See the License for the specific language governing permissions and 00014 limitations under the License. 00015 --------------------------------------------------------------------------------- */ 00016 00017 #include "ad5933.h" 00018 #include "mbed.h" 00019 00020 // Define Command bytes 00021 #define INIT_FREQ 0x10 // initialise startfreq 00022 #define INIT_SWEEP 0x20 // initialise sweep 00023 #define INCR_FREQ 0x30 // increment frequency 00024 #define REPE_FREQ 0x40 // repeat frequency 00025 #define STANDBY 0xB0 // standby 00026 #define POWERDOWN 0xA0 // PowerDown modus 00027 #define MEAS_TEMP 0x90 // temperature 00028 00029 #define WRITE_CMD 0x1A // adress + write command 00030 #define READ_CMD 0x1B // adress + read command 00031 00032 #define CLOCK_FREQ 0x00F42400 00033 #define I2C_FREQ 400000 00034 00035 #define WAITTIME 1800 // time to wait before polling for response 00036 00037 AD5933::AD5933(PinName sda, PinName scl, bool extClk) : sCom(sda, scl) 00038 { 00039 sCom.frequency(I2C_FREQ); 00040 PGAandVoltout = 0x00; 00041 _extClk = extClk; 00042 } 00043 00044 bool AD5933::gotoAdressPointer(uint8_t Adress) 00045 { 00046 sCom.start(); 00047 bool output = (sCom.write(WRITE_CMD) + sCom.write(0xB0) + sCom.write(Adress)) == 3; 00048 sCom.stop(); 00049 return output; 00050 } 00051 00052 bool AD5933::setRegister(uint8_t RegisterAdress, uint8_t RegisterValue) 00053 { 00054 sCom.start(); 00055 bool output = (sCom.write(WRITE_CMD) + sCom.write(RegisterAdress) + sCom.write(RegisterValue)) == 3; 00056 sCom.stop(); 00057 return output; 00058 } 00059 00060 bool AD5933::writeBlock(uint8_t ByteArray[], uint8_t sizeArray) 00061 { 00062 sCom.start(); 00063 bool output = (sCom.write(WRITE_CMD) + sCom.write(0xA0) + sCom.write(sizeArray)) == 3; 00064 for(uint8_t i = 0; i<sizeArray; i++) { 00065 output = sCom.write(ByteArray[i]) == 1 && output; 00066 } 00067 sCom.stop(); 00068 return output; 00069 } 00070 00071 uint8_t AD5933::getRegister(uint8_t RegisterAdress) 00072 { 00073 gotoAdressPointer(RegisterAdress); 00074 00075 uint8_t output = 0xFF; 00076 sCom.start(); 00077 if(sCom.write(READ_CMD) == 1) 00078 output = sCom.read(0); 00079 sCom.stop(); 00080 return output; 00081 } 00082 00083 bool AD5933::readBlock(uint8_t* ByteArray, uint8_t sizeArray) 00084 { 00085 sCom.start(); 00086 bool output = (sCom.write(WRITE_CMD) + sCom.write(0xA1) + sCom.write(sizeArray)) == 3; 00087 sCom.start(); 00088 output = output && (sCom.write(READ_CMD) == 1); 00089 for(uint8_t i = 0; i<sizeArray-1; i++) { 00090 ByteArray[i] = sCom.read(1); 00091 } 00092 ByteArray[sizeArray-1] = sCom.read(0); 00093 sCom.stop(); 00094 return output; 00095 } 00096 00097 bool AD5933::setControlReg(uint8_t Command) 00098 { 00099 return setRegister(0x80, PGAandVoltout | Command); 00100 } 00101 00102 bool AD5933::setFrequencySweepParam(unsigned int startFreq, unsigned int stepFreq, unsigned int nrOfSteps) 00103 { 00104 unsigned int startFreqCode = startFreq/CLOCK_FREQ*0x00000004*0x08000000; 00105 unsigned int stepFreqCode = stepFreq/CLOCK_FREQ*0x00000004*0x08000000; 00106 00107 bool output = setRegister(0x82,(startFreqCode >> 16)); 00108 output &= setRegister(0x83,(startFreqCode >> 8)); 00109 output &= setRegister(0x84,(startFreqCode)); 00110 output &= setRegister(0x85,(stepFreqCode >> 16)); 00111 output &= setRegister(0x86,(stepFreqCode >> 8)); 00112 output &= setRegister(0x87,(stepFreqCode)); 00113 output &= setRegister(0x88,(nrOfSteps >> 8)); 00114 output &= setRegister(0x89,nrOfSteps); 00115 00116 return output; 00117 } 00118 00119 bool AD5933::initFrequencySweepParam(unsigned int startFreq, unsigned int stepFreq, unsigned int nrOfSteps, unsigned int nrOfCycles, bool PGA, int RangeNr) 00120 { 00121 bool output = setFrequencySweepParam(startFreq, stepFreq, nrOfSteps); 00122 output &= setSettlingTime(nrOfCycles); 00123 output &= standby(); 00124 output &= setAnalogCircuit(PGA, RangeNr); 00125 output &= setControlReg(INIT_FREQ); 00126 wait_ms(5); 00127 output &= setControlReg(INIT_SWEEP); 00128 wait_us(WAITTIME); 00129 output &= getData(); 00130 00131 return output; 00132 } 00133 00134 bool AD5933::setSettlingTime(unsigned int nrOfCycles) 00135 { 00136 bool output = true; 00137 00138 if (nrOfCycles > 1022) { 00139 output &= setRegister(0x8A,((nrOfCycles/4) >> 8) | 0x06); 00140 output &= setRegister(0x8B,(nrOfCycles/4)); 00141 } else if(nrOfCycles > 511) { 00142 output &= setRegister(0x8A,((nrOfCycles/4) >> 8) | 0x02); 00143 output &= setRegister(0x8B,(nrOfCycles/2)); 00144 } else { 00145 output &= setRegister(0x8A,0x00); 00146 output &= setRegister(0x8B,nrOfCycles); 00147 } 00148 return output; 00149 } 00150 00151 bool AD5933::setAnalogCircuit(bool PGA, int RangeNr) 00152 { 00153 if(PGA) 00154 PGAandVoltout = 0x01; 00155 else 00156 PGAandVoltout = 0x00; 00157 00158 switch(RangeNr) { 00159 case 1: 00160 PGAandVoltout |= 0x00; 00161 case 2: 00162 PGAandVoltout |= 0x06; 00163 break; 00164 case 3: 00165 PGAandVoltout |= 0x04; 00166 break; 00167 case 4: 00168 PGAandVoltout |= 0x02; 00169 break; 00170 } 00171 00172 uint8_t data = 0x00; 00173 if(_extClk) 00174 data |= 0x08; 00175 00176 bool output = setRegister(0x81, data); 00177 output &= setRegister(0x80,PGAandVoltout); 00178 00179 return output; 00180 } 00181 00182 bool AD5933::reset() 00183 { 00184 uint8_t data = 0x10; 00185 if(_extClk) 00186 data |= 0x08; 00187 00188 return setRegister(0x81, data); 00189 } 00190 00191 bool AD5933::standby() 00192 { 00193 return setControlReg(STANDBY); 00194 } 00195 00196 bool AD5933::powerdown() 00197 { 00198 return setControlReg(POWERDOWN); 00199 } 00200 00201 bool AD5933::Measure(bool increment) 00202 { 00203 if(increment) { 00204 setControlReg(INCR_FREQ); 00205 wait_us(WAITTIME); 00206 return getData(); 00207 } else { 00208 setControlReg(0x00); 00209 setControlReg(REPE_FREQ); 00210 wait_us(WAITTIME); 00211 return getData(); 00212 } 00213 } 00214 00215 bool AD5933::getData() 00216 { 00217 int i = 0; 00218 uint8_t data[4]; 00219 bool output; 00220 00221 while(((getRegister(0x8F) & 0x02) != 0x02) && i < 10) { 00222 wait_us(500); 00223 i++; 00224 } 00225 if(i == 10) 00226 output = false; 00227 00228 output &= gotoAdressPointer(0x82); 00229 output &= readBlock(data, 4); 00230 real = data[0] << 8 | data[1]; 00231 imaginary = data[2] << 8 | data[3]; 00232 return output; 00233 } 00234 00235 float AD5933::getTemperature() 00236 { 00237 int i = 0; 00238 uint8_t data[2]; 00239 00240 setControlReg(MEAS_TEMP); 00241 wait_us(WAITTIME); 00242 00243 while(((getRegister(0x8F) & 0x01) != 0x01) && i < 10) { 00244 wait_us(500); 00245 i++; 00246 } 00247 if(i == 10) 00248 return -1; 00249 00250 gotoAdressPointer(0x92); 00251 readBlock(data, 2); 00252 00253 if((data[0] >> 6) & 1) { 00254 //negative temperature 00255 return (((data[0] << 8 | data[1]) - 16384)/32.0) ; 00256 } else { 00257 return ((data[0] << 8 | data[1])/32.0) ; 00258 } 00259 }
Generated on Thu Jul 14 2022 01:54:59 by
