Translate

Saturday, May 19, 2018

MSP430 - reading a TSL2561

The TSL2561 is a chipset capable to read IR and visible light intensity and is the chipset used in the iRis project.
This article give some suggestions about how to use this sensor.

Hardware


The sensor is available on two breakout boards, one from Sparkfun and one from Adafruit.
This article is based on the Sparkfun version, but I don't see any difference from the Adafruit one, so both should be considered identical.

To better play initially, the on-board pull-up are disabled. In this way to external resistors are needed or the ones in the MSP430 can be used.
Initially the idea was to use an MSP430f2013 for the experiments, but the need to have also other pieces of code, like the encoder management and the PWM management, forced to adopt the little bit bigger MSP430g2452.

Software


To handle the I2C protocol, I used a library developed by  Jan Rychter (thanks !!).
Very easy to deploy, only few modifications were necessary to compile it under Mspgcc (see details in the MSP430 - I2C library article)

Sequences

The I2C library uses a function capable to send out a sequence of commands, including the reading.
Here some sequences, assuming the default address of 0x39.
I obtained the sequences experimentally, the data sheet is not particularly clear on many things.
Here the sequences for the I2c with USI I obtained to work with the sensor.

Wake up

sequence : {0x39<<1, 0x80, 0x03}
length      : 3

Sleep

sequence : {0x39<<1, 0x80, 0x00}
length      : 3

Setting the sensor

sequence : {0x39<<1, 0x81, PROG_VALUE}
length      : 3

Reading byte mode - LSB - channel 0

sequence : {0x39<<1, 0x8C, I2C_RESTART, (0x39<<1)|1, I2C_READ}
length      : 5

Reading byte mode - MSB - channel 0

sequence : {0x39<<1, 0x8D, I2C_RESTART, (0x39<<1)|1, I2C_READ}
length      : 5

Reading byte mode - LSB - channel 1

sequence : {0x39<<1, 0x8E, I2C_RESTART, (0x39<<1)|1, I2C_READ}
length      : 5

Reading byte mode - MSB - channel 1

sequence : {0x39<<1, 0x8F, I2C_RESTART, (0x39<<1)|1, I2C_READ}
length      : 5

Reading word mode - channel 0

sequence : {0x39<<1, 0xAC, I2C_RESTART, (0x39<<1)|1, I2C_READ, I2C_READ}
length      : 6

Reading word mode - channel 1

sequence : {0x39<<1, 0xAE, I2C_RESTART, (0x39<<1)|1, I2C_READ, I2C_READ}
length      : 6

Code snippet

Here some basic code.

In the header files the basic sequences:

/*
 *  TSL2561 Sequences
 */
#define TSL2561_RADRS (TSL2561_ADDR<<1)|TSL2561_READBIT
#define TSL2561_WADRS (TSL2561_ADDR<<1)

#define TSL2561_READID {TSL2561_WADRS, TSL2561_REGISTER_ID, I2C_RESTART, TSL2561_RADRS, I2C_READ}
#define TSL2561_READID_LEN 5

#define TSL2561_WAKEUP {TSL2561_WADRS, TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWERON}
#define TSL2561_WAKEUP_LEN 3

#define TSL2561_SLEEP {TSL2561_WADRS, TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWEROFF}
#define TSL2561_SLEEP_LEN 3

#define TSL2561_CONFIG {TSL2561_WADRS, TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, PLACEHOLDER}
#define TSL2561_CONFIG_LEN 3

#define TSL2561_READ8_CH0_LSB {TSL2561_WADRS, TSL2561_COMMAND_BIT | TSL2561_REGISTER_CHAN0_LOW, I2C_RESTART, TSL2561_RADRS, I2C_READ}
#define TSL2561_READ8_CH0_LSB_LEN 5

#define TSL2561_READ8_CH0_MSB {TSL2561_WADRS, 0x8D, I2C_RESTART, TSL2561_RADRS, I2C_READ}
#define TSL2561_READ8_CH0_MSB_LEN 5

#define TSL2561_READ8_CH1_LSB {TSL2561_WADRS, 0x008E, I2C_RESTART, TSL2561_RADRS, I2C_READ}
#define TSL2561_READ8_CH1_LSB_LEN 5

#define TSL2561_READ8_CH1_MSB {TSL2561_WADRS, 0x008F, I2C_RESTART, TSL2561_RADRS, I2C_READ}
#define TSL2561_READ8_CH1_MSB_LEN 5

#define TSL2561_READ16_CH0 {TSL2561_WADRS, 0x00AC, I2C_RESTART, TSL2561_RADRS, I2C_READ, I2C_READ}
#define TSL2561_READ16_CH0_LEN 6

#define TSL2561_READ16_CH1 {TSL2561_WADRS, 0x00AE, I2C_RESTART, TSL2561_RADRS, I2C_READ, I2C_READ}
#define TSL2561_READ16_CH1_LEN 6






No comments:

Post a Comment