Overview
In SIMATIC STEP 7 V5.5 (and the S7-300/S7-400 CPU families that ship with it), engineers frequently ask whether the runtime program can mint a brand new Instance Data Block (IDB) the moment a process condition becomes true. The field-tested answer is: the CPU exposes a system function that creates a shared (global) DB at runtime — SFC 22 "CREAT_DB" — but the strict "Instance of FBx" relationship can only be assigned at compile time, or when the source is imported through an STL source file. This reference documents both paths, the precise parameter set, the error codes that surface in the RET_VAL word, and the limits you will hit on real hardware.
The reference target is STEP 7 V5.5 SP4 (or later SP) and any S7-300/S7-400 CPU whose firmware supports the SFCs in the "Standard Library → System Function Blocks" container. For S7-1500 work, the same idea is implemented through the "Create DB" instruction in the TIA Portal; the runtime mechanism is fundamentally different and is documented separately in the S7-1500 system manual.
Instance DB vs. Global DB — Why the Distinction Matters
Before you call CREAT_DB, you need to understand what an Instance Data Block actually is in the STEP 7 programming model:
-
Instance DB (IDB): Memory that stores the static (STAT) interface variables of a single Function Block call. The offline editor binds the DB to a specific FB number, the FB symbol can be addressed symbolically inside the instance, and the symbol table is generated with the FB name as the instance qualifier (for example,
"Instance_DB".Motor_Speed). - Global DB (also called Shared DB): A standalone block of memory. All address references in the program go through the global symbol table, and any FB, FC, or OB can read or write its tags. There is no inherent binding to a particular FB.
At the byte level, a CPU-315-2 PN/DP treats them identically — a block header followed by a contiguous byte range. The "Instance" qualifier exists only in the offline block container and in the symbol table that STEP 7 generates. Consequently, the question "can I create an IDB from code?" is really two questions: can I create a block of memory with the right size at runtime (yes, via SFC 22), and can I make the editor and monitoring tools treat that memory as an instance of FBx from a PLC program (no — this requires a programming station).
DB 200.DBD 0).Method 1 — Runtime Generation with SFC 22 "CREAT_DB"
SFC 22 lives in the "Standard Library → System Function Blocks" program and is callable from any OB, FB, or FC. It allocates a free DB number in the work memory of the CPU and initializes the block header. Once created, the block survives a warm restart, because the system persists its existence in the module's block list — though its initial values are restored on unbuffered restart.
SFC 22 Interface
| Parameter | Declaration | Data Type | Description |
|---|---|---|---|
| DB_NUMBER | INPUT | INT | Number of the DB to create. Range 1–65535. If you pass 0, the CPU assigns the next free number and returns it in RET_VAL. |
| DB_LENGTH | INPUT | WORD | Length of the new DB in bytes. Must be > 0, even, ≤ 65534, and within the CPU's per-block ceiling. |
| RET_VAL | OUTPUT | INT | Error code. If DB_NUMBER was 0 on input, RET_VAL contains the assigned DB number on success. |
SFC 22 Error Codes
| RET_VAL (hex) | Meaning | Remediation |
|---|---|---|
| 0000 | No error; DB created (or assigned number returned). | — |
| 80A1 | DB number is already in use. | Re-issue a free number, or query with SFC 24 "TEST_DB" first. |
| 80A2 | DB_LENGTH invalid (≤ 0, odd, or larger than the CPU maximum). | Validate length in the calling logic; round up to even bytes. |
| 80A3 | No free DB number available in the work memory. | Compact the user memory (Menu → PLC → Compress) or reduce the count of dynamically created DBs. |
| 80A4 | Insufficient free work memory. | Stop the CPU, compress the project, and try again. |
| 80B1 | DB_NUMBER lies in the system reserved area (e.g., DB 0). | Use numbers ≥ 1 and outside the system area documented in the CPU manual. |
| 80B2 | System error — internal resource conflict. | Re-trigger the call, or evaluate the diagnostic buffer for OB121/OB122 with detailed event ID. |
Sample STL Call in OB1
// OB1 — STL call of SFC 22 "CREAT_DB"
L 200 // desired DB number
T MW 100
L 256 // 256 bytes (must be even)
T MW 104
CALL SFC 22
DB_NUMBER :=MW100
DB_LENGTH :=MW104
RET_VAL :=MW200
L MW 200 // capture return / assigned number
L 0
<>I
JC ALRM // branch to alarm block on error
OPN DB 200 // open the freshly created DB
L 0
T DBD 0 // initialize byte 0 with 0
ALRM: NOP 0
Sample Ladder (FBD) Call
// OB1 — Ladder (FBD) call of SFC 22
// [CREATE_DB]
// DB_NUMBER := MW100 // desired DB number (0 = auto-assign)
// DB_LENGTH := MW104 // length in bytes
// RET_VAL := MW200 // error or assigned number
// --( #DB_OK )---------------- // rung continues when RET_VAL = 0
Method 2 — Compile-Time Generation via STL Source Files
The "STL source file" route is the classic way to mass-produce blocks without clicking through the SIMATIC Manager GUI. An STL source is a plain-text file with .awl extension that contains the source representation of one or more blocks. When you compile the source, STEP 7 reconstructs the offline block container and assigns the FB/DB relationships on the programming station — which is precisely what the runtime CREAT_DB call cannot do.
Chapter 13 of the SIMATIC Programming with STEP 7 V5.5 manual walks through the entire source-file workflow. The general structure for an Instance Data Block is:
// STL source file (.awl) — Instance DB for FB1
DATA_BLOCK DB 20
TITLE = 'Instance of FB1 - Mixer'
AUTHOR : ENG
FAMILY : 'PROCESS'
KNOW_HOW_PROTECT
VERSION : 1.0
// Instance declaration matches FB1 STAT section
STRUCT
Motor_Speed : REAL ; // rpm
Heater_On : BOOL ;
Recipe_Count : INT ;
Pressure : REAL ;
END_STRUCT ;
END_DATA_BLOCK
To bind the block to FB1 explicitly (so the editor lists it as an instance and exposes FB1's symbol scope), the header is amended with the FB keyword:
DATA_BLOCK DB 20
FB 1
TITLE = 'Instance of FB1 - Mixer'
STRUCT
Motor_Speed : REAL ;
Heater_On : BOOL ;
Recipe_Count : INT ;
Pressure : REAL ;
END_STRUCT ;
END_DATA_BLOCK
To import the source, place the file in the S7 program's "Sources" folder, right-click → Compile, and STEP 7 will deposit the freshly built DB into the Blocks container ready for download. This is the only way to author an "Instance of FB" with editor-grade fidelity, and it works equally well from a script (e.g., Excel VBA exporting .awl lines) for batch DB generation.
Step-by-Step — Implementing SFC 22 in OB1
- Open the project in SIMATIC Manager. Navigate to your S7 program, then Blocks, and double-click OB1 to launch the LAD/FBD/STL editor.
-
Allocate working memory for the call. In the symbol table, define
DB_REQ_NO(WORD, address MW100) andDB_REQ_LEN(WORD, address MW104). Reserve MW200 for the return value. - Wire your trigger condition. In a network upstream of SFC 22, build a one-shot (POS edge) so the create call only runs once when the process condition is met — repeated calls will re-issue error 80A1.
- Insert SFC 22. From the program elements catalog, expand "Standard Library → System Function Blocks" and drag SFC 22 onto the network. Assign the parameter symbols as listed in the interface table above.
- Evaluate RET_VAL. Add an unwind step that branches on the error code (e.g., jumps to an alarm FB if MW200 ≠ 0). For DB_NUMBER = 0 on input, MW200 contains the assigned number; copy it into your recipe block for later UDT-style access.
-
Initialize the new DB. Open the new block with
OPN DBand useTinstructions to set up the field defaults. The block's initial values from the offline project are not applied — SFC 22 produces an empty block. - Save, compile, and download. Use "PLC → Download" to push OB1 to the target. Trigger the condition in the running PLC and verify the new DB appears in the online block list ("PLC → Accessible Nodes → Blocks").
Step-by-Step — Generating an IDB from an STL Source File
- Open the "Sources" folder. In SIMATIC Manager, expand your S7 program and select the Sources container. From the menu choose Insert → S7 Software → STL Source.
-
Name the source. Use a descriptive name (e.g.,
Mixer_Recipe_Sources). Double-click to open the source editor. - Type the block definition. Use the DATA_BLOCK / FB syntax shown in Method 2. Pay attention to the closing semicolons and END_DATA_BLOCK — the parser is unforgiving.
- Save the source. File → Save (Ctrl+S). Do not close the editor yet.
- Compile the source. Source → Compile, or right-click in the Sources folder and select Compile. STEP 7 reports any syntax errors in a dialog; double-click each entry to navigate.
-
Verify the DB in the Blocks container. Open the Blocks folder. The new DB should appear with the icon for an instance block (a stacked-file glyph) when bound with
FB 1. - Download the block. Select the DB, then PLC → Download, or drag it from the offline Blocks to the online target.
Decision Flow — SFC 22 Runtime Creation
Limitations and Field-Proven Caveats
- IDB binding is offline-only. An IDB created at runtime through SFC 22 will be listed in the online "Accessible Nodes" view as a shared (global) DB. Symbolic FB-instance access will not resolve unless you pre-allocate the symbol with a placeholder DB number that matches what the runtime eventually uses — and even then, the FB-instance view in STEP 7 cannot be regenerated online.
-
Work memory vs. load memory. SFC 22 creates the block in work memory only. If the CPU performs an unbuffered power-on restart, the DB is regenerated and its contents revert to zero. To persist data, write it to a retentive area or use a flash card with
SAVEin OB121/OB122 handling. - Block count ceiling. S7-300 CPUs cap the number of blocks at the lower of (a) the configured maximum in the CPU properties and (b) work-memory availability. A CPU 314C-2 PN/DP, for example, defaults to 1024 blocks; you can raise it in the hardware configuration up to the CPU-specific ceiling (commonly 256–2048 FBs/DBs/FCs combined).
-
DB_NUMBER = 0 and race conditions. Passing 0 to obtain an auto-assigned number is fine, but you must immediately capture the RET_VAL before any other code opens a DB. The
OPN DBinstruction will overwrite the address register, and the next block open will re-bind the AR — a frequent cause of mystery data corruption in dynamic recipes. - Watchdog exposure. SFC 22 execution time scales with the requested length. A 60 KB block on an S7-315 can push the OB1 cycle time by 5–8 ms. Always benchmark in OB35 (cyclic interrupt) rather than OB1 to keep the main scan deterministic.
- Online block delete before recreate. If your process requires replacing an existing DB with a new length, call SFC 23 "DEL_DB" first. SFC 22 will reject the call with 80A1 otherwise.
-
Know-how protection on the source. The
KNOW_HOW_PROTECTkeyword in the STL source protects the compiled block from being opened in the editor, but it does not obscure the source file itself. If you need the source to remain secret, compile the STL on the engineering station and distribute only the compiled.s7pblocks.
Verification
After either method, run the following checks:
- Online block visibility. PLC → Accessible Nodes → select the target CPU → Blocks. The new DB number must appear in the list, with a "work memory" date stamp matching the trigger event.
- Symbol resolution. Open a VAT (Variable Table) and force a write to a symbol inside the new DB. If the symbol resolves, the block is in work memory and the symbol table is consistent.
- Cross-reference. In the SIMATIC Manager, right-click the new DB → Cross-References. The list must show the FB call sites that reference it (only for true IDBs created via the source-file method).
- Diagnostic buffer. PG → PLC → Diagnostic Buffer. Look for OB121/OB122 events around the time of creation — those indicate size or address violations that RET_VAL alone may have masked.
- Cycle time trend. Add a watch on OB1's local "scan time" tags before and after the CREAT_DB call. A delta greater than your worst-case budget means the call should move to a slower OB (OB35 / OB82).
Troubleshooting Matrix
| Symptom | Most Likely Cause | Fix |
|---|---|---|
| RET_VAL = 80A1, block does not appear | DB number is already in use (or the SFC 22 call repeats unintentionally each scan). | Edge-trigger the call; query with SFC 24 "TEST_DB" first. |
| RET_VAL = 80A2 on a long block | DB_LENGTH exceeds the CPU's per-block maximum (16 KB on most S7-315, 64 KB on S7-417). | Check the CPU manual; chunk the data into multiple DBs. |
| New block exists but reads zeros after restart | Unbuffered restart cleared work memory; SFC 22 result is not retained. | Re-run the trigger after restart, or move the data to a retentive area. |
| Symbol "FB1.MyVar" shows "DB not loaded" | Runtime-created DB was treated as a global block, not an instance. | Use the STL source file path for true instance binding, then re-download. |
| STL source compile reports syntax error at "FB 1" | Indentation or missing semicolon before the FB keyword. | Match the formatting in the reference example; consult the STEP 7 V5.5 programming manual ch. 13. |
| Cycle time jumps after CREAT_DB | Length too large for OB1 priority class. | Move the call into OB35 or OB82 (time-of-day / interrupt) so the OB1 deadline is preserved. |
| RET_VAL = 80B1 immediately after Power-On | CPU reserves low DB numbers (often 0–5) for system use. | Use DB numbers ≥ 6 unless the CPU manual explicitly clears the area. |
| Compiler accepts source but download fails with "Block is inconsistent" | FB referenced by the IDB is not loaded on the target. | Download the FB first (or both blocks together) so the IDB has its parent in the online block list. |
Cross-References and Companion SFCs
| SFC | Name | Use with SFC 22 |
|---|---|---|
| SFC 23 | DEL_DB | Delete a runtime-created DB before re-creating it with a new length. |
| SFC 24 | TEST_DB | Query the size and existence of a DB without opening it. |
| SFC 25 | COMPRESS | Compact user memory after many create/delete cycles to recover gaps. |
| SFC 26 | UPDAT_PI / UPDAT_PO | Refresh process-image partitions referenced by the new DB before reads. |
| SFB 0 / SFB 1 | CTU / CTD (counter background) | Not related, but often hosted inside an instance DB for fast recipe counters. |
For the canonical reference of SFC 22, SFC 23, and SFC 24 interface definitions, see the S7-300/400 System and Standard Functions reference manual. For source-file syntax, the SIMATIC Programming with STEP 7 V5.5 manual is the authoritative source.
Frequently Asked Questions
Can SFC 22 truly create an Instance Data Block in STEP 7 V5.5?
No. SFC 22 "CREAT_DB" allocates a shared (global) DB at runtime. To produce a true Instance Data Block bound to an FB symbol scope, you must use the offline STL source file method (or a manual DB insert in SIMATIC Manager), then download the block.
What is the maximum DB size I can request with SFC 22?
DB_LENGTH is a WORD, so the absolute ceiling is 65534 bytes. The CPU-specific ceiling is usually lower: an S7-315 accepts up to 16 KB per block; an S7-417 can accept 64 KB. Always validate the CPU-specific ceiling from the device manual before requesting the length.
Why does my newly created DB reset to zero after a power cycle?
SFC 22 writes the block header to work memory only. An unbuffered restart clears work memory and regenerates the DB with the offline initial values (zeros if none were set). For persistence, use retentive tags or back the DB with a flash card SAVE routine.
How do I get the auto-assigned DB number back from SFC 22?
Pass 0 in DB_NUMBER. On a successful return (RET_VAL = 0), the SFC writes the assigned number into RET_VAL. Read RET_VAL immediately and copy it into a local tag before issuing any other block-open instruction.
Can the same STL source generate both global and instance DBs?
Yes. The FB <n> keyword in the DATA_BLOCK header binds the DB to the named FB. Omitting the FB keyword leaves it as a global DB. The same .awl file can contain multiple DATA_BLOCK sections of either type, separated by a blank line.
Does SFC 22 work on every S7-300 / S7-400 CPU?
SFC 22 is in the Standard Library and is supported on every S7-300 and S7-400 CPU that runs STEP 7 V5.x firmware. The maximum DB length, however, varies by CPU model — consult the device manual for the exact per-block ceiling and total block-count maximum.