/********************************************************************************
Author : CAC (China Applications Support Team)
Date : January, 2012
File name : AD7192.c
Description : Use the GPIO to simulate the SPI communication of AD7192
Hardware plateform : ADuC7026 and AD7190/92EBZ
********************************************************************************/
#include "ad7192.h"
#include "stdio.h"
#include "spi.h"
unsigned long AD_MODE_REG=0;
unsigned long AD_CONFIG_REG=0;
void AD7192Initialization(u16 sample_rate)
{
u16 fs_data=0x60;
fs_data=4920000/1024/sample_rate;
AD7192_Reset();
AD_CONFIG_REG= CHOP_DIS|REF_IN1|AIN1_AIN2|BURN_DIS|REFDET_DIS|BUF_DIS|UB_BI|GAIN_64; // Gain = 1
AD7192_SetRegisterValue(REG_CONF,AD_CONFIG_REG,3);
AD7192InternalZeroScaleCalibration();
AD7192InternalFullScaleCalibration();
AD7192ExternalZeroScaleCalibration();
AD7192ExternalFullScaleCalibration();
AD7192StartContinuousConvertion(fs_data);
AD7192StartContinuousRead();
}
/*******************************************************************************
Description: AD7193_Reset()
Function: AD7193复位函数,连续性ad7193发送超过40个1
Input: 空
Return: 空
*******************************************************************************/
void AD7192_Reset(void)
{
unsigned char byaregister_word[6] = {0, 0, 0, 0, 0, 0}; /*发送数组*/
unsigned char byreturn_value[6] = {0, 0, 0, 0, 0, 0}; /*返回数组*/
/*发送数组赋值*/
byaregister_word[0] = 0xFF;
byaregister_word[1] = 0xFF;
byaregister_word[2] = 0xFF;
byaregister_word[3] = 0xFF;
byaregister_word[4] = 0xFF;
byaregister_word[5] = 0xFF;
/*发送复位指令*/
SPI_ReadWriteBytes(SPI1,byaregister_word,byreturn_value,sizeof(byaregister_word));
}
/*******************************************************************************
Description: SPI1_AD7193_SetRegisterValue(unsigned char byregister_address,
unsigned long dwregister_value,
unsigned char bytes_number,ConvertType convert)
Function: 设置寄存器值
Input: byregister_address:寄存器地址
dwregister_value: 寄存器值
bytes_number: 寄存器字节数
Return: 空
*******************************************************************************/
void AD7192_SetRegisterValue(unsigned char byregister_address,
unsigned long dwregister_value,
unsigned char bytes_number)
{
unsigned char byawrite_command_buf[5] = {0, 0, 0, 0, 0}; /*指令数组*/
unsigned char byareturn_value_buf[5] = {0, 0, 0, 0, 0}; /*ad返回数组*/
/*获取dwregister_value地址*/
unsigned char* data_pointer = (unsigned char*)&dwregister_value;
/*获取dwregister_value字节数*/
unsigned char bytes_nr = bytes_number;
/*通信寄存器值*/
byawrite_command_buf[0] = WEN|RW_W|(byregister_address<<3)|CREAD_DIS;
/*调整发送顺序,dwregister_value先发高位字节,后发低位字节*/
while(bytes_nr > 0)
{
byawrite_command_buf[bytes_nr] = *data_pointer;//倒着追加数据
data_pointer ++;
bytes_nr --;
}
/*写入ad7193*/
SPI_ReadWriteBytes(SPI1,byawrite_command_buf,byareturn_value_buf,bytes_number + 1);
}
/*******************************************************************************
Description: SPI1_AD7193_GetRegisterValue(unsigned char byregister_address,
unsigned char bynumber_of_bytes_for_register)
Function: 读取寄存器值
Input: byregister_address: 寄存器地址
bynumber_of_bytes_for_register:寄存器字节数
Return: 数值
*******************************************************************************/
unsigned long AD7193_GetRegisterValue(unsigned char byregister_address,
unsigned char bynumber_of_bytes_for_register)
{
unsigned char byaregister_word_buf[5] = {0, 0, 0, 0, 0}; /*发送数组*/
unsigned char byareturn_value_buf[5] = {0, 0, 0, 0, 0}; /*ad返回数组*/
unsigned long buffer = 0x0; /*返回数值*/
unsigned char i = 0;
/*通信寄存器值*/
byaregister_word_buf[0] = WEN|RW_R|(byregister_address<<3)|CREAD_DIS;
/*读取寄存器值*/
SPI_ReadWriteBytes(SPI1,byaregister_word_buf,byareturn_value_buf,bynumber_of_bytes_for_register + 1);
/*调整循序,获得返回值*/
for(i = 1; i < bynumber_of_bytes_for_register + 1; i++)
{
buffer = (buffer << 8) + byareturn_value_buf[i];
}
return buffer;
}
void AD7192StartContinuousRead(void)
{
unsigned char WriteBuf[1];
unsigned char ReadBuf[1];
WriteBuf[0] = WEN|RW_R|(REG_DATA<<3)|CREAD_EN;
AD7192_WaitRdyGoLow();
SPI_ReadWriteBytes(SPI1,WriteBuf,ReadBuf,1);
}
/*******************************************************************************
Description: AD7193_WaitRdyGoLow()
Function: 等待ad7193RDY位引脚拉低
Input: 空
Return: 空
*******************************************************************************/
void AD7192_WaitRdyGoLow(void)
{
while(AD7192_RDY)
{
}
}
void AD7192ExitContinuousRead(void)
{
unsigned char WriteBuf[1];
unsigned char ReadBuf[1];
WriteBuf[0] = WEN|RW_R|(REG_DATA<<3)|CREAD_DIS;
AD7192_WaitRdyGoLow();
SPI_ReadWriteBytes(SPI1,WriteBuf,ReadBuf,1);
}
void AD7192InternalZeroScaleCalibration(void)
{
AD_MODE_REG = 0;
AD_MODE_REG = MODE_INZCL|DAT_STA_EN|INCLK_MCLK2EN|SINC_3|ENPAR_EN|CLK_DIV_DIS|SINGLECYCLE_DIS|REJ60_DIS|0x60;
AD7192_SetRegisterValue(REG_MODE,AD_MODE_REG,3);
AD7192_WaitRdyGoLow();
}
void AD7192InternalFullScaleCalibration(void)
{
AD_MODE_REG = 0;
AD_MODE_REG = MODE_INFCL|DAT_STA_EN|INCLK_MCLK2EN|SINC_3|ENPAR_EN|CLK_DIV_DIS|SINGLECYCLE_DIS|REJ60_DIS|0x60;
AD7192_SetRegisterValue(REG_MODE,AD_MODE_REG,3);
AD7192_WaitRdyGoLow();
}
void AD7192ExternalZeroScaleCalibration(void)
{
AD_MODE_REG = 0;
AD_MODE_REG = MODE_INFCL|DAT_STA_EN|INCLK_MCLK2EN|SINC_3|ENPAR_EN|CLK_DIV_DIS|SINGLECYCLE_DIS|REJ60_DIS|0x60;
AD7192_SetRegisterValue(REG_MODE,AD_MODE_REG,3);
AD7192_WaitRdyGoLow();
}
void AD7192ExternalFullScaleCalibration(void)
{
AD_MODE_REG = 0;
AD_MODE_REG = MODE_SYSFCL|DAT_STA_EN|INCLK_MCLK2EN|SINC_3|ENPAR_EN|CLK_DIV_DIS|SINGLECYCLE_DIS|REJ60_DIS|0x60;
AD7192_SetRegisterValue(REG_MODE,AD_MODE_REG,3);
AD7192_WaitRdyGoLow();
}
void AD7192StartSingleConvertion(u16 fs_val)
{
AD_MODE_REG = 0;
AD_MODE_REG = MODE_SING|DAT_STA_EN|INCLK_MCLK2EN|SINC_3|ENPAR_EN|CLK_DIV_DIS|SINGLECYCLE_DIS|REJ60_DIS|fs_val;
AD7192_SetRegisterValue(REG_MODE,AD_MODE_REG,3);
}
void AD7192StartContinuousConvertion(u16 fs_val)
{
AD_MODE_REG = 0;
AD_MODE_REG= MODE_CONT|DAT_STA_EN|INCLK_MCLK2EN|SINC_3|ENPAR_EN|CLK_DIV_DIS|SINGLECYCLE_DIS|REJ60_DIS|fs_val; //Output Rate = MCLK/1024/128 without chop
AD7192_SetRegisterValue(REG_MODE,AD_MODE_REG,3);
}
unsigned long int AD7192ReadTemperature(void)
{
unsigned long ad_d