timerinterruptpicsamplingi2c

I²C and timer interrupt (timer1)


I'm trying to read from multiple I²C slave devices using a dsPIC33 microcontroller.

I was hoping someone could advise me on the correct method to use a timer interrupt (in this case timer1) and collecting the I²C data.

So far, I can collect data fine from the I²C slave devices by looping in a while loop, but since attempting to add a timer interrupt (so I can apply my own sampling rate rather than 'collect as fast as you can'), my I²C software driver is getting stuck.

I've tried with a very low timer speed (1 Hz at the moment) and I²C is on the standard 100 kHz speed. The PIC is processing at 80 MHz.

What is the correct method to use timers and I²C modules? I've had a look online, and it seems it could be a matter of interrupt priority as when using timer1 I have an interrupt (I²C) within an interrupt (timer1), although I didn't have any luck so far.


Solution

  • It turned out it was an interrupt priority problem. I had previously had my timer1 set to priority 7 (highest):

    IPC0bits.T1IP = 0b111; // Timer1 Interrupt priority level = 7
    

    Changing this to priority 1 solved the problem:

    IPC0bits.T1IP = 0b001; // Timer1 Interrupt priority level = 1
    

    My guess is that the different priorities conflict with the I²C interrupt.