Enter image description here

I am working on a university project in which I need to interface a PIC18F4550 with an I²C EEPROM.

I read many codes and saw many projects on this topic. And I wrote a sample code from MPLAB C18 (and I tried many codes also), but no one worked with me.

I don't know where the problem is. Everything is OK with my code and with my circuit, but the SCK signal did not generate the CLK signal for writing and nothing has been written to EEPROM.

Here is the code:

#include "p18f4550.h"
#include "i2c.h"
#pragma config FOSC = HS
#pragma config PWRT = OFF
#pragma config BOR = OFF
#pragma config MCLRE = ON
#pragma config PBADEN = OFF
#pragma config ICPRT = OFF
#pragma config LVP = OFF
#pragma config WDT = OFF,DEBUG=OFF

unsigned char arraywr[] = {1,2,3,4,5,6,7,8,0};
unsigned char arrayrd[20];
void main(void)
    OpenI2C(MASTER, SLEW_ON);// Initialize I²C module
    SSPADD = 10; //400 kHz Baud clock(10) @20 MHz
        EEByteWrite(0xA0, 0x30, 0xA5);
        EEPageWrite(0xA0, 0x70, arraywr);
        EESequentialRead(0xA0, 0x70, arrayrd, 20);

This is the code of OpenI2C:

    /* $Id: i2c_open.c,v 1.1 2004/10/06 23:16:42 curtiss Exp $ */

    #include <p18cxxx.h>
    #include <i2c.h>

    *   Function Name:  OpenI2C                                         *
    *   Return Value:   void                                            *
    *   Parameters:     SSP peripheral setup bytes                      *
    *   Description:    This function sets up the SSP module on a       *
    *                   PIC18CXXX device for use with a Microchip I²C   *
    *                   EEPROM device or I²C bus device.                *
    void OpenI2C( unsigned char sync_mode, unsigned char slew )
        SSPSTAT &= 0x3F;                // Power on state
        SSPCON1 = 0x00;                 // Power on state
        SSPCON2 = 0x00;                 // Power on state
        SSPCON1 |= sync_mode;           // Select serial mode
        SSPSTAT |= slew;                // Slew rate on/off

        #if defined(__18F2455) || defined(__18F2550) || \
            defined(__18F4455) || defined(__18F4550)

            DDRBbits.RB1 = 1;           // Set SCL (PORTB,1) pin to input
            DDRBbits.RB0 = 1;           // Set SDA (PORTB,0) pin to input


            DDRCbits.RC3 = 1;           // Set SCL (PORTC,3) pin to input
            DDRCbits.RC4 = 1;           // Set SDA (PORTC,4) pin to input


        SSPCON1 |= SSPENB;              // Enable synchronous serial port
  • \$\begingroup\$ seeing as you're at a university, how about getting and posting an oscilloscope trace of the SDA and SCL lines... I gave you an upvote in hopes of raising your privilege up to the point of being able to post images \$\endgroup\$
    – vicatcu
    Dec 21, 2011 at 18:09
  • \$\begingroup\$ I have posted the image of the circuit :) \$\endgroup\$
    – m_engineer
    Dec 21, 2011 at 20:50
  • \$\begingroup\$ what program is your image captured from, out of curiousity? \$\endgroup\$
    – vicatcu
    Dec 21, 2011 at 21:06
  • \$\begingroup\$ from isis simulator \$\endgroup\$
    – m_engineer
    Dec 21, 2011 at 21:09
  • \$\begingroup\$ That's a Proteus product, for those new to Isis. \$\endgroup\$
    – rdtsc
    Jun 17, 2015 at 20:22

2 Answers 2


Everything can't be okay with your code and circuit if SCK is not generating a clock ;-)

(Note that SCK is for SPI. SCL is the name for the I2C pin. So below the reference to SCL is what you are calling SCK)

This could be as you haven't configured the pins properly as Leon mentions, or possibly as you haven't added pullup resistors to your SDA/SCL lines.

If you show your circuit (or just let us know if there are pullups present) and any I2C initialisation code (e.g. what happens in OpenI2C) we can confirm what the problem is.

EDIT - Looking at your schematic it looks like you have no pullup on SCL, which would account for nothing being seen on it. The I2C lines are driven using open drain outputs, which means the pin can only sink current, not source it.
If you add a resistor (say, 2k to 10k) from SCL to Vdd you should see something.

EDIT 2 - I'm not sure where it says no pullup is needed on SCL. I just had a look in the datasheet and I found this (page 207):

Selection of any I 2 C mode with the SSPEN bit set forces the SCL and SDA pins to be open-drain, provided these pins are programmed as inputs by setting the appropriate TRISC or TRISD bits. To ensure proper operation of the module, pull-up resistors must be provided externally to the SCL and SDA pins.

Are you using a real PIC here or simulating things in ISIS? If it's a simulation then it may be something do to with how it's setup.
Have you got the TRIS bits set accordingly? (both to 1)
Also for good measure, set the ADCCON1 registers bits 3:0 all to 1, to set all the pins as digital. You can do e.g. ADCCON1 |= 0x0F; for this.

  • \$\begingroup\$ Oli, I have edited my post and added the image. please check it. Thanks :) \$\endgroup\$
    – m_engineer
    Dec 21, 2011 at 21:01
  • \$\begingroup\$ Also, I have added the OpenI2C function now. \$\endgroup\$
    – m_engineer
    Dec 21, 2011 at 21:11
  • \$\begingroup\$ @m_engineer - Okay, see edits - I can't see a pullup on SCK. \$\endgroup\$
    – Oli Glaser
    Dec 21, 2011 at 21:33
  • \$\begingroup\$ According to the datasheet, there is no need to have pullup on SCK. But, I tried to have pullup and nothing have been changed ! \$\endgroup\$
    – m_engineer
    Dec 21, 2011 at 21:57
  • \$\begingroup\$ @m_engineer - Where did you see this? \$\endgroup\$
    – Oli Glaser
    Dec 21, 2011 at 23:32

You need to configure the pins that are used for the I2C interface for input/output. Input pins that are shared with analogue inputs need to be set as digital inputs.

  • \$\begingroup\$ Thanks Leon :) Is there onother pins that I have to configure in addition to the scl and sdl? \$\endgroup\$
    – m_engineer
    Dec 21, 2011 at 20:58
  • \$\begingroup\$ Those are the only two pins used by I2C. \$\endgroup\$ Dec 21, 2011 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.