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

📄 cs5550.c

📁 电子称重的程序
💻 C
字号:
#include	<config.h>
#include	<head.h>
#include	<CS5550.h>
#include	<function.h>
#include	<message.h>

union
{
	long		rlong;
	struct{
			unsigned char hbyte;     // High byte
			unsigned char mbyte;     // Middle byte
			unsigned char lbyte;     // Low byte
			unsigned char zbyte;     // Zero byte
		}bytes;
} AD_NOW;
union
{
	Uint		rint;
	struct{
		unsigned char hbyte;     // High byte
		unsigned char lbyte;     // Low byte
		}bytes;
} AD_NOW_V;
BOOL	hadad;
extern Uchar adchipdog;
extern bit   resetadbit;
BOOL	getad()
{
	return	hadad;
}
long		readad()
{
	hadad=0;
	return	AD_NOW.rlong;
}
Uint		readvolt()
{
	return	AD_NOW_V.rint;
}
void		inin5550(char gain,char Offset)
{
	reset=LOW;
	delayms(1);
	reset=HIGH;
	sdo=HIGH;
	sdi=LOW;
	sclk=LOW;
	cs_writebyte_4(Sync1,Sync1,Sync1,Sync0);
	cs_writebyte_4(WReg_Config,0x01,0x00,0x01);                 //falling //使cpu时钟反转,打开高通滤波器
	cs_writebyte_4(WReg_CycleCount,0x00,0x01,0x90);     //0190        //400       04b0 //1200 //转换速率
	cs_writebyte_4(WReg_Mask,0x80,0x00,0x00);   //允许DRDY中断
	cs_writebyte_4(WRegA1_DcOffset,Offset,0,0);
	cs_writebyte_4(WRegA1_DcGain,gain,0,0);
	cs_writebyte_4(WRegA2_DcOffset,0x00,0x00,0x00);
	cs_writebyte(ContnuConvs);
	 //PX0=1;
	IT0=1;
	EX0=1;
	EA=1;
	adchipdog=0;
	resetadbit=0;
	while(!getad());
	//adchipdog=0;
}
//***********************************************************************

BOOL	cs_clock(void)
{
	BOOL	sample;
	sclk=HIGH;
	// _nop_();
	sample=sdo;
	sclk=LOW;
	// _nop_();
	return(sample);
}

//*********************************************
void cs_writebyte(Uchar cs_data)
{
	Uchar i;
	sclk=LOW;
	/*发送8位数据*/
	for(i=0;i<8;i++)
	{
		sdi=(bit)(cs_data&0x80);
		cs_data=cs_data<<1;
		cs_clock();
	}
	/*请求应答信号ACK*/
	sdi=HIGH;
}


//*****************************************************************************
void cs_writebyte_4(Uchar a,Uchar b,Uchar c,Uchar d)
{
	cs_writebyte(a);
	cs_writebyte(b);
	cs_writebyte(c);
	cs_writebyte(d);
}

//*****************************************************************************
void int_0(void) interrupt 0
{
	adchipdog=0;
	cs_writebyte(RRegA2_FILT);
	AD_NOW_V.bytes.hbyte=int_cs_readbyte();
	AD_NOW_V.bytes.lbyte=int_cs_readbyte();
	AD_NOW_V.rint+=2354;
	AD_NOW_V.rint/=25.98;
	cs_writebyte(WReg_Status);
	cs_writebyte(0xff);
	cs_writebyte(0xff);
	cs_writebyte(0xff);

	cs_writebyte(RRegA1_Output);                  //判断符号
	if(int_cs_readbyte()&0x80)AD_NOW.rlong=0;
 	else{
		cs_writebyte(RRegA1_FILT);                  //读AD值
		AD_NOW.bytes.mbyte=int_cs_readbyte();
		AD_NOW.bytes.lbyte=int_cs_readbyte();
		AD_NOW.bytes.zbyte=int_cs_readbyte();
	}
	hadad=1;
	PostMessageOne(M_ADHAD,0);
	cs_writebyte(WReg_Status);
	cs_writebyte(0xff);
	cs_writebyte(0xff);
	cs_writebyte(0xff);
}


//************************************************
Uchar int_cs_readbyte(void)
{
	Uchar cs_data=0;
	Uchar i;
	sdi=HIGH;
	for(i=0;i<8;i++)
	{
		if(i==7){sdi=LOW;}
		cs_data<<=1;
		if(cs_clock())
		cs_data++;
	}
	return(cs_data);
}

⌨️ 快捷键说明

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