Dang, my loop is to slow...
Archiver
Posts: 46,084
Greetings all,
I'm trying to run the following FOR/NEXT loop.
It works fine, but I was rather astonished that it
takes aprox 1.2 milliSeconds between repetitions
For my application I need to get it down to about 500 microSeconds...
Is that going to be impossible with the BS2, or is there a
"faster way" to write a loop to pulse a pin "X" number of times
depending on the number in a variable.
'{$STAMP BS2}
'{$PBASIC 2.5}
index VAR word
count VAR word
count = 5678
FOR index = 0 TO count
PULSOUT 9, 100
NEXT
END
Thanks much!
Steve
I'm trying to run the following FOR/NEXT loop.
It works fine, but I was rather astonished that it
takes aprox 1.2 milliSeconds between repetitions
For my application I need to get it down to about 500 microSeconds...
Is that going to be impossible with the BS2, or is there a
"faster way" to write a loop to pulse a pin "X" number of times
depending on the number in a variable.
'{$STAMP BS2}
'{$PBASIC 2.5}
index VAR word
count VAR word
count = 5678
FOR index = 0 TO count
PULSOUT 9, 100
NEXT
END
Thanks much!
Steve
Comments
Sid Weaver
W4EKQ
Port Richey, FL
[noparse][[/noparse]Non-text portions of this message have been removed]
>How fast does it have to be? Right now your pulsout os 200us.
Yes, pulsout is 200uS. If I set it less than about 150, then
downstream circuitry has trouble with it.
But I need to be able to repeat the pulse about every
600 uS. About twice as fast as it runs now. Just wondered
if there was a faster way to do the loop. I think maybe I've
got myself screwed into a corner and just need a faster device...
'{$STAMP BS2}
'{$PBASIC 2.5}
index VAR word
count VAR word
count = 5678
FOR index = 0 TO count
PULSOUT 9, 100
NEXT
END
Thanks,
Steve
>
> For my application I need to get it down to about 500 microSeconds...
>
> Is that going to be impossible with the BS2, or is there a
> "faster way" to write a loop to pulse a pin "X" number of times
> depending on the number in a variable.
>
That timing sounds about right for a BS2 running that loop. I'm not clear
exactly how the signal is being used downstream, but it sounds like a
clocking function. It's fairly straightforward to build an external circuit
to provide that clock pulse and then enable it with the Stamp pin instead of
actually creating the pulse from the Stamp. Would a 2 kHz clock with a pulse
width of 200 us be what you're trying to create?
Gary
You could probably do it with a BS2p or a BS2sx, which are at least
2.5 times faster. But the vanilla BS2 is as you have observed going
to be too slow. You might find the instruction timing information at
this url useful:
<http://www.emesystems.com/BS2speed.htm>
(FOR:NEXT) loops are relatively slow. The timing info I have at that
URL for the BS2 gives 763 microseconds for each FOR:NEXT, and 220 to
set up each PULSOUT, and 200 for the pulse itself. That adds up to
1183 microseconds, which is close to the 1.2 milliseconds you
observed.
On the BS2SX however, your code with the FOR:NEXT loop would run with
a total time of 600 microseconds, and on the BS2P about 20% faster
than the BS2sx, it comes down to nearly 500, as required, including
the 200 uS output pulse.
-- Tracy
>Greetings all,
>
>I'm trying to run the following FOR/NEXT loop.
>It works fine, but I was rather astonished that it
>takes aprox 1.2 milliSeconds between repetitions
>
>For my application I need to get it down to about 500 microSeconds...
>
>Is that going to be impossible with the BS2, or is there a
>"faster way" to write a loop to pulse a pin "X" number of times
>depending on the number in a variable.
>
>'{$STAMP BS2}
>'{$PBASIC 2.5}
>
>index VAR word
>count VAR word
>
>count = 5678
>
> FOR index = 0 TO count
> PULSOUT 9, 100
> NEXT
>
> END
>
>
>Thanks much!
>Steve
pulsout pin, myOtherVar
pause 500
next
Quoting "Gary W. Sims" <simsgw@c...>:
> From: <laurasdog@w...>
> >
> > For my application I need to get it down to about 500 microSeconds...
> >
> > Is that going to be impossible with the BS2, or is there a
> > "faster way" to write a loop to pulse a pin "X" number of times
> > depending on the number in a variable.
> >
> That timing sounds about right for a BS2 running that loop. I'm not clear
> exactly how the signal is being used downstream, but it sounds like a
> clocking function. It's fairly straightforward to build an external circuit
> to provide that clock pulse and then enable it with the Stamp pin instead of
> actually creating the pulse from the Stamp. Would a 2 kHz clock with a pulse
> width of 200 us be what you're trying to create?
>
> Gary
>
>
>
> To UNSUBSCRIBE, just send mail to:
> basicstamps-unsubscribe@yahoogroups.com
> from the same email address that you subscribed. Text in the Subject and
> Body of the message will be ignored.
>
>
> Yahoo! Groups Links
>
> To visit your group on the web, go to:
> http://groups.yahoo.com/group/basicstamps/
>
> To unsubscribe from this group, send an email to:
> basicstamps-unsubscribe@yahoogroups.com
>
> Your use of Yahoo! Groups is subject to:
> http://docs.yahoo.com/info/terms/
>
>
>to provide that clock pulse and then enable it with the Stamp pin instead of
>actually creating the pulse from the Stamp.
> Would a 2 kHz clock with a pulse
>width of 200 us be what you're trying to create?
>
>Gary
Gary, yes I need a clock pulse that I can vary from around 500 Hz
to almost 2 KHz. The pulse train is a specific number of
pulses long, with the exact number to be read from a variable.
It could be anywhere from about ten pulses all the way up to
several thousand pulses.
Thanks!
Steve
>to be too slow. You might find the instruction timing information at
>this url useful:
> <http://www.emesystems.com/BS2speed.htm>
>
>(FOR:NEXT) loops are relatively slow. The timing info I have at that
>URL for the BS2 gives 763 microseconds for each FOR:NEXT, and 220 to
Tracy, I must say that as a "newby stamper" I'm finding your emesystems
site to be incredibly helpful. What a wealth of good info.
My hat's off to you man!
Thanks,
Steve
>
>> It's fairly straightforward to build an external circuit to
>> provide that clock pulse and then enable it with the Stamp
>> pin instead of actually creating the pulse from the Stamp.
>> Would a 2 kHz clock with a pulse width of 200 us be what
>> you're trying to create?
>
> Gary, yes I need a clock pulse that I can vary from around
> 500 Hz to almost 2 KHz. The pulse train is a specific number
> of pulses long, with the exact number to be read from a
> variable. It could be anywhere from about ten pulses all the
> way up to several thousand pulses.
>
Okay, for what it's worth, here's how I'd go about designing such a circuit.
I suspect one of our number will be able to tell you "buy a Widget 409 and
read the app notes" but personally I don't know a specific part off hand. It
may help to see how I would go about finding what I need if this were my
application. Grabbing a catalog is not helpful until I understand what I'm
trying to find. So I would build a notional circuit in my mind like this:
Requirements: what do we need to control?
1. The pulse frequency: a range from 500 Hz to 2 kHz.
2. The pulse count. Must be exact -- not based on time duration, but an
actual count.
We do NOT need to control the pulse width. We can let it arise from our
other design choices within reason. From your earlier comments, I infer that
we can choose a convenient pulse width for all frequencies, so long as it
meets a minimum width required by the downstream circuits. 200 us I believe
you said, and a fifty percent duty cycle at 2 kHz would provide a pulse
width of 250 us. So as we choose parts we can keep in mind "about 250 us"
without creating other problems.
Now creating pulse trains of this frequency is not a job for interpreted
software. If we ran into this need every day, Parallax easily could put such
a function into firmware, but it's not common enough for them to have
considered it I'm sure. Besides, we tie up the microcontroller completely if
we use it to generate a long pulse train, and we might have other useful
things to do. (Even at 2 kHz, thousands of pulses takes more than one
second, and that's a very long time to a microprocessor.) So we want to
control the creation of each pulse train from our PBasic program in the
Stamp, but we'll generate it externally.
For the sake of simplicity, I'll assume we can spare enough pins on the
Stamp to output the pulse count value in parallel. That is, we can use a
statement like:
OUTL=PulseCount
to put the count on the pins where it can control the external circuit.
Actually, I assume we will need at least ten pins since you specified a
count "into the thousands" but I won't complicate that example by adding the
code to put all ten bits on the output pins.
Obviously, the simplest device to do all this would be a single integrated
circuit. If we can find one, it will be called something like: "count-down
counter with oscillator." I'll describe the use of this hypothetical perfect
chip first, and then talk about how to divide up the functions if we can't
find a single "perfect" chip that does the job.
It will have 10 to 14 pins that are how we enter the starting value for the
count. Those pins will latch the presented value when we put a high on a pin
called "enable input" or something similar. The counter will begin to count
down when we put a high on a pin called "enable count" or, again, something
similar. Obviously, in accordance with the whims of chip designers, either
enable pin might require a low instead of a high, but we don't care about
such differences until we actually start to wire and code.
The oscillator part of that chip name means it has pins for a resistor and a
capacitor -- or perhaps just a resistor, with the capacitor on board
already. This will be how we control the frequency.
So the procedure for using this hypothetical chip we'd really like to find
goes like this:
Step 1. Set the frequency for the on-chip oscillator. If you have a small
number of fixed frequencies that will be required, then you can do this
easily with an analog multiplexer. Dedicating three pins to address this
chip will give us eight different frequencies, and they can be pretty much
arbitrary frequencies. I won't get into design tricks unless you actually go
this way, but an analog multiplexer gives you a simple circuit path from one
input pin to eight output pins. That's eight different circuit paths. You
can arrange for each one to present a different resistance to the pins that
control that oscillator. The Stamp picks the frequency by picking the
resistance, which it does by picking which output pin is addressed on the
multiplexer. And it does that by putting the three-bit address on three of
its own output pins. Simpler to do then it is to say. Much.
If we need a greater selection of frequencies, and especially if they are
not pre-determined, then a digital potentiometer will be required. Their
interface is slightly more complex from a programming point of view, but in
exchange they provide more possible resistance values. On the other hand,
those values are not arbitrary, but must fall on a linear scale with a step
size that is fixed once you buy the pot. App notes abound on using a digital
pot.
Now we have set up the oscillator to provide anything from a few hundred to
a few thousand pulses per second to the counter circuit.
2. Put the count of pulses required on the output pins of the Stamp. Set
them to output mode. Pulse the Stamp pin tied to the latch enable with a
high or a low as appropriate. This will load the count value into the
counter register.
3. Enable the counter chip. This will start the emission of pulses.
Depending on the number of downstream circuits, we may have to feed the
output pulse of the counter to a driver, but that's a separate part of the
design.
4. Hopefully, the chip we chose has a stop-when-count-reaches-zero feature.
If so, it will have a status pin that we tie to a final Stamp pin so we can
tell when the pulse train is complete. We can check this pin periodically
while we do other things, or we can just wait in a tight loop until that pin
changes state. If it doesn't have such a feature (which seems unlikely)
there are other ways to stop the pulses when count reaches zero.
Comments:
A typical oscillator circuit will output roughly a square wave with a fifty
percent duty cycle. If our highest frequency is 2 kHz, then the high side of
that wave will never be less than 250 us. It may be as long as one
millisecond for the low frequencies. I postulated that we have no need to
control pulse width, but if pulses too long turn out to be a problem then
you need to configure a 555 as a one-shot. With that chip, you feed it the
output of the counter and use the 555 to explicitly control the pulse width
seen by the downstream circuits.
It's likely that a counter chip is available with all those functions in one
form or another. If we have to break this up into multiple chips, it still
won't be many. First, a 555 will provide the pulse source for us and you
still control the frequency with a variable resistance, just as I described
above. Oscillators are pretty straightforward and available in many flavors,
but a 555 is a standby available at any local five and dime. (Well, okay.
Any local Radio Shack or equal<g>.) Most important, you can find app notes
everywhere for 555 circuits.
The output of the pulse source must be used to pulse a counter set to count
down. Such counters always have an interface to load the initial value, and
we simply limit our choices to one that offers a pin that tells us when it
reaches zero. We use that pin to stop the count if it doesn't already do
that.
I don't have time to do a parts search for the specific parts we'd need for
this circuit, but you shouldn't have too much trouble finding what you need
on Digikey or Mouser with all those keywords to work with: count-down,
counter, oscillator, and so forth.
Send me a private note if I've muddied the water too much. This is one of
those simple things to do, that take a long time to describe.
Gary
about the speed of running this code on a BS2:
index VAR Word
qty VAR Word
qty = 5678
FOR index = 0 TO qty
PULSOUT 5, 100
NEXT
END
It appears that the fastest the BS2 will run it is
about 1.2 mS per iteration. You guys gave me
a lot of good ideas and possible work-arounds,
but I guess the real answer is to put a BS2 in it.
I tested the same loop with an OEM BS2SX and
it ran at aprox 500 uS per loop, which is just
fast enough for my purpose...
The lesser number of allowable EEPROM writes
may be a problem for me, but it should give me
about 3.7 years to find a better solution. (-:
Thanks all!
Steve