Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PROGRAM PLC_PRG
- VAR CONSTANT
- wNumOutputRegisters : WORD := 10; (* Number of registers to write. *)
- wNumInputRegisters : WORD := 10; (* Number of registers to read. *)
- wNumRegisters : WORD := wNumOutputRegisters + wNumInputRegisters; (* Total number of registers. *)
- END_VAR
- VAR
- wState : WORD := 0; (* State variable for the state machine. *)
- wRegister : WORD := 1; (* Register pointer. *)
- sRegister : STRING; (* Register pointer string. *)
- rRegisterArray : ARRAY [0..wNumRegisters-1] OF REAL; (* Array containing all the registers. *)
- oRegisterReader : ReadRegisterMir; (* Instantiation of the register reader. *)
- rInputValue : REAL; (* Value obtained by the register reader. *)
- xReadDone : BOOL; (* Boolean indicating if the reading of the register is done. *)
- oRegisterWriter: WriteRegisterMir; (* Instantiation of the register writer. *)
- rOutputValue : REAL; (* Value to be writen by the register writer. *)
- xWriteDone : BOOL; (* Boolean indicating if the writing of the register is done. *)
- END_VAR
- (* Main state machine *)
- CASE wState OF
- 0: (* Initialization state *)
- wRegister := 1;
- wState := 1;
- 1: (* Check the register pointer. *)
- IF wRegister > wNumRegisters THEN (* Check if the register has overflown. *)
- wState := 0;
- ELSIF wRegister <= wNumInputRegisters THEN (* Check if it is a register to read. *)
- wState := 20;
- ELSIF wRegister > wNumInputRegisters THEN (* Check if it is a register to write. *)
- wState := 30;
- END_IF;
- sRegister := WORD_TO_STRING(wRegister); (* Turn the register pointer into a string. *)
- 20: (* Read register state. *)
- oRegisterReader(sRegister:= sRegister, rValue=> rInputValue, diError=> , diDone=> xReadDone);
- IF xReadDone THEN
- wState := 21;
- END_IF;
- 21: (* Store the read variable into the array. *)
- rRegisterArray[wRegister-1] := rInputValue;
- xReadDone := FALSE;
- wState := 22;
- 22: (* Change the status of the outputs depending on the register values read. *)
- (* Set output 0 true if register 0 is bigger than 0. *)
- IF rRegisterArray[0] > 0 THEN
- outBool_01 := TRUE;
- ELSE
- outBool_01 := FALSE;
- END_IF
- (* Set output 1 true if register 1 is bigger than 0. *)
- IF rRegisterArray[1] > 0 THEN
- outBool_02 := TRUE;
- ELSE
- outBool_02 := FALSE;
- END_IF
- wState := 40;
- 30: (* Read the status of the inputs and write the values on the internal registers. *)
- (* If input 0 is active set internal register 10 to 1, otherwise 0. *)
- IF inBool_01 THEN
- rRegisterArray[wNumInputRegisters] := 1;
- ELSE
- rRegisterArray[wNumInputRegisters] := 0;
- END_IF;
- (* If input 1 is active set internal register 11 to 1, otherwise 0. *)
- IF inBool_02 THEN
- rRegisterArray[wNumInputRegisters+1] := 1;
- ELSE
- rRegisterArray[wNumInputRegisters+1] := 0;
- END_IF;
- wState := 31;
- 31: (* Read the correponding internal register into the output variable. *)
- rOutputValue := rRegisterArray[wRegister-1];
- xWriteDone := FALSE;
- wState := 32;
- 32: (* Write the corresponding register. *)
- oRegisterWriter(sRegister:= sRegister, rValue:= rOutputValue, diError=> , diDone=> xWriteDone);
- IF xWriteDone THEN
- wState := 40;
- END_IF;
- 40: (* State available for the user to change. *)
- (* Write your own code here. *)
- wState := 50;
- 50: (* Increment the register pointer. *)
- wRegister := wRegister + 1;
- wState := 1;
- END_CASE;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement