Page 1 of 3

I2c Help please

Posted: Tue Dec 24, 2013 9:16 pm
by acestu
Hi guys,

I am trying to work out how i2c works and I have a TSL2561 light sensor to mess with, to power on the device via i2c I must as the data sheet says, send a 03h to the 0h register, but what is a 03h it isn't Hex ?
03h.png
(12.51 KiB) Downloaded 8485 times
cheers
Acestu

Re: I2c Help please

Posted: Tue Dec 24, 2013 9:33 pm
by kersing
An 'h' after a number (number is 0-9, a-f) means a hex number, so 0x03 for Flowcode.

Re: I2c Help please

Posted: Tue Dec 24, 2013 10:38 pm
by acestu
Hi Kersing,

So it is just another way of saying that it is a Hex number, I was on the understanding that 0x suggested a Hex number as you say,

Thanks
Acestu

Re: I2c Help please

Posted: Tue Dec 24, 2013 11:21 pm
by acestu
Hi,

I found this conversion table online, but how do you convert a Hex to Binary if it stats with a zero ie 03h ?
Hexa-Binary-Decimal-Octal-Conversion-table.pdf
(38.85 KiB) Downloaded 282 times
thanks
Acestu

Re: I2c Help please

Posted: Tue Dec 24, 2013 11:36 pm
by acestu
Hi,

I have just found some more info on line, would i be right in thinking that you take the zero and convert to binary, and then take the three and convert to binary and put the resulting binary numbers togethor ?
hexconverter.png
(19.95 KiB) Downloaded 8472 times
cheers
Acestu


EDIT: So 03h would be 0000 0011

Re: I2c Help please

Posted: Tue Dec 24, 2013 11:40 pm
by kersing
Zero hex is binary 0000, so 03h would be 0b00000011. The easiest way to convert hex to binary is to take the individual hex digits starting at the front and use the table below to convert, concatenating the results:

Code: Select all

Hex   Bin
0  0000
1  0001
2  0010
3  0011
4  0100
5  0101
6  0110
7  0111
8  1000
9  1001
A  1010
B  1011
C  1100
D  1101
E  1110
F  1111
So 0x1f6 would be 0b0001 1111 0110 (without the spaces, I just inserted them for readability in the example)

Re: I2c Help please

Posted: Wed Dec 25, 2013 12:06 am
by acestu
Hi Kersing,

I have got that not, just need to figure out how to send to the control register, it will keep me busy over xmas :lol:

cheers
Acestu

Re: I2c Help please

Posted: Wed Dec 25, 2013 12:27 am
by GTF
Or if you are lazy, there are online conversion tools: http://www.binaryhexconverter.com/

Merry Christmas!

Grant

Edit: Or just use Windows Calculator in programming mode:

Re: I2c Help please

Posted: Wed Dec 25, 2013 12:49 am
by acestu
Hi,

So if I wanted to send (value 03h) to the control register (internal address 0h) of an i2c slave address of 0x39 , is this how I would do it ?

only I don't reall understand the AddH and AddL in Flowcode
FlowWindow.png
(20.43 KiB) Downloaded 8464 times
cheers
Acestu

Re: I2c Help please

Posted: Wed Dec 25, 2013 1:30 am
by kersing
I don't think it will work. The transaction macros are used for memory devices, not generic devices. The TransmitByte macro is the one to use.

Start communications with Start, then Use TransmitByte multiple times with arguments:
Address of slave shifted one to the left and last bit set to one (see page 12 of data sheet)
Byte 0x80 (command register, bit 7 must be set)
Byte 3 ( value)

The macro has a second argument to signal end of transaction if I recall correctly, set it for end at the last byte (3).
Finish with Stop.


To get bytes, use ReceiveByte. Don't forget to use SendByte macros to tell the slave what you want to receive.

Of course, at the start of you program, call Initialise as well.

Good luck!

Edit: check the c code at https://github.com/adafruit/Adafruit_TSL2561 for sequences.

Re: I2c Help please

Posted: Wed Dec 25, 2013 9:52 pm
by acestu
Hi Kersing,

Thanks for your valued advice, I will take a look at your link now....

Acestu

Re: I2c Help please

Posted: Wed Dec 25, 2013 10:05 pm
by JohnCrow
Hi Stu

I've attached some I2C tutorial files. Hope they might be useful
I found I was banging my head against a brick wall with I2C when I first used it, then as if by magic (the shopkeeper appeared ) and it all became clear :D :lol:
I2C.zip
(937.43 KiB) Downloaded 250 times

Re: I2c Help please

Posted: Wed Dec 25, 2013 10:23 pm
by acestu
Hi Kersing,

I had a look at the link but it appears to be code for an Arduino,

I read the documents and found the section below:

Enables the device
*/
/**************************************************************************/
void Adafruit_TSL2561_Unified::enable(void)
{
/* Enable the device by setting the control bit to 0x03 */
write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWERON);


I haven't done this before but I would imagine that the clue would be in library files or defines so I had another search and found these:

#define TSL2561_COMMAND_BIT (0x80) // Must be 1

TSL2561_REGISTER_CONTROL = 0x00,

#define TSL2561_CONTROL_POWERON (0x03)



But I am not sure about the Write8 at the beginning because this is only 6 bits ie 80h,00h,03h



thanks
Acestu


EDIT: or does it mean that each hex number is 8 binary bits, and there is 3 different write commands ?

Re: I2c Help please

Posted: Wed Dec 25, 2013 10:35 pm
by acestu
Hi John,

Thanks for the files, I will have a read now..........

cheers
Acestu

Re: I2c Help please

Posted: Thu Dec 26, 2013 12:44 am
by kersing
Hi Stu,

You are supposed to write three bytes to start the sensor. Each byte has 8 bits, write8 (defined in the same file you found the other code) writes the address (byte), the command (byte) and the data (byte). That equals writing three bytes of 8 bits.

Jac

Re: I2c Help please

Posted: Thu Dec 26, 2013 4:24 pm
by GTF
Hi,

I'm just a beginner with this myself but this is my understanding after reading the datasheet found
here: http://www.adafruit.com/datasheets/TSL2561.pdf

If the address select terminal is floating (page 9 of datasheet):
Slave address (7 bits) is 0111001
Add a 0 for the 8th bit if you are sending or writing to the device.
So the first byte(8 bits) sent after Start will be: 01110010 = 0x72 hex

If receiving or reading from the device the last bit will be 1, so the first byte sent
after Start will be 01110011 = 0x73 hex.

The next byte can either be data if sending or receiving (Figures 8 and 9),
or a Command byte if writing or reading(Figures 10 and 11) to or from a device register address.

The most significant bit in a Command byte is always "1". (1XXXXXXX)
The lower 4 bits are chosen from table 2 for the register you are writing to.
So for the Control register they would be 0000 (1XXX0000). Bits 6,5,4 are set according to table 3. Default values are 0.
For a basic power up the Command byte would be 10000000.

For a power up the next byte will be what you are writing to the Control register(Table 4), 03h or 00000011 for power up.
0h or 00000000 for power down.

So for a powerup the bytes sent would be 01110010 10000000 00000011 or 0x72, 0x80, 0x03

In FC:

I2C Init

I2C Start
I2c Transmit Byte 0x72
I2C Transmit Byte 0x80
I2c Transmit Byte 0x03
I2C Stop

For Power down the 3rd byte sent is 0x00.

Grant

Re: I2c Help please

Posted: Thu Dec 26, 2013 4:38 pm
by kersing
Hi Grant,

Small correction, the value for power up is 03h (00000011) as you mention earlier in your message, not 0x01 or 00000010 (0x02). So the third byte should be 0x03, not 0x01.

Jac

Re: I2c Help please

Posted: Thu Dec 26, 2013 4:48 pm
by GTF
kersing wrote:Hi Grant,

Small correction, the value for power up is 03h (00000011) as you mention earlier in your message, not 0x01 or 00000010 (0x02). So the third byte should be 0x03, not 0x01.

Jac
Thanks, I corrected this in my post to help avoid any confusion.
Should have waited til after my first cup of coffee this morning before making a post.

Re: I2c Help please

Posted: Fri Dec 27, 2013 10:40 am
by acestu
Hi Kersing and GTF,

Thank you for your posts as always, they are making things a lot easier for me, I have put a chart together of how I think it should be done, one thing that I am not quite sure about is the address, I know that you have to add the 1 to put into write mode, but I was told that because the Flowcode macro used was "Transmit Byte" you just put your address in and Flowcode automatically adjusted it for you.


Thanks
Acestu
TSL2561.fcfx
(7.56 KiB) Downloaded 178 times

Re: I2c Help please

Posted: Fri Dec 27, 2013 6:10 pm
by GTF
I am adding the 8th bit (R/W) to the 7 bit address to get a full byte for my I2C devices to work. I think the Transmit/Receive in FC changes the way the Acknowledge bits are handled as is mentioned in the TSL2561 datasheet.

Re: I2c Help please

Posted: Sat Dec 28, 2013 12:20 am
by acestu
Hi GTF,

Thanks for the chart, I have built a breadboard setup tonight to test the charts with an i2c lcd module which is now giving me grief, I think I will have to get a bigger breadboard and use a serial lcd in the next few days (everything is awkward at the mo because of xmas).....

cheers
Acestu

Re: I2c Help please

Posted: Sat Dec 28, 2013 4:55 am
by GTF
It could be a little more simple as below.

Re: I2c Help please

Posted: Sat Dec 28, 2013 6:53 pm
by acestu
Hi GTF,

Now I have rebuilt the breadboard I have added a push button to my chart that sets the power up process going, the value returned is 255 this is the case when the slave address is 0x39 or 0x72 so I don't think it is communicating with the chip, do you know of an easy test for this ?
TSL-TEST.JPG
(135.95 KiB) Downloaded 3325 times
TSL2561_MK2.fcfx
(8.04 KiB) Downloaded 186 times

cheers
Acestu

Re: I2c Help please

Posted: Sat Dec 28, 2013 7:04 pm
by acestu
Hi,

I am using the Adafruit 757 level shifter to power the TSL2561 , could this be the problem ?

http://www.adafruit.com/products/757

Here is a pic of how I have it wired:

level-converter.png
(357.9 KiB) Downloaded 8255 times
cheers
Acestu

Re: I2c Help please

Posted: Sat Dec 28, 2013 8:16 pm
by acestu
Hi,

I have just had a thought, because I am using the level converter, do I still need to use pull-ups or are they included in the device?

I will try and find out...

cheers
Acestu