我在使用AD7705进行双通道采样时,交替向
通信寄存器写入通道1和通道2的数据读取命令,但每次写命令后,返回的数据是随机的,并没有与通道对应好,请各位帮看下代码有无问题
Uint16 test_cnt1 = 0 ,test_cnt2 = 0;
Uint16 ch_samp_flag = 1; /* ch_samp_flag = 1,通道1采样;ch_samp_flag = 0,通道2采样 */
Uint16 out_vol_samp_dig = 0, out_cur_samp_dig = 0;
float out_vol_samp = 0, out_cur_samp = 0;
float out_vol_samp_real = 0, out_cur_samp_real = 0;
void AD7705_WriteByte(uint8_t addr)
{
CS_ADC_LOW(1);
// delay_us(1);
spi2_read_write_byte(addr);
// delay_us(1);
CS_ADC_HIGH(1);
}
void Init_AD7705(uint8_t channel)
{
uint8_t i;
for(i=0;i<150;i++)/* 多于连续32个 DIN=1 使串口复位 */
{
AD7705_WriteByte(0xff);//持续DIN高电平写操作,恢复AD7705接口
}
delay_ms(1);
switch (channel)
{
case 1:
AD7705_WriteByte(0x20);/* 写时钟寄存器选中ch1*/
AD7705_WriteByte(0x07);/* 4.9152MHz时钟,200Hz数据更新速率 */
AD7705_WriteByte(0x10);/* 选择设置寄存器,使用chnanel 1*/
AD7705_WriteByte(0x44);/* 写设置寄存器 ,设置成单极性、有缓冲、增益为1、滤波器工作、自校准 */
break;
case 2:
AD7705_WriteByte(0x21);/* 写时钟寄存器选中ch2 */
AD7705_WriteByte(0x07);/* 4.9152MHz时钟,200Hz数据更新速率 */
AD7705_WriteByte(0x11);/* 选择设置寄存器,使用chnane 2*/
AD7705_WriteByte(0x44);/* 写设置寄存器,设置成单极性、有缓冲、增益为2、滤波器工作、自校准*/
break;
default:
break;
}
}
uint16_t ReadDataAD7705_CH1(void)
{
// uint16_t result1 = 0;
uint16_t DataH = 0, DataL = 0;
sta
tic uint16_t result1_z1=0,result1 = 0;
static uint16_t ad7705ch1_write_rdy = 0 ;
result1 = result1_z1;
if(ad7705ch1_write_rdy == 0)
{
AD7705_WriteByte(READ_DATA_FROM_CH1);
ad7705ch1_write_rdy = 1;
}
else
{
if(SPI2_READ_DRDY == 0)
{
CS_ADC_LOW(1); /* 使能ADC */
DataH = spi2_read_write_byte(0xff); /* 取数据高字节 */
DataL = spi2_read_write_byte(0xff); /* 取数据低字节 */
CS_ADC_HIGH(1); /* 使能ADC */
DataH = DataH << 8;
result1 = DataH | DataL;
result1_z1 = result1;
ad7705ch1_write_rdy = 0;
test_cnt2++;
ch_samp_flag = 0;
}
}
return result1;
}
uint16_t ReadDataAD7705_CH2(void)
{
uint16_t DataH = 0, DataL = 0;
static uint16_t result2_z1 = 0, result2 = 0;
static uint16_t ad7705ch2_write_rdy = 0;
result2 = result2_z1;
if(ad7705ch2_write_rdy == 0)
{
AD7705_WriteByte(READ_DATA_FROM_CH2);
ad7705ch2_write_rdy = 1;
}
else
{
if(SPI2_READ_DRDY == 0)
{
CS_ADC_LOW(1); /* 使能ADC */
DataH = spi2_read_write_byte(0xff); /* 取数据高字节 */
DataL = spi2_read_write_byte(0xff); /* 取数据低字节 */
CS_ADC_HIGH(1); /* 使能ADC */
DataH = DataH << 8;
result2 = DataH | DataL;
result2_z1 = result2;
ad7705ch2_write_rdy = 0;
ch_samp_flag = 1;
}
}
return result2;
}
void AD7705MulChSamp(void)
{
if(ch_samp_flag == 1)
{
out_vol_samp_dig = ReadDataAD7705_CH1();
out_vol_samp = (float)(out_vol_samp_dig * 1.225/65536);
}
else
{
out_cur_samp_dig = ReadDataAD7705_CH2();
out_cur_samp = (float)(out_cur_samp_dig * 1.225/65536);
}
out_vol_samp_real = (float)out_vol_samp * OutVolSampGain;
out_cur_samp_real = (float)out_cur_samp * OutCurSampGain;
}