2 Host Read/Write Data

In this use case, the microcontroller is configured in I2C Host mode using the MSSP1 instance of the MSSP peripheral, and through multiple examples it communicates with different I2C client devices found on the PICkit Serial I2C Demo Board.

Code Examples:

  • MCC Generated (Melody)
  • Bare Metal Code

Both the MCC Generated and Bare Metal code examples implement the following steps:

  • To transmit data as an I2C host, the following sequence must be implemented:

    1. Establish connection to I2C Device
      • Generate the Start condition by setting the SEN bit in the SSPxCON2 register.
      • The SSPxIF flag in the PIR3 register is set by hardware on completion of the Start condition and must be cleared by software.
      • Load the client address into the SSPxBUF register.
      • Clear SSPxIF flag in the PIR3 register indicating that the host is ready for another byte and must be cleared by software.
      • Check the ACKSTAT bit in the SSPxCON2 register.
    2. Write the desired register to client device
      • Load the register address in the SSPxBUF register.
      • The SSPxIF flag in the PIR3 register is set by hardware indicating that the client is ready for another byte and must be cleared by software.
      • Check the ACKSTAT bit in the SSPxCON2 register.
    3. Write a byte of data to the client device
      • Load the data in the SSPxBUF register.
      • The SSPxIF flag in the PIR3 register is set by hardware indicating that the client is ready for another byte and must be cleared by software.
      • Check the ACKSTAT bit in the SSPxCON2 register.
    4. End I2C connection
      • To end the transmission, generate the Stop condition by setting the PEN bit in the SSPxCON2 register.
    Figure 2-1. Step-By-Step Description of Host Writing N Bytes to a Client Device Over I2C
  • To receive data as an I2C host, the following sequence must be implemented:

    1. Establish connection to I2C Device
      • Generate the Start condition by setting the SEN bit in the SSPxCON2 register.
      • The SSPxIF flag in the PIR3 register is set by hardware on completion of the Start condition and must be cleared by software.
      • Load the client address into the SSPxBUF register.
      • Clear SSPxIF flag in the PIR3 register indicating that the host is ready for another byte and must be cleared by software.
      • Check the ACKSTAT bit in the SSPxCON2 register.
    2. Receive and ACK data byte (Repeat N-1 times)
      • Set the RCEN bit to enable the Receive mode.
      • Clear SSPxIF flag in the PIR3 register indicating that the host is ready for another byte and must be cleared by software.
      • Read data from the SSPxBUF register.
      • Send an Acknowledge bit to tell the device to send the next byte.
      • Clear SSPxIF flag in the PIR3 register indicating that the host is ready for another byte and must be cleared by software.
    3. Receive last byte in frame
      • Set the RCEN bit to enable the Receive mode.
      • The SSPxIF flag in the PIR3 register is set by hardware and must be cleared by software.
      • Read data from the SSPxBUF register.
      • Send a Not Acknowledge bit to stop receiving bytes.
      • Clear SSPxIF flag in the PIR3 register indicating that the host is ready for another byte and must be cleared by software.
      • To end the transmission, generate the Stop condition by setting the PEN bit in the SSPxCON2 register.
    Figure 2-2. Step-By-Step Description of Host Reading N Bytes From a Client Device Over I2C
Note: For a reliable I2C operation, external pull-up resistors must be added. Refer to TB3191 - I2C Master Mode for more details.