I2C EEPROM Confusion

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
Jon Chandler
Registered User
Registered User
Posts: 185
Joined: Mon Mar 10, 2008 8:20 am
Location: Seattle, WA USA
Contact:

I2C EEPROM Confusion

Post by Jon Chandler » 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?

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)
Jon

Check out the TAP-28 PIC Application board at http://www.clever4hire.com/throwawaypic/

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: I2C EEPROM Confusion

Post by Jerry Messina » Thu Dec 29, 2016 2:13 pm

Hi Jon,
I can't really check at the moment but one thing to verify is the pin mode. On the K22 pretty much all of the pins default to analog mode, and it has slew rate limiting as well. Checkout the updated setdigitalio module.

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: I2C EEPROM Confusion

Post by David Barker » Thu Dec 29, 2016 2:45 pm

...and check pullups

Jon Chandler
Registered User
Registered User
Posts: 185
Joined: Mon Mar 10, 2008 8:20 am
Location: Seattle, WA USA
Contact:

Re: I2C EEPROM Confusion

Post by Jon Chandler » Thu Dec 29, 2016 4:01 pm

Good catch about the analog pins. My original program covers that but in my simplified version to test, I omitted the SetAllDigital command.

I forgot to mention pullups in my original post. 4k7 on SDA and SCL.
Jon

Check out the TAP-28 PIC Application board at http://www.clever4hire.com/throwawaypic/

Jon Chandler
Registered User
Registered User
Posts: 185
Joined: Mon Mar 10, 2008 8:20 am
Location: Seattle, WA USA
Contact:

Re: I2C EEPROM Confusion

Post by Jon Chandler » Thu Dec 29, 2016 5:31 pm

Yay. SetAllDigital brought the test program to life!

Now I can move on to figuring out what I'm doing. I want to write and read 5 bytes of data at a time - a LongWord timestamp and a byte of data. This shouldn't be hard :)
Jon

Check out the TAP-28 PIC Application board at http://www.clever4hire.com/throwawaypic/

Post Reply