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.
Diff: ad5933.cpp
- Revision:
- 7:0e8dabae876c
- Parent:
- 6:6b9fc31d51b0
- Child:
- 8:20576c73ba85
diff -r 6b9fc31d51b0 -r 0e8dabae876c ad5933.cpp
--- a/ad5933.cpp Wed May 20 09:17:24 2015 +0000
+++ b/ad5933.cpp Fri Aug 20 23:27:15 2021 +0300
@@ -29,10 +29,11 @@
#define WRITE_CMD 0x1A // adress + write command
#define READ_CMD 0x1B // adress + read command
-#define CLOCK_FREQ 0x00F42400
+#define CLOCK_FREQ 16000000 // internal clock frequency in Hz
#define I2C_FREQ 400000
#define WAITTIME 1800 // time to wait before polling for response
+#define POW_2_27 134217728 // 2 to the power of 27
AD5933::AD5933(PinName sda, PinName scl, bool extClk) : sCom(sda, scl)
{
@@ -101,8 +102,8 @@
bool AD5933::setFrequencySweepParam(unsigned int startFreq, unsigned int stepFreq, unsigned int nrOfSteps)
{
- unsigned int startFreqCode = startFreq/CLOCK_FREQ*0x00000004*0x08000000;
- unsigned int stepFreqCode = stepFreq/CLOCK_FREQ*0x00000004*0x08000000;
+ unsigned int startFreqCode = (unsigned int)((double) startFreq * 4 / CLOCK_FREQ * POW_2_27);
+ unsigned int stepFreqCode = (unsigned int)((double) stepFreq * 4 / CLOCK_FREQ * POW_2_27);
bool output = setRegister(0x82,(startFreqCode >> 16));
output &= setRegister(0x83,(startFreqCode >> 8));
@@ -120,13 +121,12 @@
{
bool output = setFrequencySweepParam(startFreq, stepFreq, nrOfSteps);
output &= setSettlingTime(nrOfCycles);
+ output &= setAnalogCircuit(PGA, RangeNr);
output &= standby();
- output &= setAnalogCircuit(PGA, RangeNr);
output &= setControlReg(INIT_FREQ);
- wait_ms(5);
+ wait_us(WAITTIME);
output &= setControlReg(INIT_SWEEP);
wait_us(WAITTIME);
- output &= getData();
return output;
}
@@ -158,6 +158,7 @@
switch(RangeNr) {
case 1:
PGAandVoltout |= 0x00;
+ break;
case 2:
PGAandVoltout |= 0x06;
break;
@@ -198,38 +199,32 @@
return setControlReg(POWERDOWN);
}
-bool AD5933::Measure(bool increment)
+uint8_t AD5933::Measure(bool increment)
{
if(increment) {
setControlReg(INCR_FREQ);
- wait_us(WAITTIME);
return getData();
} else {
- setControlReg(0x00);
setControlReg(REPE_FREQ);
- wait_us(WAITTIME);
return getData();
}
}
-bool AD5933::getData()
+uint8_t AD5933::getData()
{
- int i = 0;
uint8_t data[4];
- bool output;
+ uint8_t status = getRegister(0x8F) & 0x0F;
+
+ while ((status & 0x02) == 0) { // pull status register until valid real/img data
+ status = getRegister(0x8F) & 0x0F;
+ }
- while(((getRegister(0x8F) & 0x02) != 0x02) && i < 10) {
- wait_us(500);
- i++;
- }
- if(i == 10)
- output = false;
+ gotoAdressPointer(0x94);
+ readBlock(data, 4);
- output &= gotoAdressPointer(0x82);
- output &= readBlock(data, 4);
real = data[0] << 8 | data[1];
imaginary = data[2] << 8 | data[3];
- return output;
+ return status;
}
float AD5933::getTemperature()