小弟最近一直在纠结一个问题,就是ad芯片cs5532读出来的数据一直是ffff。用的单片机是pic16f1937。下面附上我的AD部分的程序,劳烦哪位前辈帮忙看下,哪里有问题。谢谢。
//write
void CS5532_Write_Byte(unsigned char dat){
unsigned char i;
ADCS_L();
delay_us(2);
ADCLK_L();
for(i=0;i<8;i++){
if((dat<<i)&0x80)
ADDI_H();
else
ADDI_L();
ADCLK_H();
delay_us(2);
ADCLK_L();
delay_us(2);
}
ADDI_L();
ADCS_H();
}
//read
void CS5532_Read_Data(void){
unsigned char i,j;
ADCS_L();
delay_us(2);
ADCLK_L();
for(j=0;j<4;j++){
for(i=0;i<8;i++){
ADCLK_H();
delay_us(2);
Sample_buf[j]<<=1;
if(PIN_C5)
Sample_buf[j] |= 0x01; //从SDO读取数据
else
Sample_buf[j] &= 0xfe;
delay_us(2);
ADCLK_L();
delay_us(2);
}
}
}
//采样
void CS5532_Sample(void){
unsigned char i;
ADCS_L();
ADDI_L();
ADCLK_L();
for(i=0;i<8;i++){
delay_us(2);
ADCLK_H();
delay_us(2);
ADCLK_L();
}
CS5532_Read_Data();
ADCS_H();
ADDI_H();
}
//初始化
void CS5532_Init(void){
unsigned char i;
// set_tris_a(0xdf);
// set_tris_c(0xd7);
ADCS_L();
delay_ms(20);
for(i=0;i<15;i++){
CS5532_Write_Byte(0xff);//发送命令
}
CS5532_Write_Byte(0xfe);
ADCS_L();
delay_ms(1000);
CS5532_Write_Byte(0x03);////写入CS5532复位命令
CS5532_Write_Byte(0x22);
CS5532_Write_Byte(0x00);
CS5532_Write_Byte(0x00);
CS5532_Write_Byte(0x00);
ADCS_L();
delay_ms(1000);
CS5532_Write_Byte(0x03);//写配置寄存器
CS5532_Write_Byte(0x00);
CS5532_Write_Byte(0x40);
CS5532_Write_Byte(0x00);
CS5532_Write_Byte(0x00);
ADCS_L();
delay_ms(1000);
CS5532_Write_Byte(0x05);//写通道设置寄存器
CS5532_Write_Byte(0x31);//物理通道1、64倍增益、15Hz字速率
CS5532_Write_Byte(0xc0);//单极性
CS5532_Write_Byte(0x31);
CS5532_Write_Byte(0xc0);
ADCS_L();
delay_ms(1000);
//偏移寄存器自校准self calibration of offset
CS5532_Write_Byte(0x81);
ADCS_L();
delay_ms(1000);
CS5532_Write_Byte(0x89);
ADCS_L();
delay_ms(1000);
//增益寄存器自校准self calibration of gain
CS5532_Write_Byte(0x82);
ADCS_L();
delay_ms(1000);
CS5532_Write_Byte(0x8a);
ADCS_L();
delay_ms(1000);
CS5532_Write_Byte(0xc0);//启动A/D连续转换
ADCS_L();
delay_ms(80);
}
|