I came across this video on YouTube today, it popped up as a suggestion as it often does, and I was interested to see his results.
In particular because I'm currently in the process of converting an Arduino library, and was considering using subroutines to replicate the Arduino pinMode(), digitalWrite() and digitalRead() functions, rather than rewriting the code not to use them. This also gives you the huge advantage that it's trivial to switch to different pins dynamically if required - something you can't do if it's fixed in the source code. My feeling however, was that it would be MUCH slower than direct PIC code - so I was interested to see that the exact same applies to the Arduino as well.
So I wrote the following simple code, using an 18F27K40 using the internal clock at 64MHz, on an existing board, commenting out each pair in turn.
As commented, the straight PIC code using LAT gave me a 4MHz output, and the digitalWrite() routines only 150KHz, this using the Free version of the XC8 compiler.
The digitalWrite() routine actually came from an example program at http://mplabxpress.microchip.com/mplabcloud/example
Incidentally, if you remove the test for the State value been 0 or 1, it increases speed to 160KHz or so.
In particular because I'm currently in the process of converting an Arduino library, and was considering using subroutines to replicate the Arduino pinMode(), digitalWrite() and digitalRead() functions, rather than rewriting the code not to use them. This also gives you the huge advantage that it's trivial to switch to different pins dynamically if required - something you can't do if it's fixed in the source code. My feeling however, was that it would be MUCH slower than direct PIC code - so I was interested to see that the exact same applies to the Arduino as well.
So I wrote the following simple code, using an 18F27K40 using the internal clock at 64MHz, on an existing board, commenting out each pair in turn.
C:
while(1)
{
//LATAbits.LATA6 = 1; //4MHz
//LATAbits.LATA6 = 0;
digitalWrite(10, 1); //150KHz
digitalWrite(10, 0);
}
As commented, the straight PIC code using LAT gave me a 4MHz output, and the digitalWrite() routines only 150KHz, this using the Free version of the XC8 compiler.
The digitalWrite() routine actually came from an example program at http://mplabxpress.microchip.com/mplabcloud/example
Incidentally, if you remove the test for the State value been 0 or 1, it increases speed to 160KHz or so.
C:
void digitalWrite(byte Pin, byte State)
{
if ((State != 0)&&(State!=1))
{
printf("digitalWrite() invalid state %d\r\n",State);
return;
}
switch (Pin) {
case 2: // RA0
LATAbits.LATA0 = State;
break;
case 3: // RA1
LATAbits.LATA1 = State;
break;
case 4: // RA2
LATAbits.LATA2 = State;
break;
case 5: // RA3
LATAbits.LATA3 = State;
break;
case 6: // RA4
LATAbits.LATA4 = State;
break;
case 7: // RA5
LATAbits.LATA5 = State;
break;
case 10: // RA6
LATAbits.LATA6 = State;
break;
case 9: // RA7
LATAbits.LATA7 = State;
break;
case 11: // RC0
LATCbits.LATC0 = State;
break;
case 12: // RC1
LATCbits.LATC1 = State;
break;
case 13: // RC2
LATCbits.LATC2 = State;
break;
case 14: // RC3
LATCbits.LATC3 = State;
break;
case 15: // RC4
LATCbits.LATC4 = State;
break;
case 16: // RC5
LATCbits.LATC5 = State;
break;
case 17: // RC6
LATCbits.LATC6 = State;
break;
case 18: // RC7
LATCbits.LATC7 = State;
break;
case 21: // RB0
LATBbits.LATB0 = State;
break;
case 22: // RB1
LATBbits.LATB1 = State;
break;
case 23: // RB2
LATBbits.LATB2 = State;
break;
case 24: // RB3
LATBbits.LATB3 = State;
break;
case 25: // RB4
LATBbits.LATB4 = State;
break;
case 26: // RB5
LATBbits.LATB5 = State;
break;
case 1: // xpress board MCLR
case 8: // xpress board VSS
case 19: // xpress board VSS
case 20: // xpress board VDD
case 27: // xpress board ICSPCLK/RB6
case 28: // xpress board ICSPDAT/RB7
default:
printf("digitalWrite() invalid pin %d\r\n",Pin);
break;
}
}