# Fastest time a PIC can set and clear a pin

What is the fastest time a microcontroller (PIC16 in this case) can be set and cleared? It is defined by the frequency, but I don't know the formula to calculate the fastest time that a single pin can be set and cleared.

Assuming the frequency (XTAL) is 20MHz, and there are no delays in between, what is the time needed for the pin to be set and cleared?

For example:

RB2=0;
RB2=1;
RB2=0;


What is the time that RB2 is high?

It depends on the particular PIC, since some PIC's can execute one instruction per cycle, and others for example, execute one instruction for every four cycles of the system clock.

Although the OP asked for info re a 20 MHz PIC16, since that has already been addressed, I am showing information for the fastest version in all the different families of PIC's. Where there are significant differences between parts in various subfamilies, like the PIC24F/PIC24EP, and PIC32MX/PIC32MZ, I am showing both separately.

I got the numbers by going onto the Digi-Key website, looking up parts for each family, and then selecting the highest speed. I then pulled up a datasheet on an example part, which also verified the MIPS value.

Family       Clock    Speed    I/O toggle time

PIC10F      16 MHz    4 MIPS    250 ns
PIC12F      20 MHz    5 MIPS    200 ns
PIC16F      20 MHz    5 MIPS    200 ns  <--- example in the original question
PIC16F      48 MHz   12 MIPS     83 ns
PIC18F      64 MHz   16 MIPS     62 ns
PIC24F      32 MHz   16 MIPS     62 ns
PIC24EP     70 MHz   70 MIPS     28 ns
dsPIC30     40 MHZ   30 MIPS     33 ns
dsPIC33EP   70 MHz   70 MIPS     28 ns
PIC32MX    100 MHz  100 MIPS     10 ns
PIC32MZ    200 MHz  200 MIPS      5 ns


If anyone has any corrections to make to this table, please don't hesitate to edit it.

MIPS is million instructions per second. I/O toggle time is the amount of time the I/O pin would be either on or off in nanoseconds (ns), and is computed as one million divided by the MIPS number.

All of these processors have the ability to turn an I/O pin on or off in a single instruction. The instructions themselves varies per processor.

• In sec 18.3 of a datasheet from the dsPIC30 family, it is shown how they get to 30MIPS - not from the 40MHz, but using the internal clock. – Keelan Oct 28 '14 at 21:54
• Thanks. I should of thought of the PLL. Those calculations can get a little weird sometimes -- like the PIC24EP, with an internal 7.37 MHz clock, I ended up at 69.6 MIPS. – tcrosley Oct 28 '14 at 22:02
• Your chart is not quite correct for the EP parts. Writes to non-cpu special function registers take two instruction cycles, not 1 like on most other PICs. A 33EP at the maximum speed of 70 MIPS can only execute something like a "BSET PORTB, #2" every 28.6 ns. Also some PICs have PWM generators that can produce pulses shorter than a instruction cycle. It's not all quite a simple as your table. – Olin Lathrop Oct 28 '14 at 22:58
• @OlinLathrop Thanks for your correction. I should have known better, I am using a 24EP in a current project. I'm also using a PIC32MX (80 MHz, I don't have one of the newer 100 MHz ones), and that can do one I/O instruction per cycle. – tcrosley Oct 28 '14 at 23:17

If you're using a 20MHz crystal, that means you have 5MIPS (5 million instructions per second), because internally the 20MHz is divided by 4.

That means that one instruction takes $\frac{1s}{5000000} = 200ns$.

You can achieve what you want in assembly, like this:

bcf PORTB, 2   // Sets the pin state to 0
bsf PORTB, 2   // Sets the pin state to 1
bcf PORTB, 2   // Sets the pin state to 0


This shows that changing the pin state takes one instruction, therefore the minimum time the pin would be on is 200ns (if you clear it directly again).

I only have experience with PIC16 and PIC18, I'm not sure about dsPIC, PIC32, and all other varieties. But in general, if you know the number of instructions per second (you can calculate it from the oscillator configuration), you can calculate the minimum on-time, as you say it.

• @Triak happy to help. Please don't rush with accepting the answer, nobody's in a hurry, and by not accepting the answer so quickly you'll draw some more attention (and possibly better answers) to your question. You can also always upvote answers that were helpful. – Keelan Oct 28 '14 at 19:35
• Oddly, the PIC10 operates at one instruction per cycle. The PIC18 is like the PIC16 (one instruction per four cycles), and the PIC24 and PIC32 are both one instruction per cycle. Thus the PIC32MX, at 80 MHz, can toggle pins up and down in 12.5 ns -- I've watched it on a scope. – tcrosley Oct 28 '14 at 19:43
• @tcrosley I'm sorry you have deleted your answer. We should've turned this into a general question about minimum instruction times on different branches, and you would've answered it much better. – Keelan Oct 28 '14 at 19:44
• While I was writing my answer, I noticed the OP mentioned in a comment that he was using a PIC16 (which wasn't mentioned in the original question), so my answer wasn't applicable. I have given specs for the other main PIC families in a comment above. – tcrosley Oct 28 '14 at 19:46
• @tcrosley the pic10 (I guess you mean pic10f200 etc) is a 12-bit core, so it operates at 4 clocks / instructions, just like the pic12 and pic 16 (12 and 14 bit cores). – Wouter van Ooijen Oct 28 '14 at 20:45