You are on page 1of 5

S7-200 Tip Real Time Clock Tip No.

SIMATIC
S7-200 Tips
Group Topic
2 How to Read and Write to the Real Time Clock of the S7-200
CPU 214

Overview
This program example deals with two special operations for the real time clock: reading and
writing the date and clock time. For these operations, the program needs an 8 byte buffer with
the following structure:

Byte 0: Year (00 - 99) Byte 4: Minute (00 - 59)


Byte 1: Month (1 - 12) Byte 5: Second (00 - 59)
Byte 2: Day (1 - 31) Byte 6: Not assigned
Byte 3: Hour (00 - 24) Byte 7: Day of week (1-7 = Su-Sa)

The data is stored as a BCD-Code (for reading or writing). When operating the switch on I0.0,
the preset date is written into the real time clock. The switch on I0.0 displays the current second
value, which is copied to the output byte QB0. Coding can be selected in BCD format (I0.1 = '1')
or binary coded (I0.1 = '0').

I0.1 = ON: Second Display in BCD Format I0.0 => Set Date: 02/14/95 and
I0.1 = OFF: Second Display Binary Clock Time: 12:00:00

95 VB400 Year
02 VB401 Month
14 VB402 Day
12 VB403 Hour
00 VB404 Minute
00 VB405 Second
00 VB406
03 VB407 Day of week
I0.1 I0.2

 
24 V DC
Figure 8.1

Copyright 1996 by SIEMENS page 1 / 5 S72_08e.doc


Status: 10/96 Version 3.2
SIMATIC S7-200 customers have free use of the application tips. These tips are only a general approach to using the S7-200 with various applications.
Your specific application may be different. It is your responsibility to use the SIMATIC S7-200 properly in your applications.
S7-200 Tip Real Time Clock Tip No. 8
Program Structure

NO Set Date and


Time
?

Yes

Call of
Subroutine 0:
Set Time

Yes No

Copyright 1996 by SIEMENS page 2 / 5 S72_08e.doc


Status: 10/96 Version 3.2
SIMATIC S7-200 customers have free use of the application tips. These tips are only a general approach to using the S7-200 with various applications.
Your specific application may be different. It is your responsibility to use the SIMATIC S7-200 properly in your applications.
S7-200 Tip Real Time Clock Tip No. 8

Program Description
When the switch on Input I0.0 is activated, Subroutine 0 is called. This subroutine initializes the
8 bytes (VB100 to VB107) according to the desired date and time setting. This setting is then
transferred to the real time clock with the instruction 'TODW' .

The real time clock is read out in every cycle. This data is stored in BCD format
(4 bits result in a number between 0 and 9) in the 8 bytes from VB400 to VB407. If the Input I0.1
is set, this value is copied directly to the output byte.

If input I0.1 is not set, the data word VW404 is copied to data word VW204, and the high byte
VB204, which contains the minute value, is cleared. This step is necessary since the
subsequent conversion of the remaining second value from BCD format to binary can only be
carried out word by word. The now binary-coded second value is moved to output byte QB0.

The size of the program is 46 words.

More information about the Real Time Clock is provided in Chapter 5.7 “Real-time Clock
Instructions” of the S7-200 Programming Reference Manual. Detailed data about converting
numbers is contained in Chapter 5.5, “Conversion Instructions”.

LAD (S7-MicroDOS) STL (IEC)


Main Program
// TITLE =Real Time Clock

·, LD I0.0 // Write key Real Time Clock


¼¶½¼¶¶¶½3¼¶¶¶¶¶¶¶¶¶¶¶¶¶¶ &$// EU
· CALL 0 // Calling subroutine 0
·605($'B57&¹
¼¶½¼¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶½(1· LD SM0.0 // Set Top of Stack
··· TODR VB400
·9%½7· // Read data of Real Time
··· // Clock and file in 8 byte
·º¶¶¶¶¶¶¶» // buffer
·,029B%¶¶¶¹
¼¶½¼¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶½(1· LD I0.1
// Switch for second display
··· // in BCD format
·9%½,1287¼4% MOVB VB405, QB0
··· // Copy current second value
·º¶¶¶¶¶¶¶» // to output byte
·,029B:¶¶¶¹
¼¶½¼¶¶¶¶¶¶¶¶¶¶¶¾¶¶¶¶¶¶¶¶¶¶½(1· LDN I0.1
···· MOVW VW404, VW204
··9:½,1287¼9: // Recopy word value
····
··º¶¶¶¶¶¶¶»
··

Copyright 1996 by SIEMENS page 3 / 5 S72_08e.doc


Status: 10/96 Version 3.2
SIMATIC S7-200 customers have free use of the application tips. These tips are only a general approach to using the S7-200 with various applications.
Your specific application may be different. It is your responsibility to use the SIMATIC S7-200 properly in your applications.
S7-200 Tip Real Time Clock Tip No. 8

··
··029B%¶¶¶¹ MOVB 0, VB204 // Clear high byte (minutes)
·¼¶¶¶¶¶¶¶¶¶¶½(1·
····
··.½,1287¼9%
····
··º¶¶¶¶¶¶¶»
··%&'B,¶¶¶¹ BCDI VW204
·¼¶¶¶¶¶¶¶¶¶¶½(1· // Conversion BCD ⇒ Binary
····
··9:½,1287¼9:
····
··º¶¶¶¶¶¶¶»
··029B%¶¶¶¹ MOVB VB205, QB0
·º¶¶¶¶¶¶¶¶¶¶½(1· // Copy current second
··· // value to output byte
·9%½,1287¼4%
···
·º¶¶¶¶¶¶¶»
·
¼¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶ 0(1' MEND
·

Subroutines
¸¶¶¶¶¶¶¶¶¶¶¹
·6%5· SBR 0 // Set date and time
º¶¶¶¾¶¶¶¶¶¶»
·60029B%¶¶¶¹ LD SM0.0 // Always On Bit
¼¶½¼¶¶¶¶¶¶¶¶¶¶¶¾¶¶¶¶¶¶¶¶¶¶½(1· MOVB 16#95, VB100 // Year: 95
····
··.+½,1287¼9%
····
··º¶¶¶¶¶¶¶»
··029B%¶¶¶¹
·¼¶¶¶¶¶¶¶¶¶¶½(1· MOVB 16#02, VB101 // Month: February
····
··.+½,1287¼9%
····
··º¶¶¶¶¶¶¶»
··029B%¶¶¶¹
·¼¶¶¶¶¶¶¶¶¶¶½(1· MOVB 16#14, VB102 // Day: 14
····
··.+½,1287¼9%
····
··º¶¶¶¶¶¶¶»
··029B%¶¶¶¹
·¼¶¶¶¶¶¶¶¶¶¶½(1· MOVB 16#12, VB103 // Hour: 12
····
··.+½,1287¼9%
····
··º¶¶¶¶¶¶¶»
··029B%¶¶¶¹
·¼¶¶¶¶¶¶¶¶¶¶½(1· MOVB 16#0, VB104 // Minute: 00
····
··.+½,1287¼9%
····
··º¶¶¶¶¶¶¶»
··029B%¶¶¶¹
·¼¶¶¶¶¶¶¶¶¶¶½(1· MOVB 16#0, VB105 // Second: 00
····
··.+½,1287¼9%
····
··º¶¶¶¶¶¶¶»
··

Copyright 1996 by SIEMENS page 4 / 5 S72_08e.doc


Status: 10/96 Version 3.2
SIMATIC S7-200 customers have free use of the application tips. These tips are only a general approach to using the S7-200 with various applications.
Your specific application may be different. It is your responsibility to use the SIMATIC S7-200 properly in your applications.
S7-200 Tip Real Time Clock Tip No. 8

··
··029B%¶¶¶¹ MOVB 16#0, VB106 // Not assigned
·¼¶¶¶¶¶¶¶¶¶¶½(1·
····
··.+½,1287¼9%
····
··º¶¶¶¶¶¶¶»
··029B%¶¶¶¹ MOVB 16#3, VB107 // Day of week: Tuesday
·¼¶¶¶¶¶¶¶¶¶¶½(1·
····
··.+½,1287¼9%
····
··º¶¶¶¶¶¶¶»
··6(7B57&¶¹ TODW VB100 // Write clock time
·º¶¶¶¶¶¶¶¶¶¶½(1·
···
·9%½7·
···
·º¶¶¶¶¶¶¶»
·
·
¼¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶ 5(7 RET // End of Subroutine

Conversion Notes
To Convert from IEC STL to S7-Micro/DOS STL:
z Add a ‘K’ before all non-Hex numerical constants (i.e. 4 ⇒ K4)
z Replace ‘16#’ with ‘KH’ for all Hex constants (i.e. 16#FF ⇒ KHFF)
z Commas denote field divisions. Use arrow or TAB keys to toggle between fields.
z To convert an S7-Micro/DOS STL program to LAD form, every network must begin
with the word ‘NETWORK’ and a number. Each network in this Application Tip
program is designated by a number on the ladder diagram. Use the INSNW
command under the EDIT menu to enter a new network. The MEND, RET, RETI,
LBL, SBR, and INT commands each receive their own networks.
z Line-Comments denoted by ‘//’ are not possible with S7-Micro/DOS, but Network-
Comments are possible.

General Notes
The SIMATIC S7-200 Application Tips are provided to give users of the S7-200 some indication
as to how, from the view of programming technique, certain tasks can be solved with this
controller. These instructions do not purport to cover all details or variations in equipment, nor do
they provide for every possible contingency. Use of the S7-200 Application Tips is free.

Siemens reserves the right to make changes in specifications shown herein or make
improvements at any time without notice or obligation. It does not relieve the user of
responsibility to use sound practices in application, installation, operation, and maintenance of
the equipment purchased. Should a conflict arise between the general information contained in
this publication, the contents of drawings or supplementary material, or both, the latter shall take
precedence.

Siemens is not liable, for whatever legal reason, for damages or personal injury resulting from
the use of the application tips.

All rights reserved. Any form of duplication or distribution, including excerpts, is only permitted
with express authorization by SIEMENS.

Copyright 1996 by SIEMENS page 5 / 5 S72_08e.doc


Status: 10/96 Version 3.2
SIMATIC S7-200 customers have free use of the application tips. These tips are only a general approach to using the S7-200 with various applications.
Your specific application may be different. It is your responsibility to use the SIMATIC S7-200 properly in your applications.

You might also like