CPU 313C Encoder Sampling: Configuring OB35 Interrupts

David Krause3 min read
S7-300SiemensTroubleshooting
Licensed PE Working through this on a live machine? A Maine-licensed engineer can take it from here — included with IMD hardware, by the hour for everything else. Book an engineer

A long cyclic scan can miss encoder-based sampling points when the position check runs only once per main program cycle.

Sampling-rate requirements

The maximum stated machine speed is 1° per 5 ms. A 5° movement therefore takes 25 ms at maximum speed. A 5 ms interrupt checks position five times during that interval; at lower speed, continue checking every 5 ms but save data only after the encoder crosses the next 5° threshold.

Evidence-based value Engineering consequence
4096 encoder positions per turn A 5° interval equals 4096 × 5 / 360 = 56.89 encoder counts, so exact equality to a fixed integer count cannot represent every 5° point.
1° per 5 ms maximum speed Each 5° sampling interval lasts 25 ms at maximum speed.
90° through 270° If both endpoints are stored, the turn produces (270 − 90) / 5 + 1 = 37 samples.
52 µs conversion per channel; two channels maximum Two stated conversions total 104 µs, excluding module transfer, interrupt, logic, and database-write time.

Configure OB35 for deterministic position checks

Configure OB35 as a 5 ms cyclic interrupt and call only the time-critical position and sampling routine from it. Keep chart transfer and other noncritical processing in the normal cyclic program. A constant interrupt interval does not mean every invocation must create a sample: encoder position remains the condition that authorizes storage.

  1. Read the current encoder position in OB35.
  2. Determine whether motion crossed the next 5° sampling boundary within the 90° to 270° acquisition window. Test for crossing rather than exact equality because 5° corresponds to a non-integer 56.89 encoder counts.
  3. When a boundary is crossed, read the required SM331 Fast analog channel or channels and store the result in the data block.
  4. Advance the next angular boundary by 5°. At the end of the turn, leave HMI transfer and chart handling to noncritical program execution.

Position interrupt versus timed polling

The evidence identifies an FM352 module when a hardware interrupt must originate directly from encoder position. The installed hardware instead includes an SM338 position module, so the supported approach described here is a timed OB35 interrupt that polls position. Treat any change to a true position-triggered architecture as a hardware decision rather than assuming the later CPU319 alone provides the same behavior.

Measure the existing cycle time in SIMATIC Manager while connected to the PLC by pressing Ctrl+D. Then verify that OB35 completes within its configured 5 ms interval under maximum program load. The 104 µs two-channel conversion total does not prove the complete routine meets 5 ms; measure the combined input access, boundary logic, database write, and interrupt execution.

Verification at operating speed

Run the machine at its maximum stated speed and confirm that every 5° boundary from 90° through 270° produces one stored record. If both endpoints are intended, verify 37 records per turn. Repeat at reduced speed: OB35 should continue running every 5 ms, while the time between stored samples increases because the encoder takes longer to travel 5°.

Also confirm that no boundary is duplicated and that the acquisition window resets correctly for the next turn. If records remain missing, compare the measured OB35 execution time and interrupt behavior against the 5 ms schedule before attributing the fault to the analog module.

FAQ

Use a 5 ms OB35 timed interrupt to check encoder position, then store analog data only when motion crosses the next 5° boundary. At the stated maximum speed, a 5° movement takes 25 ms.

Why should the PLC not compare the encoder count for exact equality?

A 4096-position encoder produces 4096 × 5 / 360 = 56.89 counts per 5°, which is not an integer. Detect that the position crossed the angular boundary instead of waiting for one exact count.

How do I check the STEP 7 program cycle time?

Connect to the PLC in SIMATIC Manager and press Ctrl+D. Separately verify that the sampling routine completes within the configured 5 ms OB35 interval.

Back to blog