多功能调试测试助手-阻抗测量AD5933环境说明:CubeSuites+
函数:
//初始化IICA0口
unsigned char I2C_Init(unsigned long clockFreq);
//写数据到从机
unsigned char I2C_Write(unsigned char slaveAddress,
unsigned char* dataBuffer,
unsigned char bytesNumber,
unsigned char stopBit);
//从从机中读数据
unsigned char I2C_Read(unsigned char slaveAddress,
unsigned char* dataBuffer,
unsigned char bytesNumber,
unsigned char stopBit);
//设置AD5933
void AD5933_SetRegisterValue(unsigned char registerAddress,
unsigned long registerValue,
unsigned char bytesNumber)
//设置AD5933时钟源,及工作时钟
void AD5933_SetSystemClk(char clkSource, unsigned long extClkFreq)
//设置AD5933 输出信号幅度范围,X5,X1放大增益
void AD5933_SetRangeAndGain(char range, char gain)
//配置AD5933 扫频需要的三个参数
void AD5933_ConfigSweep(unsigned long startFreq,
unsigned long incFreq,
unsigned short incNum)
//配置AD933开始扫频工作
void AD5933_StartSweep(void)
//给定已知阻抗电阻,及工作频率,计算增益系数
double AD5933_CalculateGainFactor(unsigned long calibrationImpedance,unsigned char freqFunction)
//给定增益系数,扫频频率,计算未知阻抗
double AD5933_CalculateImpedance(double gainFactor, unsigned char freqFunction)
细列:
//初始化IICA0口
unsigned char I2C_Init(unsigned long clockFreq)
{
unsigned long fckFreq = 32000000;
unsigned char wlValue = 0;
unsigned char whValue = 0;
/* Enable input clock supply. */
IICA0EN = 1;
/* Set the fast mode plus operation. */
SMC0 = 1;
/* Set transfer rate. */
wlValue = (unsigned char)((0.5 * fckFreq) / clockFreq);
whValue = (unsigned char)(wlValue - (fckFreq / (10 * clockFreq)));
IICWL0 = wlValue;
IICWH0 = whValue;
STCEN0 = 1; // After operation is enabled, enable generation of a start
// condition without detecting a stop condition.
WTIM0 = 1; // Interrupt request is generated at the ninth clock’s
// falling edge.
/* Enable I2C operation. */
IICE0 = 1;
/* Configure SCLA0 and SDAA0 pins as digital output. */
P6 &= ~0x03;
PM6 &= ~0x03;
return 1;
}
//写数据到从机
unsigned char I2C_Write(unsigned char slaveAddress,
unsigned char* dataBuffer,
unsigned char bytesNumber,
unsigned char stopBit)
unsigned char I2C_Write(unsigned char slaveAddress,
unsigned char* dataBuffer,
unsigned char bytesNumber,
unsigned char stopBit)
{
unsigned char byte = 0;
unsigned char status = 0;
IICAMK0 = 1; // Interrupt servicing disabled.
STT0 = 1; // Generate a start condition.
IICAMK0 = 0; // Interrupt servicing enabled.
nineClocksFlag = 0;
IICA0 = (slaveAddress << 1);
while(nineClocksFlag == 0);
if(ACKD0) // Acknowledge was detected.
{
for(byte = 0; byte < bytesNumber; byte++)
{
nineClocksFlag = 0;
IICA0 = *dataBuffer;
while(nineClocksFlag == 0);
dataBuffer++;
}
status = bytesNumber;
}
else
{
status = 0xFF;
}
if(stopBit)
{
SPT0 = 1; // Generate a stop condition.
while (IICBSY0); // Wait until the I2C bus status flag is cleared.
}
return status;
}
//从从机中读数据
unsigned char I2C_Read(unsigned char slaveAddress,
unsigned char* dataBuffer,
unsigned char bytesNumber,
unsigned char stopBit)
unsigned char I2C_Read(unsigned char slaveAddress,
unsigned char* dataBuffer,
unsigned char bytesNumber,
unsigned char stopBit)
{
unsigned char byte = 0;
unsigned char status = 0;
IICAMK0 = 1; // Interrupt servicing disabled.
STT0 = 1; // Generate a start condition.
IICAMK0 = 0; // Interrupt servicing enabled.
nineClocksFlag = 0;
IICA0 = (slaveAddress << 1) + 1;
while(nineClocksFlag == 0);
if(ACKD0) // Acknowledge was detected.
{
ACKE0 = 1; // Enable acknowledgment.
for(byte = 0; byte < bytesNumber; byte++)
{
if(byte == (bytesNumber - 1))
{
ACKE0 = 0U; // Disable acknowledgment.
}
WREL0 = 1U; // Cancel wait.
nineClocksFlag = 0;
while(nineClocksFlag == 0);
*dataBuffer = IICA0;
dataBuffer++;
}
status = bytesNumber;
}
else
{
status = 0xFF;
}
if(stopBit)
{
SPT0 = 1; // Generate a stop condition.
while (IICBSY0); // Wait until the I2C bus status flag is cleared.
}
return (status);
}
//设置AD5933
void AD5933_SetRegisterValue(unsigned char registerAddress,
unsigned long registerValue,
unsigned char bytesNumber)
{
unsigned char byte = 0;
unsigned char writeData[2] = {0, 0};
for(byte = 0;byte < bytesNumber; byte++)
{
writeData[0] = registerAddress + bytesNumber - byte - 1;
writeData[1] = (unsigned char)((registerValue >> (byte * 8)) & 0xFF);
I2C_Write(AD5933_ADDRESS, writeData, 2, 1);
}
}
//设置AD5933时钟源,及工作时钟
void AD5933_SetSystemClk(char clkSource, unsigned long extClkFreq)
{
currentClockSource = clkSource;
if(clkSource == AD5933_CONTROL_EXT_SYSCLK)
{
currentSysClk = extClkFreq; // External clock frequency
}
else
{
currentSysClk = AD5933_INTERNAL_SYS_CLK; // 16 MHz
}
AD5933_SetRegisterValue(AD5933_REG_CONTROL_LB, currentClockSource, 1);
}
//设置AD5933 输出信号幅度范围,X5,X1放大增益
void AD5933_SetRangeAndGain(char range, char gain)
{
AD5933_SetRegisterValue(AD5933_REG_CONTROL_HB,
AD5933_CONTROL_FUNCTION(AD5933_FUNCTION_NOP) |
AD5933_CONTROL_RANGE(range) |
AD5933_CONTROL_PGA_GAIN(gain),
1);
/* Store the last settings made to range and gain. */
currentRange = range;
currentGain = gain;
}
//配置AD5933 扫频需要的三个参数:起始频率,频率增量,增量数
void AD5933_ConfigSweep(unsigned long startFreq,
unsigned long incFreq,
unsigned short incNum)
{
unsigned long startFreqReg = 0;
unsigned long incFreqReg = 0;
unsigned short incNumReg = 0;
/* Ensure that incNum is a valid data. */
if(incNum > AD5933_MAX_INC_NUM)
{
incNumReg = AD5933_MAX_INC_NUM;
}
else
{
incNumReg = incNum;
}
/* Convert users start frequency to binary code. */
startFreqReg = (unsigned long)((double)startFreq * 4 / currentSysClk *
POW_2_27);
/* Convert users increment frequency to binary code. */
incFreqReg = (unsigned long)((double)incFreq * 4 / currentSysClk *
POW_2_27);
/* Configure the device with the sweep parameters. */
AD5933_SetRegisterValue(AD5933_REG_FREQ_START,
startFreqReg,
3);
AD5933_SetRegisterValue(AD5933_REG_FREQ_INC,
incFreqReg,
3);
AD5933_SetRegisterValue(AD5933_REG_INC_NUM,
incNumReg,
2);
}
//配置AD933开始扫频工作
void AD5933_StartSweep(void)
{
unsigned char status = 0;
AD5933_SetRegisterValue(AD5933_REG_CONTROL_HB,
AD5933_CONTROL_FUNCTION(AD5933_FUNCTION_STANDBY) |
AD5933_CONTROL_RANGE(currentRange) |
AD5933_CONTROL_PGA_GAIN(currentGain),
1);
AD5933_Reset();
AD5933_SetRegisterValue(AD5933_REG_CONTROL_HB,
AD5933_CONTROL_FUNCTION(AD5933_FUNCTION_INIT_START_FREQ)|
AD5933_CONTROL_RANGE(currentRange) |
AD5933_CONTROL_PGA_GAIN(currentGain),
1);
AD5933_SetRegisterValue(AD5933_REG_CONTROL_HB,
AD5933_CONTROL_FUNCTION(AD5933_FUNCTION_START_SWEEP) |
AD5933_CONTROL_RANGE(currentRange) |
AD5933_CONTROL_PGA_GAIN(currentGain),
1);//启动扫频
status = 0;
while((status & AD5933_STAT_DATA_VALID) == 0)//判断状态寄存器中 数据有效
{
status = AD5933_GetRegisterValue(AD5933_REG_STATUS,1);
};
}
//给定已知阻抗电阻,及工作频率,计算增益系数
double AD5933_CalculateGainFactor(unsigned long calibrationImpedance,unsigned char freqFunction)
{
double gainFactor = 0;
double magnitude = 0;
signed short realData = 0;
signed short imagData = 0;
unsigned char status = 0;
AD5933_SetRegisterValue(AD5933_REG_CONTROL_HB,
AD5933_CONTROL_FUNCTION(freqFunction) |
AD5933_CONTROL_RANGE(currentRange) |
AD5933_CONTROL_PGA_GAIN(currentGain),
1);
status = 0;
while((status & AD5933_STAT_DATA_VALID) == 0)
{
status = AD5933_GetRegisterValue(AD5933_REG_STATUS,1);
}
realData = AD5933_GetRegisterValue(AD5933_REG_REAL_DATA,2);
imagData = AD5933_GetRegisterValue(AD5933_REG_IMAG_DATA,2);
magnitude = sqrt(((double)realData * (double)realData) + ((double)imagData * (double)imagData)); //注意数据类型强制转换问题
gainFactor = 1 / (magnitude * calibrationImpedance);
return gainFactor;
}
//给定增益系数,扫频频率,计算未知阻抗
double AD5933_CalculateImpedance(double gainFactor, unsigned char freqFunction)
{
signed short realData = 0;
signed short imagData = 0;
double magnitude = 0;
double impedance = 0;
unsigned char status = 0;
AD5933_SetRegisterValue(AD5933_REG_CONTROL_HB,
AD5933_CONTROL_FUNCTION(freqFunction) |
AD5933_CONTROL_RANGE(currentRange) |
AD5933_CONTROL_PGA_GAIN(currentGain),
1);
status = 0;
while((status & AD5933_STAT_DATA_VALID) == 0)
{
status = AD5933_GetRegisterValue(AD5933_REG_STATUS,1);
}
realData = AD5933_GetRegisterValue(AD5933_REG_REAL_DATA,2);
imagData = AD5933_GetRegisterValue(AD5933_REG_IMAG_DATA,2);
magnitude = sqrt(((double)realData * (double)realData) + ((double)imagData * (double)imagData)); //注意数据类型强制转换问题
impedance = 1 / (magnitude * gainFactor);
return impedance;
}