标题:
AD7193驱动源程序
[打印本页]
作者:
Cheney01
时间:
2020-6-18 10:03
标题:
AD7193驱动源程序
/******************************************************************************/
#include "AD7193.h" // AD7193 definitions.
/******************************************************************************/
/************************ Variables Definitions *******************************/
/******************************************************************************/
unsigned char currentPolarity = 0;
unsigned char currentGain = 1;
/******************************************************************************/
/************************ Functions Definitions *******************************/
/******************************************************************************/
/***************************************************************************//**
* @brief Checks if the AD7139 part is present.
*
* @return status - Indicates if the part is present or not.
*******************************************************************************/
unsigned char AD7193_Init(void)
{
unsigned char status = 1;
unsigned char regVal = 0;
status = SPI_Init(0, 1000000, 1, 0);
regVal = AD7193_GetRegisterValue(AD7193_REG_ID, 1, 1);
if((regVal & AD7193_ID_MASK) != ID_AD7193)
{
status = 0;
}
return status;
}
/***************************************************************************//**
* @brief Writes data into a register.
*
* @param registerAddress - Address of the register.
* @param registerValue - Data value to write.
* @param bytesNumber - Number of bytes to be written.
* @param modifyCS - Allows Chip Select to be modified.
*
* @return none.
*******************************************************************************/
void AD7193_SetRegisterValue(unsigned char registerAddress,
unsigned long registerValue,
unsigned char bytesNumber,
unsigned char modifyCS)
{
unsigned char writeCommand[5] = {0, 0, 0, 0, 0};
unsigned char* dataPointer = (unsigned char*)®isterValue;
unsigned char bytesNr = bytesNumber;
writeCommand[0] = AD7193_COMM_WRITE |
AD7193_COMM_ADDR(registerAddress);
while(bytesNr > 0)
{
writeCommand[bytesNr] = *dataPointer;
dataPointer ++;
bytesNr --;
}
SPI_Write(AD7193_SLAVE_ID * modifyCS, writeCommand, bytesNumber + 1);
}
/***************************************************************************//**
* @brief Reads the value of a register.
*
* @param registerAddress - Address of the register.
* @param bytesNumber - Number of bytes that will be read.
* @param modifyCS - Allows Chip Select to be modified.
*
* @return buffer - Value of the register.
*******************************************************************************/
unsigned long AD7193_GetRegisterValue(unsigned char registerAddress,
unsigned char bytesNumber,
unsigned char modifyCS)
{
unsigned char registerWord[5] = {0, 0, 0, 0, 0};
unsigned long buffer = 0x0;
unsigned char i = 0;
registerWord[0] = AD7193_COMM_READ |
AD7193_COMM_ADDR(registerAddress);
SPI_Read(AD7193_SLAVE_ID * modifyCS, registerWord, bytesNumber + 1);
for(i = 1; i < bytesNumber + 1; i++)
{
buffer = (buffer << 8) + registerWord[i];
}
return buffer;
}
/***************************************************************************//**
* @brief Resets the device.
*
* @return none.
*******************************************************************************/
void AD7193_Reset(void)
{
unsigned char registerWord[6] = {0, 0, 0, 0, 0, 0};
registerWord[0] = 0xFF;
registerWord[1] = 0xFF;
registerWord[2] = 0xFF;
registerWord[3] = 0xFF;
registerWord[4] = 0xFF;
registerWord[5] = 0xFF;
SPI_Write(AD7193_SLAVE_ID, registerWord, 6);
}
/***************************************************************************//**
* @brief Set device to idle or power-down.
*
* @param pwrMode - Selects idle mode or power-down mode.
* Example: 0 - power-down
* 1 - idle
*
* @return none.
*******************************************************************************/
void AD7193_SetPower(unsigned char pwrMode)
{
unsigned long oldPwrMode = 0x0;
unsigned long newPwrMode = 0x0;
oldPwrMode = AD7193_GetRegisterValue(AD7193_REG_MODE, 3, 1);
oldPwrMode &= ~(AD7193_MODE_SEL(0x7));
newPwrMode = oldPwrMode |
AD7193_MODE_SEL((pwrMode * (AD7193_MODE_IDLE)) |
(!pwrMode * (AD7193_MODE_PWRDN)));
AD7193_SetRegisterValue(AD7193_REG_MODE, newPwrMode, 3, 1);
}
/***************************************************************************//**
* @brief Waits for RDY pin to go low.
*
* @return none.
*******************************************************************************/
void AD7193_WaitRdyGoLow(void)
{
while(AD7193_RDY_STATE)
{
;
}
}
/***************************************************************************//**
* @brief Selects the channel to be enabled.
*
* @param channel - Selects a channel.
* Example: AD7193_CH_0 - AIN1(+) - AIN2(-); (Pseudo = 0)
* AD7193_CH_1 - AIN3(+) - AIN4(-); (Pseudo = 0)
* AD7193_TEMP - Temperature sensor
* AD7193_SHORT - AIN2(+) - AIN2(-); (Pseudo = 0)
*
* @return none.
*******************************************************************************/
void AD7193_ChannelSelect(unsigned short channel)
{
unsigned long oldRegValue = 0x0;
unsigned long newRegValue = 0x0;
oldRegValue = AD7193_GetRegisterValue(AD7193_REG_CONF, 3, 1);
oldRegValue &= ~(AD7193_CONF_CHAN(0x3FF));
newRegValue = oldRegValue | AD7193_CONF_CHAN(1 << channel);
AD7193_SetRegisterValue(AD7193_REG_CONF, newRegValue, 3, 1);
}
/***************************************************************************//**
* @brief Performs the given calibration to the specified channel.
*
* @param mode - Calibration type.
* @param channel - Channel to be calibrated.
*
* @return none.
*******************************************************************************/
void AD7193_Calibrate(unsigned char mode, unsigned char channel)
{
unsigned long oldRegValue = 0x0;
unsigned long newRegValue = 0x0;
AD7193_ChannelSelect(channel);
oldRegValue = AD7193_GetRegisterValue(AD7193_REG_MODE, 3, 1);
oldRegValue &= ~AD7193_MODE_SEL(0x7);
newRegValue = oldRegValue | AD7193_MODE_SEL(mode);
PMOD1_CS_LOW;
AD7193_SetRegisterValue(AD7193_REG_MODE, newRegValue, 3, 0); // CS is not modified.
AD7193_WaitRdyGoLow();
PMOD1_CS_HIGH;
}
/***************************************************************************//**
* @brief Selects the polarity of the conversion and the ADC input range.
*
* @param polarity - Polarity select bit.
Example: 0 - bipolar operation is selected.
1 - unipolar operation is selected.
* @param range - Gain select bits. These bits are written by the user to select
the ADC input range.
*
* @return none.
*******************************************************************************/
void AD7193_RangeSetup(unsigned char polarity, unsigned char range)
{
unsigned long oldRegValue = 0x0;
unsigned long newRegValue = 0x0;
oldRegValue = AD7193_GetRegisterValue(AD7193_REG_CONF,3, 1);
oldRegValue &= ~(AD7193_CONF_UNIPOLAR |
AD7193_CONF_GAIN(0x7));
newRegValue = oldRegValue |
(polarity * AD7193_CONF_UNIPOLAR) |
AD7193_CONF_GAIN(range);
AD7193_SetRegisterValue(AD7193_REG_CONF, newRegValue, 3, 1);
/* Store the last settings regarding polarity and gain. */
currentPolarity = polarity;
currentGain = 1 << range;
}
/***************************************************************************//**
* @brief Returns the result of a single conversion.
*
* @return regData - Result of a single analog-to-digital conversion.
*******************************************************************************/
unsigned long AD7193_SingleConversion(void)
{
unsigned long command = 0x0;
unsigned long regData = 0x0;
command = AD7193_MODE_SEL(AD7193_MODE_SINGLE) |
AD7193_MODE_CLKSRC(AD7193_CLK_INT) |
AD7193_MODE_RATE(0x060);
PMOD1_CS_LOW;
AD7193_SetRegisterValue(AD7193_REG_MODE, command, 3, 0); // CS is not modified.
AD7193_WaitRdyGoLow();
regData = AD7193_GetRegisterValue(AD7193_REG_DATA, 3, 0);
PMOD1_CS_HIGH;
return regData;
}
/***************************************************************************//**
* @brief Returns the average of several conversion results.
*
* @return samplesAverage - The average of the conversion results.
*******************************************************************************/
unsigned long AD7193_ContinuousReadAvg(unsigned char sampleNumber)
{
unsigned long samplesAverage = 0;
unsigned long command = 0;
unsigned char count = 0;
command = AD7193_MODE_SEL(AD7193_MODE_CONT) |
AD7193_MODE_CLKSRC(AD7193_CLK_INT) |
AD7193_MODE_RATE(0x060);
PMOD1_CS_LOW;
AD7193_SetRegisterValue(AD7193_REG_MODE, command, 3, 0); // CS is not modified.
for(count = 0; count < sampleNumber; count++)
{
AD7193_WaitRdyGoLow();
samplesAverage += AD7193_GetRegisterValue(AD7193_REG_DATA, 3, 0); // CS is not modified.
}
PMOD1_CS_HIGH;
samplesAverage = samplesAverage / sampleNumber;
return samplesAverage;
}
/***************************************************************************//**
* @brief Read data from temperature sensor and converts it to Celsius degrees.
*
* @return temperature - Celsius degrees.
*******************************************************************************/
unsigned long AD7193_TemperatureRead(void)
{
unsigned long dataReg = 0;
unsigned long temperature = 0;
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
51hei.png
(6 KB, 下载次数: 117)
下载附件
2020-6-18 14:16 上传
原理图: 无
仿真: 无
上图4个代码文件:
ad7193_generic.zip
(10.93 KB, 下载次数: 27)
2020-6-18 10:02 上传
点击文件名下载附件
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1