TSL2568, TSL2569
LIGHT-TO-DIGITAL CONVERTER
TAOS091D DECEMBER 2008
24
r
r
Copyright E 2008, TAOS Inc.
The LUMENOLOGY r Company
www.taosinc.com
// 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 TSL2568. The equation if implemented
//
as a piecewise 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 TSL2568
//
unsigned int ch1 raw channel value from channel 1 of TSL2568
//
unsigned int iType package type (0:T, 1:CS)
//
// Return:
unsigned int the approximate illuminance (lux)
//
//////////////////////////////////////////////////////////////////////////////
unsigned int CalculateLux(unsigned int iGain, unsigned int tInt, unsigned int ch0,
unsigned int ch1, int iType)
{
//
// 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;
switch (tInt)
{
case 0:
// 13.7 msec
chScale = CHSCALE_TINT0;
break;
case 1:
// 101 msec
chScale = CHSCALE_TINT1;
break;
default:
// assume no scaling
chScale = (1 << CH_SCALE);
break;
}
// scale if gain is NOT 16X
if (!iGain) 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
unsigned long ratio1 = 0;
if (channel0 != 0) ratio1 = (channel1 << (RATIO_SCALE+1)) / channel0;
// round the ratio value
unsigned long ratio = (ratio1 + 1) >> 1;
// is ratio <= eachBreak ?
unsigned int b, m;