#include "..\h\ads1282.h"
/* array to save ADC configuration data */
unsigned char adc_regdata[11];
/****************************************************************************
* Function Name : write_CMD *
* Description : write a byte to SPI then read a byte from SPI *
* Parameters : unsigned char CMD: command suppose to write *
* *
* Return Value : unsigned char Data received *
****************************************************************************/
unsigned char write_CMD(unsigned char CMD)
{
/* Send command */
WriteSPI1(CMD);
while(!DataRdySPI1())
{
continue;
}
return(ReadSPI1()); /* read SPI RX registe and clear DataRdyADC bit */
}
/****************************************************************************
* Function Name : read_ADC_REG *
* Description : read ADC internal register value *
* Parameters : unsigned int start_addr: address in ADC register *
* unsigned int length: length to be readed (byte) *
* unsigned char *rdptr: the received data to be *
* recorded to this array *
* Return Value : unsigned int number of data bytes yet to be received *
****************************************************************************/
void read_ADC_REG(unsigned int start_addr, unsigned int length, unsigned char *rdptr)
{
unsigned char command1;
unsigned char command2;
/* Send Read Register command */
command1 = 0x20 + (0x1F & start_addr);
command2 = 0x00 + (0x1F & (length - 1));
/* send command1 */
write_CMD(command1);
DELAY_FCLK(24); /* 24 f_CLK cycles required between each byte transaction */
/* send command2 */
write_CMD(command2);
/* read byte number = length */
while(length) /* stay in loop until length = 0 */
{
DELAY_FCLK(24);
*rdptr++ = write_CMD(0x00); /* read a single byte */
length--; /* reduce string length count by 1*/
}
}
/****************************************************************************
* Function Name : write_ADC_REG *
* Description : write ADC internal register value *
* Parameters : unsigned int start_addr: address in ADC register *
* unsigned int length: the length of data *
* unsigned char *rdptr: the data to be transfered *
* recorded in this array *
* Return Value : *
****************************************************************************/
void write_ADC_REG(unsigned int start_addr, unsigned int length, unsigned char *wtptr)
{
unsigned char command1;
unsigned char command2;
/* Send Read Register command */
command1 = 0x40 + (0x1F & start_addr);
command2 = 0x00 + (0x1F & (length - 1));
/* send command1 */
write_CMD(command1);
DELAY_FCLK(24); /* 24 f_CLK cycles required between each byte transaction */
/* send command2 */
write_CMD(command2);
/* write byte number = length */
while(length) /* stay in loop until length = 0 */
{
DELAY_FCLK(24);
write_CMD(*wtptr++);
length--; /* reduce string length count by 1*/
}
}
/****************************************************************************
* Function Name : read_ADC_DATA *
* Description : read ADC value in Read Data By Command mode *
* Parameters : unsigned int ADC_data_wait: time out cycle *
* Return Value : unsigned long int Data received *
****************************************************************************/
unsigned long int read_ADC_DATA(unsigned int ADC_data_wait)
{
unsigned int wait = 0;
unsigned long int temp_read = 0; /* temporary variable for saving read data */
unsigned char i;
/* Send Read Data command */
write_CMD(ADC_RDATA);
while(!DRDY_ADC)
{
if(!ADC_data_wait || wait < ADC_data_wait) /* ADC_data_wait = 0: disable timeout */
wait++ ; /* wait for time out operation */
else
return(-1); /* Time out, return 0xffffffff */
}
/* Read Data */
for(i = 0;i < 4;i++) /* save 4 bytes to one long int */
{
temp_read <<= 8;
temp_read += write_CMD(0x00);
}
return(temp_read);
}
/****************************************************************************
* Function Name : calibration_ADC *
* Description : ADC offset or gain calibration command *
* Parameters : unsigned int CAL_CMD: ADC_OFSCAL or ADC_GANCAL *
* 0 = offset calibration, 1 = gain calibration *
* unsigned in ADC_data_wait: time out cycle, 0 diable *
* Return Value : unsigned long int Data received *
****************************************************************************/
signed int calibration_ADC(unsigned int CAL_CMD,unsigned int ADC_data_wait)
{
unsigned int wait = 0;
write_CMD(ADC_SDATAC); /* send Stop Read Data Continuous command */
DELAY_FCLK(24);
write_CMD(ADC_SYNC); /* send Synchronize command */
DELAY_FCLK(24);
write_CMD(ADC_RDATAC); /* send Read Data Continuous command */
while(!DRDY_ADC)
{
if(!ADC_data_wait || wait < ADC_data_wait)
wait++ ; /* wait for time out operation */
else
return(-1); /* Time out, return 0xff */
}
write_CMD(ADC_SDATAC);
DELAY_FCLK(24);
if(!CAL_CMD)
write_CMD(ADC_OFSCAL);
else
write_CMD(ADC_GANCAL);
DELAY_FCLK(24);
write_CMD(ADC_RDATAC);
return(0);
}
/****************************************************************************
* Function Name : read_ADC_ALLREG *
* Description : read all ADC value and save it to array *
* Parameters : *
* Return Value : *
****************************************************************************/
void read_ADC_ALLREG(void)
{
/* send stop read data continuous mode command */
write_CMD(ADC_SDATAC);
DELAY_FCLK(24);
read_ADC_REG(ADC_ADDR_ID,11,adc_regdata);
DELAY_FCLK(24);
write_CMD(ADC_RDATAC);
}
/****************************************************************************
* Function Name : ADC_configuration *
* Description : configuration the ADC internal register *
* Parameters : *
* Return Value : *
****************************************************************************/
void ADC_configuration(void)
{
unsigned char ADC_CFG[10];
ADC_CFG[0] = ADC_SYNC_P &
ADC_MODE_H &
ADC_RATE_1000 &
ADC_FIR_LIN &
ADC_FILTER_SINC_LPF;
ADC_CFG[1] = ADC_MUX_2 &
ADC_CHOP_EN &
ADC_GAIN_1;
/* send stop read data continuous mode command */
write_CMD(ADC_SDATAC);
DELAY_FCLK(24);
write_ADC_REG(ADC_ADDR_CFG0,2,ADC_CFG);
DELAY_FCLK(24);
write_CMD(ADC_RDATAC);
DELAY_FCLK(24);
}
/*
EOF
*/