Electronic – How to calculate instruction cycles on pic

microchipmicrocontrolleroscillatorpic

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:

enter image description here

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)
    {
        // Add your application code
        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:
enter image description here

Best Answer

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