2017-07-23 01:57 AM
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
2017-07-23 02:15 AM
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).
2017-07-23 04:38 AM
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
2017-07-23 08:52 AM
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.