I am trying to implement I2C comms between a PIC18F45K20 (
https://ww1.microchip.com/downloads/en/devicedoc/40001303h.pdf) as a master with a slave MCP7940N RTC (
https://ww1.microchip.com/downloads/en/devicedoc/20005010f.pdf). However, after writing a test program to just read the value of a register in the MCP7940N, my I2C seems to be stuck in the "while(I2C -> IsBusy())" loop. I generated the code using the code configurator (running MPLAB X v6.15), which is available (along with my main.c) in the Project Files zip.
Here is my main.c file (slightly trimmed to remove fluff when compared to the version in the zip. I haven't edited the outputs from mcc):
#include "mcc_generated_files/system/system.h"
#include <string.h>
#define I2C_SLAVE 0b1101111
#define MINUTES 0x01
const i2c_host_interface_t *I2C = &I2C1_Host;
//Sends a string via UART
void EUSART1_sendString(const char *str)
{
while(*str)
{
while (!(EUSART_IsTxReady()));
EUSART_Write(*str++);
}
}
//Sends an int via uart
void EUSART1_sendInt(int num)
{
char buffer[10];
int i, sign;
//Handling negatives
if (num < 0)
{
sign = -1;
num = -num;
} else {
sign = 1;
}
//Convert int to string in reverse
i = 0;
do
{
buffer[i++] = num % 10 + '0';
num /= 10;
} while (num > 0);
//Add sign if it's negative
if (sign == -1) {
buffer[i++] = '-';
}
//Send each char in reverse order
for (int j = i - 1; j >= 0; j--)
{
while (!(EUSART_IsTxReady()));
EUSART_Write(buffer[j]);
}
}
int main(void)
{
SYSTEM_Initialize();
uint8_t dataRead[1];
uint8_t dataWrite[1];
dataWrite[0] = MINUTES;
while (1)
{
if (I2C -> WriteRead(I2C_SLAVE, dataWrite, 1, dataRead, 1)){
while(I2C -> IsBusy())
{
I2C -> Tasks();
}
if (I2C -> ErrorGet() != I2C_ERROR_NONE)
{
//Success
}
else
{
//Error
}
}
//process and print dataRead
EUSART1_sendString("The number is: ");
EUSART1_sendInt(dataRead[0]);
EUSART1_sendString("\r\n");
__delay_ms(1000);
}
}
I have verified the hardware is properly soldered and have the PIC communicating via UART with success.
This test heavily from the microchip I2C mcc generated example here:
https://onlinedocs.microchip.com/pr/GUID-4F9CFB6D-C135-42A9-8522-75972CEC6F0E-en-US-8/index.html?GUID-CF356ACC-625C-4021-A3B4-33C65C9070DBProbing the SDA/SCL shows some toggling going on, but nothing resembling a correct I2C packet. The following were triggered from an initial power on of the circuit:
And zoomed in:
I'm not really sure what to try now. Am I incorrectly using the MCC generated outputs? Am I missing something important in the rtc datasheet?
The following is a snippet from my schematic: