PLUS+1 Tools > PLUS+1 GUIDE

ST coding and Initial Value of Var

(1/1)

federico.furlani:
Hi everybody,
I'm struggling with using the ST code on Guide. In particular, my problem of the day is to initialize a retain Var.
How can I access the 5 as Initial Value?

Thanks

FluidPowerTom:
I do this programmatically by having another variable function as a sort of default-setting flag:

FUNCTION_BLOCK BrakeLogic
VAR_INPUT
  BrakeTimerSetting2 : USINT;
  SetDefault : BOOL;
  SaveChanges : BOOL;
END_VAR
VAR_OUTPUT
  BrakeTimerSetting : USINT;
END_VAR
VAR
  startup : BOOL;
END_VAR
VAR RETAIN
  BrakeTimer : USINT;
  NVMem_Init : UDINT;
END_VAR

-----------------------------------------------------------------


IF SetDefault OR NVMem_Init<>666666 THEN
  BrakeTimerSetting:=0;
  BrakeTimer:=0;
  NVMem_Init:=666666;
END_IF

IF NVMem_Init=666666 AND NOT startup THEN
  BrakeTimerSetting:=BrakeTimer;
  startup:=1;
END_IF

IF SaveChanges AND NVMem_Init=666666 THEN
  BrakeTimer:=BrakeTimerSetting2;
  BrakeTimerSetting:=BrakeTimerSetting2;
END_IF


EDIT:  As you'll see in this case I'm using a variable named NVMem_Init for that, and we initialize the BrakeTimer as 0.

Navigation

[0] Message Index

Go to full version