FUNCTION_BLOCK "FB_RTM"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_INPUT 
      xEnable : Bool;
      xClock : Bool;
   END_VAR

   VAR_IN_OUT 
      stRTM : "UDT_RTM";
   END_VAR


BEGIN
	REGION Betriebsstundenzähler / RTM
	    IF #xEnable THEN
	        
	        REGION Total-Counter
	            
	            #stRTM.stTotalCounter.nSecond := #stRTM.stTotalCounter.nSecond + BOOL_TO_INT(#xClock);
	            #stRTM.stTotalCounter.nMinute := #stRTM.stTotalCounter.nMinute + BOOL_TO_INT(#stRTM.stTotalCounter.nSecond >= 60);
	            #stRTM.stTotalCounter.nHour := #stRTM.stTotalCounter.nHour + BOOL_TO_DINT(#stRTM.stTotalCounter.nMinute >= 60);
	            
	            #stRTM.stTotalCounter.nSecond := #stRTM.stTotalCounter.nSecond MOD 60;
	            #stRTM.stTotalCounter.nMinute := #stRTM.stTotalCounter.nMinute MOD 60;
	            
	        END_REGION
	        
	        REGION Charge-Counter
	            
	            #stRTM.stChargeCounter.nSecond := #stRTM.stChargeCounter.nSecond + BOOL_TO_INT(#xClock);
	            #stRTM.stChargeCounter.nMinute := #stRTM.stChargeCounter.nMinute + BOOL_TO_INT(#stRTM.stChargeCounter.nSecond >= 60);
	            #stRTM.stChargeCounter.nHour := #stRTM.stChargeCounter.nHour + BOOL_TO_DINT(#stRTM.stChargeCounter.nMinute >= 60);
	            
	            #stRTM.stChargeCounter.nSecond := #stRTM.stChargeCounter.nSecond MOD 60;
	            #stRTM.stChargeCounter.nMinute := #stRTM.stChargeCounter.nMinute MOD 60;
	            
	        END_REGION
	        
	        REGION Maintenance-Counter
	            
	            #stRTM.stMaintenance.nSecond := #stRTM.stMaintenance.nSecond + BOOL_TO_INT(#xClock);
	            #stRTM.stMaintenance.nMinute := #stRTM.stMaintenance.nMinute + BOOL_TO_INT(#stRTM.stMaintenance.nSecond >= 60);
	            #stRTM.stMaintenance.nHour := #stRTM.stMaintenance.nHour - BOOL_TO_INT(#stRTM.stMaintenance.nMinute >= 60);
	            
	            #stRTM.stMaintenance.nSecond := #stRTM.stMaintenance.nSecond MOD 60;
	            #stRTM.stMaintenance.nMinute := #stRTM.stMaintenance.nMinute MOD 60;
	            
	        END_REGION
	        
	        REGION Zero
	            IF #stRTM.stMaintenance.nHour < 0 THEN
	                #stRTM.stMaintenance.nHour := 0;
	                #stRTM.stMaintenance.nMinute := 0;
	                #stRTM.stMaintenance.nSecond := 0;
	            END_IF;
	        END_REGION
	    END_IF;
	END_REGION
	
	REGION Reset
	    
	    REGION Reset Charge Counter
	        IF #stRTM.stFlags.xResetCharge THEN
	            #stRTM.stChargeCounter.nHour := 0;
	            #stRTM.stChargeCounter.nMinute := 0;
	            #stRTM.stChargeCounter.nSecond := 0;
	            #stRTM.stFlags.xResetCharge := FALSE;
	        END_IF;
	    END_REGION
	    
	    REGION Reset Maintenance Counter
	        IF #stRTM.stFlags.xResetMaintenance THEN
	            #stRTM.stMaintenance.nHour := #stRTM.stMaintenance.nIntervall;
	            #stRTM.stMaintenance.nMinute := 0;
	            #stRTM.stMaintenance.nSecond := 0;
	            #stRTM.stFlags.xResetMaintenance := FALSE;
	        END_IF;
	    END_REGION
	  
	END_REGION
	
	REGION Limit values
	    
	    #stRTM.stMaintenance.nWarning := MIN(IN1:= #stRTM.stMaintenance.nWarning, IN2:= #stRTM.stMaintenance.nIntervall);
	    IF #stRTM.stMaintenance.nHour > #stRTM.stMaintenance.nIntervall THEN
	        #stRTM.stMaintenance.nHour := #stRTM.stMaintenance.nIntervall;
	        #stRTM.stMaintenance.nMinute := 0;
	        #stRTM.stMaintenance.nSecond := 0;
	    END_IF;
	    
	END_REGION
	
	REGION Status
	    
	    #stRTM.stFlags.xMaintenance := #stRTM.stMaintenance.nHour <= 0 AND #stRTM.stMaintenance.nIntervall > 0;
	    
	    #stRTM.stFlags.xWarning := #stRTM.stMaintenance.nHour <= #stRTM.stMaintenance.nWarning AND #stRTM.stMaintenance.nWarning > 0;
	    #stRTM.stFlags.xWarning := #stRTM.stFlags.xWarning AND NOT #stRTM.stFlags.xMaintenance;
	    
	    #stRTM.stFlags.nState := SEL(G := #stRTM.stFlags.xWarning, IN0 := 0, IN1 := 1);
	    #stRTM.stFlags.nState := SEL(G := #stRTM.stFlags.xMaintenance, IN0 := #stRTM.stFlags.nState, IN1 := 2);
	END_REGION
END_FUNCTION_BLOCK

