Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

While loop execution time

Status
Not open for further replies.

gehan_s

Member
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:
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);

     }
}
 
If you're using the simulator, set a breakpoint on one of the instructions in the loop then use the stopwatch to time a single loop.

If you're trying it out in hardware, toggle a pin at the start of each loop. The frequency of the pin will be half the loop frequency.

As an aside, using floating point calculations on a PIC16F will be quite slow; you may wish to consider using fixed-point (i.e. integer math)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top