Shop OBEX P1 Docs P2 Docs Learn Events
basic stamp as a D flip flop — Parallax Forums

basic stamp as a D flip flop

tedrobersontedroberson Posts: 8
edited 2011-04-01 19:50 in BASIC Stamp
Hi everyone.I'm new at programing,need all the help I can get.
Thanks.confused.gif
Can the bs2 be configered as a JK or D f/f?
Any info will be helpful.

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2010-02-20 16:38
    Making edge-triggered anything with a BS2 inevitably requires some compromise.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-02-20 18:58
    Why do you want to do that, tedroberson? Is it academic interest, or do you have a particular project in mind?

    It is possible to simulate state machines in firmware on the BASIC Stamp, but their response will be slow, on the order of several milliseconds instead of less than 1 microsecond. Emphasis, it has to be done with firmware code, and is not like a PGA where you are configuring hardware gates.

    D flip-flops in general have 6 i/o pins: D, Clock, Set, Reset, Q and Q\, so you might want to simulate the action of all those pins, or just a subset. A J-K flip flop only has 5 i/o pins, but the internal logic is more complicated, because levels on the J-K inputs simulate a D, T or SR flip-flop.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • tedrobersontedroberson Posts: 8
    edited 2010-02-25 16:12
    Thanks Tracy. Yes I do have a specific project in mind.

    I've designed a·switching circuit that controls three relays,·using all discrete components.

    I have a working prototype. I thought that by·using a microcontroller I could cut cost and

    components.

    At power up,my design defaults to out put 1, at this point I can randomely

    push any switch (n/o mom.) and control any out put.

    I don't have that flexibality with basis stamp,I can only· move· from 1-3 foward or reversed.

    Due to the loops, I'm always locked in a sequential direction foward or backwards.

    There must be some way I can overcome this.

    Any other comments will be greatly appreciated.

    Many thanks for your speedy reply.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-02-25 16:22
    A Stamp can be programmed to control 3 relays with 3 switches pretty much any way you want. You can't get any more flexible than that. Can you describe in more detail the rules or conditions associated with each relay and switch? There's no way to make useful suggestions without that sort of information and the process of formulating this sort of information may point the way for you.
  • tedrobersontedroberson Posts: 8
    edited 2010-02-25 19:34
    Hi Mike. the program below is one of several
    programs that I've configured,so far its the closest
    to what I'm looking for.
    At power up, red led is on.
    In3 turns off the red led and green led is on.
    In4 turns off the green led and the yellow led is on.
    In6 resets to routine1.At this time if In4 is pushed,red led is off, green is on. From this point I have to start all over.
    Normal seq. is : Routine1 is: Start up default 1,2,reset to 1,or in4 to go to 2. Then seq. starts over again.
    I would like to control all three out puts in any random order.
    That I choose. I can do this with my discrete circuit usind D/FFs.
    ' {$STAMP BS2} 'AMP SPK VER.2 ' Power up.Red LED on.
    ' {$PBASIC 2.5}

    '
    DEBUG ? IN3' SW1
    DEBUG ? IN4' SW2
    DEBUG ? IN6 'SW3
    'do
    DEBUG HOME

    HIGH 15 'red on
    DO

    routine1:
    LOOP UNTIL IN3 = 1
    LOW 15
    LOW 13
    HIGH 14 'green on
    DO
    LOOP UNTIL IN4 = 1
    LOW 14
    LOW 15
    HIGH 13 'yellow
    DO
    LOOP UNTIL IN6 = 1
    LOW 13
    GOTO reset
    reset:
    HIGH 15
    DO
    LOOP UNTIL IN6 = 1
    LOW 15
    LOW 13
    HIGH 14
    DO
    LOOP UNTIL IN4 = 1
    LOW 14
    HIGH 15
    GOTO routine1
  • Mike GreenMike Green Posts: 23,101
    edited 2010-02-25 19:53
    The reason you can't control the relays in any combination is that you're testing the switches individually. You have to read all the switches at the same time, then make a decision about what to do. It also sounds like you're interested in the change of a switch from off to on. Start with a loop to read the switches and detect whether they've gone from off to on:
    status  VAR  byte
    newStatus  VAR  byte
    newStatus = 0   ' assume all buttons off
    ' ************
    mainRoutine:
    status = newStatus   ' start with previous new status
    DO
      newStatus = INL
    LOOP until newStatus <> status   ' wait until something changes
    IF newStatus.bit6 = 1 AND status.bit6 = 0 THEN   ' IN6 turned on
    ENDIF
    IF newStatus.bit4 = 1 AND status.bit1 = 4 THEN   ' IN4 turned on
    ENDIF
    IF newStatus.bit3 = 1 AND status.bit3 = 0 THEN   ' IN3 turned on
    ENDIF
    GOTO mainRoutine
    


    You can also test to see if a switch goes from on to off. Basically you've saved the state of the switches when any of them changes, then you can test for various combinations. You can test for one switch going from off to on while another switch is on.
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-02-26 05:08
    Hi Ted,

    Does the following program do something like what you want? You have to define these problems very precisely, what do you want to happen when!!

    y VAR Nib   ' old state of inputs
    x VAR Nib    ' new state of inputs
    z VAR Nib   ' change in state of inputs
    y = 0     ' assume all buttons start inactive
    DIRD = %1110   ' need to make the led pins as outputs  <--- !!!
    OUTD = %1000   ' LED on p15 is on to start
    DO
        x = IN4 + (IN3*2) + (IN6*4) << 1 ' read inputs and reorder significance to x={%0000 to %1110}
        z = x ^ y & x           ' test for changes 0-->1 on all pins at once
       IF z THEN OUTD = z   ' a button went from 0->1, set the corresponding LED and leave it until next change
       y = x   ' update the old state
    LOOP
    



    This is based on changes, that is, if you hold down the red button and then press the yellow button, the red will go off and yellow will come on. The program would be simpler if yo used pins in sequence, for example, pins p1,p2,p3 to control leds on pins p13,p14,p15.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com

    Post Edited (Tracy Allen) : 2/26/2010 3:47:45 PM GMT
  • tedrobersontedroberson Posts: 8
    edited 2010-02-26 15:11
    Tracy thank you,Mike and all others who have taken their time to help me.
    All info received has been very helpful.
    I'm new at this programing game so I'm as dumb as I seem.
    ( LOL)Most forums, you ask dumb questions,you get dumb answers.
    Not so with you guys. Thanks
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2010-02-26 15:52
    Ted,

    New is not dumb! You got your circuit working, so with a littel more persistence you will be able to do that and more with the Stamp!

    I added one important line to my trial program. A dumb mistake on my part -- never too old to make dumb mistakes!
    DIRD = %1110
    That makes outputs out of the led pins. Otherwise the Stamp default is for them to be inputs. Commands like HIGH 15 turn pin into an output and make it high. But the OUTD command does not automatically make the pins outputs. Note that state machine logic on microcomputers most often will involve the bitwise logic operators, like ^ (stampese for bitwise XOR) and & (bitwise AND).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • tedrobersontedroberson Posts: 8
    edited 2010-03-10 22:27
    Thanks Mike. The code blocks
    And good advice you gave me was a great help.
    This is a great forums for beganners.
  • izulustudiosizulustudios Posts: 6
    edited 2011-04-01 19:04
    Hello and thank you for this thread.
    I think the code that you cover here is close to what I need for a current application.
    What I need are: a set of 10 momentary buttons with an accompanying set of 10 LED's (namely they are illuminated momentary pushbuttons)
    I need for each of them to latch; that is, for the corresponding output pin to go high and remain high until any other button is depressed, and for the latch to then shift to the next (random) button to be depressed.
    Put very simply: There are ten buttons, which when pressed, light up and stay lit up until a subsequent button is pressed.
    This circuit would be used to control an adjacent system with it's own set of digital inputs and would serve purely as indicator of the last button to have been pressed.
    From what I can make out, the example code that Tracy Allen has provided above seems to closely represent what I need however I am a little unclear as to how the Hex variables: DIRD = %1110 OUTD = %1000 work?
    Any further guidance would be hugely appreciated.
    Thanks again.
    Dan
  • davejamesdavejames Posts: 4,047
    edited 2011-04-01 19:50
    Hello Dan - check the "DIRS" section of the Basic Stamp Syntax and Reference manual. It'll explain the IN/OUT nature of the Stamp's pins. Lots good stuff in there!

    http://www.parallax.com/tabid/440/Default.aspx

    DJ
Sign In or Register to comment.