SHARE
TWEET

Untitled

a guest Sep 10th, 2019 125 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. PROGRAM PLC_PRG
  2. VAR CONSTANT
  3.     wNumOutputRegisters : WORD := 10; (* Number of registers to write. *)
  4.     wNumInputRegisters : WORD := 10; (* Number of registers to read. *)
  5.     wNumRegisters : WORD := wNumOutputRegisters + wNumInputRegisters; (* Total number of registers. *)
  6. END_VAR
  7. VAR
  8.     wState : WORD := 0; (* State variable for the state machine. *)
  9.  
  10.     wRegister : WORD := 1; (* Register pointer. *)
  11.     sRegister : STRING; (* Register pointer string. *)
  12.    
  13.     rRegisterArray : ARRAY [0..wNumRegisters-1] OF REAL; (* Array containing all the registers. *)
  14.    
  15.     oRegisterReader : ReadRegisterMir; (* Instantiation of the register reader. *)
  16.     rInputValue : REAL; (* Value obtained by the register reader. *)
  17.     xReadDone : BOOL; (* Boolean indicating if the reading of the register is done. *)
  18.    
  19.     oRegisterWriter: WriteRegisterMir; (* Instantiation of the register writer. *)
  20.     rOutputValue : REAL; (* Value to be writen by the register writer. *)
  21.     xWriteDone : BOOL; (* Boolean indicating if the writing of the register is done. *)
  22. END_VAR
  23.  
  24.  
  25. (* Main state machine *)
  26. CASE wState OF
  27. 0: (* Initialization state *)
  28.     wRegister := 1;
  29.     wState := 1;
  30.    
  31. 1: (* Check the register pointer. *)
  32.     IF wRegister > wNumRegisters THEN (* Check if the register has overflown. *)
  33.         wState := 0;
  34.     ELSIF wRegister <= wNumInputRegisters THEN (* Check if it is a register to read. *)
  35.         wState := 20;
  36.     ELSIF wRegister > wNumInputRegisters THEN (* Check if it is a register to write. *)
  37.         wState := 30;
  38.     END_IF;
  39.     sRegister := WORD_TO_STRING(wRegister); (* Turn the register pointer into a string. *)
  40.    
  41. 20: (* Read register state. *)
  42.     oRegisterReader(sRegister:= sRegister, rValue=> rInputValue, diError=> , diDone=> xReadDone);
  43.     IF xReadDone THEN
  44.         wState := 21;
  45.     END_IF;
  46.    
  47. 21: (* Store the read variable into the array. *)
  48.     rRegisterArray[wRegister-1] := rInputValue;
  49.     xReadDone := FALSE;
  50.     wState := 22;
  51.  
  52. 22: (* Change the status of the outputs depending on the register values read. *)
  53.     (* Set output 0 true if register 0 is bigger than 0. *)
  54.     IF rRegisterArray[0] > 0 THEN
  55.         outBool_01 := TRUE;
  56.     ELSE
  57.         outBool_01 := FALSE;
  58.     END_IF
  59.    
  60.     (* Set output 1 true if register 1 is bigger than 0. *)
  61.     IF rRegisterArray[1] > 0 THEN
  62.         outBool_02 := TRUE;
  63.     ELSE
  64.         outBool_02 := FALSE;
  65.     END_IF
  66.    
  67.     wState := 40;
  68.  
  69. 30: (* Read the status of the inputs and write the values on the internal registers. *)
  70.     (* If input 0 is active set internal register 10 to 1, otherwise 0. *)
  71.     IF inBool_01 THEN
  72.         rRegisterArray[wNumInputRegisters] := 1;
  73.     ELSE
  74.         rRegisterArray[wNumInputRegisters] := 0;
  75.     END_IF;
  76.  
  77.     (* If input 1 is active set internal register 11 to 1, otherwise 0. *)
  78.     IF inBool_02 THEN
  79.         rRegisterArray[wNumInputRegisters+1] := 1;
  80.     ELSE
  81.         rRegisterArray[wNumInputRegisters+1] := 0;
  82.     END_IF;
  83.    
  84.     wState := 31;
  85.    
  86. 31: (* Read the correponding internal register into the output variable. *)
  87.     rOutputValue := rRegisterArray[wRegister-1];
  88.     xWriteDone := FALSE;
  89.     wState := 32;  
  90.    
  91. 32: (* Write the corresponding register. *)
  92.     oRegisterWriter(sRegister:= sRegister, rValue:= rOutputValue, diError=> , diDone=> xWriteDone);
  93.     IF xWriteDone THEN
  94.         wState := 40;
  95.     END_IF;
  96.  
  97. 40: (* State available for the user to change. *)
  98.     (* Write your own code here. *)
  99.     wState := 50;
  100.  
  101. 50: (* Increment the register pointer. *)
  102.     wRegister := wRegister + 1;
  103.     wState := 1;
  104.  
  105. END_CASE;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top