Two I2C Bus on one PIC

34 views
Skip to first unread message

David VB

unread,
Mar 26, 2025, 11:23:18 AMMar 26
to jallib
Hi,

I will use a PIC16F18325 to control (as master) TWO separate I2C Buses.   This PIC has 2 MSSP modules, so this is possible.

But, the I2c_hardware.jal lib does not support that: it can only reference the first module.

o i have to make a copy of that lib, and rename all references to the registers of the MSSP module to access the second module ?   (Ex: SSPCON2 which is an alias for SSP1CON2 should be renamed SSP2CON2 in the copy of the lib)

Or is there another way ?

Also note that if I want to make the buses run at different speeds (ex: one at 100 kHz, and one at 400 kHz), some global variables and constants have also to be duplicated and renamed.

Any ideas welcome....

Kind regards,

David

Rob CJ

unread,
Mar 26, 2025, 12:25:08 PMMar 26
to jal...@googlegroups.com
Hi David,
I think that, just as for the serial hardware, that we need a second iic hardware library. I could make the library for you and start this weekend.  I think that it is not that much work.

Met vriendelijke groet,
Rob Jansen

From: jal...@googlegroups.com <jal...@googlegroups.com> on behalf of David VB <pinhe...@gmail.com>
Sent: Wednesday, March 26, 2025 4:23:18 PM
To: jallib <jal...@googlegroups.com>
Subject: [jallib] Two I2C Bus on one PIC
 
--
You received this message because you are subscribed to the Google Groups "jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallib+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/jallib/6d8d86fb-7d51-4b8a-93a2-fc2244d36da6n%40googlegroups.com.

vsurducan

unread,
Mar 26, 2025, 12:30:06 PMMar 26
to jal...@googlegroups.com
Hi David,
But if you use one i2c bus and switch between 100kHz and 400kHz as needed?
The only reason  I can see the need for two i2c is perhaps syncronization between two data packets or some speed constrains.
Cheers,


--

Rob CJ

unread,
Mar 26, 2025, 12:52:17 PMMar 26
to jal...@googlegroups.com
Good point, Vasile. 

Met vriendelijke groet,
Rob Jansen

From: jal...@googlegroups.com <jal...@googlegroups.com> on behalf of vsurducan <vsur...@gmail.com>
Sent: Wednesday, March 26, 2025 5:29:52 PM
To: jal...@googlegroups.com <jal...@googlegroups.com>
Subject: Re: [jallib] Two I2C Bus on one PIC
 

David VB

unread,
Mar 26, 2025, 12:52:59 PMMar 26
to jallib
The second bus will connect devices that will not always be present or powered up.   I don't want to take the risk to hang the whole main bus if those devices are not attached.  And since there are two MSSP in the PIC, it's worth to use them.

Rob,
If it this as I described (only copy and rename the registers/variables/constants by adding a '2' as needed, if you want, I can do it myself and share it later on).
I was planning to use also the i2c_level1.jal and i2c_reset_bus.jal libraries.  Therefore, I think this will need 3 new (copies) files.

David VB

unread,
Mar 27, 2025, 11:21:03 AMMar 27
to jallib
Rob,

Here attached the files to control a second I2C bus for PIC'w with 2 MSSP modules (ex: PIC16F18325 which I'm gonna use in my next project)
I did not add a control to see if the PIC has a second MSSP at compile time, because it was not controlled for PIC with one MSSP either....

Nothing is tested, but if it compiles, it should work, because what I did was only "Search and replace".   i did not touch the logic

Feel free to adapt as you wish
i2c2_level1.jal
i2c2_reset_bus.jal
i2c2_hw_slave_msg.jal
i2c2_hw_slave_isr.jal
i2c2_hw_slave.jal
i2c2_hardware.jal

Rob CJ

unread,
Mar 27, 2025, 2:15:47 PMMar 27
to jallib
Hi David,

Thanks.  I will have a look at it and test it. If it all works OK I will add it to Jallib.

Kind regards,

Rob


Van: jal...@googlegroups.com <jal...@googlegroups.com> namens David VB <pinhe...@gmail.com>
Verzonden: donderdag 27 maart 2025 16:21
Aan: jallib <jal...@googlegroups.com>
Onderwerp: [jallib] Re: Two I2C Bus on one PIC
 
--
You received this message because you are subscribed to the Google Groups "jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallib+un...@googlegroups.com.

Rob CJ

unread,
Mar 28, 2025, 5:59:47 AMMar 28
to jal...@googlegroups.com
Hi David,

It could be that renaming is sufficient but for that I need to dive a little deeper in the PIC16F18325.

You can give it a try but I might work on it later too.

Kind regards,

Rob


Verzonden: woensdag 26 maart 2025 17:52
Aan: jallib <jal...@googlegroups.com>
Onderwerp: Re: [jallib] Two I2C Bus on one PIC
 

Rob CJ

unread,
Mar 28, 2025, 7:57:12 AMMar 28
to jal...@googlegroups.com
Hi David,

Short update. I used the original I2C harware library and changed the names differntly so that it matches with the conventions that are also used for other libraries. So i2c_intialize() is called i2c_initialize2(), etc.

I also cleaned up the library a bit.

I made a sample program that uses both i2c interfaces at the same time. I connected an SSD1306 display to the first IIC bus and an I2C EEPROM to the second IIC bus. In this way I am sure that I can use both interfaces  at the same time.

And now the bad news. I did not get the second IIC inteface working. So I did an extra test by only controlling one device first with first IIC library and then did the same with the second IIC library by using exactly the same pins but still the second IIC does not want to work. In both cases I had set the PPS pins which is required for the IIC to work.

So for the first test:
RC0PPS = PPS_SCK1
RC1PPS = PPS_SDA1

and for the second test:
RC0PPS = PPS_SCK2
RC1PPS = PPS_SDA2

Since I have no clue yet I posted the issue on the Microchip Forum to see if somebody knows about this problem.

Kind regards,

Rob


Van: jal...@googlegroups.com <jal...@googlegroups.com> namens Rob CJ <rob...@hotmail.com>
Verzonden: woensdag 26 maart 2025 18:57
Aan: jal...@googlegroups.com <jal...@googlegroups.com>

David VB

unread,
Mar 28, 2025, 9:10:05 AMMar 28
to jallib
With your PPS code, you only assign outputs.   The PPS requires you to also configure the inputs on the same pins for I2C
In your case:
RC0PPS = PPS_SCK1
RC1PPS = PPS_SDA1
SSP1CLKPPS = PPS_RC0
SSP1DATPPS = PPS_RC1

But why not keep it simple and use the default pins ?
SCL1 = RC0
SDA1 = RC1
SCL2 = RC4
SDA2 = RC5

Rob CJ

unread,
Mar 28, 2025, 2:15:20 PMMar 28
to jallib
Hi David,

Thanks for the hint. I tried it but it does not work.

Reason for not using the default is because I am also using the serial port and that one is on C4.

But in the case of the sample program with the SSD1306 and EEPROM I can use the defaults. I will give that a try.

Kind regards,

Rob


Verzonden: vrijdag 28 maart 2025 14:10

Rob CJ

unread,
Mar 28, 2025, 2:15:25 PMMar 28
to jallib
Hi David,

I used the default values and works. This remains strange since it should be possible to assign these signals to other pins.

Attached the library and sample program for the 16F81325.

I added some default values to the library so that it uses 400 kHz standard and IIC signal levels. Of course you can overrule these if you want but if the default is OK you do not have to define these values anymore.

BTW. I will only add this library and will not update the other files for the second IIC hardware since I expect these variations are not used that often (but if I am wrong JAL users can let me know).

Let me know if it works for you.

Thanks.

Kind regards,

Rob


Verzonden: vrijdag 28 maart 2025 14:10
i2c_hardware2.jal
16f18325_i2c_hw2_l0.jal

pinhe...@gmail.com

unread,
Mar 28, 2025, 3:18:07 PMMar 28
to jallib
You can use PPS to reassign TX, just to be sure

Télécharger BlueMail pour Android

Rob CJ

unread,
Mar 29, 2025, 6:37:18 AMMar 29
to jallib
Hi David,

I uploaded the i2c_hardware2.jal library to GitHub including sample files for 16f18325 and 16f18425 both for i2c_hardware.jal and i2c_hardware2.jal. All sample files have been tested.

So they will be in the next bee-package.

Kind regards,

Rob


Van: jal...@googlegroups.com <jal...@googlegroups.com> namens pinhe...@gmail.com <pinhe...@gmail.com>
Verzonden: vrijdag 28 maart 2025 20:18

David VB

unread,
Mar 29, 2025, 9:01:05 AMMar 29
to jallib
Hi Rob,

Are you using a PIC16LF18325 ?    If so, beware that there is a hardware bug (in all hardware revisions except the last one) in the MSSP module when using the PIC with a VDD > 3 V

Thanks for i2c_harware2.jal, but what about the other files ?   Do you intent to duplicate them also, or do you want I do it (with the right naming convention this time) ?

Rob CJ

unread,
Mar 29, 2025, 11:46:15 AMMar 29
to jal...@googlegroups.com
Hi David,

I am using a regular 16F18325, not the LF version. I tested everything at 3.3. Volt.

For the other files. Initially I did not want to create them but since it is just a simple find & replace. I can still do that.

Kind regards,

Rob



Verzonden: zaterdag 29 maart 2025 14:01

Rob CJ

unread,
Mar 29, 2025, 11:46:19 AMMar 29
to jal...@googlegroups.com
Hi David,

I had a look at the other files. They are quite specific for 18F series so I decided not to change them for the second I2C hardware otherwise I would upload untested files.

Kind regards,

Rob

Van: Rob CJ <rob...@hotmail.com>
Verzonden: zaterdag 29 maart 2025 15:23
Reply all
Reply to author
Forward
0 new messages