Quote Originally Posted by Kristjan View Post
Hi!

I'm using PIC18F2320 to drive EEPROM M24256. Here is the code to write variable to the address 1.

Code:
'--------------------------------
DEFINE OSC 4
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
DPIN    var     PORTC.4     	' Serial Data pin
CPIN    var     PORTC.5     	' Clock pin
cont    var     byte        	' Control

Pause 1

val = 14	' Starting value
val2 = 5	' Starting value
addr = 1	' Address

loop:   

cont = %10100000    'Control is set to write
You do not need to do this

      I2Cwrite DPIN, CPIN, $A0, addr.highbyte,addr.lowbyte, [val] 	'Writes value to EEPROM address 1
      Pauseus 30

cont = %10100001    'Control is set to read
You do not need to do this

        I2Cread DPIN, CPIN, $A0, addr.highbyte, addr.lowbyte, [val2]  ' Read EEPROM Contents
        serout PORTC.6, N9600, [val2] 		' Value to serial port
   
Goto loop                      ' Forever
This should take care of your problem. You can try using your cont variable, butyou dont need to change the bit state for read / write. PBP takes care of that

JF