⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ad7714.c

📁 包含了AD7714的中文使用资料和源码资料
💻 C
字号:
#include "adjad.h"

#define loopmax 50000
unsigned char wrong_occ;   
#define ENABLE_L  P1_6
#define CLOCK_L   P1_5
#define DATA_IN   P1_4
#define DATA_OUT  P1_7
//#define P30_OUT  P3_0
//#define DATA    	P0
//#define ADDR    	P2
//#define HILO    	P2_5
//#define RD_N    	P2_6
//#define Wr_N    	P2_7
//#define Word_WrN 	P3_6
    //#define DATA_SIGN P3_3     //1-0040 ok,0-0060 ok
    //#define HZ50_60   P1_0
#define CHANNEL_AD  	0XFB
#define CHANNEL_OTC  	0XF5
#define OK  		0X00
#define ERROR  		0X01

#define OUTB  		0X04
#define INA  		0X00
#define INB  		0X01
//#define OUTA  	0XEA
//#define OUTB  	0XEB
//#define INA  		0XEA
//#define INB  		0XEB
//#define GAIN_16  	0XF0
#define GAIN_8 		0XEF
//#define GAIN_4  	0XEB
#define MV100  		0X01
#define MV60  		0X02
#define MV25  		0X03

typedef struct{
    float neg_a;
    float neg_b;
    float pos_a;
    float pos_b;
} StructFact;
extern unsigned char LampComFlg;
extern _rom StructFact CodeFact[4];
extern StructFact Cal_Fact[4];
extern unsigned int Cal_Ram_Zero[4];
void Writetoreg(unsigned char);
unsigned char Read(void);
//unsigned char ReadFpga(unsigned char);
void Writeto595(unsigned char);
//void WritetoFpga(unsigned char,unsigned char);
void Tradata(unsigned char);
void Ad7714Init(unsigned char);
void ad7714(unsigned char);
void ChannelInit(void);
extern void SENT_DATA(unsigned char,unsigned char);
extern unsigned char IN_B(void);
extern void delay10ms(unsigned char );
        	
_near unsigned int ad_data[8]_at(0x40);
_near unsigned int ad1_data[8]_at(0x60);
unsigned char channel_gain[8]={GAIN_8,GAIN_8,GAIN_8,GAIN_8,GAIN_8,GAIN_8,GAIN_8,GAIN_8,};
unsigned char channel_otc[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,};
unsigned char chn_error[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,};
unsigned char chn_adover[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,};
//unsigned char chn_mv[8]={MV100,MV60,MV25,MV100,MV60,MV25,MV100,MV25,};
unsigned char chn_mv[8]={MV25,MV25,MV25,MV25,MV25,MV25,MV25,MV25,};
//unsigned char chn_mv[8]={MV100,MV100,MV100,MV100,MV100,MV100,MV100,MV100,};
//unsigned char  ad_data;
//unsigned char REC_DATA;
unsigned char drdy_data;
unsigned char ad_value;
unsigned int cnt;
unsigned char  b,channel_t;
unsigned char ch,tt,tt1;
unsigned int  tt2;

float ad_k[8];
float ad_b[8];

unsigned int ad_buf[8][3]={
    {0x8000,0x8000,0x8000},
    {0x8000,0x8000,0x8000},
    {0x8000,0x8000,0x8000},
    {0x8000,0x8000,0x8000},
    {0x8000,0x8000,0x8000},
    {0x8000,0x8000,0x8000},
    {0x8000,0x8000,0x8000},
    {0x8000,0x8000,0x8000},
};    
unsigned char adbuf_idex = 0;
unsigned int adcode_buf[8][3]={
    {0x8000,0x8000,0x0},
    {0x8000,0x8000,0x0},
    {0x8000,0x8000,0x0},
    {0x8000,0x8000,0x0},
    {0x8000,0x8000,0x0},
    {0x8000,0x8000,0x0},
    {0x8000,0x8000,0x0},
    {0x8000,0x8000,0x0},
};    
void  ad_xs(void)
{
	float a;
	float aa;
	for (b=0; b<8;b++)
	{
		a=27305-5462;
		aa= ad_fz[2*b]-ad_fz[2*b+1];
		ad_k[b]=a/aa;
		ad_b[b]=5462;
	}
}

void ChannelInit(void)	//Read OTC status
{
	unsigned char errflg_DRDY;
	unsigned int loop;
	unsigned char  temp_c,temp;		
        unsigned char ch;
	//DATA_SIGN=0;
	for (temp_c=0; temp_c<8;temp_c++)
	{
		ENABLE_L=0;
		//WritetoFpga(OUTB,temp_c);		/*set channel */
		temp = (temp_c&0x7f);
		temp |=LampComFlg;
		SENT_DATA(OUTB,temp);
		Writeto595(CHANNEL_OTC);	/*read this channel OTC status */
		ENABLE_L=1;			/*AD7714 CS is low*/
		for (ch=0;ch<250;ch++)
		{	
			_nop();
			_nop();
			_nop();
			_nop();
			_nop();
			_nop();
		}
		for (ch=0;ch<250;ch++)
		{	
			_nop();
			_nop();
			_nop();
			_nop();
			_nop();
			_nop();
		}
		//ch=ReadFpga(INB);
		ch=IN_B();
		if((ch&0x20)==0) channel_otc[temp_c]=OK;	
		else channel_otc[temp_c]=ERROR;	
	}
  	ENABLE_L=0;
        Writeto595(0xfe);
	ENABLE_L=1;			//SEND SWITCH AND AD7714 CS is low
		for (ch=0;ch<250;ch++)
		{	
			_nop();
			_nop();
			_nop();
			_nop();
			_nop();
			_nop();
		}
		for (ch=0;ch<250;ch++)
		{	
			_nop();
			_nop();
			_nop();
			_nop();
			_nop();
			_nop();
		}
        temp = 0;
	temp |=LampComFlg;
	ENABLE_L=0;
	SENT_DATA(OUTB,temp);
//	SENT_DATA(OUTB,channel_t);		//set channel 
//	Writeto595(CHANNEL_AD);		//SET channel SWITCH
	Writeto595(CHANNEL_AD);		//SET channel SWITCH
	ENABLE_L=1;			//SEND SWITCH AND AD7714 CS is low
	for (temp_c=0;temp_c<100;temp_c++)
		_nop();
	_nop();
		for (cnt=0;cnt<50000;cnt++) 
		//for (cnt=0;cnt<40000;cnt++) 
		{
			_nop();
			_nop();
		}
		Writetoreg(~0x0c);
		drdy_data=Read(); // wait for /DRDY to go low 
		loop = 0;
		errflg_DRDY = 0;
		while( (drdy_data & 0x80)==0 && errflg_DRDY <100)
		{
			for (cnt=0;cnt<20;cnt++) 
			{
				_nop();
			}
			Writetoreg(~0x0c);
			drdy_data=Read();  // wait
			loop++;
			if(loop>=loopmax) 
			{
			    loop = 0;
			    errflg_DRDY++;
			}
		}
		loop = 0;
		errflg_DRDY = 0;
		while( (drdy_data & 0x80)!=0  && errflg_DRDY <10)
		{
			for (cnt=0;cnt<200;cnt++) 
			{
				_nop();
			}
			Writetoreg(~0x0c);
			drdy_data=Read();  // wait
			loop++;
	        if(loop>=loopmax) 
			{
			    loop = 0;
			    errflg_DRDY++;
			}
		}
		    Writetoreg(~0x5c); //next read from the data register 
		    //chn_error[chl]=0;
		    drdy_data=Read();
		    drdy_data=Read();

}

void Ad7714Init(unsigned char channel)
{
	unsigned char  temp_c,temp;		

	ENABLE_L=0;
	//WritetoFpga(OUTB,channel);		/*set channel */
        temp = (channel&0x7f);
	temp |=LampComFlg;
	SENT_DATA(OUTB,temp);
//	SENT_DATA(OUTB,channel);
	Writeto595(CHANNEL_AD);		/*SET channel SWITCH*/
	ENABLE_L=1;			/*SEND SWITCH AND AD7714 CS is low*/
	for (temp_c=0;temp_c<10;temp_c++)
	    _nop();
	_nop();

	Writetoreg(0x00);
	Writetoreg(0x00);
	Writetoreg(0x00);
	Writetoreg(0x00);

    for (temp_c=0;temp_c<10;temp_c++)
        _nop();
	_nop();
	Writetoreg(~0x24);	/*next write to high filter*/
	//Writetoreg(~0x21);	/*high filter 16bit*/
	Writetoreg(~0x61);	/*high filter 24bit*/
	Writetoreg(~0x34);	/*next write to low filter*/
	//if(HZ50_60==1)
	//Writetoreg(~0x40);	/*60 Hz low filter*/
	//else
	Writetoreg(0x7f);//(~0x80);	/*50 Hz low filter*/
		
	Writetoreg(~0x14);	/*next write to MODE register */
	temp_c=0x3c & channel_gain[channel];
	Writetoreg(~temp_c);	/*gain=16,do self calibration*/
	for (cnt=0;cnt<10000;cnt++) //do self calibration??nm
	{
		_nop();
		_nop();
		_nop();
	}
}
void ad7714(unsigned char chl)
{
	unsigned int tempu16;
	unsigned char errflg_DRDY;
	unsigned int loop;
	unsigned char temp_c,temp;
	unsigned char dead_b;
	StructFact *pFact;
	_rom StructFact *pCodeFact;
	float tempfloat;
        //DATA_SIGN=0;
/*        temp = (chl&0x7f);
	temp |=LampComFlg;
	ENABLE_L=0;
	SENT_DATA(OUTB,temp);
//	SENT_DATA(OUTB,channel_t);		//set channel 
	Writeto595(CHANNEL_AD);		//SET channel SWITCH
*/	ENABLE_L=1;			//SEND SWITCH AND AD7714 CS is low
	for (temp_c=0;temp_c<100;temp_c++)
		_nop();
	_nop();
	errflg_DRDY = 0;
	ENABLE_L=1;			//SEND SWITCH AND AD7714 CS is low
	channel_t=(chl+1)%8;
	if(channel_otc[chl]==ERROR)
	{
	    delay10ms(10);
	    ad_data[chl]=0xffff;
	    chn_error[chl]=0;
	    chn_adover[chl]=0;
	}	
	else
	{
		dead_b=0;
		//TR1=1; delay
		for (cnt=0;cnt<50000;cnt++) 
		//for (cnt=0;cnt<40000;cnt++) 
		{
			_nop();
			_nop();
		}
		Writetoreg(~0x0c);
		drdy_data=Read(); // wait for /DRDY to go low 
		loop = 0;
		errflg_DRDY = 0;
		while( (drdy_data & 0x80)==0 && errflg_DRDY <100)
		{
			for (cnt=0;cnt<20;cnt++) 
			{
				_nop();
			}
			Writetoreg(~0x0c);
			drdy_data=Read();  // wait
			loop++;
			if(loop>=loopmax) 
			{
			    loop = 0;
			    errflg_DRDY++;
			}
		}
		loop = 0;
		if(errflg_DRDY >=100) errflg_DRDY=0xff; //drdy_data==low ad_nonwork
		else
		{
		    errflg_DRDY = 0;
		    while( (drdy_data & 0x80)!=0  && errflg_DRDY <10)
		    {
			    for (cnt=0;cnt<200;cnt++) 
			    {
				    _nop();
			    }
			    Writetoreg(~0x0c);
			    drdy_data=Read();  // wait
			    loop++;
	            if(loop>=loopmax) 
			    {
			        loop = 0;
			        errflg_DRDY++;
			    }
			}
		    if(errflg_DRDY >=10) errflg_DRDY=0xff; //drdy_data==low ad_nonwork
		    else                 errflg_DRDY=0;
		}
		if(errflg_DRDY==0)
		{
		    Writetoreg(~0x5c); //next read from the data register 
		    chn_error[chl]=0;
		    tt=Read();
		    tt1=Read();
		    tt2=tt*0x100+tt1;
		    //ad_data[chl]=tt2;   //(int)zz;
		    tt1=Read();
/*		    //monitor channel 4
		    if(chl==4)
		    {
			    for (temp_c=0;temp_c<7;temp_c++)
			    {
				    ad_data[temp_c]= ad_data[temp_c+1];
			    }
			    tt = tt >>5;
		        ad_data[7]=((tt2-0x8000)<<3)+tt;   //(int)zz;
		    }
*/		
		    temp_c=chn_mv[chl];
		    if(tt>0x7f)
		    {
		        tt2 = tt2-0x8000;
		        loop = 0x8000;
		        loop = loop>>temp_c;
		        if((tt2&loop)!=0)
		        {
		            tt2=0xf000;//admax=e666
		            chn_adover[chl]=0xff;
		        }
		        else
		        {
		            tt2=tt2<<temp_c;
		            temp_c=8-temp_c;
		            tt1=tt1>>temp_c;
		            tt2=0x8000+tt2+tt1;
		            chn_adover[chl]=0x0;
		        }
		    }
		    else
		    {
		        tt2 = 0x8000-tt2;
		        loop = 0x8000;
		        loop = loop>>temp_c;
		        if((tt2&loop)!=0)
		        {
		            tt2=0x00ff;//admin=199a
		            chn_adover[chl]=0xff;
		        }
		        else
		        {
		            tt2=tt2<<temp_c;
		            temp_c=8-temp_c;
		            tt1=(0x100-tt1)>>temp_c;
		            tt2=0x8000-tt2-tt1;
		            chn_adover[chl]=0x0;
		        }
		    }
/******************nm********************/
            ad_buf[chl][adbuf_idex] =tt2;
            if(ad_buf[chl][0]>=ad_buf[chl][1])
            {
                if(ad_buf[chl][0]>=ad_buf[chl][2])//ad_buf[chl][0]=max
                {
                    if(ad_buf[chl][1]>=ad_buf[chl][2]) tt2 = ad_buf[chl][1];
                    else tt2 = ad_buf[chl][2];
                }
                else tt2 = ad_buf[chl][0];
            }
            else
            {
                if(ad_buf[chl][0]<ad_buf[chl][2])//ad_buf[chl][0]=min
                {
                    if(ad_buf[chl][1]<ad_buf[chl][2]) tt2 = ad_buf[chl][1];
                    else tt2 = ad_buf[chl][2];
                }
                else tt2 = ad_buf[chl][0];
            }
                
                    temp_c=chn_mv[chl];
            pFact = Cal_Fact+temp_c;
            if(tt2>=Cal_Ram_Zero[temp_c])
            {
                //pos
                tempfloat = (pFact->pos_a)*(float)tt2+(pFact->pos_b);
                if(tempfloat<0)tempfloat = 0;
//                tt2 = (unsigned int)(tempfloat+0.5);	    
            }
		    else
            {
                //neg
                tempfloat = (pFact->neg_a)*(float)tt2+(pFact->neg_b);
                if(tempfloat<0)tempfloat = 0;
//                tt2 = (unsigned int)(tempfloat+0.5);	    
            }
            temp_c=chn_mv[chl];
            pCodeFact = CodeFact+temp_c;
            tempfloat = (pCodeFact->pos_a)*tempfloat+pCodeFact->pos_b;
            if(tempfloat<0)tempfloat = 0;
            tt2 = (unsigned int)(tempfloat+0.5);  
//***************************************
		    if(tt2>=adcode_buf[chl][0])
		        tempu16 = tt2-adcode_buf[chl][0];
		    else
		        tempu16 = adcode_buf[chl][0]-tt2;
		    if(tempu16<50)//ok    
		    {
		        adcode_buf[chl][0] = tt2;
		        adcode_buf[chl][1] = tt2;
		        adcode_buf[chl][2] = 0;
		    }
		    else
		    {
		        if(tt2>=adcode_buf[chl][1])
		            tempu16 = tt2-adcode_buf[chl][1];
		        else
		            tempu16 = adcode_buf[chl][1]-tt2;
		        if(tempu16<50)//ok    
		        {
		            adcode_buf[chl][1] = tt2;
		            if(adcode_buf[chl][2]>=3)
		            {
		                adcode_buf[chl][0]=tt2;
		                adcode_buf[chl][2]=0;
		            }
		            else
		            {
		                adcode_buf[chl][2]++;
		                tt2=adcode_buf[chl][0];
		            }
		        }
		        else
		        {
		            adcode_buf[chl][2]=0;
		            adcode_buf[chl][1]=tt2;
		            tt2=adcode_buf[chl][0];
		        }
		    }                
		        
		    ad_data[chl]=tt2;
		}
		else
		{
		    ENABLE_L=0;
		    ad_data[chl]=0xffff;
		    chn_error[chl]=0xff;

		}
	} 
	//WritetoFpga(OUTB,channel_t);		//set channel 
        temp = (channel_t&0x7f);
	temp |=LampComFlg;
	ENABLE_L=0;
	SENT_DATA(OUTB,temp);
//	SENT_DATA(OUTB,channel_t);		//set channel 
	Writeto595(CHANNEL_AD);		//SET channel SWITCH
	ENABLE_L=1;			//SEND SWITCH AND AD7714 CS is low
	for (temp_c=0;temp_c<100;temp_c++)
		_nop();
	_nop();
}

void Tradata(unsigned char  TRA_DATA)
{
	unsigned char i;
	unsigned char temp_d;
	_nop();
	_nop();
	CLOCK_L=1;
	temp_d=TRA_DATA;
	for  (i=0;i<8;i++)
	{
		temp_d=_rolc8(temp_d,1);
		DATA_IN=C;
		//for (temp_c=0;temp_c<5;temp_c++) _nop();
		_nop();
		CLOCK_L=0;
		//for (temp_c=0;temp_c<5;temp_c++) _nop();
		_nop();
		_nop();
		_nop();
		_nop();
		_nop();
		_nop();
		_nop();
		_nop();
		_nop();
		CLOCK_L=1;
	}
	DATA_IN=1;
}   

void Writeto595(unsigned char swtch)
{ 
	Tradata(swtch);
	_nop();
	_nop();
}

void Writetoreg(unsigned char byteword)
{
	Tradata(byteword);
	_nop();
	_nop();
}

unsigned char Read(void)
{
	unsigned char i;
	unsigned char REC_DATA;
	
	DATA_OUT=1;
	CLOCK_L=1;
	REC_DATA=0;
	//for (temp_c=0;temp_c<5;temp_c++) _nop();
	_nop();
	CLOCK_L=1;
	for (i=0;i<8;i++)
	{
		//for (temp_c=0;temp_c<5;temp_c++) _nop();
		CLOCK_L=0;
		//for (temp_c=0;temp_c<5;temp_c++) _nop();
		_nop();
		_nop();
		_nop();
		_nop();
		_nop();
		_nop();
		_nop();
		_nop();
		CLOCK_L=1;
		_nop();
		C=DATA_OUT;
		REC_DATA=_rolc8(REC_DATA,1);
	}
	return REC_DATA;
}  
/*
unsigned char ReadFpga(unsigned char In_addr)
{
	unsigned char temp_c;
	DATA=0xff;
	ADDR = 0xE0 | In_addr;
	RD_N = 0;
	temp_c = DATA;
	RD_N = 1;
	return temp_c;
}

void WritetoFpga(unsigned char Out_addr,unsigned char byte)
{
	//unsigned char temp_c;
	ADDR = 0xE0 | Out_addr;
	DATA = byte;
	Wr_N = 0;
	_nop();
	Wr_N = 1;
}
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -