# How to calculate instruction cycles on pic?

I want to know how much instruction cycles are each line of a program, is there a way to do that?

It's important to me because i'm having a problem with the oscillator (or might be something else) doesn't seem be as fast as i want.

The pic in question a a PIC32MX470F512L.

I'm using the following config with MCC:

I made a simple program which only toggles a pin that i will see with an oscilloscope:

#include "mcc_generated_files/mcc.h"

/*
Main application
*/
int main(void)
{
// initialize the device
SYSTEM_Initialize();

while (1)
{
IO_RG0_Toggle();
}

return -1;
}


On the oscilloscope it toggles every ~13.8us.

This seems to be super slow.

Note: Using the timer with 1ms calculated period and alternating the signal of the port with the timer callback it alternates every 1ms.

But if i set the timer period to 8us the value goes to 16us:

• Look in the datasheet for the oscillator settings. You need to use the PLL to step the clock frequency up to anything reasonable. As it is, you're only running at 8MHz. ww1.microchip.com/downloads/en/DeviceDoc/60001185G.pdf Jun 4 '19 at 14:54

You are going about debugging the oscillator the wrong way. You are trying to measure an unknown quantity, and getting confused because it is not a number that you expect - even though you are not sure what you are expecting.

The best way to do this is to set up a timer of a convenient value ( say 1ms).

Have your pin toggle whenever the timer flag is triggered.

If you observe on your oscilloscope the correct pulse width (1ms) then you know that the oscillator is ok.

If it is not 1ms then go back and check/tweek the timer setup and the oscillator setup.

Repeat debug steps until the correct value is achieved.

P.S. A main while loop running at ~13us when the clock speed is only 8MHz actually seems resonable to me. I might have expected <10us, but 13us is not that far off

• i did a modify to include a PLL divider because of a warning i had. Yes, if i set a TMR1 to 1ms and go see on oscilloscope the callback it is nice. But if i try to see the timer at 8us it gives me a wrong value. Jun 4 '19 at 15:43
• If i set the TMR1 calculated period to 8 us it gives me 16us the callback of interrupt. Jun 4 '19 at 15:44
• That's ~110 instructions, assuming the MIPS core runs at 1 cycle per instruction. Even with some stalls or whatever because of the peripheral bus and the loop that seems a bit much for a toggle. Maybe OP is running in debug mode? Jun 4 '19 at 15:45
• @SpehroPefhany I'm not in debug mode, i program the pic, then i go see the signal with an oscilloscope. Jun 4 '19 at 15:59
• I don't have any ideas beyond the two quite sensible answers here, you should look at the emitted assembly code and see what it should be. Or run the simulator. Jun 4 '19 at 16:05

Yes, you must look at the disassembly how many opcodes the loop is, and calculate cycle count for each opcode. This is usually just easier with a debugger or simulator.

• hello, and how can i do that? I'm using MPLAB X IDE v5.05 Jun 4 '19 at 14:37