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

📄 sd16.c

📁 MSP4250单芯片电子称,采样数据无线发送给主机,编译环境IAR for MSP430
💻 C
字号:
/*
*********************************************************************************************************
*                                              MSP430
*                                        sigmal delta adc functions
*
*
* File    : SD16.C
* Data	  : April 12, 2007
*********************************************************************************************************
*/

#include "hal.h"

/*
*********************************************************************************************************
*                                              PROTOTYPES
*********************************************************************************************************
*/
long SD16Temp;                           	// Temp sum register
long SD16Result;                         	// Final averaged result
unsigned int SD16Cnt;                		// Number of resuts collected
unsigned int SD16Config;
/*	bit15			0,no  				1-New data
 *  bit14			0,zero weight	 	1,handle weight
 *  bit13..10
 *  bit09			0,do not change  	1,close voltage when finish
 *  bit08			0,0ff 				1-on
 *  bit07..04		oversampling retio		14, 11,  8,   5,   2
 *						 					64, 128, 256, 512, 1024
 *  bit03..00  		Samples number: 		6,  7,   8,   9,   10
 *											64, 128, 256, 512, 1024
 */

#ifdef SENSOR1
 long Cal0Min_t = 4060;//3990;
 long Cal0Max_t = 17415;//17081;		//right
#else
 long Cal0Min_t = 510;
 long Cal0Max_t = 30555;
#endif


/*
*********************************************************************************************************
*                                         SD16Set
*
* Description      :
* Arguments        :
* 	 Osr : oversampling ratio should be
*		SD16OSR_64		0010	14		<<4		>>10
*		SD16OSR_128		0001	11		<<4		>>7			
*		SD16OSR_256		0000	8		<<4		>>4
*		SD16OSR_512		1000	5		<<4		>>1
*		SD16OSR_1024	1001	2		<<4		
*	 count: sample count should be
*		6,7,8,9,10
*		Result >> count+2
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
void SD16Set(unsigned int Osr,unsigned int  count)
{
	
	SD16CCTL0 = Osr + SD16DF + SD16IE;
	
	if(Osr == SD16OSR_64)
		{SD16Config = (14 << 4) + SD16_ON;}
	else if (Osr == SD16OSR_128)
		{SD16Config = (11 << 4) + SD16_ON;}
	else if (Osr == SD16OSR_256)
		{SD16Config = (8  << 4) + SD16_ON;}
	else if (Osr == SD16OSR_512)
		{SD16Config = (5  << 4) + SD16_ON;}
	else if (Osr == SD16OSR_1024)
		{SD16Config = (2  << 4) + SD16_ON;}
	
	SD16Config += count ;
}

/*
*********************************************************************************************************
*                                         SD16Start
*
* Description      :
* Arguments        :
*			SD16INCH_0 		weight sensor 1
*			.				.
*			.				.
*			SD16INCH_4 		weight sensor 5
*			SD16INCH_5		power meter
*			SD16INCH_6		temprature	sensor
*			SD16INCH_7		short circuit
* Returned Values  : none
* Note(s)/Warnings :
* 		inch	:input channel should be
*********************************************************************************************************
*/
void SD16Start(char inch)
{
	
	SD16Temp = 0;
  	SD16Cnt  = 0;
	
	SD16INCTL0 = SD16GAIN_32 + inch;		//gain 32
  	SD16CCTL0 |= SD16SC;   					// Start conversion
}

/*
*********************************************************************************************************
*                                         SD16GetAverage
*
* Description      :
* Arguments        :
* Returned Values  : none
* Note(s)/Warnings :
*********************************************************************************************************
*/
// Get an average weight on current PlateNow
// WIthout a basic timer interference
int	SD16GetAverage(unsigned int n)
{
	
	long temp32;
	unsigned int i;
	istate_t  inttmp;
	
	BTIMER_OFF;											//stop basic timer
	inttmp = __get_interrupt_state();					//save interrupt state
	__bis_SR_register(GIE);								//enable global interrupt
	
	temp32 = 0;
	for(i=0;i<n;i++){
		
		SD16REF_ON;											//ref voltage on
		SD16CH_ON(PlateNow);								//sensor voltage on
		Pause_1mS(20);
		
		SD16Start(PlateNow);
		SD16Config &= ~SD16_OVER;						//clear convert finish sign
		while((SD16Config & SD16_OVER) == 0) ;			//wait SD16 convert finish
		temp32 += SD16Result;
	}
	//SD16_OFF_ALL;
	
	for(i=0;i<16;i++) if(n & (1<<i)) break;				//calculate shift number
	
	BTIMER_ON;											//enable basic timer
	__set_interrupt_state(inttmp);						//recover interrupt state
	
	return(temp32>>i);
}

/*
*********************************************************************************************************
*                                        INTERRUPT VECTOR FOR "SD16"
*
* Description      :
* Note(s)/Warnings :
*********************************************************************************************************
*/
//			 			 __________________________ ++		// adde bottle
//			 |			|
//			 |			|			 ______________	+		// change bottle
// weight1:  |__________|			|______________	0		// not dispensed
//			 |			|			|______________	-		// dispensed
// weight3:	 |	    	|			|
//			 |			|			|
// weight2:	 |    		|___________|______________ --
//			 | 	  	        Delta1	     Delta2
// PalteInfo:|   00,01  |    02	    |  		03		|	01	|
//			 |			| waiter x  |	waiter y	|		|
//
/*
 * PlateInfo
 * 00, no bottle   :
		W.0			// do nothing
		W.++		// adde bottle		goto[01]
 		W.+			// --
		W.-			// --
        W.--		// --
 * 01, bottle on   :
		W.0			// do nothing
		W.++		// adde bottle
 		W.+			// see W.++
		W.-			// dispensed 		goto[11][read waiter]
        W.--		// bottle up  		goto[10][read waiter]
 * 10, bottle up   :
		W.0			// not dispensed  	goto[00]
		W.++		// see W.+
 		W.+			// change bottle	goto[11]
		W.-			// dispensed		goto[11](read waiter)
        W.--		// do nothing		
 * 11, bottle back :
		W.x			// 					goto[01]		
*/

char wtcnt = 0;
int wtzero[8];
//------------------------------------------------------------------------------
#pragma vector = SD16_VECTOR
__interrupt void SD16_ISR(void)
{
	
  	long CurrentResult;
	int temp16;
	
	SET_TEST1;		//???????????
	
	SD16CCTL0 &= ~SD16LSBACC;
    CurrentResult = (long)(int)SD16MEM0 << 4;
    SD16CCTL0 |= SD16LSBACC;
    CurrentResult |= ((int)SD16MEM0 & 0xf0) >> (((SD16Config & 0xf0)>>4) - 4);
	 //CurrentResult |= ((int)SD16MEM0 & 0xf0) >> 4;
	
  	SD16Temp += CurrentResult;                      		// Sum up results

  	if (++SD16Cnt >= (1<< (SD16Config & 0xfu))){
		SD16CCTL0 &= ~SD16SC;								//stop convert, close ref and sensor voltage
		if(SD16Config & SD16_VOLC)
			SD16_OFF_ALL;									//reference voltage off, all sensor voltage off
		
		SD16Result = (SD16Temp >> ((SD16Config & 0xfu)+2));
		wtzero[wtcnt++] = SD16Result;
		wtcnt &= 0x07;
		
		SD16Result -= Cal0Min_t;
	
		temp16 = Wt_Delta1[PlateNow] =  (int)(Wt_Old[PlateNow] - SD16Result);
		
		if( (temp16 > HandleMinWeight)||(temp16 < (int)(-HandleMinWeight)) ){	
			SD16Config |= SD16_OVER + SD16_CHANGE;			//indicate a new data
			__low_power_mode_off_on_exit();					//wakeup cpu
		}else{
			SD16Config |= SD16_OVER;						//indicate a new zero data
			//if(PmMode ==PM_MEASURE)
				//__bis_SR_register_on_exit(LPM3);
		}
  	}
	CLR_TEST1;		//???????????
}

⌨️ 快捷键说明

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