'Library Example
'This code demonstrates use of I2C Library procedures and functions. PIC MCU is connected (pins SCL and
'SDA) to 24c02 EEPROM. Program sends data to EEPROM (data is written at address 2). Then, we read data
'via I²C from EEPROM and send its value to PORTD, to check if the cycle was successful. The figure below
'shows how to interface 24c02 to PIC.
program Eeprom_Test
dim EE_adr, EE_data, k as byte
dim jj as word
main:
I2C_Init(100000) ' Initialize full master mode
TRISD = 0 ' PORTD is output
PORTD = $FF ' Initialize PORTD
I2C_Start ' Issue I2C start signal
I2C_Wr($A2) ' Send byte via I2C(command to 24cO2)
EE_adr = 2
I2C_Wr(EE_adr) ' Send byte(address for EEPROM)
EE_data = $AA
I2C_Wr(EE_data) ' Send data(data that will be written)
I2C_Stop ' Issue I2C stop signal
' Pause while EEPROM writes data
for jj = 0 to 65500
nop
next jj
I2C_Start ' Issue I2C start signal
I2C_Wr($A2) ' Send byte via I2C
EE_adr = 2
I2C_Wr(EE_adr) ' Send byte(address for EEPROM)
I2C_Repeated_Start ' Issue I2C signal repeated start
I2C_Wr($A3) ' Send byte (request data from EEPROM)
k = I2C_Rd(1) ' Read the data
I2C_Stop ' Issue I2C stop signal
PORTD = k ' Show data on PORTD
' Endless loop
while true
nop
wend
end.