I have several interrupts that are working and I want to calculate the idle time that cpu is doing, and calculate their cpu usage too.

so I have a while loop while(); and the interrupts are working. How would I calculate 1. the idle time 2. the cpu usage of the interrupts.

I have a scope and I can clear set toggle a pin

  • \$\begingroup\$ Well obviously you can find the time spent in the interrupts by togging the pin at the start and end of each ISR. What exactly is your question about? Do you know how to measure time with a scope? \$\endgroup\$ – Dmitry Grigoryev Sep 15 '16 at 13:21

Set the pin high at the top of your ISR and then set it low again at the end of the ISR.

This would effectively give you a pulse width modulated (PWM) signal which you can smooth with an RC filter (or an opamp-based filter if you're feeling adventurous) and read the voltage by using your scope.

If you wanted to do this in your project natively then you can use an ADC instead of the scope. If your CPU has a built-in ADC then you can use that to measure it's own CPU usage as the capacitor will hold charge for long enough to do the measurement outside of the ISR. If not then use an external ADC on an SPI bus or equivalent.

When the pin is high (in the ISR) the cap will to charge up, and after leaving the ISR (idle time) it will discharge again. The voltage measured by the Scope/ADC will be a fraction of VCC and coincidentally will be close to the fraction of time spent in the ISR.

Choosing sensible values for your filtering components will allow you to integrate your CPU usage over longer or shorter timespans (milliseconds or minutes depending on your needs).

  • \$\begingroup\$ can I calculate the idle time in the cpu in the while(1) by toggling a pin ? \$\endgroup\$ – Andre Sep 14 '16 at 10:22
  • \$\begingroup\$ @Andre, you'd have to toggle it very fast in order for your ISR to cause visible gaps in the toggle pattern. You'd find it tricky to trigger your scope properly I think. In fact you wouldn't be able to do anything else in the main loop! \$\endgroup\$ – Wossname Sep 14 '16 at 10:29

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