Siemens S7/TIA v18: Trying to increment a tag by 1 when a button's pressed...

Mas01

Member
Join Date
Oct 2020
Location
Leicester, England
Posts
1,109
Hi,

Am I being daft (again)?

I want to increment a tag (Integer) by 1 each time a button on the HMI is pressed.

Before the button press, the value is 321.

After a single button press, the values goes to 354.

See pictures.

Then I pressed it again, it went to 392.

What am I doing wrong?

Thanks

Before press.png After press.png
 
Last edited:
Most likely you need a oneshot -- the value is incrementing every time the instruction is scanned while the button is pressed.
 
Oh hang on - I think I am being thick...it may seem like a momentary press of the HMI button, but it must be equivalent to several scans w.r.t. the PLC, so the number keeps incrementing until the button is released.

EDIT: cross-posted, but didn't see @plvlce's reply.
 
I rarely deal with Siemens, but iirc you want -|P|- for a rising edge oneshot and -|N|- for falling edge. I don't know their proper names.
 
Thanks for the replies.

I'm not sure what the difference is between -|P|- and -(P)-, but this implementation below seems to be working fine....i.e. counter increments by 1 each time the button is pressed.

See picture.

Incrementer working OK.png
 
Last edited:
Thanks for the replies.

I'm not sure what the difference is between -|P|- and -(P)-, but this implementation below seems to be working fine....i.e. counter increments by 1 each time the button is pressed.

See picture.

-|P|- would be used in-line as an input condition after the button but before the addition, saving a rung.

-(P)- is used as you have done, requiring a separate rung for the oneshot but allowing it to be examined in multiple locations.
 
-|P|- would be used in-line as an input condition after the button but before the addition, saving a rung.

-(P)- is used as you have done, requiring a separate rung for the oneshot but allowing it to be examined in multiple locations.

OK understood, many thanks for the explanation.
 
I'm not sure what the difference is between -|P|- and -(P)-

As noted by others, the primary differences are in what type of input value they examine for the rising edge, and what type of value their output is:

  • for -|P|-
    • the input is a Boolean value 0 or 1, and
    • the output is a True or False RLO state
  • for -(P)-
    • the input is a True or False RLO state, and
    • the output is a Boolean value of 0 or 1
The differences affect how and where the output of each instruction can be applied.

See the image below

  • Network 1 uses -(P)-, and the output TA_DB.ReportPuls0 could be used anywhere else in the program
  • Network 2 uses -|P|-, and its output RLO state can be use only by logic connected to that rung after the -|P|-
  • Network 2 demonstrates one-shot, edge detection built from atomic non-edge-detecting instructions
Note that understanding these rungs requires understanding the PLC scan cycle i.e. thinking about how PLCs operate in time. Specifically, during the current scan cycle, there are two values of the .ReportPulseN booleans to be considered on each rung, one value from each of two different times:

  • The value read by the -(P)-, -|P|-, and -| |- instructions when detecting the edge, which value is the that of TA_DB.TA_Report_Button_Pressed at the time these rung were evaluated during the previous scan cycle.
  • The value written by the -(P)-, -|P|-, and -( )- instructions, which value is the that of TA_DB.TA_Report_Button_Pressed time these rung are being evaluated during the current scan cycle.
oneshots.png

 
Even simpler.
In the HMI, use setbit function on the activated event on a regular button.
In the PLC, code this:
Code:
IF bit_from_hmi THEN 
    counter := counter + 1 ;
    bit_from_hmi := FALSE ;
END_IF ;
(SCL only because it is easier to write in a post).
 
Last edited:
Even simpler.
In the HMI, use setbit function on the activated event on a regular button.
In the PLC, code this:
Code:
IF bit_from_hmi THEN 
    counter := counter + 1 ;
    bit_from_hmi := FALSE ;
END_IF ;
(SCL only because it is easier to write in a post).


^ this.


If the "button" press is done on an HMI, then this is the best option. It's called "set and forget:" the HMI is only responsible for writing a 1 into the value of the tag, and the PLC is responsible for writing a 0 into the tag, as above, or in ladder via the Reset instruction -(R)-. It should be obvious that, if the PLC acts on the value of 1 (e.g. via NO contact -\ '-) and then immediately resets the value to 0, it will function as a one-shot.

By the way, my experience with HMIs writing both the 1 on the button press as well as the 0 on the button release is that it is far less reliable. I don't know why, it almost seems as if sometimes the write of the 1 on the press gets lost, then the write of the 0 on the release gets sent (which changes nothing because the 1 was lost), and then the HMI notices that the 1 packet was not acknowledged so it sends it again, and if that second 1 is not lost then the bit value is left as a 1 even though the button has been released. Set and forget is more reliable: at least if the 1 packet is lost the user has only to press the button again and nothing gets out of sequence.
 
Yes HMI's can be doing a lot of processing so coms can be missed, like Jesper posted, SET bit on HMI & Reset it in PLC code has worked for me as well as using the Pulse function.
 

Similar Topics

Context: PLC= S7-1212C, HMI=KTP1200 Basic. Hi again, When the "REPORT" button is pressed (on a different screen), it takes the operator to the...
Replies
7
Views
669
Context: PLC= S7-1212C, HMI=KTP1200 Basic. Hi, The operator has reported that, from time-to-time, when he presses the "Generate Report" button...
Replies
5
Views
466
General Question: The PLC and HMI that I've been working on (a laser measurement system) is soon to be transported to the site where it will be...
Replies
2
Views
701
Hi, I'm not sure how to do this... Basically, I want to restrict the user input values for this tag to be in the range 20.001 to 25.0. I...
Replies
17
Views
1,637
Can someone help me with this? I'm no good at SCL - virtually everything I've done so far has been ladder logic. The return value from the...
Replies
13
Views
1,110
Back
Top Bottom