Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
GTBecker wrote:What might affect how the IDE interprets the parameter type here?
The problem occurs when the parameter is of type Single but the value is not known at compile time. In that case, the compiler should generate code to convert the time, in seconds, to Timer0 ticks. The problem is that the compiler is emitting the wrong value for the conversion (512 instead of 230400).
When the parameter is of type Single but the value is known at compile time, the conversion to Timer0 ticks is done at compile time and the correct conversion factor is used.
I arrived at the problem indicated in this thread, and it appears the workaround as presented above does not resolve the issue.
A simple test program for toggling a led for a given amount of time (on a ZX-328nu, but behaves the same on a ZX-328L, i.e. native mode devices) reveals the issue:
This is the base version, using Pause and the amount of seconds specified as a Single (and which does not work properly):
Option TargetCPU ZX328nu
Dim pauseTime as Single = 1.0
Sub Main()
Call Putpin(Pin.GreenLED, zxOutputLow)
Do
Call Pause(pauseTime)
Call Putpin(Pin.GreenLED, zxOutputToggle)
Loop
End Sub
Replacing the Pause statement with Delay resolves the issue.
Option TargetCPU ZX328nu
Dim pauseTime as Single = 1.0
Sub Main()
Call Putpin(Pin.GreenLED, zxOutputLow)
Do
Call Pause(CUInt(pauseTime * CSng(Register.RTCTimerFrequency)))
Call Putpin(Pin.GreenLED, zxOutputToggle)
Loop
End Sub
the problem persists with the same behaviour as the original code. For me this is not a big issue since I use only small, single-task apps (and thus can use Delay/Sleep instead of Pause) but I thought it was useful to signal it
kranenborg wrote:the problem persists with the same behaviour as the original code.
I believe that what you're seeing is a result of the limited range of the Pause() parameter (1-65535) rather than a manifestation of the original issue. In your case, you're requesting a pause of 1 second, far too large to be represented as RTC timer ticks (at 230.4KHz) in 16 bits. The documentation for Pause() indicates that the maximum pause time is approximately 284mS.
Thanks Don, and when going back to the manual for the definition of the Pause command I saw that it all there, both the description of the limit as well as the way to create longer pauses if needed; interesting to see how I became completely blind for information when I was sure that something was terribly wrong. Case closed I think, I tested the code with five Pause(0.2) commands on a row and they correctly created a 1 sec pause.