Speeding up code execution - part 2
On 24 Jul 01 at 4:31, egroups@d... wrote:
Simon-
Here's a version that should run faster and saves one word and a
byte of variable space (id and channel not used and removed). I also
changed the way the eeprom read addresses were calculated in the limit
check loop since it appeared to me you were using the same set of
limits each time. Lastly, added 1/2 second delay when out of limit
condition detected so LED illumination would be clearly visible.
Regards,
Steve
cs con 0
clk con 1
dio con 2
' Set the variables
result var word
chan var nib
memloc var word
lolimit var word
hilimit var word
sdata var word
sdatalb var sdata.lowbyte
sdatahb var sdata.highbyte
countr var nib
'Set starting the memory location
memloc= 0
' Serin the low and high limits for each channel into memory
' locations 0 - 31. Onve the data is received in and saved,
' we serout it back to pc so the pc can send the next value.
' 'a' is the low limit for chan1,
' 'b' is the high limit.
' 'c' is the low limit for chan 2 etc...
for countr = 0 to 15
serin 13,16468,[noparse][[/noparse]wait("a" + countr),dec5 sdata]
write (countr*2),sdatalb
write ((countr*2)+1),sdatahb
serout 11,16468,[noparse][[/noparse]dec sdata]
next
' Loop over each channel. For each loop gets the limits from
' memory and checks against current value. If error condition
' then led (pin 7) goes high.
again:
low 7
for chan = 0 to 7
read memloc+chan,lolimit.lowbyte
read memloc+chan+1,lolimit.highbyte
read memloc+chan+2,hilimit.lowbyte
read memloc+chan+3,hilimit.highbyte
low cs
shiftout dio, clk, msbfirst,[noparse][[/noparse](chan + %11000)\5]
shiftin dio, clk,msbpost,[noparse][[/noparse]result\13]
high cs
serout 10,16468,[noparse][[/noparse]dec chan,":",dec result]
pause 30
if result > lolimit and result < hilimit then okay
high 7
pause 500 ' long enough to notice LED
okay:
next
goto again
Simon-
Here's a version that should run faster and saves one word and a
byte of variable space (id and channel not used and removed). I also
changed the way the eeprom read addresses were calculated in the limit
check loop since it appeared to me you were using the same set of
limits each time. Lastly, added 1/2 second delay when out of limit
condition detected so LED illumination would be clearly visible.
Regards,
Steve
cs con 0
clk con 1
dio con 2
' Set the variables
result var word
chan var nib
memloc var word
lolimit var word
hilimit var word
sdata var word
sdatalb var sdata.lowbyte
sdatahb var sdata.highbyte
countr var nib
'Set starting the memory location
memloc= 0
' Serin the low and high limits for each channel into memory
' locations 0 - 31. Onve the data is received in and saved,
' we serout it back to pc so the pc can send the next value.
' 'a' is the low limit for chan1,
' 'b' is the high limit.
' 'c' is the low limit for chan 2 etc...
for countr = 0 to 15
serin 13,16468,[noparse][[/noparse]wait("a" + countr),dec5 sdata]
write (countr*2),sdatalb
write ((countr*2)+1),sdatahb
serout 11,16468,[noparse][[/noparse]dec sdata]
next
' Loop over each channel. For each loop gets the limits from
' memory and checks against current value. If error condition
' then led (pin 7) goes high.
again:
low 7
for chan = 0 to 7
read memloc+chan,lolimit.lowbyte
read memloc+chan+1,lolimit.highbyte
read memloc+chan+2,hilimit.lowbyte
read memloc+chan+3,hilimit.highbyte
low cs
shiftout dio, clk, msbfirst,[noparse][[/noparse](chan + %11000)\5]
shiftin dio, clk,msbpost,[noparse][[/noparse]result\13]
high cs
serout 10,16468,[noparse][[/noparse]dec chan,":",dec result]
pause 30
if result > lolimit and result < hilimit then okay
high 7
pause 500 ' long enough to notice LED
okay:
next
goto again
Comments
I've been working on some code (below), but I think that it could be
better laid out to improve loop execution times.
Thanks for all the help in stage 1 guys, I only hope that you can
help with this latest stage.
Cheers,
Simon
Here's the code....
' Serin upper and lower limits for a 8 chan adc.
' Save these limits to memory and loop through
' then while retrieving data from an adc.
' Set the constants
cs con 0
clk con 1
dio con 2
' Set the variables
result var word
chan var nib
channel var word
memloc var word
lolimit var word
hilimit var word
sdata var word
sdatalb var sdata.lowbyte
sdatahb var sdata.highbyte
id var byte
countr var nib
'Set starting the memory location
memloc= 0
' Serin the low and high limits for each channel into memory
' locations 0 - 31. Onve the data is received in and saved,
' we serout it back to pc so the pc can send the next value.
' 'a' is the low limit for chan1,
' 'b' is the high limit.
' 'c' is the low limit for chan 2 etc...
for countr = 0 to 15
lookup countr,
[noparse][[/noparse]"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p"],id
serin 13,16468,[noparse][[/noparse]wait(id),dec5 sdata]
write (countr*2),sdatalb
write ((countr*2)+1),sdatahb
serout 11,16468,[noparse][[/noparse]dec sdata]
next
' Loop over each channel. For each loop gets the limits from
' memory and checks against current value. If error condition
' then led (pin 7) goes high.
again:
low 7
for chan = 0 to 7
read memloc,lolimit.lowbyte
read memloc+1,lolimit.highbyte
read memloc+2,hilimit.lowbyte
read memloc+3,hilimit.highbyte
channel = %11000+chan
low cs
shiftout dio, clk, msbfirst,[noparse][[/noparse]channel\5]
shiftin dio, clk,msbpost,[noparse][[/noparse]result\13]
high cs
if result > lolimit and result < hilimit then okay
high 7
okay:
serout 10,16468,[noparse][[/noparse]dec chan,":",dec result]
pause 30
next
goto again
> for countr = 0 to 15
> lookup countr,
> [noparse][[/noparse]"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p"],id
You could unroll this and save a relatively expensive lookup:
id = "a"-1
for countr = 0 to 15
id=id+1 ' I'm guessing this is faster than the lookup, but maybe not?
Otherwise, looked good at a quick glance.
Regards,
Al Williams
AWC
* PAK-IX 5 channel, 10-bit floating point A/D now ON SALE
http://www.al-williams.com/awce/pak9.htm
Comments in line...
>
>' Serin upper and lower limits for a 8 chan adc.
>' Save these limits to memory and loop through
>' then while retrieving data from an adc.
>
>' Set the constants
>
>cs con 0
>clk con 1
>dio con 2
>
>' Set the variables
>
>result var word
>chan var nib
>channel var word
>memloc var word
>lolimit var word
>hilimit var word
>sdata var word
>sdatalb var sdata.lowbyte
>sdatahb var sdata.highbyte
>id var byte
>countr var nib
>
>
>'Set starting the memory location
>memloc= 0
>
>' Serin the low and high limits for each channel into memory
>' locations 0 - 31. Onve the data is received in and saved,
>' we serout it back to pc so the pc can send the next value.
>' 'a' is the low limit for chan1,
>' 'b' is the high limit.
>' 'c' is the low limit for chan 2 etc...
>
>for countr = 0 to 15
> lookup countr,
>[noparse][[/noparse]"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p"],id
Try id="a"+countr instead of the lookup. Be sure you are aware of the
caveats about serin data at 9600 baud. The incoming data has to be
slowly paced so that the stamp can keep up.
> serin 13,16468,[noparse][[/noparse]wait(id),dec5 sdata]
> write (countr*2),sdatalb
> write ((countr*2)+1),sdatahb
> serout 11,16468,[noparse][[/noparse]dec sdata]
Separator? dec sdata,tab
Do you need to run this code every time the program restarts? If
not, it would be more reliable to have a special button or command
something that would take the program into this alarm setting
routine. For example, just use a timeout on the serin command, so
that if the BS2 does not receive the new alarm points within a few
seconds, it will just continue on and use the old alarm points.
>next
>
>' Loop over each channel. For each loop gets the limits from
>' memory and checks against current value. If error condition
>' then led (pin 7) goes high.
>
>again:
> low 7
> for chan = 0 to 7
This needs something to set the memloc pointer:
memloc=chan*4
> read memloc,lolimit.lowbyte
> read memloc+1,lolimit.highbyte
> read memloc+2,hilimit.lowbyte
> read memloc+3,hilimit.highbyte
> channel = %11000+chan
> low cs
> shiftout dio, clk, msbfirst,[noparse][[/noparse]channel\5]
> shiftin dio, clk,msbpost,[noparse][[/noparse]result\13]
> high cs
> if result > lolimit and result < hilimit then okay
> high 7
>
> okay:
> serout 10,16468,[noparse][[/noparse]dec chan,":",dec result]
Separator? dec chan,":",dec result,CR
> pause 30
> next
>goto again
>
Looks good!!
-- Tracy