I2C Master Slave Interface.

Thread Starter

lmoraxs

Joined Nov 18, 2009
4
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.
 

C7H5N3O6

Joined Nov 20, 2009
1
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
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:

ftsolutions

Joined Nov 21, 2009
48
+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)
 

Thread Starter

lmoraxs

Joined Nov 18, 2009
4
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??
 
Top