Program Help
Newzed
Posts: 2,503
I need some H E L P !!
I have a board with 4 R/C circuits.· Each circuit has a HIGH and LOW LED indicator.· The resulting program to accomplish this exceeds memory size of the BS2, which I am committed to using.
Here is half of the proigram that controls the LOW control.· The HIGH section is identical except for the values of A,B,C and D.· Is there any way to shorten this program?
· if timeA<22 then················ 'A
· cmd = cmd-64········· 'turn on LOW A
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
·
· if timeA<22 and timeB<22 then···· 'A+B
· cmd = cmd-16
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeB<22 then············· 'B
· cmd = cmd-16
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeA<22 and timeB<22 and timeC<22 then·· 'A+B+C
· cmd = cmd-4
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeA<22 and timeC<22 then···· 'A+C
· cmd = cmd-4
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeB<22 and timeC<22 then···· 'B+C
· cmd=cmd-4
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeC<22 then········· 'C
· cmd = cmd-4
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeA<22 and timeB<22 and timeC<22 and timeD<22 then 'A+B+C+D
· cmd = cmd-1
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeA<22 and timeB<22 and timeD<22 then···· 'A+B+D
· cmd = cmd-1
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeA<22 and timeC<22 and timeD<22 then····· 'A+C+D
· cmd = cmd-1
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· IF timeB<22 and timeC<22 and timeD<22 then····· 'B+C+D
· cmd = cmd-1
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeA<22 and timeD<22 then·················· 'A+D
· cmd = cmd-1
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeB<22 and timeD<22 then···· 'B+D
· cmd = cmd-1
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
·
· if timeC<22 and timeD<22 then···· 'C+D
· cmd = cmd-1
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeD<22 then············· 'D
· cmd = cmd-1
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
Thanks
Sid
·
I have a board with 4 R/C circuits.· Each circuit has a HIGH and LOW LED indicator.· The resulting program to accomplish this exceeds memory size of the BS2, which I am committed to using.
Here is half of the proigram that controls the LOW control.· The HIGH section is identical except for the values of A,B,C and D.· Is there any way to shorten this program?
· if timeA<22 then················ 'A
· cmd = cmd-64········· 'turn on LOW A
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
·
· if timeA<22 and timeB<22 then···· 'A+B
· cmd = cmd-16
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeB<22 then············· 'B
· cmd = cmd-16
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeA<22 and timeB<22 and timeC<22 then·· 'A+B+C
· cmd = cmd-4
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeA<22 and timeC<22 then···· 'A+C
· cmd = cmd-4
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeB<22 and timeC<22 then···· 'B+C
· cmd=cmd-4
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeC<22 then········· 'C
· cmd = cmd-4
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeA<22 and timeB<22 and timeC<22 and timeD<22 then 'A+B+C+D
· cmd = cmd-1
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeA<22 and timeB<22 and timeD<22 then···· 'A+B+D
· cmd = cmd-1
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeA<22 and timeC<22 and timeD<22 then····· 'A+C+D
· cmd = cmd-1
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· IF timeB<22 and timeC<22 and timeD<22 then····· 'B+C+D
· cmd = cmd-1
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeA<22 and timeD<22 then·················· 'A+D
· cmd = cmd-1
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeB<22 and timeD<22 then···· 'B+D
· cmd = cmd-1
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
·
· if timeC<22 and timeD<22 then···· 'C+D
· cmd = cmd-1
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
·
· if timeD<22 then············· 'D
· cmd = cmd-1
· low latch
· shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
· pulsout latch, 10
· low latch
· endif
Thanks
Sid
·
Comments
Send_Cmd:
· SHIFTOUT Ser, Clk, MSBFIRST, [noparse][[/noparse]cmd]
· PULSOUT Latch, 10
· RETURN
You don't need the "LOW Latch" before and after these commands -- use it once on reset to setup the latch pin.· After that, PULSOUT will return the Latch pin to a low state.
Another thing you could do is create a single control variable and just one set of threshold comparisons:
· ctrl = %0000
· IF (A < 22) THEN ctrl.BIT0 = 1
· IF (B < 22) THEN ctrl.BIT1 = 1
· IF (C < 22) THEN ctrl.BIT2 = 1
· IF (D < 22) THEN ctrl.BIT3 = 1
Now you have a nice nib variable that you can use with LOOKUP for cmd before sending off to the Send_Cmd subroutine.·· BTW ... the variable names·'A' - 'D' are·considered weak style as they give no indication to anyone else but the original program creator what their value is intended to hold and/or do.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA
However, attached is a version with all the copied code reduced to a subroutine call.· This gives you lots more room, and does the exact same thing.
For all LEDs to be off cmd has to be %11111111.· So would this work:
cmd = %11111111
if a<22 then bit.0 = 0
if b<22 then bit.1 = 0
if c<22 then bit.2 = 0
if d<22 then bit.3 = 0
'and so on
goto SendCMD
SendCMD:
low latch
shiftout ser, clk, msbfirst, [noparse][[/noparse]cmd]
pulsout latch, 10
low latch
goto start
Where do you keep all the medals you get for life-saving?
Sid
cmd = $FF
IF timeA < 22 THEN cmd.BIT0 = 0
IF timeB < 22 THEN cmd.BIT1 = 0
D'oh!· Use a 'GOSUB', NOT a 'GOTO'.· The difference is a GOSUB will return to wherever it is called from.· This is much easier to debug than a GOTO which then GOTO's somewhere else.· The GOTO..GOTO..GOTO stuff is called "spaghetti code" and becomes really hard to understand and verify.
·
Added a RETURN to SebdCMD, then wrote:
GOSUB SendCmd
goto start
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA
Here are the connections:
LED······················· 595 Shift Register
A HIGH·················· Qa
A LOW·················· Qb
B HIGH···················Qc
B LOW·················· Qd
and so on.· So now I would write:
if A>82 then bit.0 = 0
if A<22 then bit.1 = 0
if B>82 then bit.2 = 0
if B<22 then bit.3 = 0
and so on.
I am assuming that if I shiftout MSBFIRST then·11111110 would set
Qa low.· Do I have this right or do I have it backwards?· Should I shiftout LSBFIRST?
Sid
I think the MSBFirst or LSBFirst depends on how you have it wired -- is the '595 shifting UP or DOWN?
Fastest answer: Try it and see what you get.
Note: I don't understand your 'A Low', 'A High' comment.· Are you driving bi-directional LED's?· If so, when 'A Low' is low, shouldn't 'A High' be high?· And vice versa?
·
cmd = 11111110
and·shiftout MSBFIRST, does that take Qa low or does it take Qh low?
Thanks again.
Sid
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA
Sorry for all the questions, but·my computer crashed yesterday and I had to buy a new one.· Lost all my files so I don't have anything I can look back at.· We're trying to recover the files from the crashed computer hard drive, but not there yet.
Sid