View Full Version : basic stamp as a D flip flop

02-20-2010, 11:06 PM
Hi everyone.I'm new at programing,need all the help I can get.
Can the bs2 be configered as a JK or D f/f?
Any info will be helpful.

PJ Allen
02-20-2010, 11:38 PM
Making edge-triggered anything with a BS2 inevitably requires some compromise.

Tracy Allen
02-21-2010, 01:58 AM
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 (http://www.emesystems.com)

02-25-2010, 11:12 PM
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


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 Green
02-25-2010, 11:22 PM
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.

02-26-2010, 02:34 AM
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}


HIGH 15 'red on

LOW 15
LOW 13
HIGH 14 'green on
LOW 14
LOW 15
HIGH 13 'yellow
LOW 13
GOTO reset
LOW 15
LOW 13
LOW 14
GOTO routine1

Mike Green
02-26-2010, 02:53 AM
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
' ************
status = newStatus ' start with previous new status
newStatus = INL
LOOP until newStatus <> status ' wait until something changes
IF newStatus.bit6 = 1 AND status.bit6 = 0 THEN ' IN6 turned on
IF newStatus.bit4 = 1 AND status.bit1 = 4 THEN ' IN4 turned on
IF newStatus.bit3 = 1 AND status.bit3 = 0 THEN ' IN3 turned on
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 Allen
02-26-2010, 12:08 PM
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
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

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 (http://www.emesystems.com)

Post Edited (Tracy Allen) : 2/26/2010 3:47:45 PM GMT

02-26-2010, 10:11 PM
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 Allen
02-26-2010, 10:52 PM

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 (http://www.emesystems.com)

03-11-2010, 05:27 AM
Thanks Mike. The code blocks
And good advice you gave me was a great help.
This is a great forums for beganners.

04-02-2011, 02:04 AM
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.

04-02-2011, 02:50 AM
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!