www.pudn.com > c51_ads1232.rar > ADS1232.c, change:2013-01-13,size:3698b


#include<reg51.h> 
#include<stdio.h> 
 
 
sfr	T2CON=0xc8; 
sfr RCAP2H=0xcb; 
sfr	RCAP2L=0xca; 
long int Number,DeadWeight; 
long int twos; 
bit rsstat,BitNegative; 
 
 
sbit	Gain0=P1^0; 
sbit	Gain1=P1^1; 
sbit	Speed=P1^2; 
sbit 	PWDN=P1^3; 
sbit  clock=P1^4; 
sbit  DRDY=P1^5; 
 
sbit	Tare=P3^7; 
void ReadDataFromADS1232(); 
void TwosCompliment(); 
void SendDataToDisplay(); 
void Delay(); 
void Serial1(); 
void Wait(); 
void ReadData16Times();	 
void OffsetCalibrationADS1232(); 
main() 
	{ 
	long int booting; 
	IP=0xc5; 
	IE=0x80; 
	 
	rsstat=0; 
	SCON=0x52; 
	T2CON=0x34;  
	RCAP2H=0xff;  
	RCAP2L=0x64;  
	Wait();	Delay();Delay(); 
	PWDN=0; 
	Speed=0; 
	Gain0=1; 
	Gain1=1; 
	for(booting=11111;booting<=99999;booting=booting+11111) 
		{ 
		twos=booting; 
		SendDataToDisplay();Delay();Delay();Delay();Delay(); 
		} 
	PWDN=1; 
	clock=0;	 
		for(booting=99999;booting>00000;booting=booting-11111) 
		{ 
		twos=booting; 
		SendDataToDisplay();Delay();Delay();Delay();Delay(); 
		} 
	OffsetCalibrationADS1232(); 
	for(booting=0;booting<=10;booting++) 
		{ 
		ReadData16Times();	 
		DeadWeight=twos; 
		} 
		BitNegative=0; 
while(1) 
		{	 
		 
			ReadData16Times(); 
			if(twos>DeadWeight){twos=twos-DeadWeight;BitNegative=0;} 
			else{twos=DeadWeight-twos;BitNegative=1;} 
			twos=twos/10; 
			SendDataToDisplay(); 
			if(Tare==0) 
				{ 
				ReadData16Times();	 
				DeadWeight=twos; 
				} 
		Tare=1;		 
		} 
		 
	} 
	/*********************ADS1232 Routines*************************/ 
	 
void ReadData16Times() 
	{ 
	int loop; 
	long int Dst=0; 
	for(loop=1;loop<=16;loop++) 
		{ 
		do{}while(DRDY==1); 
		ReadDataFromADS1232(); 
		TwosCompliment(); 
		Dst=Dst+twos; 
		} 
	twos=Dst/16;	 
	} 
void ReadDataFromADS1232() 
	{ 
	int i; 
	bit temp; 
	Number=0; 
			for(i=1;i<=24;i++) 
			{ 
			Number = (Number << 1);	/* shift 1 place to the left or shift in 0 */ 
			temp = DRDY;		/* save input */ 
			clock = 1;		/* toggle clock high */ 
			Number = Number | temp;	/* if high, make bit high */ 
			clock = 0;	 
			}	 
			clock=1; 
			Wait(); 
			DRDY=1; 
			clock=0;			 
			Wait(); 
			do{}while(DRDY==0); 
			 
	} 
void OffsetCalibrationADS1232() 
	{ 
	int i; 
	bit temp; 
	 
			for(i=1;i<=24;i++) 
			{ 
			Number = (Number << 1);	/* shift 1 place to the left or shift in 0 */ 
			temp = DRDY;		/* save input */ 
			clock = 1;		/* toggle clock high */ 
			Number = Number | temp;	/* if high, make bit high */ 
			clock = 0;	 
			}	 
			clock=1; 
			Wait(); 
			clock=0;			 
			Wait(); 
			clock=1; 
			Wait(); 
			clock=0;			 
			Wait(); 
			do{}while(DRDY==0); 
			Wait(); 
			do{}while(DRDY==1); 
	} 
/******************************* Display Routines ********************************/	 
void TwosCompliment() 
	{ 
	twos=~Number; 
	twos=twos+1;	 
	twos=twos&0x00ffffff; 
	} 
	 
void SendDataToDisplay() 
	{ 
	char	DData[6]; 
	int temp; 
	//twos=twos/20; 
		DData[1]=(twos%10)|0x30; 
		twos=twos/10; 
		DData[2]=(twos%10)|0x30; 
		twos=twos/10; 
		DData[3]=(twos%10)|0x30; 
		twos=twos/10; 
		DData[4]=(twos%10)|0x30; 
		twos=twos/10; 
		DData[5]=(twos%10)|0x30; 
		twos=twos/10; 
		DData[6]=(twos%10)|0x30; 
		twos=twos/10; 
		for(temp=6;temp>1;temp--) 
				{ 
				if(DData[temp]=='0'){DData[temp]=0x20;}else{temp=0;} 
				} 
		if(BitNegative==1){DData[6]='-';}		 
		ACC='s'; 
		Serial1(); 
		ACC=DData[6];Serial1(); 
		ACC=DData[5];Serial1(); 
		ACC=DData[4];Serial1(); 
		ACC=DData[3];Serial1(); 
		ACC=DData[2];Serial1(); 
		ACC=DData[1];Serial1(); 
		ACC='z'; 
		Serial1(); 
	} 
	 
void Serial1() 
	{ 
	do{}while(TI==0); 
	TI=0; 
	SBUF=ACC; 
	}	 
void Wait() 
	{ 
	 int Delay=5; 
		while(Delay>0) 
		{Delay--;} 
	}	 
void Delay() 
	{ 
	 int Delay=10000; 
		while(Delay>0) 
		{Delay--;} 
	}