Browse Source

bug fix
- fix the bug of calculating the lux value.

Jack Shao 11 năm trước cách đây
mục cha
commit
43db36fa0c
2 tập tin đã thay đổi với 15 bổ sung14 xóa
  1. 9 10
      Digital_Light_TSL2561.cpp
  2. 6 4
      Digital_Light_TSL2561.h

+ 9 - 10
Digital_Light_TSL2561.cpp

@@ -6,7 +6,7 @@
  * Website    : www.seeed.cc
  * Author     : zhangkun
  * Create Time:
- * Change Log :
+ * Change Log :  Jack Shao, Nov 2014, bug fix and update for user experience
  *
  * The MIT License (MIT)
  *
@@ -28,14 +28,13 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
-
 #include <Digital_Light_TSL2561.h>
 #include <Arduino.h>
 #include <Wire.h>
-int TSL2561_CalculateLux::readRegister(int deviceAddress, int address)
+uint8_t TSL2561_CalculateLux::readRegister(int deviceAddress, int address)
 {
 
-    int value;
+    uint8_t value;
      Wire.beginTransmission(deviceAddress);
      Wire.write(address);                // register to read
      Wire.endTransmission();
@@ -46,7 +45,7 @@ int TSL2561_CalculateLux::readRegister(int deviceAddress, int address)
      return value;
 }
 
-void TSL2561_CalculateLux::writeRegister(int deviceAddress, int address, int val)
+void TSL2561_CalculateLux::writeRegister(int deviceAddress, int address, uint8_t val)
  {
      Wire.beginTransmission(deviceAddress);  // start transmission to device
      Wire.write(address);                    // send register address
@@ -62,8 +61,8 @@ void TSL2561_CalculateLux::writeRegister(int deviceAddress, int address, int val
     CH1_LOW=readRegister(TSL2561_Address,TSL2561_Channal1L);
     CH1_HIGH=readRegister(TSL2561_Address,TSL2561_Channal1H);
 
-    channel0=CH0_HIGH*256+CH0_LOW;
-    channel1=CH1_HIGH*256+CH1_LOW;
+    ch0 = (CH0_HIGH<<8) | CH0_LOW;
+    ch1 = (CH1_HIGH<<8) | CH1_LOW;
  }
  void TSL2561_CalculateLux::init()
  {
@@ -96,8 +95,8 @@ void TSL2561_CalculateLux::writeRegister(int deviceAddress, int address, int val
 }
 if (!iGain)  chScale = chScale << 4; // scale 1X to 16X
 // scale the channel values
-channel0 = (channel0 * chScale) >> CH_SCALE;
-channel1 = (channel1 * chScale) >> CH_SCALE;
+channel0 = (ch0 * chScale) >> CH_SCALE;
+channel1 = (ch1 * chScale) >> CH_SCALE;
 
   ratio1 = 0;
  if (channel0!= 0) ratio1 = (channel1 << (RATIO_SCALE+1))/channel0;
@@ -142,7 +141,7 @@ channel1 = (channel1 * chScale) >> CH_SCALE;
  }
   temp=((channel0*b)-(channel1*m));
   if(temp<0) temp=0;
-  temp+=(1<<LUX_SCALE-1);
+  temp+=(1<<(LUX_SCALE-1));
   // strip off fractional portion
   lux=temp>>LUX_SCALE;
   return (lux);

+ 6 - 4
Digital_Light_TSL2561.h

@@ -28,10 +28,11 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
-
 #ifndef Digital_Light_TSL2561_H
 #define Digital_Light_TSL2561_H
 
+#include <Arduino.h>
+
 #define  TSL2561_Control  0x80
 #define  TSL2561_Timing   0x81
 #define  TSL2561_Interrupt 0x86
@@ -106,10 +107,11 @@ class TSL2561_CalculateLux
   unsigned long calculateLux(unsigned int iGain, unsigned int tInt,int iType);
   void getLux(void);
   void init(void);
-  int readRegister(int deviceAddress, int address);
-  void writeRegister(int deviceAddress, int address, int val);
+  uint8_t readRegister(int deviceAddress, int address);
+  void writeRegister(int deviceAddress, int address, uint8_t val);
  private:
-  int CH0_LOW,CH0_HIGH,CH1_LOW,CH1_HIGH;
+  uint8_t CH0_LOW,CH0_HIGH,CH1_LOW,CH1_HIGH;
+  uint16_t ch0,ch1;
   unsigned long chScale;
   unsigned long channel1;
   unsigned long channel0;