现在用在主程序里面单独采集1个通道,采集AD值都没有问题。
第1-3通道采集的是4-20ma的信号,第4通道采集的是PT100,差分信号。
出现问题:
1、执行Cs5523ReadAdData();采集出来的通道号与AD值无法对应,有时候,第1通道采集的是第二通道的值,有时候是第3通道的值,不定?
2、这4个通道能否一次采集完成?如果可以程序里面应该怎么设置,
///////////////////////////////////////
//写配置寄存器清RS位,写RS=0以便退出复位模式
ReadBuffer[0] = 0x05; //comm=03h* write config_reg
ReadBuffer[1] = 0x30; // config=04h,30h,00h
ReadBuffer[2] = 0x00;
RWCR = 0x03;
WriteCommand(RWCR);
Cs5523WriteData(3);
把配置信息设置成上面的无法实现4个通道一次采集完成。
#include "Global.h"
#include "CS5523.h"
#define CS5523_CS PTED_PTED6
#define CS5523_SCLK PTED_PTED4
#define CS5523_SDI PTED_PTED7
#define CS5523_SO PTED_PTED5
#define NOP
unsigned char PartSelect = 1;
////////////////cs5532 command/////////////////////////////
unsigned char RWIOCR ;
unsigned char RWIGRR ;
unsigned char RWCR ;
unsigned char RWCSR ;
////////////////////
unsigned char PerformConver ; // 执行转换
unsigned char PerformCalib ; // 执行校准
unsigned char Sync1 ; // 串行口重新部分初始化顺序
unsigned char Sync0 ; // 串行口结束部分初始化顺序
unsigned char Null ; // 清除口标志,保持系统处于连续的转换模式
/////////////////////////////////////////////
unsigned char ReadBuffer[12];
unsigned char OutRate;
////////////////////////////////////////////
void Delay(void)
{
Cpu_Delay100US(10000);
}
void CS5523_Init(void)//初始化端口
{
// PTED: PTED7=1,PTED6=1,PTED5=1,PTED4=1
setReg8Bits(PTED, 0xF0);
// PTEPE: PTEPE7=1,PTEPE6=1,PTEPE5=1,PTEPE4=1
setReg8Bits(PTEPE, 0xF0);
// PTEDD: PTEDD7=1,PTEDD6=1,PTEDD5=0,PTEDD4=1
clrSetReg8Bits(PTEDD, 0x20, 0xD0);
}
unsigned char SPI_HostReadByte()
{
unsigned char i,rByte;
rByte = 0;
for(i=0;i<8;i++)
{
CS5523_SCLK = 0;
CS5523_SCLK = 1;
rByte <<= 1;
rByte |= CS5523_SO;
}
return rByte;
}
///////////////////////////////////
void SPI_HostWriteByte(unsigned char wByte)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(((wByte << i ) & 0x80) == 0x80)
{
CS5523_SDI = 1;
}
else
{
CS5523_SDI = 0;
}
CS5523_SCLK = 0;
CS5523_SCLK = 1;
}
}
void WriteCommand(unsigned char Commandcs)
{
SPI_HostWriteByte(Commandcs);
}
void Read_PressAD(void)
{
unsigned char i;
CS5523_SCLK = 0;
NOP;
CS5523_CS = 0;
Cpu_Delay100US(20);
PerformConver = 0x80 ;//执行通道1 ,采集4-20ma压力值
SPI_HostWriteByte(PerformConver);
CS5523_SO = 1;
while(CS5523_SO == 1);
PerformConver = 0x00;
SPI_HostWriteByte(PerformConver);
//Temp=SPI_HostReadByte();
for (i = 0;i < 3 ;i++)
{
ReadBuffer[i]=SPI_HostReadByte();
}
Avq_SamplePress = ReadBuffer[0]*256+ReadBuffer[1];//执行通道1
CS5523_CS = 1;
}
void Read_TemperatureAD(void)
{
unsigned char i;
CS5523_SCLK = 0;
NOP;
CS5523_CS = 0;
PerformConver = 0x88 ;//执行通道2,采集4-20ma温度值
SPI_HostWriteByte(PerformConver);
CS5523_SO = 1;
while(CS5523_SO == 1);
PerformConver = 0x00;
SPI_HostWriteByte(PerformConver);
//Temp=SPI_HostReadByte();
for (i = 0;i < 3 ;i++)
{
ReadBuffer[i]=SPI_HostReadByte();
}
Avq_SampleTemperature = ReadBuffer[0]*256+ReadBuffer[1];//执行通道1
CS5523_CS = 1;
}
void Read_FlowAD(void)
{
unsigned char i;
CS5523_SCLK = 0;
NOP;
CS5523_CS = 0;
PerformConver = 0x90 ;//执行通道3-采集4-20ma流量值
SPI_HostWriteByte(PerformConver);
CS5523_SO = 1;
while(CS5523_SO == 1);
PerformConver = 0x00;
SPI_HostWriteByte(PerformConver);
//Temp=SPI_HostReadByte();
for (i = 0;i < 3 ;i++)
{
ReadBuffer[i]=SPI_HostReadByte();
}
Avq_SampleFlow = ReadBuffer[0]*256+ReadBuffer[1];//执行通道1
SPI_HostWriteByte(0x00);
CS5523_CS = 1;
}
void Read_PT100AD(void)
{
unsigned char i;
CS5523_SCLK = 0;
NOP;
CS5523_CS = 0;
PerformConver = 0x98 ;//执行通道4----采集PT100温度值
SPI_HostWriteByte(PerformConver);
CS5523_SO = 1;
while(CS5523_SO == 1);
PerformConver = 0x00;
SPI_HostWriteByte(PerformConver);
//Temp=SPI_HostReadByte();
for (i = 0;i < 3 ;i++)
{
ReadBuffer[i]=SPI_HostReadByte();
}
Avq_SamplePT100 = ReadBuffer[0]*256+ReadBuffer[1];//执行通道1
CS5523_CS = 1;
}
////////////////////////////////////
void Cs5523ReadAdData(void)
{
Read_PressAD();
Read_TemperatureAD();
Read_FlowAD();
Read_PT100AD();
}
///////////////////////////////////
void Cs5523WriteData(unsigned char GeShu)
{
unsigned char i;
for (i = 0;i < GeShu ;i++)
{
SPI_HostWriteByte(ReadBuffer[i]);
}
}
/////////////////////////////////
void CS5523_initialize()
{
//unsigned char j;
unsigned char flag22 = 0;
unsigned int j;
CS5523_Init();
Delay();//延时500ms
CS5523_CS = 0;
for(j=0;j<2047;j++) //延时2048个CS5523_SCLK周期,等待晶振稳定
{
CS5523_SCLK = 1;
CS5523_SCLK = 0;
}
//Reset Serial Port
CS5523_SDI = 1;
for(j=0;j<254;j++) //延时2048个CS5523_SCLK周期,等待晶振稳定
{
CS5523_SCLK = 1;
CS5523_SCLK = 0;
}
CS5523_SDI = 0;
CS5523_SCLK = 1;
CS5523_SCLK = 0;
//写配置寄存器置RS位,使复位,复位后RV=1,直到配置寄存器被读取后,RV=0
RWCR=0x03;// comm=03h* write config_reg
ReadBuffer[0]=0x00; // config=00h,35h,80h
ReadBuffer[1]=0x30;
ReadBuffer[2]=0x80;
WriteCommand(RWCR);
Cs5523WriteData(3);
//读配置寄存器
loop:
RWCR=0x0b;// comm=0bh* read config_reg
WriteCommand(RWCR);
ReadBuffer[0] = SPI_HostReadByte();
ReadBuffer[1] = SPI_HostReadByte();
ReadBuffer[2] = SPI_HostReadByte();
if ((ReadBuffer[2]& 0x40)==0x00) goto loop;//测RV=1,如不为1,则要再读配置寄存器
///////////////////////////////////////
//写配置寄存器清RS位,写RS=0以便退出复位模式
ReadBuffer[0] = 0x00; //comm=03h* write config_reg
ReadBuffer[1] = 0x30; // config=04h,30h,00h
ReadBuffer[2] = 0x00;
RWCR = 0x03;
WriteCommand(RWCR);
Cs5523WriteData(3);
///////////////////////////////////////
//写配置寄存器,必须重新写一次配置寄存器以便设置配置寄存器的其它位
ReadBuffer[0] = 0x00; //comm=03h* write config_reg
ReadBuffer[1] = 0x30; // config=04h,30h,00h
ReadBuffer[2] = 0x00;
RWCR = 0x03;
WriteCommand(RWCR);
Cs5523WriteData(3);
//////////////////////
//delayms(1000);
Cpu_Delay100US(500);
ReadBuffer[0] = 0x01; //通道1,速率30.0hz,增益2.5V,单极性
ReadBuffer[1] = 0xb0; //通道2,速率30.0hz,增益2.5V,单极性
ReadBuffer[2] = 0x9b; //通道3,速率30.0hz,增益2.5V,单极性
ReadBuffer[3] = 0x11; //通道4,速率30.0hz,增益2.5V,双极性
ReadBuffer[4] = 0xb1;
ReadBuffer[5] = 0x9a;
RWCSR = 0x05;
WriteCommand(RWCSR);
Cs5523WriteData(6);
//delayms(1000);
Cpu_Delay100US(500);
//写配置寄存器,必须重新写一次配置寄存器以便设置配置寄存器的其它位
/*ReadBuffer[0] = 0x04; //comm=03h* write config_reg
ReadBuffer[1] = 0x30; // config=04h,30h,00h
ReadBuffer[2] = 0x00;
RWCR = 0x03;
WriteCommand(RWCR);
Cs5523WriteData(3);*/
//写零点 //load offset reg of channel 0~3
/*for(i=0;i<4;i++)
{
data[0]=z_add[i];
//td=i*3;//读出EEPROM通道零点
data[1]=0;//EEPROMread(td);
data[2]=0;//EEPROMread(td+1);
data[3]=0;//EEPROMread(td+2);
wrad(3);
//写增益 //load offset reg of channel 0~3
for(i=0;i<4;i++)
{
data[0]=m_add[i];
data[1]=64;//EEPROMread(buf_num);
data[2]=0x00;
data[3]=0x00;
wrad(3); */
CS5523_CS = 1;
}
|