I2C between 2 PIC-Micros

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
User avatar
tiny
Posts: 200
Joined: Wed Jul 08, 2009 8:29 am
Has thanked: 51 times
Been thanked: 93 times
Contact:

I2C between 2 PIC-Micros

Post by tiny »

If somebody has interest, I have created a Master-Slave communication between 2 PIC-Micros. Configured for PIC18F2680, goes for all PICs with HW-I2C.
Attachments
Slave10.fcf
(9.5 KiB) Downloaded 1522 times
Master.fcf
(16 KiB) Downloaded 1384 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: I2C between 2 PIC-Micros

Post by Benj »

Hello Tiny

Yes i'm interested I have not yet explored the master / slave operation of the I2C. I will certainly have a play with these.

Many thanks for posting up your programs. :mrgreen:

pilou8830
Flowcode V4 User
Posts: 58
Joined: Mon Aug 16, 2010 12:52 am
Been thanked: 1 time
Contact:

Re: I2C between 2 PIC-Micros

Post by pilou8830 »

hello, I can not see where is the WR_MASTER DATA [0] = 37 on the side of the slave. It is not WR_MASTER [0 ]???? as inftead of going out on the slave side PORTB I put it in a variable that I named data [0] with data [0] = WR_MASTER [0];
followed by "if data [0] = 37 is OK if not.
pilou8830
Flowcode V4 User

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: I2C between 2 PIC-Micros

Post by Enamul »

Hi Tiny

I quite new in the Forum..but working with PIC for last 9 years..I was looking for something last night. Glad to see you have made something really useful.

Thank you...I should thank Ben as well as he has directed me from other web page..Thanks Ben..

Enamul
Enamul
University of Nottingham
enamul4mm@gmail.com

soos-dezso
Posts: 8
Joined: Sat Jun 30, 2012 1:14 pm
Has thanked: 3 times
Been thanked: 2 times
Contact:

Re: I2C between 2 PIC-Micros

Post by soos-dezso »

sorry ... v5 cannot compile this project file...
...
Master.c(507:1): error: unknown identifier 'eccp1con'
Master.c(507:1): error: invalid operand 'eccp1con'
Master.c(507:9): error: failed to generate expression
Master.c(508:1): error: unknown identifier 'cmcon'
Master.c(508:1): error: invalid operand 'cmcon'
Master.c(508:6): error: failed to generate expression
Master.c success

Work only v4 ?

(sorry my poor english)

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: I2C between 2 PIC-Micros

Post by Enamul »

Thanks for letting us know. If you change the chip to PIC16f877a it compiles fine which means that there might be some FCD issue in the target chip used in the code in FCV5. I will have a look and let you know. But you can easily use the code with other chip.
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: I2C between 2 PIC-Micros

Post by Enamul »

Hi
I have found out the bugs in the FCD and this is because of being in same group..PIC18f2585, PIC18f2680, PIC18f4585 & PIC18f4680. Last two has CCP and ECCP both with comparator whereas first two has CCP but no ECCP and no comparator. But same initialization causing the problem. Here is the corrected FCD for first two chips..you have to replace old one in FCD folder in program files of FCv5.
I have checked after correction the code compiles Ok in FCV5 now.
Attachments
18F2680.fcd
(14.61 KiB) Downloaded 685 times
18F2585.fcd
(14.61 KiB) Downloaded 609 times
Enamul
University of Nottingham
enamul4mm@gmail.com

cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

Re: I2C between 2 PIC-Micros

Post by cobra1 »

I have just looked at these 2 files, am more interested in the slave file.

Can someone help me understand a few things please.

I am not sure how the data is help in the slave,

If the master sends : S,16,37,77,P.
Does all of this data get received by the slave and then it prcesses it all at the same time, Or does it process it 1 part at a time.
Looking at the slave it appears to do it all at the same time.

If it does this, how many bytes can be sent to the slave in one go?

This part is also puzzling me

{
if (sspstat&0x04)
{
FCV_WR_BIT = 1;
}
else
{
FCV_WR_BIT = 0;
}
}

What is this doing, and then why does it set C2 output? What is C2 output supposed to do?



Finally,

In a c box, titled Read address there is this line of code.

FCV_TMP = sspbuf;

However the variable TMP, does not appear to be used at any point



Any info or help would be great :)

cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

Re: I2C between 2 PIC-Micros

Post by cobra1 »

Can anyone help with this?

dazz
Posts: 1314
Joined: Sun Jan 15, 2012 3:15 pm
Has thanked: 474 times
Been thanked: 458 times
Contact:

Re: I2C between 2 PIC-Micros

Post by dazz »

Hi Cobra
im not too good with c , you will see the tmp variable is assigned a few times for reading and checking the input, i may be slightly off but it looks like the code below is assigning the variable wr_bit(used in the decision branches later) based on whether the sspstat register is set to read or write.

Code: Select all

 if (sspstat&0x04)
	     {
	       FCV_WR_BIT = 1;  //tx
	     }
	   else
	     {
	       FCV_WR_BIT = 0;  //rx
In the next bit the buffer is read and stored in tmp depending on sspstat.

Code: Select all

//Decision: WR_Bit = 0?
	if (FCV_WR_BIT == 0)
	{

		//C-Kode
		//C Code:
		FCV_TMP = sspbuf;   //Read serial buffer and store in temp
sspstat is later stored in temp and output to PORTB( i suspect it a set of heartbeat leds to see the data)

Code: Select all

        //Read Status
		//C Code:
		FCV_TMP = sspstat;


		//Output Status
		//Output: tmp -> PORTB
		trisb = 0x00;
		portb = (FCV_TMP);e]
Hope it helps

Regards
Dazz
To sign up to the V5 forum follow this link http://www.matrixmultimedia.com/forum_upgrades.php

cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

Re: I2C between 2 PIC-Micros

Post by cobra1 »

Thanks for that Dazz,

So do you know if the i2c receives all the data and then processes it or does it process each byte on the go?

Looking at the code it seems it receives it all first, but i might be wrong there.

dazz
Posts: 1314
Joined: Sun Jan 15, 2012 3:15 pm
Has thanked: 474 times
Been thanked: 458 times
Contact:

Re: I2C between 2 PIC-Micros

Post by dazz »

Hi Cobra.
Im not 100%, but the following from the datasheet suggests it read it byte by byte. Best have a read of the datasheet attached.

Code: Select all

In receive operations, SSPSR and SSPBUF together
create a double-buffered receiver. When SSPSR
receives a complete byte, it is transferred to SSPBUF
and the SSPIF interrupt is set.
Regards
Dazz
39625c.pdf
(7.99 MiB) Downloaded 602 times
To sign up to the V5 forum follow this link http://www.matrixmultimedia.com/forum_upgrades.php

cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

Re: I2C between 2 PIC-Micros

Post by cobra1 »

I cant get my head round why the reads are done one after the other, if the interrupt is dissabled how does the i2c carry on receiving data?

User avatar
tiny
Posts: 200
Joined: Wed Jul 08, 2009 8:29 am
Has thanked: 51 times
Been thanked: 93 times
Contact:

Re: I2C between 2 PIC-Micros

Post by tiny »

Hi Cobra,
I2C is best understood when you read the I2C charts of the controller. There specified exactly how the process is done.
The interrupt will remain inactive until the transfer has been completed (I2C stop).
The Tmp variable is added for 2 reasons: for the buffer to empty and to prevent timing problems.
I hope I have helped you.
Tiny

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: I2C between 2 PIC-Micros

Post by Creative25 »

Hi I am following this thread with interest.
Will these Macros also work with a Pic16f1938 and a pic16f1939 as a master?
Best Regards:
Uli

User avatar
tiny
Posts: 200
Joined: Wed Jul 08, 2009 8:29 am
Has thanked: 51 times
Been thanked: 93 times
Contact:

Re: I2C between 2 PIC-Micros

Post by tiny »

Hi Creative25,

I think that the two controllers are functional, as both have a master synchronous serial port. You only need to define master and slave fix, a multi-master environment does not work with these macros.
It's best to load the progam change the processor type and compile everything. If compiles without error, you have the security that all is well. Under certain circumstances, C code must be corrected. Specific names are given in the data sheet.

tiny

Creative25
Posts: 323
Joined: Tue Sep 06, 2011 2:54 am
Has thanked: 166 times
Been thanked: 26 times
Contact:

Re: I2C between 2 PIC-Micros

Post by Creative25 »

Hi I have tried to compile the code.
It all works.

But I still have a question I was thinking of making 8 different buttons and use a Pic configured as a I2C slave to read the buttons.
However to me it looks like the transfer will be quite slow with all those 2mS and 4mS delays. I want to read the Data from the slave Pic at least 10 times per second. And I am a bit concerned that it will slow down my whole program.

Is there A way one could make these delays shorter, or will it become unstable?

Best Regards:
Uli.

User avatar
tiny
Posts: 200
Joined: Wed Jul 08, 2009 8:29 am
Has thanked: 51 times
Been thanked: 93 times
Contact:

Re: I2C between 2 PIC-Micros

Post by tiny »

Hi,
I used in the tests a number of different delays in order to implement the transfer. I think that it depends also on the clock frequency that the timer can run shorter. I used 2 PIC18F2680 with 4MHz clock frequency and the settings in the macros easily. Maybe shorten the settings for the timer and then try out the transmission.

Tiny

Jellyfish
Posts: 4
Joined: Fri Sep 27, 2013 2:06 am
Has thanked: 1 time
Contact:

Re: I2C between 2 PIC-Micros

Post by Jellyfish »

Hi,

I had downloaded the 2 flowcode of i2c master & slave, i configure the both ic to 16f887 and downloaded. but for the slave site no receive any data, the receive interrupt no working at all.
can somebody provide me the circuit diagram for this master & slave i2c communication ?

can somebody help me on this, i m very new on this i2c communication. Thank you.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: I2C between 2 PIC-Micros

Post by Benj »

Hello,

Have you got any external pull up resistors on the I2C signals. 4.7K or 10K on the data and clock signals should do the job.

Jellyfish
Posts: 4
Joined: Fri Sep 27, 2013 2:06 am
Has thanked: 1 time
Contact:

Re: I2C between 2 PIC-Micros

Post by Jellyfish »

Yes, i had added 4.7k for SDL and SDA, and do i still need to do any setting on the clock ?

Jellyfish
Posts: 4
Joined: Fri Sep 27, 2013 2:06 am
Has thanked: 1 time
Contact:

Re: I2C between 2 PIC-Micros

Post by Jellyfish »

After i tested, i found out that the i2c slave no receive any data. I had connected both pic16f887 pin SCK together same as pin SDA also. Both of them also added a 4.7k pull up resistor, so still cant get the result. So how i check whether got data transmitted from master and also how to check whether the slave is successful receive data ? Please help me on this, for my fyp.

(Sorry that on my previous reply, type wrong on the SDL, suppose is SCK)

Post Reply