PLC Clock to reset a Counter

Clear the next month's total (which is also the oldest month's total), and increment the current month's total, every scan.

GSV WallClockTime LocalDateTime YmdHMS MOV YmdHMS[1] NextMonthOffset SUB NextMonthOffset 1 ThisMonthOffset GRT NextMonthOffset 11 CLR NextMonthOffset
CLR MotorRunHoursByMonth[NextMonthOffset] ADD MotorRunHoursByMonth[ThisMonthOffset] DeltaHours Motor RunHoursByMonth[ThisMonthOffset]


Either this routine will be executed only when DeltaHours is greater than 0, or DeltaHours will be 0 on most scans.


January's total is in MotorRunHoursByMonth[0], February's in [2], ..., Decembers's in [11].


The previous ten months' totals are maintained; this month's total is in MotorRunHoursByMonth[ThisMonthOffset].
 
Last edited:
Keep it simple.

1X RTO for your hour pulse. Which when .DN. increments a CTU then Resets.

1x CTU for each month in an Array of 13 (So you don't need to use[0])

Your PLC Month is your Counter Array Pointer.

ie. C_Monthly_Hour_Count[PLC_Month]

Reset the CTU at the start of a new month.

No need to make it complicated.
 
Last edited:
Hello I_Automation
I attached the sample PLC program and I have the run hours being stored to each of the month array tags.

Now how would i add the reset to reset the monthly run hours each month?

thanks for all your help
 
Now how would i add the reset to reset the monthly run hours each month?

Exactly as you showed in a previous post (See instruction (6) MOV 0 Month_Run_Hrs_Total below).


...when the systemtime[1] which is August gets moved to the tag "Month_Stored" won't this will always be equal? This is the part that i'm confused on.

I think you still do not understand the scan cycle. I annotated the code you posted earlier to try to clear this up; see below.

There will be only and exactly one scan in any month when SystemTime[1] will Not EQual Month_Stored at the start of this rung, and that will be the first scan executed in that month. Also note that, although they may be unequal at the start of this rung, at the exit of this rung, SystemTime[1] will always be equal to Month_Stored, whether they were equal at the start of the rung or not.

Remember, it's about time:

  • Instruction executions are discrete events, executed in a time-ordered sequence left-to-right and top-to-bottom, within the execution of the rung they are on.
  • Rung executions are discrete events, executed top-to-bottom, within the execution of a single scan.
  • Scan cycle executions are discrete events, executed in a time-ordered sequence, one after the other in time, with breaks for I/O scans and other housekeeping.
The point here is that we need to "think" like the PLC, and play the instructions, rungs, and scan cycles in our heads, but to do that we need to understand the scan cycle.

The only reason for the program to do anything different from one scan cycle to the next is if external inputs change (e.g. SystemTime[1]), and/or its internal state changes (e.g. Month_Stored). The rung in view here ensures that, at the end of the rung execution, Month_Stored has the same value as the SytemTime[1] value current as the the current scan. So the only way for them to be unequal at the start of the rung's execution on the next scan, is if SystemTime[1] changes between the current scan and that next scan; and it should be obvious that will happen when the current scan executes in one month, and the next scan executes in the following month.
Run Hrs Stored.png
 
Hello I_Automation
I attached the sample PLC program and I have the run hours being stored to each of the month array tags.

Now how would i add the reset to reset the monthly run hours each month?

thanks for all your help

Again keep it simple.

If Current_Month NEQ Old_Month > (RES) Then MOV Current_Month To OLD_ Month.

There is no need for reams and reams of lengthy code for something very simple.
 
Last edited:
.but when the systemtime[1] which is August gets moved to the tag "Month_Stored" won't this will always be equal. This is the part that i'm confused on.


What you're not realizing is the instruction NEQ checks that the 2 values are NOT the same. They will be equal every scan of a month EXCEPT the first scan, and that is the only single scan of the month where this rung will execute.



To check if 2 value ARE the same use EQU.
 
Hello I_Automation
I attached the sample PLC program and I have the run hours being stored to each of the month array tags.

Now how would i add the reset to reset the monthly run hours each month?

thanks for all your help


Finally got on a computer to see your file.


The way you have it you will be writing the log every scan of the year, and never reset the main log DINT.


Here it is using the NEQ so it only runs once per month, each month on a branch, then the last branch resetting the hours and recording the new month thus ensuring this rung will not run until next month.


EDIT: Sorry for the burriness - had to reduce the image size of the screenshot on a 4K monitor so it's not huge on this forum. Any chance Phil can set a MAX Display Size for photo's that are too big?


EDIT2: I just thought to save a tag and keep everything together, since you're not using Array[0] you could use that instead of creating Month_Stored. That way looking at the array values [0] would be the last stored month and [1] thru [12] the monthly values.

Capture.PNG
 
Last edited:
Thanks Everyone for the detailed explanations, your time, and patience!
Its very much appreciated

I was able to get this working on my emulator.


thanks again
 
Is 27 instructions considered simple, when half a dozen would do exactly the same thing?


I am not trying to be a know-it-all, I am actually just asking.
 
Last edited:
Is 27 instructions considered simple, when half a dozen would do exactly the same thing?


I am not trying to be a know-it-all, I am actually just asking.


I just added to his upload.


Personally I would do math on the present month and have one branch MOV Hrs Array[Month_Calculated] instead of 12 branches followed by the reset. That could cut 13 branches down to 4.

Capture.PNG
 
I was working on 3 branches, moving the 2 MOV's on the last branch, but Studio5000 threw a Windows C++ error and crashed at that time.


The MOV SystemTime[1] Month_Array[0] could go on the first branch and the MOV 0 up one branch after the actual logging on the third branch.
 

Similar Topics

At the moment on my application (S7-1212C), the PLC/HMI are showing the correct time, i.e. the displayed time matches the time of the clock on the...
Replies
10
Views
880
The PLC in question is a S7-1212C. The PLC will be transportable on a wheeled trolley and used wherever it's needed in the factory. I've been...
Replies
14
Views
1,278
Hi, I'm using CX Programmer and just seeing what the best way to trigger an output from the plc clock time would be. I need this to come on at 9am...
Replies
4
Views
1,699
Hi all, I'm running into an issue with a Modicon M340 PLC Clock and daylight savings time. I found an old thread that wrote out specific code...
Replies
3
Views
1,841
I have a system installed in my company which have M340 PLC and it has a program which is used toh turn lights on and off. I am attaching program...
Replies
5
Views
3,281
Back
Top Bottom