Problem implementing USB and I2C in PIC18F25K50


My current project uses a PIC18F25K50 to interface with an ADC via I2C, and
sends the result to the PC via USB.

After much effort I have successfully set up the USB interface, taking example code from
the Microchip Library for Applications (MLA) as starting point. This works fine, and
communicates with the HID window on my PC.

I have also, separately, set up the I2C interface by using the MCC tool in MPLABX IDE.
This also works with no problems, captures readings from the ADC.
In the mcc.c file I have edited all the config settings and the oscillator settings
to be the same as in the USB version. All still works fine.

The problems start when I try to combine the two.

The MCC generates two pairs of files which are required for I2C:
i2c.h and i2c.c
interrupt_manager.h and interrupt_manager.c

Starting with my USB working code, I have copied these files into the project directory.
I have added the necessary lines of code to the main.c and system.c files, ie:
#include interrupt_manager.h
and of course the code to call the I2C write and read.
Without going into too much detail you will have to take my word that I have
done all this correctly, as seems to be the case since it compiles (builds) without errors.

But when I program the chip, it does not work.
Now, I have the usual while(1) forever loop, and I have one output pin set
to give a single pulse at start of loop which I monitor with a 'scope.
When not working, this pulse is absent, which strongly suggests that the
program flow has got stuck. Looking closely, I can see that it appears momentarily once
only at power on.

If I just comment out the the code to call the I2C write and read, then normal correct operation is restored.

All the above seems to me to indicate a conflict of some kind in the interrupts
generated/required by USB and I2C, so that it gets stuck waiting for a response
which never happens.

Whatever the reason, it is now my project that is stuck!
I have not tried using Debug, mainly as I’m not familiar with it, and suspect it would not identify the problem.
I can put my code in this post if required, but there is rather a lot of it.

I cannot find anything on the Microchip websites on using USB and I2C in the same device.

Bottom line: Can anyone suggest how I should proceed, or where I should look for help?

Many thanks,

Hi Yumi,

Hmm sounds like a frustrating problem. Unfortunately I’m not sure we’ll be able to help too much without seeing all of your code. Perhaps the quickest way to get a response is to post in microchip forum. A debugger would be the best way to go because you can step through your code line for line and find where the hold up is.