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

General discussion on mikroC.
Author
Message
prancius
Posts: 148
Joined: 26 Sep 2007 21:52

send data

#16 Post by prancius » 16 Sep 2008 14:06

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

prancius
Posts: 148
Joined: 26 Sep 2007 21:52

What do you think

#17 Post by prancius » 19 Jun 2009 12:22

//------------------------------------------------------------------------------
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?

lmoraxs
Posts: 1
Joined: 26 Nov 2009 21:52

HELP

#18 Post by lmoraxs » 26 Nov 2009 21:57

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???

neoblood
Posts: 1
Joined: 07 Jan 2010 03:54

#19 Post by neoblood » 07 Jan 2010 03:58

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]

ideas101
Posts: 1
Joined: 13 Jan 2010 04:31

#20 Post by ideas101 » 13 Jan 2010 04:32

Great post!!! looking forward on sharing my expertise and learning new ideas here at the same time..i'll see you around guys...

Ikari
Posts: 18
Joined: 24 Apr 2008 06:52

#21 Post by Ikari » 21 Jan 2010 19:11

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.

MKS-Chicken
Posts: 3
Joined: 29 Jan 2010 10:49

#22 Post by MKS-Chicken » 29 Jan 2010 11:51

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

bobx
Posts: 115
Joined: 14 Mar 2010 08:35

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

#23 Post by bobx » 10 Jun 2010 08:04

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

jcdarden
Posts: 41
Joined: 03 Aug 2008 07:42
Location: California USA
Contact:

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

#24 Post by jcdarden » 01 Jul 2010 01:02

Did you get an answer to this?

I also need multiple slaves ..

Thanks .... JCD
Thousand Oaks, CA
Skype Address is: "PatDarden"

blips
Posts: 30
Joined: 11 Nov 2007 22:28

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

#25 Post by blips » 20 Jul 2010 18:13

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?

prancius
Posts: 148
Joined: 26 Sep 2007 21:52

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

#26 Post by prancius » 02 Aug 2011 13:50

Does anybody try to send more than 14 bytes? I have problems trying to send more than 14 bytes. Anu ideas?

tariq7868
Posts: 5
Joined: 26 Sep 2011 21:05

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

#27 Post by tariq7868 » 04 Oct 2011 16:35

@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

aka
Posts: 2
Joined: 16 Oct 2011 06:09

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

#28 Post by aka » 16 Oct 2011 06:22

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.

aka
Posts: 2
Joined: 16 Oct 2011 06:09

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

#29 Post by aka » 18 Oct 2011 06:55

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.

programmer5
Posts: 69
Joined: 28 Jan 2012 06:50
Location: Pakistan
Contact:

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

#30 Post by programmer5 » 18 Feb 2012 09:22

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

Post Reply

Return to “mikroC General”