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. [[#examples|View examples]] [[#finding_and_inserting_the_functions|How to insert]] {{ :en-us_products:mp_studio:bf-hero.png?1600 |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 | * **Toggles on each press**\\ ''FF'' toggles its bit only at the moment the condition turns ON, so you can connect it directly after a push button contact without a rising edge contact. * **Shifts one place at a time**\\ Each time the condition turns ON, ''BSFT'' shifts N bits by one place and puts a new value into the empty place. * **A whole range at once**\\ ''BRST'' and ''BSETN'' turn off or turn on all N bits from the start bit. * **Separate ON and OFF times**\\ ''TFLK'' repeatedly turns on for the ON time and off for the OFF time. **On this page** * [[#finding_and_inserting_the_functions|Finding and Inserting the Functions]] * [[#common_behavior|Common Behavior]] * [[#ff_flip-flop|FF Flip-Flop]] * [[#bsft_bit_shift_register|BSFT Bit Shift Register]] * [[#brst_and_bsetn_reset_and_set_bit_range|BRST and BSETN Reset and Set Bit Range]] * [[#tflk_flicker_timer|TFLK Flicker Timer]] * [[#inputs_that_do_not_compile|Inputs That Do Not Compile]] * [[#out_of_range_and_ixerr|Out of Range and @IXERR]] * [[#monitoring_and_simulation|Monitoring and Simulation]] * [[#examples|Examples]] * [[#things_to_check|Things to Check]] ~~NOTOC~~ ===== 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 [[:en-us_products:mp_studio:functions#insert_function_window|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 | {{ :en-us_products:mp_studio:bf-palette-marked.svg?300 |Figure 1. The Functions panel with the Bit Logic and Timer categories expanded }} * **① Bit Logic category:** the number on the right is the number of functions in the category (4). * **② FF, BSFT, BRST and BSETN:** double-click to insert the function at the cursor cell. * **③ TFLK:** the last row of the Timer category. 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. {{ :en-us_products:mp_studio:bf-insert.png?470 |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'', ''BSFT'', ''BRST'' and ''BSETN'' pass the condition on the left straight through to the right of the block, so you can put a coil after them. The right side of ''TFLK'' is the blinking output. * ''FF'' and ''BSFT'' remember the previous condition separately for each block. * If the condition is already ON in the first scan after power-on or a download, it is treated as a change from OFF to ON. In that case, ''FF'' toggles once and ''BSFT'' shifts one place. * Inside an ''MCS'' section, while the section is off, the block works as if its condition were OFF, and the right side of the block is off too (other function blocks run regardless of ''MCS''). If the condition is ON when the section turns back on, it is treated as a change from OFF to 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. {{ :en-us_products:mp_studio:bf-ff-sim.png?1022 |Figure 3. FF turning P32 on after P0 was turned on in simulation }} * OUT can be a ''P'' or ''M'' bit, a bit inside a D word such as ''D10.5'', or a ''#name'' user bit. * Two ''FF'' functions can toggle the same bit. If you put ''FF P32'' after both ''P0'' and ''P1'', pressing either button changes ''P32''. If both conditions turn ON in the same scan, the bit is toggled twice and stays as it was. > **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. * **BIT:** the start bit. You can use a ''P'' or ''M'' bit, or a bit inside a D word such as ''D10.0''. Bits inside D words continue from ''D10.15'' to ''D11.0''. * **N:** the number of bits. Use a constant of 1 or more, or a byte or word address such as ''D0'' or ''BD0''. * **IN:** the value to put in. Use 0, 1 or a bit such as ''M5'' or ''P1''. * **DIR:** the shift direction. Use 0, 1 or a bit. If IN or DIR is a bit, its value in the scan in which the condition turned ON is read. {{ :en-us_products:mp_studio:bf-bsft-sim-marked.svg?857 |Figure 4. BSFT in simulation, with the reject detection results shifted into M200 to M207 }} * **① BSFT block:** the BIT, N, IN and DIR cells. * **② Data Table:** shows the values shifted into ''M200'' to ''M207''. * **③ Eject rung:** ''M207'' is on, so ''P33'' is on. ===== 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. * The current value of the timer in the TIMER cell is the time elapsed within one cycle. When it reaches the ON time plus the OFF time, it starts counting again from 0. * When the condition turns OFF, the output turns off at once and the current value becomes 0. When the condition turns ON again, it starts again with the output on. * If the ON time is 0, the output is always off. If the OFF time is 0, the output stays on while the condition is ON. * The scan that turns the output on is counted too, so if the ON time is shorter than one scan, the output can turn off right after it turns on. * A blink with equal ON and OFF times and a fixed period can also be made with the [[:en-us_products:mp_studio:memory_symbols#system_bits_and_user_variables|system bit]] ''@Fn''. Use ''TFLK'' when you want to set the ON time and the OFF time separately. {{ :en-us_products:mp_studio:bf-tflk-sim-marked.svg?1022 |Figure 5. TFLK during its OFF time in simulation }} * **① ''T5'' current value:** the time elapsed within one cycle. * **② ON and OFF:** the ON time (''20'' = 0.2 seconds) and the OFF time (''180'' = 1.8 seconds). * **③ ''P34'':** the output of ''TFLK''. * **④ ''T5'' contact:** turns ''P35'' on and off with the same value as the output of ''TFLK''. 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 [[:en-us_products:mp_studio:menus_settings#timer_base|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 [[:en-us_products:mp_studio:indexed_address|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. {{ :en-us_products:mp_studio:bf-red-reason.png?1000 |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 [[:en-us_products:mp_studio:memory_symbols#data_memory|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. * It skips only that block. It writes no bits, so no other memory is overwritten, and the right side of the block passes the condition through unchanged (unlike an indexed address that goes out of range, the power output does not turn off). * The system bit ''@IXERR'' turns on. * While the condition is OFF (for ''BSFT'', in any scan other than the one in which the condition turns ON), the range is not checked, so ''@IXERR'' does not turn on. 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 [[:en-us_products:mp_studio:indexed_address#out_of_range_and_ixerr|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. {{ :en-us_products:mp_studio:bf-ixerr-notice.png?591 |Figure 7. The notice shown in a program that has @IXERR in the Data Table }} {{ :en-us_products:mp_studio:bf-ixerr-sim-marked.svg?793 |Figure 8. @IXERR turned on in simulation because the count of BRST went out of range }} * **① N cell ''D10'':** the value is 0, so ''BRST M300 D10'' was skipped. * **② ''@IXERR'' rung:** ''@IXERR'' is on, so the coil ''M1'' after it is on. ===== Monitoring and Simulation ===== The five functions work the same in simulation as on the PLC. * When the condition is ON, the blocks of ''FF'', ''BSFT'', ''BRST'' and ''BSETN'' are shown in the power-flow color. A cell with an address shows the current value of that address, and a constant cell shows no value. * The value in the BIT cell is the value of the start bit alone. To see the whole range, add the addresses to the [[:en-us_products:mp_studio:monitoring#data_table|Data Table]]. * The TIMER cell of ''TFLK'' shows the current value of the timer. During the ON time, the block and the output are shown in the power-flow color. During the OFF time, they are shown without output, as while a ''TON'' is counting. * To see a single moment of the blink, use **Pause** and **Step one scan** on the [[:en-us_products:mp_studio:simulation#simulation_bar|simulation bar]]. * In **Run → Used Data**, if N is a constant, the N bits from BIT are painted as used, and if N is an address, only the start bit is painted (''M'' and ''D'' areas). ===== 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. - In **Settings → Symbol**, register LAMP_BTN for ''P0'' and LAMP for ''P32''. - 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''. - In **Settings → Symbol**, register MOVE_PULSE for ''P1'', NG_SENSOR for ''P2'', CLEAR_BTN for ''P3'', TRACK for ''M200'' and EJECT for ''P33''. - 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). - In **Settings → Symbol**, register ALARM for ''P4'' and BEACON for ''P34''. - 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 ===== * The bits that ''FF'' and ''BSFT'' use to remember the previous condition are set up automatically and do not appear in the symbol table, the Data Table or Used Data. > **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. ===== Related Pages ===== * [[:en-us_products:mp_studio:ladder_editing|Phase 3 · Ladder Editing]] * [[:en-us_products:mp_studio:functions|Phase 4 · Functions]] * [[:en-us_products:mp_studio:memory_symbols|Phase 5 · Memory and Symbols]] * [[:en-us_products:mp_studio:monitoring|Phase 7 · Monitoring and the Data Table]] * [[:en-us_products:mp_studio:simulation|Phase 8 · Simulation]] * [[:en-us_products:mp_studio:indexed_address|Indexed Addresses (Pointers)]] [[:en-us_products:mp_studio|← Back to the MP STUDIO 2 User Manual]]