Here is an example of how I use I2C with an EEPROM and the I/O Expander, I hope it helps.
DEFINE I2C_HOLD 1
Address VAR WORD
SCL VAR PORTC.3 ' I2C Clock pin
SDA VAR PORTC.4 ' I2C Data pin
Address = 0
'------------------------------------------------------------------------------------------------
' Read Password from EEPROM
'------------------------------------------------------------------------------------------------
ReadPassword:
Address = 0
I2CREAD SDA,SCL,$A0,Address,[Password[1],Password[2],Password[3],Password[4]]
Return
'------------------------------------------------------------------------------------------------
'------------------------------------------------------------------------------------------------
' Save Password to EEPROM
'------------------------------------------------------------------------------------------------
SavePassword:
Address = 0
I2CWRITE SDA,SCL,$A0,Address,[Password[1],Password[2],Password[3],Password[4]]
Return
'------------------------------------------------------------------------------------------------
'------------------------------------------------------------------------------------------------
' Subroutine to handle I2C errors
'------------------------------------------------------------------------------------------------
I2CError:
LCDOut $fe,1
LCDOut $fe,2, "I2C Bus Timed Out" ' I2C command timed out
Pause 1000
GoTo loop
'------------------------------------------------------------------------------------------------
'------------------------------------------------------------------------------------------------
' Setup I2C I/O Expander
'------------------------------------------------------------------------------------------------
SetupIOExpander:
'*** Setup I/O Expander PortA as Output, PortB as Input ***
I2CWrite SDA,SCL,$40,[6,0,31], I2CError
Pause 150
'*** Set the Interrupt Capture Rate to Fast ***
'I2CWrite SDA,SCL,$40,[11,1], I2CError
'Pause 150
'*** Set I/O Expander PortA Outputs to LOW ***
I2CWrite SDA,SCL,$40,[0,0], I2CError
Pause 150
RESUME
'------------------------------------------------------------------------------------------------
Bookmarks