资源简介
一个市面上较常用的光感应蕊片的C程序TSL2561
代码片段和文件信息
#include
#include
#include
#include
#include
#include “TSL2561.h“
TSL2561::TSL2561(uint8_t addr) {
_addr = addr;
_initialized = false;
_integration = TSL2561_INTEGRATIONTIME_13MS;
_gain = TSL2561_GAIN_16X;
// we cant do wire initialization till later because we havent loaded Wire yet
}
boolean TSL2561::begin(void) {
Wire.begin();
// Initialise I2C
Wire.beginTransmission(_addr);
Wire.send(TSL2561_REGISTER_ID);
Wire.endTransmission();
Wire.requestFrom(_addr 1);
int x = Wire.receive();
//Serial.print(“0x“); Serial.println(x HEX);
if (x & 0x0A ) {
//Serial.println(“Found TSL2561“);
} else {
return false;
}
_initialized = true;
// Set default integration time and gain
setTiming(_integration);
setGain(_gain);
// Note: by default the device is in power down mode on bootup
disable();
return true;
}
void TSL2561::enable(void)
{
if (!_initialized) begin();
// Enable the device by setting the control bit to 0x03
write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL TSL2561_CONTROL_POWERON);
}
void TSL2561::disable(void)
{
if (!_initialized) begin();
// Disable the device by setting the control bit to 0x03
write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL TSL2561_CONTROL_POWEROFF);
}
void TSL2561::setGain(tsl2561Gain_t gain) {
if (!_initialized) begin();
enable();
_gain = gain;
write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING _integration | _gain);
disable();
}
void TSL2561::setTiming(tsl2561IntegrationTime_t integration)
{
if (!_initialized) begin();
enable();
_integration = integration;
write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING _integration | _gain);
disable();
}
uint32_t TSL2561::calculateLux(uint16_t ch0 uint16_t ch1)
{
unsigned long chScale;
unsigned long channel1;
unsigned long channel0;
switch (_integration)
{
case TSL2561_INTEGRATIONTIME_13MS:
chScale = TSL2561_LUX_CHSCALE_TINT0;
break;
case TSL2561_INTEGRATIONTIME_101MS:
chScale = TSL2561_LUX_CHSCALE_TINT1;
break;
default: // No scaling ... integration time = 402ms
chScale = (1 << TSL2561_LUX_CHSCALE);
break;
}
// Scale for gain (1x or 16x)
if (!_gain) chScale = chScale << 4;
// scale the channel values
channel0 = (ch0 * chScale) >> TSL2561_LUX_CHSCALE;
channel1 = (ch1 * chScale) >> TSL2561_LUX_CHSCALE;
// find the ratio of the channel values (Channel1/Channel0)
unsigned long ratio1 = 0;
if (channel0 != 0) ratio1 = (channel1 << (TSL2561_LUX_RATIOSCALE+1)) / channel0;
// round the ratio value
unsigned long ratio = (ratio1 + 1) >> 1;
unsigned int b m;
#ifdef TSL2561_PACKAGE_CS
if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1C))
{b=TSL2561_LUX_B1C; m=TSL2561_LUX_M1C;}
else if (ratio <= TSL2561_LUX_K2
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2468 2011-08-30 08:03 TSL2561\examples\tsl2561\tsl2561.pde
文件 6169 2012-12-23 22:09 TSL2561\TSL2561.cpp
文件 6242 2012-12-23 22:08 TSL2561\TSL2561.h
目录 0 2012-05-01 12:33 TSL2561\examples\tsl2561
目录 0 2012-05-01 12:33 TSL2561\examples
目录 0 2003-01-02 17:25 TSL2561
----------- --------- ---------- ----- ----
14879 6
评论
共有 条评论