While loop execution time

Discussion in 'Programmer's Corner' started by gehan_s, Mar 5, 2013.

  1. gehan_s

    Thread Starter Member

    Sep 17, 2012
    38
    0
    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 !!!!!!!!!!!!!!!

    Code ( (Unknown Language)):
    1. void main(){
    2.  
    3.      float error   = 0;   // present error
    4.      float error_1 = 0;   // previous error
    5.      float acc_err = 0;   // accumulated error
    6.      float vout    = 0;   // actual O/P value
    7.      float set     = 0;   // set O/P value
    8.      float pwm     = 0;
    9.      float Kp      = 0;
    10.      float Ki      = 0;
    11.      float Kd      = 0;
    12.      float T       = 0;   // time between two samples
    13.      char txt1[5];
    14.      char txt2[5];
    15.      
    16.      TRISB         = 0;
    17.  
    18.      Lcd_Init(&PORTB);
    19.      Pwm_Init(10000);
    20.      Pwm_Start();
    21.      Pwm_Change_Duty(pwm);
    22.      Lcd_Cmd(LCD_CURSOR_OFF);
    23.      Lcd_Cmd(LCD_CLEAR);
    24.  
    25.      delay_ms(10);
    26.  
    27.      while(1){
    28.      
    29.           set     = Adc_Read(1);
    30.           Kp      = Adc_Read(2);
    31.           Ki      = Adc_Read(3);
    32.           Kd      = Adc_Read(4);
    33.           vout    = Adc_Read(0);
    34.           set     = (set/1024)*5;
    35.           Kp      = (Kp/1024)*100;
    36.           Ki      = (Ki/1024)*100;
    37.           Kd      = (Kd/1024)*100;
    38.           vout    = (vout/1024)*5;
    39.           error_1 = error;
    40.           error   = set - vout;
    41.           acc_err = acc_err + error;
    42.  
    43.           pwm     = pwm + (Kp*error) + (Kd*(error-error_1)/T) + (Ki*acc_err*T);
    44.  
    45.           if(pwm>=255){
    46.           pwm  = 255;
    47.           }
    48.           else if(pwm<0){
    49.           pwm = 0;
    50.           }
    51.  
    52.           Pwm_Change_Duty(pwm);
    53.  
    54.      }
    55. }
     
  2. tgil

    New Member

    May 18, 2011
    19
    4
    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.
     
    gehan_s likes this.
  3. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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?
     
    gehan_s likes this.
  4. gehan_s

    Thread Starter Member

    Sep 17, 2012
    38
    0
    Thank you both for replying !!!!!!!!!!

    I will try out the method. I am using mikroc V8.2

    Regards
     
  5. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    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.
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    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).
     
  7. MrChips

    Moderator

    Oct 2, 2009
    12,449
    3,363
    I simply toggle an output pin and monitor on a scope.
    I do this all the time when analyzing code for performance.
     
    Last edited: Mar 5, 2013
  8. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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.
     
  9. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    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.
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    Umm, are you saying that thatoneguy is a nobody?

     
  11. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    Is that what "code stopwatch" means? Sorry, I didn't recognize it by that name.
     
  12. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    Stopwatch? Why would we call it that?

    [​IMG]
     
  13. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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.
     
  14. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    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.
     
  15. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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.
     
  16. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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.
     
Loading...