From 7095c424774edea5812e105afa61dc8431d7ccdf Mon Sep 17 00:00:00 2001 From: aeprox Date: Sat, 22 Aug 2015 21:19:27 +0200 Subject: [PATCH] Attempt to fix reading of registers --- app/tsl2561/tsl2561.c | 26 ++++++++++++++------------ app/tsl2561/tsl2561.h | 6 +++--- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/app/tsl2561/tsl2561.c b/app/tsl2561/tsl2561.c index 0ec40235..0d6cee26 100644 --- a/app/tsl2561/tsl2561.c +++ b/app/tsl2561/tsl2561.c @@ -1,7 +1,7 @@ /**************************************************************************/ /*! @file tsl2561.c - @author K. Townsend (microBuilder.eu) + @author K. Townsend (microBuilder.eu)/ Adapted for nodeMCU by Michael Lucas (Aeprox @github) @brief Drivers for the TAOS TSL2561 I2C digital luminosity sensor @@ -21,7 +21,7 @@ uint32_t lux; // Initialise luminosity sensor - tsl2561Init(); + tsl2561Init(sda_pin, scl_pin); // Optional ... default setting is 400ms with no gain // Set timing to 101ms with no gain @@ -100,20 +100,22 @@ tsl2561Error_t tsl2561Read16(uint8_t reg, uint16_t *value) platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); platform_i2c_send_byte(tsl2561_i2c_id, reg); platform_i2c_send_stop(tsl2561_i2c_id); + + platform_i2c_send_start(tsl2561_i2c_id); + platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_RECEIVER); + uint8_t ch_low = platform_i2c_recv_byte(tsl2561_i2c_id, 0); + platform_i2c_send_stop(tsl2561_i2c_id); + + platform_i2c_send_start(tsl2561_i2c_id); + platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); + platform_i2c_send_byte(tsl2561_i2c_id, reg+1); + platform_i2c_send_stop(tsl2561_i2c_id); + platform_i2c_send_start(tsl2561_i2c_id); platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_RECEIVER); uint8_t ch_high = platform_i2c_recv_byte(tsl2561_i2c_id, 0); platform_i2c_send_stop(tsl2561_i2c_id); - platform_i2c_send_start(tsl2561_i2c_id); - platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); - platform_i2c_send_byte(tsl2561_i2c_id, reg); - platform_i2c_send_stop(tsl2561_i2c_id); - platform_i2c_send_start(tsl2561_i2c_id); - platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_RECEIVER); - uint8_t ch_low = platform_i2c_recv_byte(tsl2561_i2c_id, 0); - platform_i2c_send_stop(tsl2561_i2c_id); - // Shift values to create properly formed integer (low byte first) *value = (ch_low | (ch_high << 8)); @@ -181,7 +183,7 @@ tsl2561Error_t tsl2561SetTiming(tsl2561IntegrationTime_t integration, tsl2561Gai error = tsl2561Enable(); if (error) return error; - // Turn the device off to save power + // set timing and gain on device error = tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, integration | gain); if (error) return error; diff --git a/app/tsl2561/tsl2561.h b/app/tsl2561/tsl2561.h index 061f263b..98cb9f6f 100644 --- a/app/tsl2561/tsl2561.h +++ b/app/tsl2561/tsl2561.h @@ -1,13 +1,13 @@ /**************************************************************************/ /*! @file tsl2561.h - @author K. Townsend (microBuilder.eu) + @author K. Townsend (microBuilder.eu) / Adapted for nodeMCU by Michael Lucas (Aeprox @github) @section LICENSE Software License Agreement (BSD License) - Copyright (c) 2010, microBuilder SARL + Copyright (c) 2010, microBuilder SARL/ Adapted for nodeMCU by Michael Lucas (Aeprox @github) All rights reserved. Redistribution and use in source and binary forms, with or without @@ -41,7 +41,7 @@ #define TSL2561_PACKAGE_CS // Lux calculations differ slightly for CS package // #define TSL2561_PACKAGE_T_FN_CL -#define TSL2561_ADDRESS (0x72) // 0111001 shifted left 1 bit = 0x72 (ADDR = GND or floating) +#define TSL2561_ADDRESS (0x39) // or 0x29, or 0x49 #define TSL2561_READBIT (0x01) #define TSL2561_COMMAND_BIT (0x80) // Must be 1