pic Interrupt on change doesn't trigger first time
<p>I'm trying to comunicate a pic18f24k50 with an arduino 101. I'm using two lines to establish a synchronized comunication. On every change from low to high of the first line, I read the value from the second line.
I have no problems with the arduino code, my problem is that in the pic, the Interrupt on change triggers on the second change from low to high instead of triggering on the first change. This only happens the first time I send data, after that, it works perfectly (it triggers on the first change and I receive the byte properly). Sorry for my english, I'll try to explain myself better with this image:</p>

<p><a href="https://i.stack.imgur.com/UkoSF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UkoSF.png" alt="enter image description here"></a></p>

<p>Channel 1 is the clock signal, channel2 is the data (Im sending one byte for the moment, with bit values 10101010). Channel 4 is an output I'm changing every time I process a bit. (as you can see, it begins on the second rise of the clock signal instead of the first one). This is captured on the first sent byte, the next ones works ok.</p>

<p>I post the relevant code in the pic here:</p>

<p>This is where I initialize things:</p>

<pre><code>TRISCbits.TRISC6 = 0;
TRISCbits.TRISC1 = 1;
TRISCbits.TRISC2 = 1;
IOCC1 = 1;
ANSELCbits.ANSC2=0;
IOCC2 = 0;
INTCONbits.IOCIE = 1;
INTCONbits.IOCIF = 0;
</code></pre>

<p>And this is on the interrupt code:</p>

<pre><code>void interrupt SYS_InterruptHigh(void)
{
    if (INTCONbits.IOCIE==1 &amp;&amp; INTCONbits.IOCIF==1)
    {
          readByte();
    }
}

void readByte(void)
{
while(contaBits&lt;8)
{
    INTCONbits.IOCIE = 0;
    INTCONbits.IOCIF = 0;

    while (PORTCbits.RC1 != HIGH)
    {
    }    

    if (PORTCbits.RC1 == HIGH)
    {

            LATCbits.LATC6 = !LATCbits.LATC6;


        //LATCbits.LATC6 = ~LATCbits.LATC6;
        switch (contaBits)
        {
            case 0:
                if (PORTCbits.RC2 == HIGH)
                    varByte.b0 = 1;
                else
                    varByte.b0 = 0;
                break;
            case 1:
                if (PORTCbits.RC2 == HIGH)
                    varByte.b1 = 1;
                else
                    varByte.b1 = 0;
                break;    
            case 2:
                if (PORTCbits.RC2 == HIGH)
                    varByte.b2 = 1;
                else
                    varByte.b2 = 0;
                break;
            case 3:
                if (PORTCbits.RC2 == HIGH)
                    varByte.b3 = 1;
                else
                    varByte.b3 = 0;
                break;
            case 4:
                if (PORTCbits.RC2 == HIGH)
                    varByte.b4 = 1;
                else
                    varByte.b4 = 0;
                break;
            case 5:
                if (PORTCbits.RC2 == HIGH)
                    varByte.b5 = 1;
                else
                    varByte.b5 = 0;
                break;
            case 6:
                if (PORTCbits.RC2 == HIGH)
                    varByte.b6 = 1;
                else
                    varByte.b6 = 0;
                break;
            case 7:
                if (PORTCbits.RC2 == HIGH)
                    varByte.b7 = 1;
                else
                    varByte.b7 = 0;
                break;
        }

        contaBits++;
    }

}//while(contaBits&lt;8)
INTCONbits.IOCIE = 1;
contaBits=0;
}
</code></pre>

<p>LATCbits.LATC6 = !LATCbits.LATC6; &lt;-- this is the line corresponding to channel 4.</p>

<p>RC1 is channel 1
and RC2 is channel 2</p>

<p>My question is what am I doing wrong, why on the first sent of bytes the interrupt doesn't triggers on the first change of the line 1?</p>

<p>Thank you.</p>