Shop OBEX P1 Docs P2 Docs Learn Events
Program Help — Parallax Forums

Program Help

NewzedNewzed Posts: 2,503
edited 2005-03-24 16:08 in BASIC Stamp
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
·

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-03-24 13:51
    You have a bunch of redundant code that can be put into a subroutine:

    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
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-03-24 14:06
    It's kind of unclear what you are trying to do.· With all those 'condition' statements, you'll be sending lots of messages.· It might be better to have a 'phase' in your program where you construct the output message, then only send it once.

    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.
  • NewzedNewzed Posts: 2,503
    edited 2005-03-24 14:07
    Thanks, Jon

    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
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-03-24 14:10
    Yes, you have it mostly correct, but the syntax is:
    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.
    ·
  • NewzedNewzed Posts: 2,503
    edited 2005-03-24 14:24
    Thanks to you, too, Alan.

    Added a RETURN to SebdCMD, then wrote:

    GOSUB SendCmd
    goto start
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-03-24 14:42
    Sorry, it was early when I posted and I still haven't gone to see my friends at Starbucks. You can still get rid of the "Low Latch" lines in your subroutine -- if you're looking to trim code you might as well go all the way; you never know when you're going to need that space.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • NewzedNewzed Posts: 2,503
    edited 2005-03-24 14:49
    One more question, then I can enjoy life again:

    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
  • allanlane5allanlane5 Posts: 3,815
    edited 2005-03-24 15:00
    Yes, if Bit0 is LOW, then you have %11111110

    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?
    ·
  • NewzedNewzed Posts: 2,503
    edited 2005-03-24 15:06
    Allan, I don't have the boards yet so I can't test it out.· Let me ask the question another way.· If I write:

    cmd = 11111110

    and·shiftout MSBFIRST, does that take Qa low or does it take Qh low?

    Thanks again.

    Sid
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-03-24 15:23
    Send the data the way the '595 wants it (MSBFIRST), and Qa will end up getting BIT0 of your value.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas, TX· USA
  • NewzedNewzed Posts: 2,503
    edited 2005-03-24 16:08
    Thanks, Jon.· Everything is looking OK.

    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
Sign In or Register to comment.