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
- Committer:
- dipi
- Date:
- 2015-05-11
- Revision:
- 0:6a71184e6f66
- Child:
- 2:93dd1ebfedea
File content as of revision 0:6a71184e6f66:
#include "ad5933.h"
#include "mbed.h"
// Define Command bytes
#define INIT_FREQ 0x10 // initialise startfreq
#define INIT_SWEEP 0x20 // initialise sweep
#define INCR_FREQ 0x30 // increment frequency
#define REPE_FREQ 0x40 // repeat frequency
#define STANDBY 0xB0 // standby
#define MEAS_TEMP 0x90 // temperature
#define WRITE_CMD 0x1A // adress + write command
#define READ_CMD 0x1B // adress + read command
#define CLOCK_FREQ 0x00F42400
AD5933::AD5933(PinName sda, PinName scl, bool extClk) : sCom(sda, scl)
{
sCom.frequency(400000);
firstMeasurement = true;
PGAandVoltout = 0x01;
_extClk = extClk;
if(_extClk)
setRegister(0x81, 0x08);
else
setRegister(0x81, 0x00);
}
bool AD5933::gotoAdressPointer(uint8_t Adress)
{
sCom.start();
bool output = (sCom.write(WRITE_CMD) + sCom.write(0xB0) + sCom.write(Adress)) == 3;
sCom.stop();
return output;
}
bool AD5933::setRegister(uint8_t RegisterAdress, uint8_t RegisterValue)
{
sCom.start();
bool output = (sCom.write(WRITE_CMD) + sCom.write(RegisterAdress) + sCom.write(RegisterValue)) == 3;
sCom.stop();
return output;
}
bool AD5933::writeBlock(uint8_t ByteArray[], uint8_t sizeArray)
{
sCom.start();
bool output = (sCom.write(WRITE_CMD) + sCom.write(0xA0) + sCom.write(sizeArray)) == 3;
for(uint8_t i = 0; i<sizeArray; i++) {
output = sCom.write(ByteArray[i]) == 1 && output;
}
sCom.stop();
return output;
}
uint8_t AD5933::getRegister(uint8_t RegisterAdress)
{
gotoAdressPointer(RegisterAdress);
uint8_t output = 0xFF;
sCom.start();
if(sCom.write(READ_CMD) == 1)
output = sCom.read(0);
sCom.stop();
return output;
}
bool AD5933::readBlock(uint8_t* ByteArray, uint8_t sizeArray)
{
sCom.start();
bool output = (sCom.write(WRITE_CMD) + sCom.write(0xA1) + sCom.write(sizeArray)) == 3;
sCom.start();
output = output && (sCom.write(READ_CMD) == 1);
for(uint8_t i = 0; i<sizeArray-1; i++) {
ByteArray[i] = sCom.read(1);
}
ByteArray[sizeArray-1] = sCom.read(0);
sCom.stop();
return output;
}
bool AD5933::setControlReg(uint8_t Command)
{
return setRegister(0x80, PGAandVoltout | Command);
}
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;
uint8_t CodeArray[8];
CodeArray[0] = (uint8_t) (startFreqCode >> 16);
CodeArray[1] = (uint8_t) (startFreqCode >> 8);
CodeArray[2] = (uint8_t) (startFreqCode);
CodeArray[3] = (uint8_t) (stepFreqCode >> 16);
CodeArray[4] = (uint8_t) (stepFreqCode >> 8);
CodeArray[5] = (uint8_t) (stepFreqCode);
CodeArray[6] = (uint8_t) (nrOfSteps >> 8);
CodeArray[7] = (uint8_t) (nrOfSteps);
bool output = gotoAdressPointer(0x82);
firstMeasurement = true;
return writeBlock(CodeArray, 8) && output;
}
bool AD5933::setSettlingTime(unsigned int nrOfCycles)
{
uint8_t CodeArray[2];
if (nrOfCycles > 1022) {
CodeArray[0] = ((nrOfCycles/4) >> 8) | 0x06;
CodeArray[1] = (uint8_t) (nrOfCycles/4);
} else if(nrOfCycles > 511) {
CodeArray[0] = ((nrOfCycles/4) >> 8) | 0x02;
CodeArray[1] = (uint8_t) (nrOfCycles/2);
} else {
CodeArray[0] = 0x00;
CodeArray[1] = (uint8_t) (nrOfCycles);
}
bool output = gotoAdressPointer(0x8A);
return writeBlock(CodeArray, 2) && output;
}
bool AD5933::setAnalogCircuit(bool PGA, int RangeNr)
{
if(PGA)
PGAandVoltout = 0x01;
else
PGAandVoltout = 0x00;
switch(RangeNr) {
case 2:
PGAandVoltout |= 0x06;
break;
case 3:
PGAandVoltout |= 0x04;
break;
case 4:
PGAandVoltout |= 0x02;
break;
}
firstMeasurement = true;
return true;
}
bool AD5933::reset()
{
uint8_t data = 0x10;
if(_extClk)
data |= 0x08;
return setRegister(0x81, data);
}
bool AD5933::Measure(bool increment)
{
if(firstMeasurement) {
reset();
setControlReg(INIT_FREQ);
wait_ms(100);
if(getRegister(0x8F) != 0x04)
return false;
setControlReg(INIT_SWEEP);
wait_ms(5);
return getData();
} else if(increment) {
setControlReg(INCR_FREQ);
wait_ms(5);
return getData();
} else {
setControlReg(REPE_FREQ);
wait_ms(5);
return getData();
}
}
bool AD5933::getData()
{
int i = 0;
uint8_t data[4];
bool output;
while((getRegister(0x8F) != 0x02) && i < 10) {
wait_ms(5);
i++;
}
if(i == 10)
return false;
output = gotoAdressPointer(0x82);
output &= readBlock(data, 4);
real = data[0] | data[1] << 8;
imaginary = data[2] | data[3] << 8;
return output;
}