Internet PLC Forum

General => Technical support => Topic started by: Lorne Van Dusen on March 06, 2018, 08:03:02 AM

Title: Simulation of Clock
Post by: Lorne Van Dusen 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.
Title: Re:Simulation of Clock
Post by: garysdickinson 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
Title: Re:Simulation of Clock
Post by: Lorne Van Dusen 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
Title: Re:Simulation of Clock
Post by: garysdickinson 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 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:

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
Title: Re:Simulation of Clock
Post by: Lorne Van Dusen 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