What is total delay of this subroutine?

wapo54001

Senior Member
I am working on a portion of a program that has rather a stringent timing requirement. I would like to know what the total delay is for the following code, assuming that W3 is equal to zero, and then also if not equal to zero. Can anyone offer any information?

if w3=0 then
pause 1
endif
 

womai

Senior Member
The topic (code execution speed) has been creeping up many times in the past of this forum. Short answer - timing depends somewhat on very subtle details out of your direct control (related to where exactly the commands are tokenized, i.e. if the token spans a byte boundary or not - the former taking slightly more time to decode). So changing some other - unrelated - part of your code can slightly alter the execution speed of your code snippet.

That said, a rough estimate is 2000 command/sec at 4 MHz (and half that at 8 MHz), so for w3=0 your code should take about 1ms (pause duration) + 2 * 0.5ms (command execution, "if", and "pause"), i.e. 2ms total at 4 MHz. For w3<>0 it should be 0.5ms ("if" only).

For more precise numbers, try this:

high 0
for w0 = 1 to 10000
if w3=0 then
pause 1
endif
next w0
low 0

vs. this code:

high 0
for w0 = 1 to 10000
next w0
low 0

Attach an LED to output 0 and measure the time it is on. The difference between the two cases (divided by 10000 of course) is the next execution time. Repear with w3 set to 0 vs. w3 set to <>0.

For more precise (fine grained) timing control you can use pulseout (on all Picaxe devices) or pause_us (I believe X1 parts only) for resolution down to 10us at 4 MHz.

Wolfgang
 
Last edited:

hippy

Ex-Staff (retired)
The timing will also depend on exactly where the lines of code end up in memory and you don't get a lot of control over that. Alter anything in the program before it and your measured timing goes to pot. It may get faster, it may get slower.

Best solution to overcome that is to put the code right at be beginning of the program with a GOTO around it, then GOSUB to the code when needed or GOTO it then GOTO back out, but timing will still change as the program gets altered.

How stringent is the necessary timing ?
 

wapo54001

Senior Member
Wolfgang,

Thank you for your clarification. Yes, I've read the timing threads in the past, but the answers seemed to vary widely, and I never got a comfortable feeling that I understood the issue.

You are saying that the difference between w3=0 and w3<>0 is close to the difference between 2ms and 1ms, and that may be OK.

I am using an 08M to convert an input voltage to a different output voltage. One A-to-D input is adjusted mathematically and then output to the integrator circuit which is in turn tested and corrected by feedback via the second A-to-D. If the delay is too long after the output goes high or low to adjust the voltage, I'm afraid that the adjustment will overshoot and constantly "hunt" for the precise voltage desired. This delay is required only when the target voltage is zero, so I could write code to put it outside of the normal integrator adjust loop, but I'm very short of code space so need to find ways to keep this as short as possible.

Thanks again.
 

BeanieBots

Moderator
I might have misunderstood your application, but why do you need feedback?
Read the A2D, do your sums, output using PWM into an RC. Buffer the RC with an op-amp. Whatever you put out on the PWM will be faithfully reproduced as a voltage at the op-amp output. No need for feedback and hence no possibility of hunting.
 

womai

Senior Member
Actually, what I am saying is that I expect the difference between =0 and <>0 to be around 1.5ms, not 1ms, because afaik the pause command does not take into account the time it takes to parse itself.

You can alleviate the timing uncertainty by increasing the clock speed of your Picaxe to 8MHz, and doubling the value for pause. That way the command execution time becomes a smaller part of the overall delay, and the pause time becomes dominant. I.e. instead of

Code:
if w3=0 then
pause 1
endif
use

Code:
setfreq m8 ' at the very beginning of you program - 
           'no need to execute that more than once
and then modify your code to:

Code:
if w3=0 then
pause 2 ' double parameter value to get same delay as at 4 MHz
endif
Wolfgang
 

wapo54001

Senior Member
Originally, I was planning to use pwm as the output. However, wilf_nv showed me how to use an integrator with feedback to achieve extremely accurate output using iterative momentary outputs either high or low into an RC network that in turn drives a mosfet to act as a variable resistor. It has worked extremely well, except in the single instance that the mosfet resistance must be zero. In this case, the mosfet doesn't get that low unless I hold the output to the integrator (and mosfet gate) high for a few milliseconds to drive it right down to minimum resistance.

What I was concerned about was what would the three lines of code in the routine do to the actual period that the output to the integrator is held high. I don't want it to be so long that the integrator overshoots the target voltage before the output is re-read by the a2d and the output has to be sent low to correct the overshoot. I'd like the output to be tri-stated most of the time because the target voltage has been achieved and is being held without action from the 08M.

It sounds like if w3<>0, the if-then-endif code by itself will hold the output high for maybe an additional millisecond, and longer if w3=0. I think that will be OK, although it will cause a slight time imbalance between the "high 2" and "low 2" loops. Right now, each "high" and "low" command is followed immediately by an "input" command which keeps each output duration very short. The "if w3=0 then" code would go between the "high 2" and "input" code which would serve to keep the pin high for a longer period.
 

BeanieBots

Moderator
I don't want to push you down the path of a re-design but if your feedback is a PICAXE ADC, you have 10-bit resolution. The PWMout command is also 10-bit so the overall accuracy would be the same either way.

If you have a negative rail available, injecting a small negative current would mean that the FET would not need to get to zero to achieve zero output.
Alternatively, fitting a diode between 'generated' voltage and the feedback/load node would require a small offset voltage to be generated before there was any conduction, hence also giving the ability to go down to 0v. (this is the same trick used for improving the swing of so called rail to rail op-amps).

Totally up to you. Just a thought for a hardware solution to the problem.
 

wapo54001

Senior Member
Well, I've already finalized the 08M circuit and etched a small circuit card that carries it, and done virtually all of the software work. I just need to finalize some software details and this is where I've started looking at the delay caused by the if-endif code to see if it makes any difference to the circuit operation. It appears that it will be just fine.

I've been fiddling with this for a long time; I think it's time to put it to bed, but thanks very much for the suggestion about using pwm.
 
Top