Basic i2c problems and questions

gerrymcc

New Member
I have done a few successful PICAXE projects, although my programming skills are not particularly good.

I am now wanting to do an i2c project for work. So far, everything is looking good, except for getting the i2c driven 12 bit ADC working. I have bought a Linear Technology LTC2305 demo board, to simplify construction with small SMD devices. (I only want one copy of this project.)

I am using a 28X1 on my own (soldered) proto board. I have used it in the past, and it works fine. For this project, I have the 2x16 LCD module working from this proto board. I have 4k7 resistors on the board as the pull-up resistors for SCA and SDA lines. There will be only one i2c device on the bus.

I now have a scope at home and I cannot see any activity on the SCL and SDA lines. SCL remains hi, and SDA is lo.

I have questions about how the LTC2305 should be addressed- but first I would like to see some (any!) activity on the SCL and/or SDA lines. At this stage, I am suspecting that I am not setting up the i2c bus properly.

I am confused between what seems to be two sets of i2c commands: i2c slave, i2cwrite, i2cread, and a larger set of hi2cin; hi2cout and the hi2csetup- master and slave. What is the differences functionally?

Here is the simple code I have been trying:

start:
i2cslave %1101011, i2cslow, i2cbyte
writei2c %1101011,(%1000100)
pause 10
readi2c %1101011, (w0, w1)
pause 1000
goto start
end

%1000100 is for configuring the ADC- unipolar, channel 0 etc.

1101011 is the generic address for the LTC2305 mentioned in the datasheet.

The LTC2305 has two address pins, giving 8 possible states. (Pins can be hi, lo or floating.) An earlier part of the datasheet gives a table of addresses using just these pins. These pins are both jumpered lo, and the corresponding address in this table is 0001000. Replacing 1101011 with 0001000 makes no difference.

Any comments, suggestions etc. would be most welcome.

Thank you
 

gerrymcc

New Member
Should have included above that I have read through the i2c tutorial here, and also searched through the forum. gerrymcc
 
When triggering a conversion using the write command and then reading from the I2C device, you don't need to use the ADC IC address again because you have already defined it using the i2cslave command. Also you have told the device that you want bytes but then used words in the read command.

I have used I2C with a PICAXE to read a 12 bit ADC although not the same one that you are using.

FYI my code for a different 12 bit ADC was

i2cslave %10011011, i2cfast_16, i2cbyte Defines the I2c slave device, speed and data format
writei2c (b0) Triggers conversion
readi2c (b11, b10) Reads the two bytes of data from the conversion

Hope this helps
 

westaust55

Moderator
I am confused between what seems to be two sets of i2c commands: i2c slave, i2cwrite, i2cread, and a larger set of hi2cin; hi2cout and the hi2csetup- master and slave. What is the differences functionally?
To answer your question over the two groups of i2c commands:
1. i2cslave, i2cwrite (writei2c), and i2cread (readi2c) are older command which were primarily applicable to older chips. These commands are now deprecated and the programming Editor will auto convert to the newer commands.

2 hi2csetup, hi2cout and hi2cin are newer commands which came into use with the X1 parts.
As signified by the "h" at the start of the command word, they are using the PIC chips inbuilt hardware comms
Module rather than bit-bashed in the firmware.
One hardware module is available for both i2c and SPI hence usual to use only one of these comms protocols at a time.
 
Last edited:

Grogster

Senior Member
I now have a scope at home and I cannot see any activity on the SCL and SDA lines. SCL remains hi, and SDA is lo.
Should not BOTH SCL and SDA be idle-high?
In the I2C tutorial, and any other information I can find about using the I2C bus, clock and data lines are idle-high via 4k7 pull-up resistors. With nothing going on on the bus, both those lines should read as HIGH.

Do not leave floating(not connected to either supply or gnd), or tie either one to gnd.
 

westaust55

Moderator
The LTC2305 has two address pins, giving 8 possible states. (Pins can be hi, lo or floating.) An earlier part of the datasheet gives a table of addresses using just these pins. These pins are both jumpered lo, and the corresponding address in this table is 0001000. Replacing 1101011 with 0001000 makes no difference.
Your indication that 2 pins gave 8 slave addresses had me confused. 2 pins suggests either 2^2 = 4 addresses or if 3 state inputs then 3^2 = 9 addresses.
Datasheet Table 2 (and yes, there are two Table 2's in the datasheet - pages 13 and again on page 18 :mad:) shows that it is three state inputs and there are 9 possible slave address.


As other have already indicated, the datasheet gives you the 7-bit address (eg %0001000 ) but as per the Figure 8 on page 18 of the datasheet you must also add the R/W bit as the least significant bit (bit0) of the addressing byte to make up the entire byte sent from the PICAXE to the LTC2305.

You must write a byte (the datasheet uses the term "word") to the LTC2305 to configure the chip and set whether it is channel 1 or 2 or in differential mode. From the datasheet page 12:

The input data "word" for the LTC2305 is defined as follows:
S/D O/S X X UNI SLP
S/D = SINGLE-ENDED/DIFFERENTIAL BIT
O/S = ODD/SIGN BIT
UNI = UNIPOLAR/BIPOLAR BIT
SLP = SLEEP MODE BIT​
See also Table 1 on the same datasheet page.
 

inglewoodpete

Senior Member
Should not BOTH SCL and SDA be idle-high?
In the I2C tutorial, and any other information I can find about using the I2C bus, clock and data lines are idle-high via 4k7 pull-up resistors. With nothing going on on the bus, both those lines should read as HIGH.

Do not leave floating(not connected to either supply or gnd), or tie either one to gnd.
Groggy's right. Both i2c lines should idle high. The PICAXE and the LTC2305 are both socketed, aren't they? With the power off, remove both chips, restore power and check the state of both scl and sda. If they are not both high, you need to find what is pulling the offending line low. If both are high with no chips, power down and insert the LTC2305 and check them again. They should remain high. If not, the LTC is faulty. Finally, add the PICAXE and test again if necessary.

Of course, you have the 4.7k-or-so pullups in place, don't you?
 

westaust55

Moderator
Here is some sample code (untested) but as a result of having a further read of the datasheet:

Code:
SYMBOL LTC2305 = %00010000 ; LT2305 Slave Address with both the AD0 and AD1 pins pulled low

; Control byte format: S/D O/S X X UNI SLP X X - See datasheet Fig 7b
; For two channel unipolar single ended operation reference to ground and using NAP mode ie SLP bit = 0
SYMBOL Channel0 = %10001000 ; this specified for ADC for single ended channel 0
SYMBOL Channel1 = %11001000 ; this specified for ADC for single ended channel 1

Init:
Init:
	HI2CSETUP I2CMaster, LTC2305, I2CFast, I2CByte ; we can use I2CFast as the chip will work at 400 kHz
						 ; use I2C byte as there are no register locations as such

Main:
	HI2COUT (Channel0)
	PAUSE 1 		             ; this may not be necesary but included to give LTC2305 time for conversion 250 ns
	HI2CIN (b9,b8)	             ; read the 16 bit data stream into word variable w4 = B9:b8
	w4 = w4 / 16 	             ; could use w4 = w4 >> 4 with and X1 or X2 part to shift 4 bits to right
	SERTXD ("Channel 0 reading = ", #w4, cr, lf)
	PAUSE 1000		; pause for 1 second
	GOTO Main
 

gerrymcc

New Member
westaust55 is a genius, and I want to thank him many times over for taking the time to put together the code he offered in in his earlier reply. It worked beautifully, first time. If only I could do that!! Now I have to study it carefully to understand totally how it works. Thank you also to all the other members who offered suggestions- they were read with interest and considered.

I want to add a few other comments about what I have found between my first post and now.

I did have the 4K7 resistors in place from the start. I decided to use the Linear technology demo board to minimise the chances of problems due to implementing the LT2305. Despite the cost, I think it was the right way to go. When the code worked, I could check it immediately by using the Vref test point (2.5volts) as a source of input voltage within the board. The LT2305 has 4.096 volts as the maximum input.

With the scope, I tried as many things as I could to see the clock (SCK) and data (SDA) lines toggle. (I do apologise- both lines were high- blame a scope probe maybe?) Finally, I changed the 28X1 over to the only other one I had, and the lines toggled immediately. I had used the old chip to develop the LCD display part of this project, using the OutC pins, and it works fine. There must be a problem with the i2c pins on that chip. Another LLM. (Life's Little Mystery.) There are some aspects of the Linear Technology datasheet I do not understand, and looking at the scope, I cannot yet reconcile the scope display and the datasheet. This is probably due to my ignorance of the exact meaning of digital terminology (I am an RF engineer). Some of my confusion is the i2c 7 bit address, but PICAXE apparently requiring 8 bits.

Regarding the Float input for the 9 addresses from two lines, I just wonder how reliable this state is, considering high RF fields, ESD spikes, or leakage across poor quality PCB material contaminants on the board surface. Must be good, though.

So, thank you forum members for your help, and another big thank you to austwest55. Very much appreciated.
 

westaust55

Moderator
Some of my confusion is the i2c 7 bit address, but PICAXE apparently requiring 8 bits.
For the i2c Slave addressing, this is nearly always an 8-bit (byte) value comprising 7 bits for the slave address plus a Read/Write bit as the least significant but = 8 bits
- the exception is a 10-bit address not used by PICAXE.

Hence for the LTC2305 ADC chip with both AD pins low the 7-bit slave address is %0001000 and this is placed in the upper 7 bits of the byte slave address. By default with PICAXE chips it is usual to just make the R/W bit = 0 thus the full i2c slave address for the LTC2305 becomes %00010000.

When the HI2COUT or the HI2CIN commands are used, the PICAXE firmware takes the declared device slave address and automatically sets or clears the R/W bit to suit the data direction.


Note that for the channel selection values I gave, the SLP bit is set to 0, so the LTC2305 goes into Nap mode after an A to D conversion which lowers the power consumption (to ~225 uA) but keeps certain parts such as Vref active and therefore only takes 250 ns to resume.
If the SLP bit is set to 1, then the chip goes into sleep mode after the A to D conversion which saves even more power (down to ~7 uA / 35 uW) but it will take 200 ms to be ready after wakeup - which will need a delay by PAUSE 200.

SLEEP mode is likely only warranted if running on battery power with long intervals between each Analog measurement.
 

westaust55

Moderator
3 state input would equate to 2^3=8.
hippy and nick12ab have already responded but for further explanation:

in a binary system as used by microcontrollers where there are just 2 states 0 and 1 then number of options is: number_base^(number of bits)
so binary with 3 bits (eg pins 0, 1 and 2) ==> 2^3 = 8 permutations as %000 (0dec) to %111 (7dec)

However the LCT2305 chips is in effect using a trinary base system (with three input states = base 3) hence with 2 bits/pins we have 3^2 = 9 options as 00 (0dec) to 22 (8dec from 2*3 + 2*1).
 

SAborn

Senior Member
Some of my confusion is the i2c 7 bit address, but PICAXE apparently requiring 8 bits.
I have never seen a I2C device that uses a 7 bit address as yet, but it is normally just a matter of how you read the data supplied for the device, as often it will give 7 bits of information with reference to the 8th bit elsewhere.

Dont be fooled to think the 8 bits is for picaxe only as it is not, and the full byte address is required for all masters.

Tri state pins for the address setting is common for many devices i have used, although i have noticed not all devices will allow all 3 pins to be floating for a valid address.
 
Top