View previous topic :: View next topic |
Author |
Message |
ntrungtruc2003
Joined: 16 Feb 2011 Posts: 42
|
Help light sensor TSL2561 I2C |
Posted: Mon Jun 20, 2011 4:24 am |
|
|
Hi All!
I have a problem with I2c
TAOS TSL2561 light sensor connect to PIC16F877A by I2c. I can not receive data from TSL2561 light sensor . It has the value is 255 on LCD
In the following is datasheet of TSL2561
http://www.taosinc.com/downloaddetail.aspx?did=140
Please show me what i am wrong.
Here is my code:
tsl2561.c
Code: |
#define TSL2561_SDA PIN_C4
#define TSL2561_SCL PIN_C3
#use i2c(master, sda=TSL2561_SDA, scl=TSL2561_SCL)
//==========================
//==========================
void init_TSL2561()
{
output_float(TSL2561_SCL);
output_float(TSL2561_SDA);
}
//==========================
//==========================
// power up TSL2561
void Power_On()
{
i2c_start();
i2c_write(0x29);
i2c_write(0x80);
i2c_write(0x03);
i2c_stop();
}
int16 read_TSL2561C0() // read adc channel 0
{
int channel0;
BYTE DataHigh,DataLow;
i2c_start();
i2c_write(0x29); //address
i2c_write(0x8C);// 8 is the command register and C is the data low register of channel 0
i2c_start(); // restart I2c
i2c_write(0x29+1);// address + 1 is the read mode
DataLow=i2c_read(); // read low data of channel 0
i2c_stop();
i2c_start();
i2c_write(0x29); // address
i2c_write(0X8D); // 8 is the command register and D is the high data register of channel 0
i2c_start();
i2c_write(0x29+1); address+ 1 the the read mode
DataHigh=i2c_read();// read high data of channel 0
i2c_stop(); // stop i2c
Channel0 = DataHigh*256+DataLow; // conjunction 16 bit data of channel 0
return(Channel0);
}
|
Here is code in main
Code: |
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=8000000)
#include "tsl2561.c"
#include "LCD_TM.c"
#use rs232(baud=9600,parity= N,xmit=PIN_C6,rcv=PIN_C7,bits = 9)
#define BUFFER_SIZE 32
void main()
{
int16 L1;
init_TSL2561();
Power_On(); // power up
L1 = read_TSL2561C0(); //read data 16 bits of channel 0
lcd_gotoxy(12,1);
printf(lcd_putc,"%Lu",L1);
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19506
|
|
Posted: Mon Jun 20, 2011 4:41 am |
|
|
Your address has to be wrong.
I2C write addresses are always _even_ numbers (the read/write bit is a zero).
Depending on the voltage at the ADDR SEL terminal, the device addresses are:
GND 0x52/0x53
Float 0x72/0x73
VDD 0x92/0x93
Addresses are _left justified_ in the byte. You are treating them as right justified.
Best Wishes |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Mon Jun 20, 2011 5:28 am |
|
|
Also be sure to use the proper pullup resistors on the I2C lines ! |
|
|
ntrungtruc2003
Joined: 16 Feb 2011 Posts: 42
|
|
Posted: Tue Jun 21, 2011 7:56 pm |
|
|
Thanks all
It hasn't received data from TSL2561 light sensor yet.
Following your guide I change address of light sensor 0x52(write-GND mode) and 0x53(read-GND mode). The pull up resistor is 4k7 Ohm.
May be an error in the following code.
Code: | void init_TSL2561()
{
output_float(TSL2561_SCL);
output_float(TSL2561_SDA);
} |
Please show me what am i doing wrong ? |
|
|
|