Page 2 of 3

send data

Posted: 16 Sep 2008 14:06
by prancius
Hello,

Can you post some lines to send data to slave?

I have trouble to send byte to slave. Affter one loop it hangs.

Thank you
Pranas

What do you think

Posted: 19 Jun 2009 12:22
by prancius
//------------------------------------------------------------------------------
void interrupt(){ // I2C slave interrupt handler
if (PIR1.SSPIF == 1)
{ // I2C Interrupt

//transmit data to master
if (SSPSTAT.R_W == 1){ // Read request from master
SSPBUF = tx_data; // Get data to send
SSPCON.CKP = 1; // Release SCL line
j = SSPBUF; // That's it
return;
}
if (SSPSTAT.BF == 0){ // all done,
j = SSPBUF; // Nothing in buffer so exit
return;
}

//recieve data from master
if (SSPSTAT.D_A == 1){ // Data [not address]
j = SSPBUF; // get data
return;
}

j = SSPBUF; // read buffer to clear flag [address]
PIR1.SSPIF = 0; // reset SSP interrupt flag

}

}


What do you think about theese changes?

HELP

Posted: 26 Nov 2009 21:57
by lmoraxs
Hi read your code and they were really helpful i am tryn to interface two PIC18f2420 as master and slave but it does not seem to work here are my master and slave codes can you point my errors out please? I am using the MCC18 compiler with the provided header files.

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


//unsigned int bits;
unsigned char counter;


void main (void)
{

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

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

while (1) {

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

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

WriteI2C ( counter ); //
IdleI2C ();

PORTC = counter;
counter = 0b10101010;


// PORTB = counter;
//for (counter = 0; counter <= 10; counter++);
StopI2C ();
}

SLAVE CODE

#include <p18f2420.h>
#include <I2c.h>
#pragma config OSC = INTIO67
#pragma config MCLRE = OFF

unsigned short data_in;

//PIE1bits.SSPIE = 1

void main (void)
{



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


//initialise I2C//

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

DataRdyI2C ();

if ( DataRdyI2C == 1)
{
data_in = SSPBUF;
}

//ReadI2C ();
PORTC = data_in;

}
I have tried all but i am confused now..... must i neccesarily include an interrupt???

Posted: 07 Jan 2010 03:58
by neoblood
I found an error in your slave code.

This
[code PIE1.SSPIF = 1; // enable SSP interrupts[/code]

should be this.
[/code PIE1.SSPIE = 1; // enable SSP interrupts[/b]

Posted: 13 Jan 2010 04:32
by ideas101
Great post!!! looking forward on sharing my expertise and learning new ideas here at the same time..i'll see you around guys...

Posted: 21 Jan 2010 19:11
by Ikari
Dozenth this code only let you pass one variable?
So you can only read / write one fix variable. and Short only.

I have a very similar code but allows 255 different variables to be read and a another 255 to write.
You can also pass integers or Long variables.


If any one interested i can post here or in a new post.

Posted: 29 Jan 2010 11:51
by MKS-Chicken
Hi. I´m new here and have not much experience with the I2C-Bus.

I would like to connect 2 PIC16F877 over I2C-Bus and i can´t get it to work.

One of them should be the Master who reads and writes int numbers and the Slave save the int numbers and send it to Master, if he is calling.

I use mikroC Pro for PIC.

Can anyone help me with this?

Greetings Thomas

Excuse my bad English :)

Re: PIC 2 PIC Master/Slave Communication using I2C... Enjoy

Posted: 10 Jun 2010 08:04
by bobx
Hi,TomSSS.I find your I2C very usefull.So I want to modify that code (your I2C firmware)
to have one PIC as a Master and three PIC as a Slave.Would you please tell me how I should
modify that code (adding or omitting instruction,etc.) to have one PIC as a Master and three PIC as a Slave.
Thanks,
bobx

Re: PIC 2 PIC Master/Slave Communication using I2C... Enjoy

Posted: 01 Jul 2010 01:02
by jcdarden
Did you get an answer to this?

I also need multiple slaves ..

Thanks .... JCD

Re: PIC 2 PIC Master/Slave Communication using I2C... Enjoy

Posted: 20 Jul 2010 18:13
by blips
Its been days and I haven't been able to get the posted code to work as it is. One error I'm sure of is the fact the slave address is 0x69, which is not an even number and hence the least significant bit will be set. There is also an SSPBUF read for every interrupt which is not O.K when you've got other interrupts enabled. The code also does not handle all the five possible states needed for according to microchips paper (AN734). So I ask, does/did the code work for anyone in its current form or not?

Re: PIC 2 PIC Master/Slave Communication using I2C... Enjoy

Posted: 02 Aug 2011 13:50
by prancius
Does anybody try to send more than 14 bytes? I have problems trying to send more than 14 bytes. Anu ideas?

Re: PIC 2 PIC Master/Slave Communication using I2C... Enjoy

Posted: 04 Oct 2011 16:35
by tariq7868
@Ikari: Please post the code you mentioned about passing multiple (255bytes) to read write. My object is to send about 20bytes to slave Microcontroller on single read request interrupt from master...
thanks

Re: PIC 2 PIC Master/Slave Communication using I2C... Enjoy

Posted: 16 Oct 2011 06:22
by aka
Hi,
I'm trying to connect an accelerometer with 16F876A, as a part of my project. Since I'm new to accelerometers, i2c and have little experience with microcontrollers, I decided to use another PIC 16F877A as slave, just to test and confirm the master code. I used the exact codes posted here, added send method to master main method, connected the data and clock pins correctly, and used an array of LEDs on slave portb to check the output. But I cant see anything happening. I simulated the same thing with proteus, but results are same. It would be a great help if you can let me know what might have gone wrong here.

Thank you.

Re: PIC 2 PIC Master/Slave Communication using I2C... Enjoy

Posted: 18 Oct 2011 06:55
by aka
Hi, the problem with my proteus simulation was I hadnt used pull up resistors, which I did use in the actual circuit. Now the simulation works, portb of slave shows the value sent by the master and master port be shows that as well. I think thats what supposed to happen. But, Im heading nowhere with the real hardware. I have used a 20MHz oscillator for master and 40 MHz for the slave. And I find theres nothing more to be checked in circuit components and connections. Your support on this will be enormous, because my project has been currently stopped by this issue.

Re: PIC 2 PIC Master/Slave Communication using I2C... Enjoy

Posted: 18 Feb 2012 09:22
by programmer5
I was trying to use i2c protocol for communication between two PIC microcontrollers, after one week effort my i2c master-slave communication on proteus is going perfect