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

📄 tls2561.c

📁 亮度传感器TLS2561读取
💻 C
字号:
#pragma DEBUG CODE
//#include <w78le52.h>
#include <reg51.h>
#include <intrins.h>

//#define TWait() _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
//#define TWait() _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_()
//#define TWait() _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_()
sbit TSDA=P3^4;        //2561pin6
sbit TSCL=P3^5;			//2561pin4

unsigned char data BriSeg[4];
void TWait(void)
{
unsigned char i=120;
	do{
	_nop_();
	}while(--i);
}
/********************************************/

void TLSStart(void)
{ 	TSDA=1;
	TSCL=1;
	TWait();
	TSDA=0;
	TSCL=0;
	TWait();
}
/********************************************/
void TLSStop(void)
{	TSDA=0;
	TSCL=0;
	TWait();
    TSCL=1;
    TSDA=1;

}
/********************************************/
void SendTAck(bit ack)           //确认接收
{ 
	TSDA=ack;
    TSCL=1;
 	TWait();
	TSCL=0;
}
bit TestTAck(void)           
{bit ack=0;
    TSCL=1;
	TWait();
	if(TSDA)
		ack=1;
	TSCL=0;
	return ack;
}
TLSSentByte(unsigned char bytedata)          //传送一个字节数据
{    unsigned char i=8;
	do{
		TSDA=bytedata&0x80;
		TSCL=1;
		TWait();
		bytedata<<=1;
		TSCL=0;
		TWait();
    	}while(--i);
}
unsigned char TLSReceiveByte(void)             				//接收一个字节数据
{	unsigned char x,i=8;
	TSDA=1;
	do
	{	TSCL=1;
		TWait();
		x<<=1;
		if(TSDA) 
			x++;
		TSCL=0;
		TWait();
	}while(--i);
	return(x);
}
unsigned char TLSWriteReg(unsigned char addr,unsigned char regdata)
{unsigned char sign;
		sign=0;
		TLSStart();
		TLSSentByte(0x72);
		if(TestTAck())
			sign+=0x01;
		TLSSentByte(0x80|addr);
		if(TestTAck())
			sign+=0x02;
		TLSSentByte(regdata);
		if(TestTAck())
			sign+=0x04;
		TLSStop();
		return sign;

}
unsigned char ReadRegMuti(unsigned char data *p,unsigned char addr,unsigned char i)
{
	unsigned char sign=0;
	TLSStart();
	TLSSentByte(0x72);
	if(TestTAck())
		sign+=0x01;
	TLSSentByte(0x80+addr);
	if(TestTAck())
		sign+=0x02;
	TLSStart();
	TLSSentByte(0x73);
	if(TestTAck())
		sign+=0x04;
	for(;;)
	{
		*p=TLSReceiveByte();
		p++;
		i--;
		if(!i) 
			break;
		SendTAck(0);
	}
	SendTAck(1);
	TLSStop();
    return(sign);
}
		
void TLS2561Init(void)
{
	TLSWriteReg(0,3);//03,power up;00,power down
	TLSWriteReg(1,1);//resv ,resv,resv,resv,gain,manual,resv,integ0,integ1
	TLSWriteReg(6,0x00);
}

/*
For 0    < CH1/CH0 =< 0.50  Lux = 0.0304 * CH0 - 0.062 * CH0 * ((CH1/CH0)1.4)
For 0.50 < CH1/CH0 =< 0.61 	Lux = 0.0224 * CH0 - 0.031 * CH1
For 0.61 < CH1/CH0 =< 0.80 	Lux = 0.0128 * CH0 - 0.0153 * CH1
For 0.80 < CH1/CH0 =< 1.30 	Lux = 0.00146* CH0 - 0.00112* CH1
For 		CH1/CH0 > 1.30  Lux = 0
*/
#define LUX_SCALE 14 // scale by 2^14
#define RATIO_SCALE 9 // scale ratio by 2^9
#define CH_SCALE 10 // scale channel values by 2^10
#define CHSCALE_TINT0 0x7517 // 322/11 * 2^CH_SCALE
#define CHSCALE_TINT1 0x0fe7 // 322/81 * 2^CH_SCALE
//...................................................
// T Package coefficients
//...................................................
// For Ch1/Ch0=0.00 to 0.50
// Lux/Ch0=0.0304-0.062*((Ch1/Ch0)^1.4)
// piecewise approximation
// For Ch1/Ch0=0.00 to 0.125:
// Lux/Ch0=0.0304-0.0272*(Ch1/Ch0)
//
// For Ch1/Ch0=0.125 to 0.250:
// Lux/Ch0=0.0325-0.0440*(Ch1/Ch0)
//
// For Ch1/Ch0=0.250 to 0.375:
// Lux/Ch0=0.0351-0.0544*(Ch1/Ch0)
//
// For Ch1/Ch0=0.375 to 0.50:
// Lux/Ch0=0.0381-0.0624*(Ch1/Ch0)
//
// For Ch1/Ch0=0.50 to 0.61:
// Lux/Ch0=0.0224-0.031*(Ch1/Ch0)
//
// For Ch1/Ch0=0.61 to 0.80:
// Lux/Ch0=0.0128-0.0153*(Ch1/Ch0)
//
// For Ch1/Ch0=0.80 to 1.30:
// Lux/Ch0=0.00146-0.00112*(Ch1/Ch0)
//
// For Ch1/Ch0>1.3:
// Lux/Ch0=0
//...................................................
#define K1T 0x0040 // 0.125 * 2^RATIO_SCALE
#define B1T 0x01f2 // 0.0304 * 2^LUX_SCALE
#define M1T 0x01be // 0.0272 * 2^LUX_SCALE
#define K2T 0x0080 // 0.250 * 2^RATIO_SCALE
#define B2T 0x0214 // 0.0325 * 2^LUX_SCALE
#define M2T 0x02d1 // 0.0440 * 2^LUX_SCALE
#define K3T 0x00c0 // 0.375 * 2^RATIO_SCALE
#define B3T 0x023f // 0.0351 * 2^LUX_SCALE
#define M3T 0x037b // 0.0544 * 2^LUX_SCALE
#define K4T 0x0100 // 0.50 * 2^RATIO_SCALE
#define B4T 0x0270 // 0.0381 * 2^LUX_SCALE
#define M4T 0x03fe // 0.0624 * 2^LUX_SCALE
#define K5T 0x0138 // 0.61 * 2^RATIO_SCALE
#define B5T 0x016f // 0.0224 * 2^LUX_SCALE
#define M5T 0x01fc // 0.0310 * 2^LUX_SCALE
#define K6T 0x019a // 0.80 * 2^RATIO_SCALE
#define B6T 0x00d2 // 0.0128 * 2^LUX_SCALE
#define M6T 0x00fb // 0.0153 * 2^LUX_SCALE
#define K7T 0x029a // 1.3 * 2^RATIO_SCALE
#define B7T 0x0018 // 0.00146 * 2^LUX_SCALE
#define M7T 0x0012 // 0.00112 * 2^LUX_SCALE
#define K8T 0x029a // 1.3 * 2^RATIO_SCALE
#define B8T 0x0000 // 0.000 * 2^LUX_SCALE
#define M8T 0x0000 // 0.000 * 2^LUX_SCALE
// lux equation approximation without floating point calculations
//////////////////////////////////////////////////////////////////////////////
// Routine: unsigned int CalculateLux(unsigned int ch0, unsigned int ch0, int iType)
//
// Description: Calculate the approximate illuminance (lux) given the raw
// channel values of the TSL2560. The equation if implemented
// as a piece.wise linear approximation.
//
// Arguments: unsigned int iGain . gain, where 0:1X, 1:16X
// unsigned int tInt . integration time, where 0:13.7mS, 1:100mS, 2:402mS,
// 3:Manual
// unsigned int ch0 . raw channel value from channel 0 of TSL2560
// unsigned int ch1 . raw channel value from channel 1 of TSL2560
// unsigned int iType . package type (T or CS)
//
// Return: unsigned int . the approximate illuminance (lux)
//
//////////////////////////////////////////////////////////////////////////////
unsigned int CalculateLux(unsigned int iGain, unsigned int tInt, unsigned int ch0,unsigned int ch1)
{
//........................................................................
// first, scale the channel values depending on the gain and integration time
// 16X, 402mS is nominal.
// scale if integration time is NOT 402 msec
unsigned long chScale;
unsigned long channel1;
unsigned long channel0;
unsigned long temp;
unsigned long lux;
unsigned long ratio1 = 0;
unsigned long ratio;//
unsigned int b, m;
	switch (tInt)
		{
		case 0: chScale = CHSCALE_TINT0;
				break;						// 13.7 msec
		case 1: chScale = CHSCALE_TINT1;
				break;						// 101 msec
		default:chScale = (1 << CH_SCALE);
				break;						// assume no scaling
		}
	if (!iGain) 							// scale if gain is NOT 16X
		chScale = chScale << 4; 			// scale 1X to 16X
											// scale the channel values
	channel0 = (ch0 * chScale) >> CH_SCALE;
	channel1 = (ch1 * chScale) >> CH_SCALE;
//........................................................................
// find the ratio of the channel values (Channel1/Channel0)
// protect against divide by zero
	if (channel0 != 0) 
		ratio1 = (channel1 << (RATIO_SCALE+1)) / channel0;
	ratio = (ratio1 + 1) >> 1;					// round the ratio value

	if ((ratio >= 0) && (ratio <= K1T))			// is ratio <= eachBreak ?
		{b=B1T; m=M1T;}
	else 
		if (ratio <= K2T)
			{b=B2T; m=M2T;}
		else 
			if (ratio <= K3T)
				{b=B3T; m=M3T;}
			else 
				if (ratio <= K4T)
					{b=B4T; m=M4T;}
				else 
					if (ratio <= K5T)
						{b=B5T; m=M5T;}
					else
						if (ratio <= K6T)
							{b=B6T; m=M6T;}
						else
							if (ratio <= K7T)
								{b=B7T; m=M7T;}
							else 
								if (ratio > K8T)
									{b=B8T; m=M8T;}
	temp = ((channel0 * b) - (channel1 * m));
	if (temp < 0) 							// do not allow negative lux value
		temp = 0;
	temp += (1 << (LUX_SCALE-1));			// round lsb (2^(LUX_SCALE.1))

	lux = temp >> LUX_SCALE;				// strip off fractional portion
	return(lux);
}

unsigned int GetBright(void)
{
unsigned int ch0,ch1;
	ReadRegMuti(BriSeg,12,4);
	ch0=BriSeg[1]*256+BriSeg[0];
	ch1=BriSeg[3]*256+BriSeg[2];
	if(ch0<3)
		{
		if(ch0==0&&ch1==0)
			TLS2561Init();
		return 1;
		}
	else
		{
		if((BriSeg[0]&BriSeg[1]&BriSeg[2]&BriSeg[3])==0xff)
			return 60000;
		else
			return CalculateLux(0, 1,ch0,ch1);
		}
}

⌨️ 快捷键说明

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