Div_by_zero

Cleanest way to convert milliseconds to hh:mm:ss

2 posts in this topic

Using ladder logic and standard PLC functions (in my case, an Automation Direct P2K), what is the cleanest way to convert a timer value that's in in milliseconds to hours : minutes : seconds?

ETA: the "timer value" is actually coming in from an external microprocessor via Modbus in milliseconds.  It's not a P2K "TMR".

Thanks.

 

Edited by Div_by_zero

Share this post


Link to post
Share on other sites

How does the P2K handle integer division? I've done this in AB and Mitsubishi, and both of them can give you two results: an un-rounded quotient, and the remainder. I can use this to divide up a ms value into the components in relatively few steps.

May or may not be useful, but here's how I would do this in Mitsubishi. Mitsubishi automatically puts the quotient from a division instruction into the destination register and the remainder in the next consecutive register. I'm assuming that you're using 32-bit integers, otherwise your time value could never get over 32 seconds. Here's how I would write the code. Note that in Mitsubishi, a 32-bit integer takes two memory addresses. For this example, say your starting value is 9,462,622ms.

DDIV D0 K1000 D10 - Converts time in ms (D0-D1) to time in seconds (D10-D11)

  • D0-D1 = 9,462,622
  • D24-D25 = 9,462
  • D26-D27 - 622

DDIV D24 K60 D22 - Puts time in minutes in D22-D23, and the remainder (seconds) in D24-D25

  • D24-D25 = 9,462 (before instruction execution)
  • D22-D23 = 157
  • D24-D25 = 42 (after instruction execution)

DDIV D22 K60 D20 - Puts time in hours in D20-D21, and the remainder (minutes) in D22-D23

  • D22-D21 = 157 (before instruction execution)
  • D20-D21 = 2
  • D22-D23 = 37 (after instruction execution)

So now you have your hours in D20-D21, minutes in D22-D23, seconds in D24-25, and ms in D26-D27.

 

 

1 person likes this

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now