Set_CPU_DateTime

Overview

The Set_CPU_DateTime function block sets the current date and time of the PLC CPU real-time clock (RTC). The provided values are written directly to the CPU RTC when the function block is executed.

💡 Why would you set the CPU date/time?

At program download, the CPU RTC is initialized with the date and time from the PC. However, there may be cases where you want to update the CPU date/time after deployment, such as correcting for drift, setting a specific time for testing, or synchronizing with an external time source.

Month               :=  (USINT)
Day                 :=  (USINT)
Year                :=  (USINT)
Hours               :=  (USINT)
Minutes             :=  (USINT)
Seconds             :=  (USINT)
STS_Done            =>  (BOOL)
STS_Failed          =>  (BOOL)
STS_Fault_Code      =>  (USINT)

Interface

Inputs

NameTypeRange / UnitsDescription
MonthUSINT1 - 12The month to set (1-12).
DayUSINT1 - 31The day to set (1-31).
YearUSINT0 - 255The year to set (0-99).
HoursUSINT0 - 23The hour to set (0-23).
MinutesUSINT0 - 59The minute to set (0-59).
SecondsUSINT0 - 59The second to set (0-59).
💡 RTC Behavior

The CPU real-time clock (RTC) is initially set during program download using the PC’s system time. It continues running independently of the PLC program execution and is maintained by a backup battery when the PLC is powered off.

Outputs

NameTypeDescription
STS_DoneBOOLTRUE when all configuration data is within range.
STS_FailedBOOLTRUE if any configuration data is out of range.
STS_Fault_CodeUSINTDiagnostic code indicating the reason for failure.

Fault Codes

CodeMeaningDescription
96Config not OKAt least one configuration values is out of range.

Example:

PROGRAM PROG_Set_CPU_DateTime
    VAR
        CPU_Time_Set : Set_CPU_DateTime;
    END_VAR

    (* Example: Set date/time to 15 March 2025, 14:30:00 *)
    CPU_Time_Set(
        Month := 3,
        Day := 15,
        Year := 25,
        Hours := 14,
        Minutes := 30,
        Seconds := 0
    );
END_PROGRAM

CONFIGURATION Config0
    RESOURCE Res0 ON PLC
        TASK task1_100ms(INTERVAL := T#100ms, PRIORITY := 1);
        PROGRAM Prog_Instance_SetCPUDateTime WITH task1_100ms : PROG_Set_CPU_DateTime;
    END_RESOURCE
END_CONFIGURATION