1
\$\begingroup\$

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

\$\endgroup\$
1
1
\$\begingroup\$

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

\$\endgroup\$
7
  • \$\begingroup\$ 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. \$\endgroup\$
    – Nmaster88
    Jun 4 '19 at 15:43
  • \$\begingroup\$ If i set the TMR1 calculated period to 8 us it gives me 16us the callback of interrupt. \$\endgroup\$
    – Nmaster88
    Jun 4 '19 at 15:44
  • \$\begingroup\$ 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? \$\endgroup\$ Jun 4 '19 at 15:45
  • 1
    \$\begingroup\$ @SpehroPefhany I'm not in debug mode, i program the pic, then i go see the signal with an oscilloscope. \$\endgroup\$
    – Nmaster88
    Jun 4 '19 at 15:59
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Jun 4 '19 at 16:05
1
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ hello, and how can i do that? I'm using MPLAB X IDE v5.05 \$\endgroup\$
    – Nmaster88
    Jun 4 '19 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.