Understanding I2C addressing

Krafter

Member
I'm confused and can't seem to find the answer I'm looking for. The way I'm interpret the I2C setup is as follows

hi2csetup i2cmaster, Slaveaddress, Mode, Addresslen:
  • HI2CSETUP = Tell the picaxe you want to use and setup I2c
  • I2CMASTER = Master mode aka the one in command
  • Slaveaddress = The address of the slave that you want to communicate with (where I'm confused).
  • Mode = Slow or fast
  • Addresslen = The data type. Either byte or word

I'm confused by the address. In my world, when I have a device with an address of 68, I refer to address 68 and not %11010000 or 208. I'm getting this from my DS1307. I know that the devices address is 68 so how does the address of %11010000 know to talk to address 68?

This is the setup for the DS1307 which is working well.
hi2csetup i2cmaster, %11010000, i2cslow_8, i2cbyte

I'm asking this because I'm now trying to get a 24LC256 working and cant seem to get there. All I want is to write a 0x09 to slot 0 in the EEPROM which is at address 50 and I think that understanding the addressing better, I'll have more luck. Here is the code which is pieced from info I have found.

Code:
setfreq m8


pause 1000
hi2csetup i2cmaster, %10100000,i2cfast,i2cword
pause 10 ;optional delay time

hi2cout 0,($09) ;save raw Minutes
pause 10 ;recommended write delay time


geteeprom:
hi2csetup i2cmaster, %10100000,i2cfast,i2cword
pause 10

hi2cin 0, (b0)
pause 10
sertxd (#b0, cr, lf)
pause 500
goto geteeprom
No matter what I try to write, I get a value of 161 returned. What the heck am I missing here?
 

neiltechspec

Senior Member
Don't work in decimal for device addresses, use hexadecimal or binary.

DS1307 is address $68 or %1101000 in 7bit, for PICAXE just add an extra 0 - %11010000 which becomes $D0.

Your 24LC256 is address $50 or %1010000 in 7bit, for PICAXE just add an extra 0 - %10100000 which becomes $A0.

Neil
 
Last edited by a moderator:

inglewoodpete

Senior Member
The data sheet specifies that the DS1307 will only work reliably at 100kbits/sec. This means that the i2c bus cannot be used faster than this, even for the EEPROM chip, when they share the i2c bus. So you must specify i2cSlow_8 for both devices.
 

hippy

Ex-Staff (retired)
No matter what I try to write, I get a value of 161 returned.
When starting with I2C it is recommended to use the default PICAXE speed ( therefore no SETFREQ commands ) and to start with I2CSLOW bus speed. Once things are working the PICAXE and bus speed can be increased if required.

You are probably not writing 161, only reading 161. That 161 is $A1, the I2C Device Address with lsb set. Reading such a value is usually indicative of some sort of I2C bus or hardware issue.

If you could post a picture of your hardware members may be able to comment on that.
 

Krafter

Member
Thanks guys. As usual, you were a great help. I now know how the addressing works and it makes since. I've also successfully wrote to the EEPROM! Thanks to hippy, I stopped concentrating on software and looked into the hardware. I was mistakenly using pulldown resistors instead of pullup resistors on the buss. I'm just learning I2C and was thinking that pulldown was required but a quick google search said different.

Thanks again.


edit:

Simplified code:
Code:
hi2csetup i2cmaster, %10100000, i2cfast, i2cword

geteeprom:
pause 10

hi2cout 0,($09)
pause 10 

hi2cin 0, (b0)
pause 10
sertxd (#b0, cr, lf)
pause 500
goto geteeprom
 

inglewoodpete

Senior Member
As I mentioned previously, the DS1307 cannot run reliably at 400kbits/sec (i2cFast) and will corrupt the i2c bus periodically when it misinterprets 400kbits/sec bus signalling. You need to restrict the signalling to i2cSlow whenever you have a slow peripheral connected to the i2c bus.
 

Krafter

Member
As I mentioned previously, the DS1307 cannot run reliably at 400kbits/sec (i2cFast) and will corrupt the i2c bus periodically when it misinterprets 400kbits/sec bus signalling. You need to restrict the signalling to i2cSlow whenever you have a slow peripheral connected to the i2c bus.
My apologizes for the confusion. I only mentioned the DS1307 in reference to addressing. I'm using it on a Raspberry Pi project and not this one. I will keep that in mind however.
 

mikeyBoo

Senior Member
Simply multiply the actual 7-bit address by 2 to get the "Picaxe I2C" address
(works for hex, base 10, etc.)
I always put the actual address in the comment
e.g.
symbol JoystickI2C = $42 ; actual I2C address for Joystick is 33(21h)
symbol DS1307 = $D0 ; actual I2C address for clock is 104(68h)
symbol LEDdisp = $E4 ; actual I2C address for Adafruit display is 114 (72h) (Use Later for ????)
symbol PCA9685 = $80 ; actual I2C address for Adafruit PCA9685 is 64 (40h) (Used for Lights)
e.g.
hi2csetup i2cmaster, DS1307, i2cslow, i2cbyte ; plug in Real-Time Clock I2C address
hi2cout $0,(seconds,minutes,hours,day,date,month,year,fout) ; write clock registers
 
Top