A lib in development for TSL2561 light sensor communication.
Dependents: TSL2561_Townsend TSL2561_Light_sensor Light
Revision 1:ab906ac6e90b, committed 2014-01-09
- Comitter:
- anhnt2407
- Date:
- Thu Jan 09 10:16:51 2014 +0000
- Parent:
- 0:df9baf6459b7
- Commit message:
- TSL2561(updated)
Changed in this revision
TSL2561.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r df9baf6459b7 -r ab906ac6e90b TSL2561.cpp --- a/TSL2561.cpp Tue Sep 10 15:13:49 2013 +0000 +++ b/TSL2561.cpp Thu Jan 09 10:16:51 2014 +0000 @@ -1,5 +1,7 @@ #include "TSL2561.h" +#include "mbed.h" + Serial DEBUG(USBTX, USBRX); #define DEBUG_PRINTX(z,x) if(z==1) DEBUG.printf(x); @@ -9,7 +11,7 @@ TSL2561::TSL2561():i2c(TSL2561_I2C_PINNAME_SDA,TSL2561_I2C_PINNAME_SCL){ i2c.frequency (300); - _addr = TSL2561_ADDR_FLOAT<<1; + _addr = TSL2561_ADDR_FLOAT; _initialized = false; _integration = TSL2561_INTEGRATIONTIME_13MS; _gain = TSL2561_GAIN_16X; @@ -17,7 +19,7 @@ TSL2561::TSL2561(uint8_t addr):i2c(TSL2561_I2C_PINNAME_SDA,TSL2561_I2C_PINNAME_SCL) { - _addr = addr<<1; + _addr = addr; _initialized = false; _integration = TSL2561_INTEGRATIONTIME_13MS; _gain = TSL2561_GAIN_16X; @@ -26,7 +28,7 @@ TSL2561::TSL2561(PinName sda, PinName scl):i2c(sda, scl) { - _addr = TSL2561_ADDR_FLOAT<<1; + _addr = TSL2561_ADDR_FLOAT; _initialized = false; _integration = TSL2561_INTEGRATIONTIME_13MS; _gain = TSL2561_GAIN_16X; @@ -35,7 +37,7 @@ TSL2561::TSL2561(PinName sda, PinName scl, uint8_t addr):i2c(sda, scl) { - _addr = addr<<1; + _addr = addr; _initialized = false; _integration = TSL2561_INTEGRATIONTIME_13MS; _gain = TSL2561_GAIN_16X; @@ -57,7 +59,7 @@ } else if (channel == 2) { // Reads all and subtracts out just the visible! - return ( (x & 0xFFFF) - (x >> 16)); + return ( (x & 0xFFFF) - (x >> 16) ); } // unknown channel! @@ -85,19 +87,19 @@ break; } -DEBUG_PRINTLNXY(1," Integration:= %d",_integration); +//DEBUG_PRINTLNXY(0," Integration:= %d",_integration); uint32_t x; x = read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW); - DEBUG_PRINTLNXY(1," x:= %d",x); + // DEBUG_PRINTLNXY(0," x:= %d",x); x <<= 16; x |= read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW); - DEBUG_PRINTLNXY(1," x:= %d",x); + //DEBUG_PRINTLNXY(0," x:= %d",x); - wait(3); + //wait(3); disable(); return x; @@ -106,20 +108,16 @@ bool TSL2561::begin(void) { - char reg = TSL2561_REGISTER_ID; - char read; - - i2c.start(); - i2c.write(_addr); - i2c.write(reg); - - //i2c.start(); - //i2c.write(reg); - read = i2c.read(1); - i2c.stop(); - + char reg[1]; + reg[0] = TSL2561_REGISTER_ID; + char receivedata[1]; + char read; + i2c.write(_addr<<1, reg, 1); + i2c.read(_addr<<1, receivedata, 1); + read=receivedata[0]; + if (read & 0x0A ) { - DEBUG_PRINTLNXY(1,"Read 0x%x => Found TSL2561",read); + DEBUG_PRINTLNXY(0,"Read 0x%x => Found TSL2561",read); } else { return false; } @@ -137,35 +135,37 @@ uint16_t TSL2561::read16(uint8_t reg) { - uint16_t x; uint16_t t; - char _x;char _t;char r[1]; + uint16_t x; + uint16_t t; + char _x; + char _t; + char r[1]; r[0] = reg; - - i2c.start(); - i2c.write(_addr); - i2c.write(reg); + char receivedata[2]; - //i2c.start(); - //i2c.write(reg); - _t = i2c.read(1); - _x = i2c.read(1); + i2c.write(_addr<<1, r, 1); + i2c.read(_addr<<1, receivedata, 2); + + _t=receivedata[0]; + _x=receivedata[1]; - DEBUG_PRINTLNXY(0,"%x",_x); - DEBUG_PRINTLNXY(0,"%x",_t); - - i2c.stop(); + DEBUG_PRINTLNXY(0,"_t:=0x%x",_t); + DEBUG_PRINTLNXY(0,"_x:=0x%x",_x); - t=(uint16_t)&_t; - x=(uint16_t)&_x; + t=(uint16_t)_t; + x=(uint16_t)_x; x <<= 8; x |= t; + + DEBUG_PRINTLNXY(0,"x:= %d",x); + return x; } void TSL2561::write8 (uint8_t reg, uint8_t value) { i2c.start(); - i2c.write(_addr); + i2c.write(_addr<<1); i2c.write(reg); i2c.write(value); i2c.stop(); @@ -175,45 +175,45 @@ if (!_initialized) begin(); -else DEBUG_PRINTLNX(1,"--------------Set Timing---------"); +else DEBUG_PRINTLNX(0,"--------------Set Timing---------"); enable(); _integration = integration; - DEBUG_PRINTLNXY(1,"Integration: 0x%x",_integration); - DEBUG_PRINTLNXY(1,"Gain: 0x%x",_gain); - DEBUG_PRINTLNXY(1,"Integration | Gain: 0x%x",_integration | _gain); + DEBUG_PRINTLNXY(0,"Integration: 0x%x",_integration); + DEBUG_PRINTLNXY(0,"Gain: 0x%x",_gain); + DEBUG_PRINTLNXY(0,"Integration | Gain: 0x%x",_integration | _gain); write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, _integration | _gain); disable(); - DEBUG_PRINTLNX(1,"--------------Complete Set Timing-------------"); + DEBUG_PRINTLNX(0,"--------------Complete Set Timing-------------"); - wait(1); + //wait(1); } void TSL2561::setGain(tsl2561Gain_t gain) { if (!_initialized) begin(); -else DEBUG_PRINTLNX(1,"-------------Set Gain--------------"); +else DEBUG_PRINTLNX(0,"-------------Set Gain--------------"); enable(); - DEBUG_PRINTLNXY(1,"Intergration: 0x%x",_integration); - DEBUG_PRINTLNXY(1,"Gain: 0x%x",_gain); - DEBUG_PRINTLNXY(1,"Intergration | Gain: 0x%x",_integration | _gain); + DEBUG_PRINTLNXY(0,"Intergration: 0x%x",_integration); + DEBUG_PRINTLNXY(0,"Gain: 0x%x",_gain); + DEBUG_PRINTLNXY(0,"Intergration | Gain: 0x%x",_integration | _gain); _gain = gain; write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, _integration | _gain); //write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, _gain); disable(); - DEBUG_PRINTLNX(1,"---------------Complete Set Gain----------------"); - wait(1); + DEBUG_PRINTLNX(0,"---------------Complete Set Gain----------------"); + //wait(1); } @@ -222,7 +222,7 @@ if (!_initialized) begin(); // Enable the device by setting the control bit to 0x03 - DEBUG_PRINTLNX(1," Power On"); + DEBUG_PRINTLNX(0," Power On"); write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWERON); } @@ -231,6 +231,6 @@ if (!_initialized) begin(); // Disable the device by setting the control bit to 0x03 - DEBUG_PRINTLNX(1," Power Off"); + DEBUG_PRINTLNX(0," Power Off"); write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWEROFF); } \ No newline at end of file