I2C Master Slave Interface.

Discussion in 'Embedded Systems and Microcontrollers' started by lmoraxs, Nov 19, 2009.

  1. lmoraxs

    Thread Starter New Member

    Nov 18, 2009
    4
    0
    am trying to interface a MASTER PIC with a SLAVE PIC using PIC18F2420 for both master and slave. I have played around with my code so much but when i simulate it i dont seem to get the results i expect. I have used the header files provided by microchips MCC18 compiler for the pic and I2c. I am trying to write a very simple program for the master pic telling the slave pic to output a string of zero's and one's that will light up LED's accordingly. The source code is given below:

    #include <p18f2420.h>
    #include <I2c.h>
    #define SLAVE_7_STSP_INT 0b00001110
    #pragma config OSC = INTIO67

    //unsigned char counter;
    void initialisePorts (void)
    {
    TRISC = 0b10011000; //I2c bits set

    }
    void main (void)
    {
    OpenI2C ( MASTER, SLEW_ON ); //intialise 12C
    SSPADD = 0x63; // set up baud rate


    StartI2C (); //send start condition
    WriteI2C (SLAVE_7_STSP_INT); // write to address (slave pic)
    while (1) {
    //I2C_SDA
    //I2C_SDA


    //for (counter = 0; counter <= 10; counter++)
    WriteI2C ();

    TRISCbits.TRISC4 = 0b11111111; //output to SDA
    TRISCbits.TRISC4 = 0b10000001;

    }



    StopI2C ();
    }

    Can anyone please show me wher i am going wrong. My ultimate aim is to interface a SE95 temperatue sensor with the master pic via the i2c bus but i belive once i can get this to work that should be easy. Thank you.
    [​IMG]
     
  2. Jorgy

    New Member

    Oct 10, 2009
    8
    0
    Are you using any pull up resistors when you connect your data and clock lines? If not I would suggest adding a 3.3k resistor from your data and clock pins up to Vcc. Here is a schematic of what the hardware should look like.

    http://www.ee.unb.ca/Courses/EE4333/CPD/Notes/I2C08.pdf
     
  3. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234

    If you read the I2C specs is says NOT to put any pull ups or pull downs on the clk line.... just the data line....

    My .02
     
  4. C7H5N3O6

    New Member

    Nov 20, 2009
    1
    0
    This is quite wrong.
    The only way to the slave to make delayed response is to assert the clk line low so that the master may see it. It is only possible if master is open collector/drain with a pullup. This clock-stretching method is part of the Philips I2C standard.
     
    Last edited: Nov 20, 2009
  5. lmoraxs

    Thread Starter New Member

    Nov 18, 2009
    4
    0
    thanx for the help i will try adding the pull up resistors but do you think the code should work fine then?
     
  6. ftsolutions

    Active Member

    Nov 21, 2009
    48
    2
    +1 on pulling up BOTH I2C lines, 3.3K should be fine as long as you're not driving this over a long (>12") cable. Also, you didn't post any slave code - make sure that you don't have a condition where they are both trying to access the bus (2 masters fighting) or slave not ACKing when it is supposed to (or ACKing when it shouldn't)
     
  7. lmoraxs

    Thread Starter New Member

    Nov 18, 2009
    4
    0
    Here is my slave code but the I2c connection seems to be live but it does not pass the data into the slave PORTC so i can see on LED's any help please?
    #include <p18f2420.h>
    #include <I2c.h>
    #pragma config OSC = INTIO67

    //void main (void)
    //{
    //initialise ports//
    void main (void)
    {
    //void initialisePorts2 (void)


    TRISB = 0b00000000;
    TRISC = 0b00011000; //I2c bits set as inputs
    PORTB = 0x00;
    PORTC = 0x00;


    //initialise I2C//

    OpenI2C ( SLAVE_7_STSP_INT, SLEW_ON );
    SSPADD = 0b00001110;


    }

    Please is there anything i am suppose to add again?
    This is my revised master program:

    #include <p18f2420.h>
    #include <I2c.h>
    #define SLAVE_7_STSP_INT 0b00001110
    #pragma config OSC = INTIO67


    //unsigned int bits;
    unsigned char counter;


    void main (void)
    {

    TRISB = 0b00000000;
    TRISC = 0b00000000; //I2c bits set
    PORTB = 0x00;
    PORTC = 0x00;

    OpenI2C ( MASTER, SLEW_OFF ); //intialise 12C
    SSPADD = 0x63; // set up baud rate

    while (1) {

    StartI2C (); //send start condition
    IdleI2C ();

    WriteI2C (SLAVE_7_STSP_INT); // write to address
    IdleI2C ();

    WriteI2C ( PORTC ); /
    IdleI2C ();

    PORTC = 0b10101010;
    //for (counter = 0; counter <= 10; counter++);

    StopI2C ();

    I expect these to just write 10101010 to the slave PORTC and view it with LED's on the the slave pic but it is not working??
     
Loading...