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
| Name | Type | Range / Units | Description |
|---|---|---|---|
| Month | USINT | 1 - 12 | The month to set (1-12). |
| Day | USINT | 1 - 31 | The day to set (1-31). |
| Year | USINT | 0 - 255 | The year to set (0-99). |
| Hours | USINT | 0 - 23 | The hour to set (0-23). |
| Minutes | USINT | 0 - 59 | The minute to set (0-59). |
| Seconds | USINT | 0 - 59 | The 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
| Name | Type | Description |
|---|---|---|
| STS_Done | BOOL | TRUE when all configuration data is within range. |
| STS_Failed | BOOL | TRUE if any configuration data is out of range. |
| STS_Fault_Code | USINT | Diagnostic code indicating the reason for failure. |
Fault Codes
| Code | Meaning | Description |
|---|---|---|
| 96 | Config not OK | At 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