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:
- 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.
- 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.
- 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.
- End I2C connection
- To end the transmission, generate the Stop condition by setting the PEN bit in the SSPxCON2 register.
- Establish connection to I2C
Device
-
To receive data as an I2C host, the following sequence must be implemented:
- 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.
- 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.
- 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.
- Establish connection to I2C
Device
Note: For a reliable I2C operation,
external pull-up resistors must be added. Refer to TB3191 - I2C Master Mode for more
details.