Skip to main content
Ahmed Zahran
Associate II
July 23, 2017
Question

big execution time

  • July 23, 2017
  • 3 replies
  • 1077 views
Posted on July 23, 2017 at 10:57

Hi!

when I tried to run the code below at an interrupt it took 123u sec! (I measured that by toggle PGIOB7) which is too big for me . any one can advice?

I am using STM32F429 running at 70MHz (I can't increase the clk frequency for some hardware limitations)

HAL_GPIO_WritePin (GPIOB, GPIO_PIN_7,GPIO_PIN_SET);

//HAL_GPIO_TogglePin (GPIOB, GPIO_PIN_7);

D101Value=((ADC1_ConvertedValue[0]*3.3/ADC1_Full_Scale)-0)*CURRENT_SENSOR_SCALE;

D102Value=((ADC1_ConvertedValue[0]*3.3/ADC1_Full_Scale)-0)*CURRENT_SENSOR_SCALE;

D103Value=((ADC1_ConvertedValue[0]*3.3/ADC1_Full_Scale)-0)*CURRENT_SENSOR_SCALE;

D104Value=((ADC1_ConvertedValue[0]*3.3/ADC1_Full_Scale)-0)*VOLTAGE_SENSOR_SCALE;

D105Value=((ADC1_ConvertedValue[0]*3.3/ADC1_Full_Scale)-0)*TEMP_SENSOR_SCALE1;

D106Value=((ADC1_ConvertedValue[0]*3.3/ADC1_Full_Scale)-0)*TEMP_SENSOR_SCALE2;

D107Value=((ADC1_ConvertedValue[0]*3.3/ADC1_Full_Scale)-0)*PRESSURE_SENSOR_SCALE;

HAL_GPIO_WritePin (GPIOB, GPIO_PIN_7,GPIO_PIN_RESET);

I made another trial by replacing 

ADC1_ConvertedValue[0] with constant value (eg 50) to be sure if the problem from the calculation time or what. I wondered from the result where execution of (

D101Value=((50*3.3/ADC1_Full_Scale)-0)*CURRENT_SENSOR_SCALE;) took only 0.25u sec instead of 20u sec previously !!! so the long time should be spend in calling variable  

ADC1_ConvertedValue[0]

  from the memory and really I don't have explanation for that

    This topic has been closed for replies.

    3 replies

    Vangelis Fortounas
    Associate II
    July 23, 2017
    Posted on July 23, 2017 at 11:15

    hello!

    D101Value=((ADC1_ConvertedValue[0]*3.3/ADC1_Full_Scale)-0)*CURRENT_SENSOR_SCALE;

    is equal to D101Value=(ADC1_ConvertedValue[0]*3.3/ADC1_Full_Scale)*CURRENT_SENSOR_SCALE;

    is equal to D101Value=ADC1_ConvertedValue[0] * CURRENT_SENSOR_SCALE*3.3/ADC1_Full_Scale;

    CURRENT_SENSOR_SCALE*3.3/ADC1_Full_Scale, is a constant.  Use it as a constant K

    So D101Value=ADC1_ConvertedValue[0] *K;      (k is calculated outside the loop)

    Other methods

    1. Don't  make the conversions  inside the interrupt .

    2. Make the conversions outside the loop

    3.Don't use Floating Point variables (especialy when your FP HW is disabled).

    Ahmed Zahran
    Associate II
    July 23, 2017
    Posted on July 23, 2017 at 11:38

    thanks a lot for your quick replay

    '0' in the equation will be replaced by offset value later so I can't remove it. Also I need to make the conversion inside the interrupt for another purposes related to the code functionality.

    I made another trial by replacing 

    ADC1_ConvertedValue[0] with constant value (eg 50) to be sure if the problem from the calculation time or what. I wondered from the result where execution of (

    D101Value=((50*3.3/ADC1_Full_Scale)-0)*CURRENT_SENSOR_SCALE;) took only 0.25u sec instead of 20u sec previously !!! so the long time should be spend in calling variable  

    ADC1_ConvertedValue[0]

      from the memory and really I don't have explanation for that

    Tesla DeLorean
    Guru
    July 23, 2017
    Posted on July 23, 2017 at 15:52

    So look at the generated code assembler.

    Constants might be folded by the compiler.

    The math may also be folded by repetitive use of 

    ADC1_ConvertedValue[0]

     instead of values unique to each ADC sample.

    Consider using integer math.

    Avoid division, multiply by a scaling reciprocal.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..