恭喜,你发布的帖子
发布于 2024-05-17 22:37:46
10楼
FUNCTION_BLOCK "FB_GetSumOfArithmeticProgression"
TITLE = FB_GetArithmeticProgressionSum
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
//求公差为1的等差数列之和
VAR_INPUT
"begin" { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : DInt;
last { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : DInt;
END_VAR
VAR_OUTPUT
error : Bool;
sum { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : DInt;
END_VAR
VAR
statOldBegin : DInt;
statOldLast : DInt;
statIndex : DInt;
statError : Bool;
statSum : DInt;
END_VAR
VAR_TEMP
tempBegin : DInt;
tempLast : DInt;
tempIndex : DInt;
tempSum : DInt;
END_VAR
VAR CONSTANT
INCREMENT : DInt := 16#1;
ZERO : DInt := 16#0;
END_VAR
BEGIN
REGION INITIALIZATION
#tempBegin := #begin;
#tempLast := #last;
#statOldBegin := #ZERO;
#statOldLast := #ZERO;
#statIndex := #ZERO;
#statError := FALSE;
#statSum := #ZERO;
END_REGION
REGION LOGIC & CONTROL
#statError := #tempBegin > #tempLast;
IF #statError = FALSE OR #tempBegin <> #statOldBegin OR #tempLast <> #statOldLast THEN //no error and setpoint has changed,then calculate the sum
IF #statIndex <> #tempLast THEN
FOR #tempIndex := #tempBegin TO #tempLast BY #INCREMENT DO
#statSum := #statSum + #tempIndex;
#statIndex := #tempIndex;
END_FOR;
END_IF;
#statOldBegin := #tempBegin;
#statOldLast := #tempLast;
END_IF;
END_REGION
REGION OUTPUTS
#error := #statError;
#sum := #statSum;
END_REGION
END_FUNCTION_BLOCK
请填写推广理由:
分享
只看
楼主