Author Topic: Simulation of Clock  (Read 14796 times)

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
Simulation of Clock
« on: March 06, 2018, 08:03:02 AM »
I am developing a program using multiple on and off times and would like to know if there is a way to test it on the simulator without having a PLC connected.
I have tried to do things like TIME[1] = DM[1] then putting the value in DM[1] but that doesn't work.
Cant find how to adjust clock on the simulator.

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:Simulation of Clock
« Reply #1 on: March 06, 2018, 08:43:09 AM »
Lorne,

You are doing something very wrong.  The simulator does a reasonable job of simulating the hardware RTC clock mechanism.  I can set the time.  

I have attached a demo program that runs in the simulator.  Run it and click on "View" and you will set the current time on the 4x20 LCD display.  Click on the RELAY Set and the current values of the variables A,B and C will be load the Hours, Minutes and second registers.

This is the code to display the RTC time:

T = Status(18)
S = T MOD 100
T = T/100
M = T MOD 100
H = T/100

SetLCD 1,1,str$(H,2)+":"+str$(M,2)+":"+str$(s,2)

This is the code to change the RTC registers:

' SetTime - set RTC from the variables A:B:C
'
TIME[1] = A   ' set the hours
TIME[2] = B ' set the minutes
TIME[3] = C ' set the minutes


After you run the code, the question that you need to ask is why did I use Status(18) to read the time registers.  If you ask, I will tell you why.

Best regards,

Gary Dickinson
« Last Edit: March 06, 2018, 11:56:34 AM by garysdickinson »

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
Re:Simulation of Clock
« Reply #2 on: March 06, 2018, 08:55:09 AM »
Gary
Again thanks for the quick replay and great explanation.
As usual you are amassing at coming up with solutions.
I will test your suggestion
As for the Status 18 it is used to return the current real time clock value

garysdickinson

  • Hero Member
  • Posts: 502
  • Old PLC Coder
    • View Profile
Re:Simulation of Clock
« Reply #3 on: March 06, 2018, 11:21:15 AM »
Lorne,

You are right that Status(18) returns hours, minutes and seconds as a single integer. There are two reasons that Status(18) is very important:
  • It simplifies your code when testing to see if the current time is equal to some target time.  Otherwise you have to make tests that involve TIME[1], TIME[2], and TIME[3].
  • It solves a problem with CF reads of the TIME[] registers in the PLC firmware. The registers update asynchronously to the execution of custom functions.  As an example, if the PLC's RTC is at 23:59:59 then it is possible to observe the time registers going through all sorts of incorrect values before getting to 00:00:00.


It is possible to observe the following transitions: 23:59:59 -> 23:59:00 -> 23:00:00 -> 00:00:00
[/list]

The following test code can be used to avoid the illogical  TIME/DATE register transitions:

' AvoidRipple
'
while 1
   ' read all TIME[] and DATE[] registers, one time
   '
   DM[301] = TIME[1]   ' Hours
   DM[302] = TIME[2]   ' Minutes
   DM[303] = TIME[3]   ' Seconds
   DM[304] = DATE[1]   ' Year
   DM[305] = DATE[2]   ' Month
   DM[306] = DATE[3]   ' Day
 
   ' re-read the TIME[] register and verify that they did not change
   ' from the previous set.  The whole point of this exercise is to
   ' detect and discard erroneous readings that can occur because the PLC
   ' firmware is constantly updating these register.  It is possible to
   ' observe the following transitions:
   '
   '  23:59:59 -> 23:59:00 -> 23:00:00 -> 00:00:00
   '
   if      DM[301] <> TIME[1]
      continue
   elif    DM[302] <> TIME[2]
      continue
   elif    DM[303] <> TIME[3]
      continue
   elif    DM[304] <> DATE[1]
      continue
   elif    DM[305] <> DATE[2]
      continue
   elif    DM[306] <> DATE[3]
      continue
   else
      exit
   endif

endwhile


If you directly access the TIME registers you need to:
  • Only use Status(18)
  • Never read/write them if there is any possibility that the RTC will update the registers.  Say only at time xx:xx:30.
  • Read all registers as a set and then re-read all of the registers until you get the same values twice in a row.
  • Shut off the RTC mechanism during CF access to the registers.

Please note that the same issues happen with the DATE[] registers.

Please be aware that if you set the RTC registers it is possible for the registers to be changed while you are updating.  You should either shut down the RTC during the writing or following the write operations re-read the registers to verify that you have set them without interference from the PLC firmware.

Gary D
« Last Edit: March 06, 2018, 11:52:45 AM by garysdickinson »

Lorne Van Dusen

  • Jr. Member
  • Posts: 93
  • I'm a old guy
    • View Profile
Re:Simulation of Clock
« Reply #4 on: March 06, 2018, 11:21:24 AM »
Gary

Your circuit works perfectly however you had a small typo error
TIME[3] = C ' should have been set the seconds

Also I added Date[3] = D 'Set the day of the week
and added SETLCD 2,1 "DAY = " + STR$ (D,2)

Now I was able to test my multiple times and days perfectly

Again thanks for your help

Lorne