While loop execution time

Thread Starter

gehan_s

Joined Sep 17, 2012
38
Hi all,

How can I determine the total runtime of this while(1) loop? The microcontroller is a 16F877A and has a oscillator frequency of 20MHz.

Thanks in advance !!!!!!!!!!!!!!!

Rich (BB code):
void main(){

     float error   = 0;   // present error
     float error_1 = 0;   // previous error
     float acc_err = 0;   // accumulated error
     float vout    = 0;   // actual O/P value
     float set     = 0;   // set O/P value
     float pwm     = 0;
     float Kp      = 0;
     float Ki      = 0;
     float Kd      = 0;
     float T       = 0;   // time between two samples
     char txt1[5];
     char txt2[5];
     
     TRISB         = 0;

     Lcd_Init(&PORTB);
     Pwm_Init(10000);
     Pwm_Start();
     Pwm_Change_Duty(pwm);
     Lcd_Cmd(LCD_CURSOR_OFF);
     Lcd_Cmd(LCD_CLEAR);

     delay_ms(10);

     while(1){
     
          set     = Adc_Read(1);
          Kp      = Adc_Read(2);
          Ki      = Adc_Read(3);
          Kd      = Adc_Read(4);
          vout    = Adc_Read(0);
          set     = (set/1024)*5;
          Kp      = (Kp/1024)*100;
          Ki      = (Ki/1024)*100;
          Kd      = (Kd/1024)*100;
          vout    = (vout/1024)*5;
          error_1 = error;
          error   = set - vout;
          acc_err = acc_err + error;

          pwm     = pwm + (Kp*error) + (Kd*(error-error_1)/T) + (Ki*acc_err*T);

          if(pwm>=255){
          pwm  = 255;
          }
          else if(pwm<0){
          pwm = 0;
          }

          Pwm_Change_Duty(pwm);

     }
}
 

tgil

Joined May 18, 2011
19
One easy way is to start a timer at the beginning of the loop and then read it's value of the end of the loop. You can set the timer to be as fast or as slow (ie a microsecond timer or millisecond timer) as you want depending on the precision you need.

Another easy way (if you have an oscilloscope) is to use an IO signal and have it go high when the loop starts and low when the loop stops.

The hard way is to analyze the assembly code and count clock cycles.
 

thatoneguy

Joined Feb 19, 2009
6,359
In circuit, use above method and time with a scope.

Software, use the code stopwatch function to determine number of cycles.

Which compiler are you using?
 

WBahn

Joined Mar 31, 2012
32,707
If timing the loop execution time, a good practice is to put the loop inside another lopp and execute it many times. If you execute it, say, a thousand times then you get a thousand times better resolution on your answer for the same resolution on your timer.

Though I just noticed that this is a while(forever) loop and you don't have a break statement, so this loop will never complete. I guess that means that you are looking not for the total time for the loop, but the time for one pass through the loop. For that, I would toggle a bit on each pass through the loop that can then be monitored on a scope. A simple way to do that is to simply increment a counter and use one of the bits as your output. The higher the bit number, the more passes are represented by each state change.
 

ErnieM

Joined Apr 24, 2011
8,415
thatoneguy's methods are classic.

I do note you read an A2D and set the PWM. The ADRES result will fit inside an unsigned int type (2 bytes). You've constrained PWM to 8 bits or a char type size.

Using float types for this computation is a huge waste of code space (to fit the code itself) and time (to do these computations).

Precision when using int types can be gained by "changing the units" by adjusting where the binary point is placed.

Giving your code a quick scan I would say you could achieve this by using long types, then just delaying when you do the /1024 division (leave it to the end).
 

MrChips

Joined Oct 2, 2009
34,629
I simply toggle an output pin and monitor on a scope.
I do this all the time when analyzing code for performance.
 
Last edited:

THE_RB

Joined Feb 11, 2008
5,438
Yep the output pin and scope rules, because you can see other things like variance in delay time (because functions sometimes take longer, or make decisions to do longer processes).

Another good way is to clear a 16bit timer before the function, then after the function grab the timer value and display it on the LCD in real time. That's very useful if you have an LCD that has some spare room to show some "debug" values.
 

John P

Joined Oct 14, 2008
2,051
Nobody seems to have mentioned yet that you can run simulations of a processor using MPLab and when you do, it counts time.

However, if you care about time very much, you ought to limit the amount of floating-point math that your chip has to do. Keep your variables in integers as much as possible, and use mathematical tricks (they do exist!) to allow you to avoid floating point calculations if you can.
 

thatoneguy

Joined Feb 19, 2009
6,359
The better known term is "profiling tool" or similar to determine time spent in a loop or function.

In BoostC, window is titled "Speed Tester", but has a stopwatch icon for the profiler as well.
MPLAB calls it the stopwatch
MikroC calls it Watch Clock, but uses the stopwatch icon.
 

John P

Joined Oct 14, 2008
2,051
I'm sure you're right, but I've never used any of those. I've always set up an output pin and watched it on a scope when I've needed to check timing. But I know it's possible to do it.
 

thatoneguy

Joined Feb 19, 2009
6,359
I'm sure you're right, but I've never used any of those. I've always set up an output pin and watched it on a scope when I've needed to check timing. But I know it's possible to do it.
That's what I do for the final test. I mostly use the profiler/stopwatch to find slow functions and speed them up if possible.

When it comes to measuring actual timing, I measure the running circuit, since oscillators drift, clock in sim may have been incorrect, actual output hardware switching time, etc.
 

THE_RB

Joined Feb 11, 2008
5,438
I agree, and also most of the things I make have significant inputs/outputs etc so timings are often based on repsonses with external hardware, making the simulator pretty useless. Actually I don't think I've bothered to run a simulator in 6 or 7 years.
 
Top