Hi Mike and Jerson!
Good news. It started to work finally
Not going straight into point, I must mention, that my EEPROM is M24256W. Address PINs are in EEPROM - M24256B! So there is kind of big difference, which doesn't seem so important at first place.
To conclude some things in my experiment (perhaps it would be helpful for somebody, who deals with same problem):
Check pin connections! (This time all connections were right)
Data pin must be pulled-up! (If you are using open collector, then also clock pin)
Pause after writing has to be at least 10ms. (Not 30us as I had it in my code)
By the way. Everything else what was mentioned under this topic was right. This address splitting to higher and lower byte is working and also it is not necessary to set Control byte separately for reading and writing. Anyways, as it came out now, it is also working without splitting and you can set also Control byte separately for reading and writing. It works!
And now the main point. I went through again PIC Basic handbook, after hours of experimenting with different cases and connections. I even changed ports for data and clock in my PIC. So in this book, there are descriptions about defines for I2C. So, when I added DEFINE I2C_SCLOUT 1, everything started to work. This define makes the I2C clock line bipolar. I don't know why it is necessary in my case, but it makes everything to work
So here is the final code, which works with this PIC and EEPROM:
-------------------------------------------------------------
DEFINE OSC 4
define I2C_SCLOUT 1 ' Makes I2C clock line bipolar
Include "modedefs.bas"
CVRCON = 0 ' No reference
CMCON = 7 ' No comparator
TRISA = %00000000 ' Set PORTA pins into output
TRISB = %00000000 ' Set PORTB pins into output
TRISC = %00000000 ' Set PORTC pins into output
addr var word ' Address of EEPROM
val var byte ' Value of writable variable
val2 var byte ' Value of readable variable
D var PORTC.2 ' Serial Data pin
C var PORTC.3 ' Clock pin
cont var byte ' Control
Pause 1000 ' Pause 1 sec.
val2 = 5 ' Starting value
loop: ' Loop forever
' OK to use cont = %10100000 'Control is set to write
val = 0
for addr = 1 to 10
val = val + 1
I2Cwrite D, C, $A0, addr, [val] 'Write to EEPROM
' OK I2Cwrite D, C, $A0, addr.highbyte, addr.Lowbyte, [val] 'Writes value to EEPROM address 1
' OK I2Cwrite D, C, cont, addr.highbyte, addr.Lowbyte, [val] 'Writes value to EEPROM address 1
' OK I2Cwrite D, C, cont, addr, [val]
Pause 10 ' Pause for 10ms. Needed for writing!
next addr
' OK to use cont = %10100001 'Control is set to read
for addr = 1 to 10
I2Cread D, C, $A0, addr, [val2] 'Read from EEPROM
' OK I2Cread D, C, $A0, addr.highbyte, addr.Lowbyte, [val2] ' Dump EEPROM Contents
' OK I2Cread D, C, cont, addr.highbyte, addr.Lowbyte, [val2] ' Dump EEPROM Contents
' OK I2Cread D, C, cont, addr, [val2]
serout PORTC.6, N9600, [val2] 'Value to serial port
next addr
Pause 200
Goto loop ' Forever
-----------------------------------------------------------------
Soon I'll got also EEPROM 24LC16B in my hand, which I want to drive with this PIC. I hope it will not cause anymore problems.
Thank you for your support Mike and Jerson!
Kristjan
Bookmarks