I2C EEPROM Confusion
Posted: Thu Dec 29, 2016 7:51 am
I am trying to write/read a 24LC64 EEPROM using a PIC18F25k22 and managing to get nowhere, even using sample code provided with Swordfish.
I'm using the 8 pin EEPROM package, with the 3 address pins and the WP pin connected to ground. That makes the address %10100000 = $A0 = 160.
In the sample program attached, which is stripped down to almost the Swordfish sample program, the program gets past writing to the EEPROM but hangs up during the read process. I don't know if anything was actually written since I can't read it back.
I'm also using an MCP23017 in the circuit and communicating just fine to it. SDA and SCL go to the correct pins, and there's a 0.1uF bypass cap adjacent to the chip.
This looks like it should work; it agrees with the data sheet procedure. What Am I overlooking?
I'm using the 8 pin EEPROM package, with the 3 address pins and the WP pin connected to ground. That makes the address %10100000 = $A0 = 160.
In the sample program attached, which is stripped down to almost the Swordfish sample program, the program gets past writing to the EEPROM but hangs up during the read process. I don't know if anything was actually written since I can't read it back.
I'm also using an MCP23017 in the circuit and communicating just fine to it. SDA and SCL go to the correct pins, and there's a 0.1uF bypass cap adjacent to the chip.
This looks like it should work; it agrees with the data sheet procedure. What Am I overlooking?
Code: Select all
Device = 18f25k22
Clock = 20
Config 'for K-series device
FOSC = HSHP ,'HS oscillator (high power > 16 MHz)
PLLCFG = Off ,'Oscillator used directly
'PLLCFG = On , 'PLL 5x Clock Multiplier - note timings are off by a factor of 4
PRICLKEN = Off ,'Primary clock can be disabled by software
FCMEN = Off ,'Fail-Safe Clock Monitor disabled
IESO = Off ,'Oscillator Switchover mode disabled
'PWRTEN = Off ,'Power up timer disabled
PWRTEN = on ,'Power up timer enabled
BOREN = Off ,'Brown-out Reset disabled in hardware and software
'BOREN = on ,'Brown-out Reset enabled
BORV = 285 ,'VBOR set to 2.85 V nominal
WDTEN = Off ,'Watch dog timer is always disabled. SWDTEN has no effect.
WDTPS = 256 ,'1:256
PBADEN = Off ,'PORTB<5:0> pins are configured as digital I/O on Reset
HFOFST = Off ,'HFINTOSC output and ready status are delayed by the oscillator stable status
MCLRE = EXTMCLR ,'MCLR pin enabled, RE3 input pin disabled
STVREN = On ,'Stack full/underflow will cause Reset
'LVP = On ,'Single-Supply ICSP enabled if MCLRE is also 1
LVP = Off ,'Single-Supply ICSP disabled
XINST = Off ,'Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
Debug = Off'Disabled
Include "suart.bas"
Include "convert.bas"
Include "i2c.bas"
Const Timer1 = 0
dim I2C_EEPROM as word
Dim
Value As Byte,
Address As Word
// program start...
Address = 0
i2c_eeprom = 160
I2C.Initialize
UART.SetBaudrate(sbr9600)
UART.SetMode(umTrue)
UART.SetTX(PORTB.7) ' matches the PICkit 2 UART tool input pin when connected to the ICSP connector
UART.Write("start ", 13, 10)
// write some data...
I2C.Start
I2C.WriteByte(I2C_EEPROM)
I2C.WriteByte(Address.Byte1)
I2C.WriteByte(Address.Byte0)
I2C.WriteByte(%10101010)
I2C.Stop
// allow external EEPROM to write data...
DelayMS(50)
uART.Write("Got this far ", 13, 10)
// read the data back...
I2C.Start
I2C.WriteByte(I2C_EEPROM)
I2C.WriteByte(Address.Byte1)
I2C.WriteByte(Address.Byte0)
I2C.Restart
I2C.WriteByte(I2C_EEPROM + 1)
Value = I2C.ReadByte
I2C.Acknowledge(I2C_NOT_ACKNOWLEDGE)
I2C.Stop
// output the result
UART.Write("Value = ", BinToStr(Value), 13, 10)