Congo,
Let me explain what I was doing with this snippet of code:
' Reload HSCPV[1] with residual count value
' the while loop is used to verify that HSCPV[1] was successfully reloaded
'
while (HSCPV[1] >= 100)
HSCPV[1] = HSCPV[1] - 100 ' Reload counter with residual count value
endwhile
The HSC was programmed to call the CF in response to HSCPV[1] being equal to 100. I wanted the value of HSCPV[1] to increment from 0 to 100 and when it hit 100 the CF that was called was to reload HSCPV[1] back to a value of 0. The problem is that the CF is NOT called on the instant that the HSCPV[1] becomes zero, but is called at some time later and synchronized to the ladder logic.
In my application the value of HSCPV[1] may have been incremented beyond the target value of 100 by the time the CF is called. If the HSCPV[1] has been incremented to 105, this code would reload HSCPV[1] with the value of 5 so that 95 counts later HSCPV[1] will reach 100.
HSCPV[1] = HSCPV[1] - 100 ' Reload counter with residual count value
If your applications increments the HSC at a rate of 50 Hz, then the value of the HSC counter will change every 20 mS. The HSCPV[1] will only be at 100 for 20 mS. In order to poll the HSCPV using CF, you will have to poll at 2x rate that the HSCPV can increment. The minimum poll rate would be 100 Hz or once every 10 mS (Nyquist rate...).
If your application absolutely must perform your math equation only when the HSCPV[1] is exactly 100, 200, 300 ... this may be impossible to achieve with the PLC. Remember that the HSCPV[1] is only at a given value for 20 mS! And your PLC code must complete each scan in only 10 mS to ensure that you do not miss the critical HSCPV[1] value.
If your application would be OK with HSCPV[1] values of 100, 202, 300, 401, 505 ..., then I see no problem with using the PLCs to get this job completed.
I suggest that you do not use the HSCDEF interrupt mechanism, but simply call a custom function on every scan of the ladder logic. This code just compares the current HSCPV[1] against a 32-bit limit value stored in DM32[]. Each time you do the math just add 100 to the limit...
This is the simplest code that I know of that might solve your problem:
' HSCPV[1] Polled on every scan
'
' LimitValue is a 32 bit variable in DM32[] Use a #Define to set this up
'
if HSCPV[1] < LimitValue
return
endif
' Setup next LimitValue
'
LimitValue = LimitValue + 100
' Your code specific to your task follows:
'
This code simply allows HSCPV[1] to increment continuously. There is no need to reload HSCPV[1] when the limit or target value is reached as this code does not use the HSCDEF to set up an interrupt function.
The HSC counters are implemented in hardware and are 32-bit counters. TBASIC works fine with 32-bit values and will treat the HSCPV as 32-bit signed variable (positive and negative values). When the 32-bit counter gets to the biggest positive number (134217727 decimal or &h7FFFFFF) the next count will be the most negative number ( -2147483648 or &H8000000). The code that I presented should work fine. However, I have been proven wrong on many occasions.
Best regards,
Gary D*ckinson