0
\$\begingroup\$

I am on the process of interfacing a RTC DS1307 with PIC18F. So, the controller will be the master and I2C the slave, therefore the controller will generate clock and data as outputs, If i am right?. However, I went through a couple of codes and noticed that the RC3 and RC4 pins are configured as inputs during the I2C initialization. My question is, Aren't these pins supposed to be configured as outputs in order to clock out and data out (as it is in Master Mode) to the RTC?.

Below is an example from one of those codes:

void i2c_init()
{
 TRISC3 = 1;
 TRISC4 = 1;
 SSPSTAT |= 0x80; //Slew Rate Disabled
 SSPADD = 49;

 SSPCON=0b00101000; //Master mode
 SSPADD = 49;

 /*SSPEN = 1
 Enables the Serial Port and configures the SDA and SCL
 Pins as the Serial Pins
 SSPM3:SSPM0 --> 1000
 I2C Master Mode
 Clock = Fosc/4*(SSPADD+1)
 */

}

Can someone please explain why are they configuring these pins as inputs ?

Thank you all in advance!

Regards

~VD

\$\endgroup\$
2
\$\begingroup\$

It is because the PIC I2C bus is a multi master module, so it needs to detect the bus is free before sending data to it. The requirement to configure the pins as inputs are in the datasheet (I'm using 18F4550 as a reference):

The MSSP module in I2C mode fully implements all master and slave functions (including general call support) and provides interrupts on Start and Stop bits in hardware to determine a free bus (multi-master function). [...] The user must configure these pins as inputs by setting the associated TRIS bits.

(http://ww1.microchip.com/downloads/en/DeviceDoc/39632e.pdf - Page 209)

EDIT: I just realized you specified master mode. In this case, refer to page 228 of the same datasheet:

In Master mode, the SCL and SDA lines are manipulated by the MSSP hardware if the TRIS bits are set.

\$\endgroup\$
2
  • \$\begingroup\$ I checked in the datasheet. Just to confirm, so you mean configuring these pins as inputs would make sure that the MSSP peripheral will take control and manipulate the SDA and SCL lines automatically as desired? \$\endgroup\$ – PsychedGuy Nov 20 '15 at 1:14
  • 1
    \$\begingroup\$ Yes, it's a requirement for initialisation that the port direction is set correctly. The MSSP module will then manipulate the TRIS bits during send and receive. \$\endgroup\$ – David Nov 20 '15 at 9:22
1
\$\begingroup\$

Generally:
When doing bit-banging, i.e. build your own communication interface in pure software using GPIOs, it's your responsibility how to dynamically configure a pin at a given time (depending on who is expected to send/receive something).

But when using peripherals - such as the I2C unit - you can't easily tell whether the GPIO configuration of the associated pins have to be input or output. You always have to read the datasheet.
On PIC, both pins have to be input, on another micro they may have to be output, on another micro it may not matter at all.
Just make sure that the initial configuration is ok, the rest is managed by the peripheral itself.

\$\endgroup\$
2
  • \$\begingroup\$ No, I am not using bit banging here. So, your telling, since we cant say if the GPIO needs to behave explicitly as an input or output, therefore setting these pins as input will make sure that the I2C peripheral will behave as an input or output automatically when desired?. For example the SDA pin can behave as an output when sending data to slave and behave as input while receiving the read data from the slave?. \$\endgroup\$ – PsychedGuy Nov 20 '15 at 1:20
  • 1
    \$\begingroup\$ Yes, if correctly configured (the peripheral as well as the pin directions according to the datasheet) I2C peripheral will automatically take care of all this. \$\endgroup\$ – mic Nov 20 '15 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.