MP STUDIO 2 · Functions · V1.4.0 and later

MP STUDIO 2 · Bit Control Functions

The bit control functions are five functions (V1.4.0 and later): FF, BSFT, BRST and BSETN, which toggle bits, shift them one place at a time, or turn a range of them off or on at once, and the timer TFLK, which blinks with separately set ON and OFF times. Circuits that used to take several rungs, such as turning an output on and off with a single push button, tracking the position of products and rejects on a conveyor, or blinking a beacon, can be written with a single function.

View examples How to insert

The whole MP STUDIO 2 window while an example program runs in simulation, with the Bit Logic category of the Functions panel and the values of the FF, BSFT and TFLK blocks shown

Functions Categories Execution Version
5 Bit Logic, Timer Edge, level, cycle V1.4.0 and later

On this page

Finding and Inserting the Functions

FF, BSFT, BRST and BSETN are in the Bit Logic category of the Functions panel, and TFLK is in the Timer category. You insert them the same way as other functions: double-click a row in the Functions panel, or type the whole instruction on one line in the Insert Function window (Ctrl+E), as in FF P32. You can also find them by typing bit, flip, shift or flicker in the search box of the Functions panel.

Function Name in the Functions panel Block cells (from the left)
FF FF: Flip-Flop (Toggle Bit) OUT
BSFT BSFT: Bit Shift Register BIT, N, IN, DIR
BRST BRST: Reset Bit Range BIT, N
BSETN BSETN: Set Bit Range BIT, N
TFLK TFLK: Flicker Timer (ON/OFF) TIMER, ON, OFF

Figure 1. The Functions panel with the Bit Logic and Timer categories expanded

In the Insert Function window, the argument order of TFLK is shown as [TFLK] [IN] [ON] [OFF], and the first argument (the timer address) goes into the TIMER cell of the block.

Figure 2. The Insert Function window with TFLK T5 20 180 entered, showing the argument order and description

Common Behavior

All five functions run on the condition to the left of the block. When they act depends on the function.

Function When it acts
FF, BSFT Once, in the scan in which the condition goes from OFF to ON (the same timing as a rising edge contact)
BRST, BSETN In every scan in which the condition is ON
TFLK Repeats the ON time and the OFF time while the condition is ON

FF Flip-Flop

FF toggles the OUT bit (0 becomes 1, and 1 becomes 0) only in the scan in which the condition goes from OFF to ON. It does not toggle again while the condition stays ON. For example, FF P32 after a P0 contact turns P32 on and off each time P0 is pressed.

Figure 3. FF turning P32 on after P0 was turned on in simulation

A coil on the same bit overwrites the FF result
If you also use the OUT bit of FF in a normal coil, the coil writes that bit again in every scan, so the value that FF toggled does not stay. Like the bit of a SET coil, the OUT bit is not covered by the duplicate coil check, so the check does not report it either. Do not use the OUT bit of FF in a normal coil as well.

BSFT Bit Shift Register

Each time the condition goes from OFF to ON, BSFT shifts the N bits from BIT by one place and puts the value of IN into the place that becomes empty. DIR sets the direction. Use it in circuits that move values along with a position, such as tracking and ejecting rejects on a conveyor, sequential lighting and process history.

DIR Shift direction Where IN goes Value shifted out and lost
0 Toward higher numbers BIT The value of BIT+N-1
1 Toward lower numbers BIT+N-1 The value of BIT

For example, each time the condition turns ON, BSFT M200 8 P2 0 moves the values of M200 to M206 up by one place (to M201 to M207) and puts the value of P2 at that moment into M200. The value that was in M207 is lost.

If IN or DIR is a bit, its value in the scan in which the condition turned ON is read.

Figure 4. BSFT in simulation, with the reject detection results shifted into M200 to M207

BRST and BSETN Reset and Set Bit Range

BRST writes 0 to all N bits from BIT, and BSETN writes 1 to all of them. They write in every scan in which the condition is ON, and writing the same value again and again does no harm. BIT and N take the same values as in BSFT.

Example Action
BRST M200 8 Turns off M200 to M207.
BSETN D20.0 8 Turns on bits 0 to 7 of D20.
BRST M0 D0 Turns off as many bits from M0 as the value of D0.

TFLK Flicker Timer

When the condition turns ON, TFLK starts with the output on and repeats the cycle: on for the ON time, then off for the OFF time. The output is the power that continues to the right of the block, and the contact of the timer in the TIMER cell (for example T5) turns on and off with the same value, so other rungs can use the same blink.

Figure 5. TFLK during its OFF time in simulation

ON and OFF use the same units as the set value (SET VALUE) of TON. A number alone is in units of 10 ms (20 is 0.2 seconds), and a value with a unit, such as 200MS, 2S, 1M or 1H, is that time. A value shorter than 10 ms, such as 5MS, is entered as 50 ms, 10 times longer, and an orange warning is shown on the cell. You can also use byte, word and double word addresses such as BD0, D0 and DD0.

In a project whose timer base is set to 1 ms, numbers are in units of 1 ms. In the Change timer base window that appears when you change the timer base, the ON and OFF values of TFLK written as numbers are counted as set values too, and if you choose Adjust values too (keep the same time), they are also changed to match the new base.

As with TON, the TIMER cell can be BT5 (byte), T5 (word) or DT5 (double word), and the size of the timer sets the longest possible cycle (ON time+OFF time). The timer number must not be the same as that of another timer function. If it is, “Timer memory cannot be used more than once.” is shown when you compile.

TIMER cell Longest cycle (timer base 10 ms) Longest cycle (timer base 1 ms)
BT 2.55 s 0.255 s
T 655.35 s 65.535 s
DT About 497 days About 49.7 days

Inputs That Do Not Compile

For the inputs below, the cell turns red and the program cannot be compiled. Select the cell to see the reason in the Inspector, or hover over it to see the reason in a tooltip.

Cell Inputs that do not compile
OUT of FF A system bit that the PLC sets in every scan, such as @BEGIN, a word address such as T0 or D10, or a comparison such as D0>3
BIT of BSFT, BRST and BSETN A #name user bit, an @ system bit, a T, C or D word address, or a comparison
N of BSFT, BRST and BSETN A constant of 0 or less, a count that runs past the end of the area (for example BSETN M2040 9 when the M area ends at M2047), or a double word address such as DD0
IN and DIR of BSFT A constant other than 0 or 1 (such as 2 or -1), or a byte or word address such as BD0 or D0
ON and OFF of TFLK A negative value, or constants whose sum does not fit in the timer (for example 40000 and 40000 when TIMER is T1)
TIMER of TFLK A timer narrower than the value in ON (for example T1 when ON is DD0 or 70000)

The cells of the five functions cannot take an indexed address (D100[D0]).

For example, if you enter TFLK T6 40000 40000, the OFF cell turns red and “The ON time plus the OFF time of TFLK (80000) is more than 65535, the largest value timer T6 can hold, so the program does not compile. Shorten the times or use a wider timer (DT).” is shown.

Figure 6. The OFF cell shown in red because the sum of ON and OFF does not fit in timer T6, and the reason in the Inspector

Out of Range and @IXERR

If you write an address such as D10 in N, the count is decided while the program runs. The end of the area is the size set in Data Memory for M and D. For P, it is the end of the whole P area, P55 or P127 depending on the model, not the last real I/O terminal. If, when the block runs, N is 0 or less or the N bits from BIT run past the end of their area, the PLC does the following.

If you write addresses in ON and OFF of TFLK and one cycle exceeds the maximum in the table above when the block runs, @IXERR turns on, and the output stays off with the current value held at 0.

@IXERR is the same bit that turns on when an indexed address goes out of range, and it does not turn off by itself. You turn it off the same way as for indexed addresses. In a program that does not use indexed addresses, the notice during monitoring and simulation (“An index went out of range — …”) appears only when @IXERR is on the ladder screen or in the Data Table. To catch a bit control function going out of range, put an @IXERR contact in the ladder.

Figure 7. The notice shown in a program that has @IXERR in the Data Table

Figure 8. @IXERR turned on in simulation because the count of BRST went out of range

Monitoring and Simulation

The five functions work the same in simulation as on the PLC.

Examples

You can write the three examples in order in a new MPA-8A8R(T) project (default data memory) and check them in simulation.

Example 1. Toggling a Lamp with a Single Push Button

In this example, each press of P0 turns the P32 lamp on or off.

  1. In Settings → Symbol, register LAMP_BTN for P0 and LAMP for P32.
  2. Write the rung below.
Rung Input Function What it does
0 P0 FF P32 Toggles P32 each time P0 is pressed.

Start Run → Simulation, and turn the P0 chip on the simulation bar on and off several times. Each time you turn P0 on, P32 switches between on and off (Figure 3), and it does not change while P0 stays on.

Example 2. Tracking Rejects on a Conveyor

In this example, each time the conveyor moves one place, the reject detection result is shifted into M200 to M207, and when a reject reaches the eject position at the eighth place (M207), it is ejected with P33.

  1. In Settings → Symbol, register MOVE_PULSE for P1, NG_SENSOR for P2, CLEAR_BTN for P3, TRACK for M200 and EJECT for P33.
  2. Write the rungs below after the rung of Example 1.
Rung Input Function or coil What it does
1 P1 BSFT M200 8 P2 0 Each time the conveyor moves one place, puts the value of P2 into M200 and shifts M200 to M207 by one place.
2 M207 Coil P33 Ejects the reject that has reached the eject position.
3 P3 BRST M200 8 Clears all tracking records.

During simulation, add M200 to M207 to the Data Table, and turn P1 on and off once with P2 on. Then turn P2 off and turn P1 on and off seven more times. The first reject reaches M207, and P33 turns on. Figure 4 shows the case in which the first and third products are rejects. Turning P3 on turns off all of M200 to M207, and because rung 2 comes before rung 3, P33 turns off in the next scan.

Example 3. Blinking a Beacon

In this example, while the alarm input P4 is ON, the beacon P34 repeatedly turns on for 0.2 seconds and off for 1.8 seconds (timer base 10 ms).

  1. In Settings → Symbol, register ALARM for P4 and BEACON for P34.
  2. Write the rungs below after the rungs of Example 2.
Rung Input Function or coil What it does
4 P4 TFLK T5 20 180 followed by coil P34 Turns on for 0.2 seconds and off for 1.8 seconds. TFLK T5 200MS 1800MS works the same way.
5 T5 Coil P35 Gives the same blink on P35.

During simulation, when you turn P4 on, P34 and P35 turn on for 0.2 seconds every 2 seconds, and the current value of T5 rises from 0 to 199 and then returns to 0 (Figure 5). When you turn P4 off, they turn off at once and T5 becomes 0.

Things to Check

Cannot be opened in V1.3.0 and earlier
A .mpx project that uses even one bit control function cannot be opened in V1.3.0 and earlier versions of the app.

← Back to the MP STUDIO 2 User Manual