I2C between 2 PIC-Micros
I2C between 2 PIC-Micros
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 1647 times
-
- Master.fcf
- (16 KiB) Downloaded 1446 times
- 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
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.
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.

Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: I2C between 2 PIC-Micros
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
followed by "if data [0] = 37 is OK if not.
pilou8830
Flowcode V4 User
- Enamul
- Posts: 1772
- Joined: Mon Mar 05, 2012 11:34 pm
- Location: Nottingham, UK
- Has thanked: 271 times
- Been thanked: 814 times
Re: I2C between 2 PIC-Micros
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
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
-
- Posts: 8
- Joined: Sat Jun 30, 2012 1:14 pm
- Has thanked: 3 times
- Been thanked: 2 times
Re: I2C between 2 PIC-Micros
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)
...
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)
- Enamul
- Posts: 1772
- Joined: Mon Mar 05, 2012 11:34 pm
- Location: Nottingham, UK
- Has thanked: 271 times
- Been thanked: 814 times
Re: I2C between 2 PIC-Micros
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
- Posts: 1772
- Joined: Mon Mar 05, 2012 11:34 pm
- Location: Nottingham, UK
- Has thanked: 271 times
- Been thanked: 814 times
Re: I2C between 2 PIC-Micros
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.
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 797 times
-
- 18F2585.fcd
- (14.61 KiB) Downloaded 711 times
Re: I2C between 2 PIC-Micros
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
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

Re: I2C between 2 PIC-Micros
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.
In the next bit the buffer is read and stored in tmp depending on sspstat.
sspstat is later stored in temp and output to PORTB( i suspect it a set of heartbeat leds to see the data)
Hope it helps
Regards
Dazz
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
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
Code: Select all
//Read Status
//C Code:
FCV_TMP = sspstat;
//Output Status
//Output: tmp -> PORTB
trisb = 0x00;
portb = (FCV_TMP);e]
Regards
Dazz
To sign up to the V5 forum follow this link http://www.matrixmultimedia.com/forum_upgrades.php
Re: I2C between 2 PIC-Micros
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.
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.
Re: I2C between 2 PIC-Micros
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.
Regards
Dazz
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.
Dazz
To sign up to the V5 forum follow this link http://www.matrixmultimedia.com/forum_upgrades.php
Re: I2C between 2 PIC-Micros
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
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
-
- Posts: 323
- Joined: Tue Sep 06, 2011 2:54 am
- Has thanked: 166 times
- Been thanked: 26 times
Re: I2C between 2 PIC-Micros
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
Will these Macros also work with a Pic16f1938 and a pic16f1939 as a master?
Best Regards:
Uli
Re: I2C between 2 PIC-Micros
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
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
-
- Posts: 323
- Joined: Tue Sep 06, 2011 2:54 am
- Has thanked: 166 times
- Been thanked: 26 times
Re: I2C between 2 PIC-Micros
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.
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.
Re: I2C between 2 PIC-Micros
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
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
Re: I2C between 2 PIC-Micros
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.
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.
- 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
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Re: I2C between 2 PIC-Micros
Yes, i had added 4.7k for SDL and SDA, and do i still need to do any setting on the clock ?
Re: I2C between 2 PIC-Micros
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)
(Sorry that on my previous reply, type wrong on the SDL, suppose is SCK)